1

I am working with a cadastral project, and I need a solution for a certain process, neighboring. Based on ArcGIS.

Scenario: Have some parcels, every parcel has some neighbors.

I would like to add info in every parcel for:

1-Nearest neighbor grouped by direction (N,S,E,W)


import arcpy

try:
    #Read parameters from dialog
    fc = arcpy.GetParameterAsText(0)
    fieldName = arcpy.GetParameterAsText(1)

    #Add appropriate fields if they don't already exist
    if fieldName+"_N" not in arcpy.ListFields(fc):
        arcpy.AddField_management(fc, fieldName+"_N", "TEXT")
    if fieldName+"_S" not in arcpy.ListFields(fc):
        arcpy.AddField_management(fc, fieldName+"_S", "TEXT")
    if fieldName+"_E" not in arcpy.ListFields(fc):
        arcpy.AddField_management(fc, fieldName+"_E", "TEXT")
    if fieldName+"_W" not in arcpy.ListFields(fc):
        arcpy.AddField_management(fc, fieldName+"_W", "TEXT")

    #Make a feature layer for SelectByLocation
    lyrFile = "lyr"
    arcpy.MakeFeatureLayer_management(fc, lyrFile)

    #Function that creates a point feature and performs neighbor selections and returns adjacent page values
    def findLocation(x, y):
        arcpy.CreateFeatureclass_management(r"in_memory", "tempFC", "POINT")
        ptLayerCur = arcpy.InsertCursor(r"in_memory\tempFC")
        ptLayerRow = ptLayerCur.newRow()
        pt = arcpy.CreateObject("Point")
        pt.X = x
        pt.Y = y
        ptLayerRow.Shape = pt
        ptLayerCur.insertRow(ptLayerRow)

        arcpy.SelectLayerByLocation_management(lyrFile, "INTERSECT", r"in_memory\tempFC", "", "NEW_SELECTION")

        lyrCursor = arcpy.SearchCursor(lyrFile)
        lyrRow = lyrCursor.next()
        pageNumber = ""
        while lyrRow:
            #pageNumber = lyrRow.MAPSHEETNUMBER
            pageNumber = getattr(lyrRow, fieldName)
            lyrRow = lyrCursor.next()

        arcpy.Delete_management(r"in_memory\tempFC")

        return pageNumber

    #Loop through each index polygon and calculate appropriate neighbors   
    mapIndexCursor = arcpy.UpdateCursor(fc)
    mapIndexRow = mapIndexCursor.next()
    while mapIndexRow:
        #Calculate North Page XY values
        northX = (mapIndexRow.Shape.extent.XMin) + ((mapIndexRow.Shape.extent.XMax - mapIndexRow.Shape.extent.XMin) / 2)
        northY = (mapIndexRow.Shape.extent.YMax) + ((mapIndexRow.Shape.extent.YMax - mapIndexRow.Shape.extent.YMin) * 0.15)
        setattr(mapIndexRow, fieldName+"_N", findLocation(northX, northY))

        #Calculate South Page XY values
        southX = (mapIndexRow.Shape.extent.XMin) + ((mapIndexRow.Shape.extent.XMax - mapIndexRow.Shape.extent.XMin) / 2)
        southY = (mapIndexRow.Shape.extent.YMin) - ((mapIndexRow.Shape.extent.YMax - mapIndexRow.Shape.extent.YMin) * 0.15)
        setattr(mapIndexRow, fieldName+"_S", findLocation(southX, southY))

        #Calculate East Page XY values
        eastX = (mapIndexRow.Shape.extent.XMax) + ((mapIndexRow.Shape.extent.XMax - mapIndexRow.Shape.extent.XMin) * 0.15)
        eastY = (mapIndexRow.Shape.extent.YMin) + ((mapIndexRow.Shape.extent.YMax - mapIndexRow.Shape.extent.YMin) / 2)
        setattr(mapIndexRow, fieldName+"_E", findLocation(eastX, eastY))

        #Calculate West Page XY values
        westX = (mapIndexRow.Shape.extent.XMin) - ((mapIndexRow.Shape.extent.XMax - mapIndexRow.Shape.extent.XMin) * 0.15)
        westY = (mapIndexRow.Shape.extent.YMin) + ((mapIndexRow.Shape.extent.YMax - mapIndexRow.Shape.extent.YMin) / 2)
        setattr(mapIndexRow, fieldName+"_W", findLocation(westX, westY))

        mapIndexCursor.updateRow(mapIndexRow)
        mapIndexRow = mapIndexCursor.next()

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))
  arcpy.Delete_management(r"in_memory\tempFC")
  • How many parcels? What does grouped by direction mean? Surely each parcel only has 1 in each direction. Are the parcels adjacent? Do they share a boundary? If they do not share a boundary what about them counting as neighbors. – If you do not know- just GIS Nov 17 '14 at 14:20
  • The are around 2800 parcels. topology checked, so they fits perfectly. One parcel for example: parcel 1, has parcel 2 in West of it, has parcel 4 in East of it, has parcel 5 in North-west – Shefqet LULJA Nov 17 '14 at 14:27
3

Try using the Calculate Adjacent Fields, it will do this for 8 directions (NW, NE, SW, SE in addition to N, S, E and W). 8 fields are added with the neighbouring polyon in each direction. But there will be only one adjacent polygon for each direction, there is no grouping of the neighbours.

If you want one single field concatenating the neighbouring parcels' names - meaning you don't care about the direction but only about the fact that a parcel is a neighbour -,

  • add a text field long enough to hold the contents of the 8 direction fields
  • use an expression like

    !<NAME>_SE! + "," + !<NAME>_S! + "," + !<NAME>_SW! + ...

in the field calculator (Python parser). You will have commas separating nothing when one of the direction fields is empty, if you want to avoid this take a look at answers to this question: Concatenate only when all fields to be concatenated have values.

  • How can we add all polygons divided by comma for each side (N,S,E,W). – Shefqet LULJA Nov 17 '14 at 14:37
  • Do you have any idea for doing that? – Shefqet LULJA Nov 17 '14 at 14:53
  • !< !VL8602VL_P1.NR_PASURIS! >_SE! + "," + !< !VL8602VL_P1.NR_PASURIS! >_S! + "," + !< !VL8602VL_P1.NR_PASURIS! >_SW! – Shefqet LULJA Nov 17 '14 at 15:09
  • CANT CALCULATE IT!! – Shefqet LULJA Nov 17 '14 at 15:09
  • can you modify the script below? – Shefqet LULJA Nov 17 '14 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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