I have a series of fields and roads. I am trying to find the nearest road to each field. I want to add field to each of my polygons with the index/name etc. of the closest road.
Would this be the correct method to find the nearest road for each field?
Below is the code I am trying
So I have loaded my shapes and created an empty index.
fieldlyr = fiona.open(fields, 'r')
roadlyr = fiona.open(roads, 'r')
# search radius, i've changed sizes to make it large enough
areaft = 2500
index = rtree.index.Index()
I add my fields to the index.
for fid, feat in fieldlyr.items():
geometry = shape(feat['geometry'])
index.insert(fid, geometry.bounds)
I then buffer my roads
for feat in roadlyr:
geometry = shape(feat['geometry'])
geometry_buffered = geometry.buffer(areaft)
Then I believe this will give me the lists of intersections.
fids = [int(i) for i in index.intersection(geometry_buffered.bounds)]
How would I get road "id"
A is nearest to polygon "id"
2 and so on.
Is this even the correct method?