[GPS] Create a kml file in Python

Overview

Create a ** kml file ** that is 3D geospatial information using Python and display it on Google Earth.

Advance preparation

Since simplekml is required, install the simplekml library according to the OS.

Purpose

To create a kml file from a csv file that summarizes latitude, longitude, and altitude. Since it is xml-based, ElementTree was fine, but when I looked it up, there was a simplekml that could create a kml, so I created a kml file using simplekml.

Source

gpsdata_to_kml.py


#!/usr/bin/python2.7
#-*- coding: utf-8 -*-
import simplekml

#Display in point format
## [Place name,longitude,latitude]
sample_points = [["Tokyo Station", 139.766389, 35.681340],
                 ["Yurakucho Station", 139.763360, 35.675056],
                 ["Shinjuku station", 139.700432, 35.690938],
                 ["Ikebukuro Station", 139.711570, 35.730235],
                 ["Akihabara Station", 139.774091, 35.698704],
                 ["Ueno Station", 139.777195, 35.713714]]


kml = simplekml.Kml()
for point in sample_points:
    kml.newpoint(name=unicode(point[0], 'utf-8'), coords=[(point[1], point[2])])

kml.save('yamanote_line.kml')

#Display in LINE STRING format including altitude
## [Object name,longitude,latitude,Altitude,color]
sample_linestrings = [["Sky tree", 139.810657, 35.710089, 634.000000, simplekml.Color.grey],
                      ["○ Dam", 139.810557, 35.710089, 18.000000 , simplekml.Color.white],
                      ["○ Lutraman", 139.810657, 35.709989, 40.000000, simplekml.Color.red],
                      ["○ Itan 3", 139.810757, 35.710089, 120.000000, simplekml.Color.yellow],
                      ["○ Nbuster", 139.810657, 35.710189, 200.000000, simplekml.Color.black]]

kml = simplekml.Kml()

for linestring in sample_linestrings:
    ls = kml.newlinestring(name=unicode(linestring[0], 'utf-8'))
    ls.style.linestyle.color = linestring[4]
    ls.style.linestyle.width = 8
    ls.extrude = 1
    ls.altitudemode = simplekml.AltitudeMode.absolute
    ls.coords = [(float(linestring[1]), float(linestring[2]), float(linestring[3]))]

kml.save('skytree.kml')

Output with Google Earth

Display in point format

img_1.jpg

Displayed in LINE STRING format

img_2.jpg

Recommended Posts

[GPS] Create a kml file in Python
Create a binary file in Python
Create a GIF file using Pillow in Python
Create a function in Python
Create a dictionary in Python
Create a MIDI file in Python using pretty_midi
Create a DI Container in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Create a deb file from a python package
File operations in Python
File processing in Python
Create a Python module
Create SpatiaLite in Python
File operations in Python
Create a Python environment
Create a Vim + Python test environment in 1 minute
Read a file containing garbled lines in Python
Create an executable file in a scripting language
Create a standard normal distribution graph in Python
Create a virtual environment with conda in Python
Create a Photoshop format file (.psd) with python
Create a package containing global commands in Python
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Test & Debug Tips: Create a file of the specified size in Python
Take a screenshot in Python
Create a Wox plugin (Python)
Download the file in Python
Create gif video in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
Make a bookmarklet in Python
Create a python numpy array
Create a dummy data file
Python script to create a JSON file from a CSV file
Draw a heart in Python
Run a Python file with relative import in PyCharm
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a directory with python
Create a fake Minecraft server in Python with Quarry
Create a 2d CAD file ".dxf" with python [ezdxf]
Create an exe file that works in a Windows environment without Python with PyInstaller
Try creating a Deep Zoom file format .DZI in Python
[Python] Create a file & folder path specification screen with tkinter
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Create a child account for connect with Stripe in Python
How to import a file anywhere you like in Python
[Note] Import of a file in the parent directory in Python
Create code that outputs "A and pretending B" in python
Create a tool to check scraping rules (robots.txt) in Python
Maybe in a python (original title: Maybe in Python)
Count specific strings in a file
Write a binary search in Python
File / folder path manipulation in Python