wtorek, 22 października 2013

Updating a field in a Geodatabase

I wanted to update in ArcGIS a point feature class with values representing polygons ID's in which the points are located:



I could do this by means of "Join Data" tool based on spatial location (ArcMap > Table of content > local menu > Joins and Relates > Join...). But this tool creates a new feature class which I do not need.

I could use "Spatial Join" tool from the ToolBox but then also a new feature class  is created ("The output must be unique from the input.").

Maybe there's a way in ArcGIS to just update like I wanted but I couldn't find it (some people say there isn't - see:

So I run SQL Server client and typed:

UPDATE Geobaza_08.dbo.PUNKT_ODWIERT
SET OBSZAR_ID = ob.Obszar_ID
FROM Geobaza_08.dbo.PUNKT_ODWIERT po, Geobaza_08.dbo.OBSZAR_BADAN ob
WHERE po.SHAPE.STIntersects(ob.SHAPE) = 1
;


I refreshed the table and I got what I needed:


I tried to make this in ArcGIS without using pure SQL. So I used Python and Arcpy (there should be an edit session opened before executing the following script):


#create update cursor 
uc = arcpy.da.UpdateCursor("PKTO",("OBSZAR_ID","SHAPE@","OBJECTID")) 
#iterate on points
for pkt in uc: 
     #select a polygon containing the point
     arcpy.SelectLayerByLocation_management("OBSZ","CONTAINS",pkt[1]) 
     #create select cursor from the selection
     obs = arcpy.SearchCursor("OBSZ")   
     #iterate on the select cursor 
     for ob in obs: 
          #write the selected polygon id into point field
          pkt[0] = ob.getValue("Obszar_ID")
          #update row in update cursor
          uc.updateRow(pkt)


Hmm... I rather prefer simple SQL update in the DBMS.