Skip to content

Instantly share code, notes, and snippets.

@mapcloud
Created July 28, 2017 13:27
Show Gist options
  • Save mapcloud/036688121a87f960e35cc03077001e93 to your computer and use it in GitHub Desktop.
Save mapcloud/036688121a87f960e35cc03077001e93 to your computer and use it in GitHub Desktop.
Pandas to GeoJSON (Multiples points + features) with Python
# Real source heroes here: https://gis.stackexchange.com/questions/220997/pandas-to-geojson-multiples-points-features-with-python?rq=1
import json
import simplekml
import geojson
import pandas as pd
def data2kml(df):
kml = simplekml.Kml()
df.apply(lambda X: kml.newpoint(
name=X["name"],
description=unicode(X["description"].decode('utf8')),
coords=[( X["long"], X["lat"], X["elev"])]
)
, axis=1)
kml.save(path='map.kml')
def data2geojson(df):
points = []
df.apply(lambda X: points.append( (float(X["long"]),
float(X["lat"]))
)
, axis=1)
with open('map.geojson', 'w') as fp:
geojson.dump(geojson.MultiPoint(points), fp, sort_keys=True)
col = ['lat','long','elev','name','description']
data = [[-29.9953,-70.5867,760,'A','Place a'],
[-30.1217,-70.4933,1250,'B','Place b'],
[-30.0953,-70.5008,1185,'C','Place c']]
df = pd.DataFrame(data, columns=col)
data2kml(df)
data2geojson(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment