Pandas to GeoJSON (Multiples points + features) with Python
Using what i learned from the answer of @gene this solution avoids the use of iterrows because iterrows have performance issues.
Valid for Python 3.X
import pandas as pd
import geojson
def data2geojson(df):
features = []
insert_features = lambda X: features.append(
geojson.Feature(geometry=geojson.Point((X["long"],
X["lat"],
X["elev"])),
properties=dict(name=X["name"],
description=X["description"])))
df.apply(insert_features, axis=1)
with open('map1.geojson', 'w', encoding='utf8') as fp:
geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True, ensure_ascii=False)
col = ['lat','long','elev','name','description']
data = [[-29.9953,-70.5867,760,'A','Place ñ'],
[-30.1217,-70.4933,1250,'B','Place b'],
[-30.0953,-70.5008,1185,'C','Place c']]
df = pd.DataFrame(data, columns=col)
data2geojson(df)
Valid for Python 2.X
import pandas as pd
import geojson
def data2geojson(df):
features = []
df.apply(lambda X: features.append(
geojson.Feature(geometry=geojson.Point((X["long"],
X["lat"],
X["elev"])),
properties=dict(name=X["name"],
description=unicode(X["description"].decode('utf8'))))
)
, axis=1)
with open('map.geojson', 'w') as fp:
geojson.dump(geojson.FeatureCollection(features), 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)
data2geojson(df)
Geoff Boeing provides a solution in Exporting Python Data to GeoJSON and Convert a pandas dataframe to geojson for web-mapping (Jupyter notebook) for 2D coordinates and you can adapt his script for 3D coordinates
def df_to_geojson(df, properties, lat='lat', lon='long', z='elev'):
geojson = {'type':'FeatureCollection', 'features':[]}
for _, row in df.iterrows():
feature = {'type':'Feature',
'properties':{},
'geometry':{'type':'Point','coordinates':[]}}
feature['geometry']['coordinates'] = [row[lon],row[lat],row[z]]
for prop in properties:
feature['properties'][prop] = row[prop]
geojson['features'].append(feature)
return geojson
cols = ['name', 'description']
df_to_geojson(df, cols)
{'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coordinates': [-70.5867, -29.9953, 760]}, 'type': 'Feature', 'properties': {'name': 'A', 'description': 'Place a'}}, {'geometry': {'type': 'Point', 'coordinates': [-70.4933, -30.1217, 1250]}, 'type': 'Feature', 'properties': {'name': 'B', 'description': 'Place b'}}, {'geometry': {'type': 'Point', 'coordinates': [-70.5008, -30.0953, 1185]}, 'type': 'Feature', 'properties': {'name': 'C', 'description': 'Place c'}}]}
To explain the process with single features
for i, row in df.iterrows():
print i,
feature = {'type':'Feature','properties':{},'geometry':{'type':'Point','coordinates':[]}}
feature['geometry']['coordinates'] = [row.long,row.lat,row.elev]
for prop in cols:
feature['properties'][prop] = row[prop]
print feature
0 {'geometry': {'type': 'Point', 'coordinates': [-70.5867, -29.9953, 760]}, 'type': 'Feature', 'properties': {'name': 'A', 'description': 'Place a'}}
1 {'geometry': {'type': 'Point', 'coordinates': [-70.4933, -30.1217, 1250]}, 'type': 'Feature', 'properties': {'name': 'B', 'description': 'Place b'}}
2 {'geometry': {'type': 'Point', 'coordinates': [-70.5008, -30.0953, 1185]}, 'type': 'Feature', 'properties': {'name': 'C', 'description': 'Place c'}}