Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Created December 17, 2017 12:01
Show Gist options
  • Save nishadhka/3ba801ca980da5b76004631c1935f604 to your computer and use it in GitHub Desktop.
Save nishadhka/3ba801ca980da5b76004631c1935f604 to your computer and use it in GitHub Desktop.
Read kml file into dataframe
from pykml import parser
import pandas as pd
filename='ref1.kml'
with open(filename) as f:
folder = parser.parse(f).getroot().Document.Folder
plnm=[]
cordi=[]
for pm in folder.Placemark:
plnm1=pm.name
plcs1=pm.Point.coordinates
plnm.append(plnm1.text)
cordi.append(plcs1.text)
db=pd.DataFrame()
db['place_name']=plnm
db['cordinates']=cordi
def dump(obj):
for attr in dir(obj):
if hasattr( obj, attr ):
print( "obj.%s = %s" % (attr, getattr(obj, attr)))
#the kml output plnm1 and plcs1 are objects actually which can be viewed using the dump function from [SO](https://blender.stackexchange.com/questions/1879/is-it-possible-to-dump-an-objects-properties-and-methods)
db['Longitude'], db['Latitude'],db['value'] = zip(*db['cordinates'].apply(lambda x: x.split(',', 2)))
db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment