Select to view content in your preferred language

UpdateCursor not respecting spatial reference

905
1
09-14-2011 02:04 PM
by Anonymous User
Not applicable
I have a script that reports out the bounding coordinates (hull rectangle) for features in a fc, using a print statement.

I changed the script to use and UpdateCursor to write the coordinates to the fc instead of printing it out in the Python window.

When I comment out the update rows line, the results print out correctly, using the spatial reference (wgs84), but when I uncomment the update rows line, the results print out in their native coordinates and the spatial reference is not used.

Here is the script:

# Import system modules
import arcinfo, time, arcpy

print "Start time: " + str(time.strftime("%H:%M:%S"))
sttime = time.time()

# Set the path to the Tracts dataset
Tracts_Path = r"C:\Scratch_Katie\Test.mdb\CA_TNC_TRACTS"

# Create a spatial reference object and use a projection file to define the spatial reference's properties, this will be used
# to reproject the results on the fly to WGS84 the spatial reference object is a parameter specified in the searchcursor object

prjFile = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
sr = arcpy.SpatialReference(prjFile)

# Open a update cursor update rows with coordinates transformed into wgs84, also print out information

rows = arcpy.UpdateCursor (Tracts_Path,"DEALTRACKER_ID is not NULL",sr)

for row in rows:
# when these two lines are commented out this script prints out the coordinates in wgs84, when they are uncomments the script
# prints out the coordinates in their native coordinate system, and the spatial reference is ignored
#  row.EXTENT_COORDS = "?extent=" + str(row.shape.extent.XMin) + "," + str(row.shape.extent.YMin) + "," + str(row.shape.extent.XMax) + "," + str(row.shape.extent.YMax)
#  rows.updateRow(row)
  print row.TRACTNAME
  print row.DEALTRACKER_ID
  print "?extent=" + str(row.shape.extent.XMin) + "," + str(row.shape.extent.YMin) + "," + str(row.shape.extent.XMax) + "," + str(row.shape.extent.YMax)


del rows
del Tracts_Path
del sr

print "End time: " + str(time.strftime("%H:%M:%S"))
endtime = time.time()
print "Elapsed time (mins): " + str((endtime - sttime)/60)
Tags (2)
0 Kudos
1 Reply
StacyRendall1
Frequent Contributor
That is odd...

Try writing the string to a variable beforehand, then printing it and adding it to the field. This way you know absolutely it should be writing the same thing as it is printing. However, it may be that the extent object itself is being converted for some reason...
for row in rows:
# when these two lines are commented out this script prints out the coordinates in wgs84, when they are uncomments the script
# prints out the coordinates in their native coordinate system, and the spatial reference is ignored
  extentStr = "?extent=" + str(row.shape.extent.XMin) + "," + str(row.shape.extent.YMin) + "," + str(row.shape.extent.XMax) + "," + str(row.shape.extent.YMax)
  row.EXTENT_COORDS = extentStr 
  rows.updateRow(row)
  print row.TRACTNAME
  print row.DEALTRACKER_ID
  print extentStr


You could also (although I really doubt it would make a difference) try using setValue on the row, rather than row.FIELD = value, i.e.:
  extentStr = "?extent=" + str(row.shape.extent.XMin) + "," + str(row.shape.extent.YMin) + "," + str(row.shape.extent.XMax) + "," + str(row.shape.extent.YMax)
  row.setValue(EXTENT_COORDS, extentStr)
0 Kudos