Reading KML files from google maps
This project should be relatively short. The goal is to make sight edits to a KML file from google maps. A KML is a file used to display information of google maps or google maps. Geolocations, directions etc.
The file I’m using looks like this:
This file consists of outdoor locations around London. To export the file from must choose the “Export KML/KMZ” like below:
Then choose the KML option:
Now we have the file in are project folder we must get the third-party package that will parse through the file.
Using this package called FastKML installed via pip. Reading the docs we can turn the file into a string so it can be read.
The code I used below:
from fastkml import kml
with open('Outdoor London.kml', 'rt', encoding="utf-8") as myfile:
doc = myfile.read()
print(doc)
The file printed on the command line below:
Now we have the file parse we want to start editing. I want to edit the current icon and also create a new icon with its own location.
So first to edit the current icons we need to scroll down to were the names of the place markers are shown:
In this example, I will shorten the of the place marker to meanwhile Gardens.
Simply done by deleting the text:
Now we want to add a new place marker into the file. Likely I will make a placemark tag with other children tags like above.
Copying and pasting this example to use:
Now I must choose custom coordinates of the location. Using google maps picked a location to decide on:
When adding the coordinates to the file, is that the order of the numbers when copying is the wrong way around. So when paste you must change the order.
Now, with the edited code, we should open the file on google maps
To do that we head to google maps them navigate to your places tabs:
After that we click on the maps tabs then we head to “create new map”:
After we get moved into a new page called google myMaps. Now we can import are KML file:
After uploading the loading the file, we can see the map. Below we can we the custom example I added:
Also, the example edited the text is also changed:
Now we have successfully edited the KML file. From coding this project while writing I noticed that if your KML file does not have much data points then the fastKML library is not needed as you can just directly edit the KML file itself.