Create point feature class (shapefile) using Fiona

Xiaojiang Li
1 min readDec 10, 2019

--

It is very simple to write a point feature class based on the coordinate information. You can use ArcGIS, QGIS or other software packages to implement it. However, in some cases, you may find you need to know how to write the shapefile in a more flexible way, which is based on Programing in Python. You can use GDAL, but GDAL is difficult to install some times. You can also use Geopandas, but Geopandas cannot handle large datasets. Therefore, we can use another module — Fiona, which is located between GDAL and Geopandas. Here is a simple code to show you how to write a shapefile from the longitude, latitude, and attributes using Fiona and Shapely.

import fiona
from fiona.crs import from_epsg
from shapely.geometry import Point, mapping
outname = 'firstPeriodNYC.shp'cursor.execute(query) #in case you do query from database# prepare the schema for your shapefile
schema = {'geometry': 'Point',
'properties': {'num': 'int',
'panodate': 'str',
'panoid': 'str',
}
}
# write your shapefile, as projection of epsg: 4326
with fiona.open(outname, 'w', 'ESRI Shapefile', schema=schema, crs = from_epsg(4326)) as out_pnt:
for row in cursor.fetchall():
num, date, panoid, lon, lat = row
point = Point(float(lon), float(lat))
out_pnt.write({'geometry': mapping(point), 'properties'{
'num': num, 'panodate': date,'panoid': panoid}})

--

--

Xiaojiang Li

Spatial Data Scientist, Urban Scientists, Prof at UPenn, Founder of Biomteors, Alum of MIT Senseable, http://www.urbanspatial.info, http://www.biometeors.com