I want to follow that post More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc
My target is to count points in polygons and export to new shapefiles the polygons that have a specific number of points on the count or score.
import fiona
from shapely.geometry import shape
polygons = [pol for pol in fiona.open('poly.shp')]
points = [pt for pt in fiona.open('point.shp')]
# attributes of the polygons
for poly in polygons:
print poly['properties']
for pt in points:
print i['properties']
for i, pt in enumerate(points):
point = shape(pt['geometry'])
#iterate through polygons
for j, poly in enumerate(polygons):
if point.within(shape(poly['geometry'])):
# sum of attributes values
polygons[j]['properties']['score'] = polygons[j]['properties']['score'] + points[i]['properties']['score']
If I run this code with my shapefiles I take keyerror 'score', that is because on my files I don't have a score field.
How to fix that without changing my schema from the input shapefiles?
I don't want to import new field in my files. Any ideas?
