There are quite a few ways allowing us to create the .kml file for Google Earth and Google Maps. The solution presented here is not quicker, but for sure quite convenient for someone, who recently started his adventure with the Python programming language.
Here I would like to demonstrate to you how to create some random geographical points easily. In Python programming language we have basically 2 ways of creating the .kml file. The first way leads us to the code, thanks to which the .kml file can be created literally from scratch. The second one shows a much quicker option with using an already existing tool, including all relevant code files stored in the folder. I am going to deal with the second way because I believe most people are short of time.
Well, the tool we can download from this website, and next extract all files from the zipped folder (Pic. 1, 2).
The “Setup” file is not for file creation. You must install some Python console, which can be for instance the IDLE Python 3.7. Next, create the new .py file and for the .kml file creating purpose write some small code, which can look like this (Pic. 3):
Another way to execute the .kml file from Python looks like this (Pic. 4):
Both codes must be saved into separate files and placed within the main SimpleKML tool folder (Pic. 5).
Along with these files, you can prepare the new folder in order to cache your newly created .kml files. The Python files with the code shown above must be located inside the major tool catalog when they can fetch the relevant Python code from the files based in the “simplekml” folder.
Now, let’s double-click on one of them – the KML3.py file. Right afterward, our .kml file is to be found at the bottom of our list, as we haven’t set the saving path in the code (Pic. 3, 5).
Now let’s double-click on our new .kml file and open it in Google Earth (Pic. 6). It appears as a yellow pin, with scant balloon information. We can put some description in the code (Pic. 11).
The other code also works fine, and we can find the file under the specified location (Pic. 7) established in the script (Pic. 2).
Also, in this case, everything looks fine on Google Earth (Pic. 8).
OK, the first part has been done. Now I am interested in plotting into the code more than 1 placemark to show.
In the first code adding more than a few placemarks can be awkward. I copied and modified this code only once (Pic. 9).
This code can be simplified. Instead of using each list separately, we can use the for statement, according to this explanation (Pic. 11). As a result, we can put more placemarks, keeping our code tidier.
This code can be written in a different way. We can include the collection.namedtuple for the markers. In this event it would look like follows:
In Google Earth, the result is exactly the same.
Another code is more simplified (Pic. 13), where basically within 1 line you can place the .kml placemark with the most important features like description or name. On top of that, I did also some smallish HTML amendments, making the font bold (Pic. 14).
Developing this code, we can add obviously the next files. However, the most important thing is, that you can easily expand this code on some styling. As you will notice, I used unique HTML features for each individual placemark. The last one was extended with the font styling feature, where I added an additional parameter (Pic. 15, 16).
Building .kml files in Python is not confined to placemarks only. We can also create lines or polygons. More details with ready code snippets are to be found here. I just presented some of these examples below (Pic. 18 – 21). You can try on your own, building your personal example.
Besides the best, I guess, the SimpleKML tool, there are other ones, where you can build your Google Earth placemark or object. They are:
– FastKML v0.11
– Clawpack v.5.3.1 (also .kmz files)
– PyKML v0.1.0
– GeotagToKML (creating .kml file from geotagged pictures)
Nevertheless, I would recommend you to stay with the SimpleKML tool, which appears to be the most popular and also kept up-to-date. The attention can draw only the last one from the list, which can draw your route based on the geotagged images, although this is a subject for further writing.
- Constructing the KML file from scratch in Python
- SimpleKML v1.3.1. documentation
- SimpleKML v1.3.1. download
- SimpleKML styling
- Python 3.7.0
- FastKML documentation
- FastKML v0.11
- Clawpack v5.3.1
- GeotagToKML – details