2

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.

1
  • 2
    Please, add a minimum reproducible code, where a definition of feature2, index and areaLayer is provided.
    – mgri
    Mar 20, 2017 at 14:48

1 Answer 1

1

If you are using ARC a simpler solution is available. Using the definition query you are able to eliminate unwanted values such as "Null". You can find the definition query tab under the properties for the layer. By typing "[Field] Is NOT "Null"" it should eliminate all the null values within the attribute table.

Let me know if this works. You may have to adjust the syntax above slightly for it to work.

3
  • But I would like to keep those records, I want to set a value in the attribute where there is now a NULL.
    – Tins
    Mar 20, 2017 at 15:03
  • Now I understand, Try adding a field and than using the field calculator with the show code block box checked. You can than use a simpler loop statement. Not the best at python but here is a example using VBA
    – Reisenrich
    Mar 20, 2017 at 15:07
  • Wont let me edit for whatever reason here is the example vba code you code use in the field calculator Dim output If ( [Field] ) = "Null" Then output = 1 Else output=[Field] End If
    – Reisenrich
    Mar 20, 2017 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.