I have a point layer with several NULL values and based on a near analysis with another layer, I would like to replace those NULLs. However, with the code I use it only does a couple and then stops. Is there something wrong with my for or if loops? I am sure that more values fall within the distance band of 1000 that I apply.
inFile0 = r'C:\data\areas.shp'
inData0 = driver.Open(inFile0, 0)
areaLayer = inData0.GetLayer()
inFile1 = r'C:\data\points.shp'
inData1 = driver.Open(inFile1, 1)
pointLayer = inData1.GetLayer()
index = r.tree.index.Index(interleaved = False)
for i in range(0, areaLayer.GetFeatureCount()):
areaFeat = areaLayer.GetFeature(i)
geometry = areaFeat.GetGeometryRef()
xmin, xmax, ymin, ymax = geometry.GetEnvelope()
index.insert(i, (xmin, xmax, ymin, ymax))
distance = 1000
for point in range(0, pointLayer.GetFeatureCount()):
pointFeat = pointLayer.GetFeature(point)
if pointFeat.GetField('CLASS') is None:
geometryP = pointFeat.GetGeometryRef()
xmin, xmax, ymin, ymax = geometry.GetEnvelope()
searchEnvelope = (xmin - distance, xmax + distance, ymin - distance, ymax
+ distance)
for area in list(index.intersection(searchEnvelope)):
areaFeat = areaLayer.GetFeature(area)
geometryA = areaFeat.GetGeometryRef()
if geometryP.Distance(geometryA) <= distance:
classType = areaFeat.GetField('FEAT_TYPE')
distance = geometryP.Distance(geometryA)
pointFeat.SetField('CLASS', classType)
pointFeat.SetField('DISTANCE', distance)
pointLayer.SetFeature(pointFeat)
else:
continue
FIXED: the loop reassigns a value to the variable 'distance' which should not happen, over time this would reduce the distance band breaking the loop.