- Saved searches
- Use saved searches to filter your results more quickly
- amsimoes/excel2kml
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Bob’s Tech Corner
- Know your data
- Code to CSV
- Distort and Import
- mciantyre / KmlToCsv.py
- START OF CODE
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Python script that generates a KML file to mark points on googlemaps from an excel sheet, with custom icons colors.
amsimoes/excel2kml
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Python (v3.5) script to generate a KML file to mark points on googlemaps from an excel sheet, with custom icons’ colors.
Firstly, there has to be a column with the places’ names as you provide their range on the script.
Then, point the column which have the respective pair of (Latitude,Longitude) coordinates.
Finally assign whether there is a colors column for the markers if you intend to have custom ones. (Attention: The default color is RED for no custom colors)
If everything worked as intended you shall have the kml file fully functional waiting to be imported.
Access «My Maps», and in any map on a empty layer, you’ll see the Import button. Just upload the kml file and it will mark every point, with it’s color, on the right spot on the map.
- Add more colors possibilities.
- Option to have column with points descriptions.
- Point if user have Lat,Long or Long,Lat format.
- If cell is blank on colors column -> default color.
- Get user input for default color as well as Input for layer’s name and Map’s name.
About
Python script that generates a KML file to mark points on googlemaps from an excel sheet, with custom icons colors.
Bob’s Tech Corner
I’m sharing a couple Python scripts that I used to update my Google MyMaps map for a website that I manage.
Know your data
On the website I manage for the Friends of Silverbrook Cemetery, I created a Map of Sections page with an iframe of a Google MyMaps page.
Each of these colored sections is a map layer of polygons. The major sections are Old Grounds, City Addition, Bond Addition, New Addition, Garden of Memory and Garden of Eternal Peace.
Each of those polygons was hand drawn in MyMaps.
In addition to the polygon, I have pins in a separate layer called «Points of Interest».
Those pins have a slightly different data structure than the polygons. For now I’m just going to focus on the polygons.
As an example, I’m going to take that large yellow triangle in section New-63 and distort it. First, I need to export the New layer of the map to a KML file.
I select «Export to KML/KMZ».
I just need the KML file. A KMZ is a zip archive of KML files and a file of meta data. I don’t need all that. I just want to work with one layer at a time.
As you can see, a KML file is an XML file with a schema defined by OpenGIS.
Searching for New-63, I see this section.
A triangle polygon has four points. The first and fourth points are the same. In a «line» type shape, the first and last points do not have to match.
I want a spreadsheet that contains all the coordinates, grouped by section and ordered by their sequence around the polygon. The first line will be a header.
Code to CSV
Here’s the whole Python script.
import xml.etree.ElementTree as ET
# Where is «coordinates» in each Placemark?
# kmlfile = «Bond’s Addition.kml»
# kmlfile = ‘Garden of Memory.kml’
csvfile = kmlfile.split(sep=’.’)[0] + ‘.csv’
for placemark in docelem.findall(‘Placemark’,ns):
for ln in coords.splitlines()[1:-1]:
The only module I imported is the ElementTree API. One thing I struggled with is that the Namespace for your XML schema needs to be specified in most of the calls to API. You can specify your namespaces as an array of dicts (dictionary pairs). I’m just using one namespace, so I left its name a null.
The coordinates for the polygon are several layers of elements deep. Rather than have nested find functions on the Placemark node, I just define the find string once.
I wanted to work with one KML file at a time. Rather than parameterize it, I just comment out the layers I’m not working with at the moment.
«fout» is the output file handle to the CSV file. I print the header to the file and the screen.
The parse function reads the file and puts it into an XML data structure. This is definitely labor-saving magic.
«root» is a pointer to the top of the XML tree.
«docelem» points to the Document element.
Each polygon is an element contained in the Placemark element. Therefore, there is a for-loop on Placemark.
The name of each polygon («Section») is the text of the name element.
The «coordinates» element is three elements in. The «wherecoords» string hops into it.
Each coordinate pair is terminated with a comma, a zero and a newline. The for-loop assigns each coordinate line to «ln», skipping over that first empty line feed.
Distort and Import
Now I have a CSV file of the polygons in the New Addition.
For demonstration, I’m going to take the bottom point of New-63 and make it «droopy» down to the bottom of New-62.
All I have to do is copy the yellow cells to the green cells, and then save the CSV file.
The following Python script reads the KML file into an XML memory structure as before. Then it reads the CSV file and updates each of the coordinates elements. The script is below. You will notice some code reuse.
import xml.etree.ElementTree as ET
# Where is «coordinates» in each Placemark?
# print(«Update tree here for section » + CurrentSection)
FindPath = «./Document/Placemark/[name='» + CurrentSection +»‘]»
# Find Placemark of currect Section
# Dig down to find the coordinates element
# Define a KML and CSV files
# kmlfile = «Bond’s Addition.kml»
# kmlfile = ‘Garden of Memory.kml’
kmloutfile = filebase + ‘_out.kml’
# Open and read KML file into a tree
#Discard the header, if there is one
coords += «%12s» % » » # Indent 12 chars after last newline
# Append to coords variable
coords += ‘%14s’ % » » # Indent 14 chars
coords += cols[2] + ‘,’ + cols[3] + «,0\n»
coords += «%12s» % » » # Indent 12 chars after last newline
As before, import the ElementTree API, parse the file names, read in the KML file to the XML structure.
This time we loop on each line read from the CSV file. If the coordinates are in the same section, add those coordinates to the «coords» string. If the section changes, update the XML structure and then reset the coords string. The else block takes care of the last read of the CSV file.
I used the global «root» node pointer and CurrentSection string in the function update(tree) instead of passing them in as parameters. It’s a little less portable, but works fine here.
One annoying feature of ElementTree is that is wants to prefix a namespace tag when you write out the structure. MyMaps hates this.
«xmlstr» strings gets a dump of the XML structure. «xmlstr_nons0» removes the «ns0:» and «:ns0» strings.
When everything works, the CSV file looks like this:
To test this out, I’ll create a new map and import the file.
Back in MyMaps, I click on «+Create a New Map» button.
Now I’ll import the file ‘New Addition_out.kml’. Here’s the map after the import.
Yes, it does need formatting. But the bottom of section New-63 is now the same as New-62.
If you want to replace the existing layer, delete the old layer and replace it with your new one.
«MyMaps to Spreadsheet, KML to CSV and back»
Bob Nightingale, info@rvnllc.com
mciantyre / KmlToCsv.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
«»» |
A script to take all of the LineString information out of a very large KML file. It formats it into a CSV file so |
that you can import the information into the NDB of Google App Engine using the Python standard library. I ran this |
script locally to generate the CSV. It processed a ~70 MB KML down to a ~36 MB CSV in about 8 seconds. |
The KML had coordinates ordered by |
[Lon, Lat, Alt, ‘ ‘, Lon, Lat, Alt, ‘ ‘. ] (‘ ‘ is a space) |
The script removes the altitude to put the coordinates in a single CSV row ordered by |
[Lat,Lon,Lat,Lon. ] |
Dependencies: |
— Beutiful Soup 4 |
— lxml |
I found a little bit of help online for using BeautifulSoup to process a KML file. I put this online to serve as |
another example. Some things I learned: |
— the BeautifulSoup parser *needs* to be ‘xml’. I spent too much time debugging why the default one wasn’t working, and |
it was because the default is an HTML parse, not XML. |
tl;dr |
KML —> CSV so that GAE can go CSV —> NDB |
«»» |
from bs4 import BeautifulSoup |
import csv |
def process_coordinate_string ( str ): |
«»» |
Take the coordinate string from the KML file, and break it up into [Lat,Lon,Lat,Lon. ] for a CSV row |
«»» |
space_splits = str . split ( » » ) |
ret = [] |
# There was a space in between » «-80.123. hence the [1:] |
for split in space_splits [ 1 :]: |
comma_split = split . split ( ‘,’ ) |
ret . append ( comma_split [ 1 ]) # lat |
ret . append ( comma_split [ 0 ]) # lng |
return ret |
def main (): |
«»» |
Open the KML. Read the KML. Open a CSV file. Process a coordinate string to be a CSV row. |
«»» |
with open ( ‘doc.kml’ , ‘r’ ) as f : |
s = BeautifulSoup ( f , ‘xml’ ) |
with open ( ‘out.csv’ , ‘wb’ ) as csvfile : |
writer = csv . writer ( csvfile ) |
for coords in s . find_all ( ‘coordinates’ ): |
writer . writerow ( process_coordinate_string ( coords . string )) |
if __name__ == «__main__» : |
main () |
I would like to thank Ian for this very useful post and for his kind help to tune the code for Python 3.5 (original code is for 2.7).
The following script worked like a charm in order to convert a Google Earth *.kml file into a *.csv file:
START OF CODE
from bs4 import BeautifulSoup
import csv
def process_coordinate_string(str):
«»»
Take the coordinate string from the KML file, and break it up into [Lat,Lon,Lat,Lon. ] for a CSV row
«»»
ret = []
comma_split = str.split(‘,’)
return [comma_split[1], comma_split[0]]
def main():
«»»
Open the KML. Read the KML. Open a CSV file. Process a coordinate string to be a CSV row.
«»»
with open(‘inputfile.kml’, ‘r’) as f:
s = BeautifulSoup(f, ‘xml’)
with open(‘out.csv’, ‘w’, newline=») as csvfile:
writer = csv.writer(csvfile)
for coords in s.find_all(‘coordinates’):
writer.writerow(process_coordinate_string(coords.string))
if name = :
main()