LabelStyleElement.appendChild(scaleElement) ScaleElement = kmlDoc.createElement('scale') StyleElement.appendChild(labelStyleElement) LabelStyleElement = kmlDoc.createElement('LabelStyle') #This image can be embedded when the KML is converted to KMZ (in google earth). # //Change 'blu-diamond.png' to a web address or any other image in the same directory as the CSV + application. IconLocation = kmlDoc.createTextNode('blu-diamond.png') HrefElement = kmlDoc.createElement('href') IconElement = kmlDoc.createElement('Icon') IdText = kmlDoc.createTextNode('NormIconID') StyleIDElement = kmlDoc.createElement('id') StyleElement = kmlDoc.createElement('Style')ĭocumentElement.appendChild(styleElement) KmlElement = kmlDoc.appendChild(kmlElement)ĭocumentElement = kmlDoc.createElement('Document')ĭocumentElement = kmlElement.appendChild(documentElement) KmlElement = kmlDoc.createElementNS('', 'kml') # This constructs the KML document from the CSV file.
PlacemarkElement.appendChild(pointElement)ĬoorElement = kmlDoc.createElement('coordinates')ĬoorElement.appendChild(kmlDoc.createTextNode(coordinates))ĭef createKML(csvReader, fileName, order): PointElement = kmlDoc.createElement('Point') ValueElement = kmlDoc.createElement('value') # Loop through the columns and create a element for every field that has a value.ĭataElement = kmlDoc.createElement('Data') PlacemarkElement.appendChild(styleUrlElement) StyleUrlElement.appendChild(styleUrlText) StyleUrlText = kmlDoc.createTextNode('#caseStyle') StyleUrlElement = kmlDoc.createElement('styleUrl') # //Change 'ID' in nameText to the CSV column heading with desired labels.ĮxtElement = kmlDoc.createElement('ExtendedData') PlacemarkElement.appendChild(nameElement) NameElement = kmlDoc.createElement('name') PlacemarkElement = kmlDoc.createElement('Placemark') # This creates a element for a row of data. # combine lat-longs from their columns, returned as a string. Someone more experienced may have a better solution, but this works well for me. Also, place-mark images can be embedded when you eventually convert the KML to KMZ.
#Converting kml to csv code
Notes: if you do want the default place-markers (yellow pins) you can delete much of the code related to the style. Run the script (f5) and you will have a working KML. Save the py script in the same folder as the CSV file (and place-marker image if desired). Places that you may want to change are identified as '#//'. Copy-paste the code below.ģ) Edit the code to suit your data and design. Save as CSVĢ) Dowload python to suit your OS and architecture and after the install, open IDLE (python GUI) and a new window (ctrl-n). Also, allows for labeled and customized place-markers.ġ) Prepare your data - with latitude and longitude in individual columns (decimal format) and include other columns that you would like to be displayed in a pop-up box when clicked. This is designed for cases where you already have the lat-longs in a CSV. The example on the site above will work in Python 2, and converts addresses to lat-longs and then creates a KML. If you're like me and are cautious about uploading sensitive data for online conversion then try this Python code.