Creating Bar plots kml files for Google Earth Pro using Python googleearthplot referring to this site.

Install

Suppose pandas is installed. Install simplekml and googleearthplot.


pip install simplekml
pip install googleearthplot

googleearthplot is a Python2 code and some modification is required in Python3 because of an error associated with print statement. First find the location of installed googleearthplot as follows:


pip show googleearthplot

Edit googleearthplot.py to modify the lines of print statement by converting print **** to print(****) using parentheses.

Usage

Open a Jupyter Notebook and import packages. Referring to the original site, some examples are shown. Double cricking on created kml file, a plot should be presented on Google Earth Pro.


from googleearthplot import googleearthplot as gep
import pandas as pd

Point plot

The following is a sample plot for a point plot.


lon=139.0 #degree
lat=35.5 #degree
gep1=gep.googleearthplot()
gep1.PlotPoints(lat,lon,"point")
gep1.GenerateKMLFile(filepath="sample1.kml")

Bar plot

The following is a sample plot for bar plot.


#A bar plot 
gep2=gep.googleearthplot()
lat=35.5 #degree
lon=139.5 #degree
num=100 #bar height size
size=1  #meter
name="test"
color="red"
gep2.PlotBarChart(lat,lon,num,size,name,color)
gep2.GenerateKMLFile(filepath="sample2.kml")

You can create multiple bar plots using for loop.

Read data from file and create bar plot

Suppose a text data file of sample3.txt. Its contents are three columns of longitude, latitude, and height:


  139.662750    35.477141   166.771019
  139.663961    35.477226   171.858589
  139.665148    35.477312   169.775301
  139.666306    35.477394   160.395112

Read this file and create a pandas DataFrame.


fpath = "sample3.txt"
names = ["lon", "lat", "height"]
df = pd.read_csv(fpath, sep='\s+', header=None, names=names)
### Exclude lines of height==0
lon_p = df[df['height'] != 0.0]['lon'].values
lat_p = df[df['height'] != 0.0]['lat'].values
height_p = df[df['height'] != 0.0]['height'].values

size=2
name=None
color='red'
gep3 = gep.googleearthplot()
for (lon, lat, height) in zip(lon_p, lat_p, height_p):
    gep3.PlotBarChart(lat, lon, height, size, name, color)
gep3.GenerateKMLFile(filepath="sample3.kml")

There is also a method to directly convert csv data file to bar plots using PlotBarChartsFromCSV method. For more information, see the original site.

References

  • PyPi googleearthplot url
  • GitHub AtsushiSakai/googleearthplot url