Skip to content

Instantly share code, notes, and snippets.

@fawcett
Created March 27, 2013 03:37
Show Gist options
  • Save fawcett/5251407 to your computer and use it in GitHub Desktop.
Save fawcett/5251407 to your computer and use it in GitHub Desktop.
This is the fifth in a series of tests using fiona and shapely to read features from shapefiles and do intersect operations on them. The features represent station points in counties. There are 7142 stations and 87 counties with mildly overlapping bboxes. This example adds an RTree index based on the rtree module. In this example, the strategy i…
import fiona, time
from shapely.geometry import shape
from shapely import speedups
from rtree import index
searchTime = time.time()
stationPath = "C:/stations.shp"
countyPath = "C:/counties.shp"
stationCollect = fiona.open(stationPath,'r')
countyCollect = fiona.open(countyPath,'r')
stnCntyList = []
idx = index.Index()
for county in countyCollect:
geom = shape(county['geometry'])
idx.insert(int(county['id']), geom.bounds, obj={'properties': county['properties'], 'geom':geom})
for station in stationCollect:
aPoint = shape(station['geometry'])
for hit in idx.intersection((aPoint.x,aPoint.y,aPoint.x,aPoint.y), objects="raw"):
if hit['geom'].intersects(aPoint):
stnCntyList.append(dict([(station['properties']['SYS_LOC_CO'],hit['properties']['COUNTYFIPS'])]))
searchTime = time.time() - searchTime
print searchTime
print "Length " + str(len(stnCntyList))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment