Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Last active November 21, 2017 11:45
Show Gist options
  • Save nishadhka/97b06c21235f57552b5e471b5b6480a8 to your computer and use it in GitHub Desktop.
Save nishadhka/97b06c21235f57552b5e471b5b6480a8 to your computer and use it in GitHub Desktop.
Pandas dataframe into Fiona geometric dictionary of points
import pandas as pd
from shapely.geometry import Point, shape
from geopandas import GeoDataFrame
import fiona
from collections import OrderedDict
# a test to create shape file
df=pd.read_csv('VNP14IMGTDL_NRT_South_Asia_24h.csv')
geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
df = GeoDataFrame(df, geometry=geometry)
df.to_file(driver = 'ESRI Shapefile', filename= "testpoint.shp")
# a test to see how the fiona looks if it read from shape file
points = [pt for pt in fiona.open('testpoint.shp')]
print point
# a functin to convert the dataframe rows into fiona dict
def extract_point_coords(index,row):
geom=row['geometry'].coords[:][0]
res = zip(row[rdd].index,row[rdd])
return {'geometry':{'coordinates': geom,'type': 'Point'},
'id':index,
'properties':OrderedDict(res),
'type': 'Feature'}
dd=df.loc[:, df.columns != 'geometry']
rdd=dd.columns
pointpd=[]
for index, row in df.iterrows():
ee=extract_point_coords(index,row)
pointpd.append(ee)
# pointpd and point are same now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment