Select to view content in your preferred language

How to preserve OID's in Spatial Join done with Model Builder Python script

1940
3
09-28-2011 06:22 AM
by Anonymous User
Not applicable
Original User: gdporto

I want to know how to preserve OID's in a spatial join. I have done a spatial join between a point and a polygon feature class. The output is a new point feature class with the fields of both point and polygon features (each point was given the attributes of the polygon that it is located in). I have created a Model Builder Python script to run this join and overwrite the newly creat point feature. The only problem is I need to OID's of my original point feature to persist in the output feature class. I am doing this in an SDE database. I have attached the Python script (.py) as well as the same script in .txt form.
Thank You!
0 Kudos
3 Replies
MathewCoyle
Honored Contributor
The best solution I have found is to copy the original OID field, and then carry on. Changing the copied OID field to the actual OID field, if necessary, would require more than the arcpy package has to offer I believe.
0 Kudos
by Anonymous User
Not applicable
Original User: mnakleh

I can't help but mention that you could shorten a LOT of your code by using something like:
dict_temp = {}
for variable in dir(arcpy.env):
    dict_temp[variable] = eval('arcpy.env.' + variable)
to save your environment variables (in ArcGIS10)
and something like:
for k,v in dict_temp.iteritems():
    setattr(arcpy, 'env.' + k, v)

To rewrite the saved values to the actual environment variables again. It's customary to warn people that using eval allows the execution of arbitrary code if you don't keep a tight reign on waht goes into your arcpy.env variables, but I think that a case like yours, where it's run within the confines of your script so as to not manually enter values one by one, is just perfect for this.
I'm pretty convinced that this would hold in 9.3, but I'm gonna go check it right now.

Also, though I find it unfortunate, I don't think that there's an easy way of mapping the original OID unto your Spatially Joined feature class. I've actually never really liked the Mappings object in ArcGIS, but that might just be me 🙂
Creating a new reference field that has your OID values should definitely work, as you'll be able to map that to your output.

Cheers,
Marc
0 Kudos
MarcNakleh
Regular Contributor
hm... it seems like the 9.3 geoprocessing object doesn't store any of the environment variables in a way that can be called up through dir()... even inspect seems to fail.

well, could still do something like:
funcs = ['autoCommit','cartographicCoordinateSystem','cellSize','coincidentPoints',
             'compression','configKeyword','derivedPrecision','extent',
             'geographicTransformations','mask','MDomain','MResolution','MTolerance',
             'newPrecision','outputCoordinateSystem','outputMFlag','outputZFlag','outputZValue',
             'overwriteoutput','projectCompare','pyramid','qualifiedFieldNames','randomGenerator',
             'rasterStatistics','referenceScale','scratchWorkspace','snapRaster','spatialGrid1',
             'spatialGrid2','spatialGrid3','terrainMemoryUsage','tileSize','workspace','XYDomain',
             'XYResolution','XYTolerance','ZDomain','ZResolution','ZTolerance']

dict_temp = {}
for variable in funcs:
    dict_temp[variable] = eval('gp.' + variable)


for the first part....
0 Kudos