I am new to Python. I found this code on the internet to parse names and coordinate strings from KML files and write them into a .csv file. Works excellent! However, for my purpose, I need to convert the coordinate string to GeoJSON as output. For my GeoJSON format I need to remove the height/accuracy from each coordinate. So I only need the lat,lon coordinates and want to remove the height/accuracy. Besides that I need to enclose each individual lat,lon coordinate with square brackets []. So, for example, I now have this output format for my coordinate string:
-89.42462055297086,16.35478756523923,507.7306687727497
-89.41330216185102,16.33874177258091,521.3689105782825
And need to convert this coordinate string to this format:
[-89.42462055297086,16.35478756523923],[-89.41330216185102,16.33874177258091]
Below is the code I am using now. I want to add some code to create the GeoJSON coordinate string format I need. How can I do this?
import xml.sax, xml.sax.handler
inputname = 'Test4.kml'
kml = open(inputname, 'r')
class PlacemarkHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.inName = False # handle XML parser events
self.inPlacemark = False
self.mapping = {}
self.buffer = ""
self.name_tag = ""
def startElement(self, name, attributes):
if name == "Placemark": # on start Placemark tag
self.inPlacemark = True
self.buffer = ""
if self.inPlacemark:
if name == "name": # on start title tag
self.inName = True # save name text to follow
def characters(self, data):
if self.inPlacemark: # on text within tag
self.buffer += data # save text if in title
def endElement(self, name):
self.buffer = self.buffer.strip('\n\t')
if name == "Placemark":
self.inPlacemark = False
self.name_tag = "" #clear current name
elif name == "name" and self.inPlacemark:
self.inName = False # on end title tag
self.name_tag = self.buffer.strip()
self.mapping[self.name_tag] = {}
elif self.inPlacemark:
if name in self.mapping[self.name_tag]:
self.mapping[self.name_tag][name] += self.buffer
else:
self.mapping[self.name_tag][name] = self.buffer
self.buffer = ""
parser = xml.sax.make_parser()
handler = PlacemarkHandler()
parser.setContentHandler(handler)
parser.parse(kml)
def build_table(mapping):
sep = ';'
output = 'Name' + sep + 'Coordinates' + sep + 'test column\n'
points = ''
lines = ''
shapes = ''
for key in mapping:
coord_str = '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[' + mapping[key]['coordinates'] + ']]]}}]}' + sep
if 'LookAt' in mapping[key]: #points
points += key + sep + coord_str + "\n"
elif 'LineString' in mapping[key]: #lines
lines += key + sep + coord_str + "\n"
else: #shapes
shapes += key + sep + 'coord_str\n'
output += points + lines + shapes
return output
outstr = build_table(handler.mapping)
out_filename = 'output-result.csv'#output filename same as input plus .csv
f = open(out_filename, "w")
f.write(outstr)
f.close()
print(outstr)
°°°