Change linear unit of spatial reference for geometry in cursors?

2614
3
Jump to solution
12-09-2013 02:21 PM
ZachLiu1
Occasional Contributor II
Hi, I want to change the spatial reference linear unit for geometry calculations.

I know you can pass the sr object in cursor, so I tried this:

I read from the document that linearUnitName is both readable and writable.

sr = arcpy.Describe(inLine).spatialReference sr.linearUnitName = "Foot" cursor = arcpy.da.SearchCursor(fc, ["SHAPE@"], "", sr)


but it looks not working.

Could anyone help?:D

I am trying to find points along polylines based on distance, so I need the distance in another unit.

What else could I do?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ZachLiu1
Occasional Contributor II
All right, thanks to the post on Arcpy Cafe, I figured this out.

http://arcpy.wordpress.com/tag/spatialreference/

It looks like changing sr.linearUnitName doesn't change the spatial unit,

but this solution works!

View solution in original post

0 Kudos
3 Replies
ShaunWalbridge
Esri Regular Contributor

cursor = arcpy.da.SearchCursor(fc, ["SHAPE@"], "", sr)



There are two separate SearchCursors, the top-level one, and the one within the data access module. The data access module one that you're using above is designed for performance, and in doing so loses some of the flexibility of the top-level one. Try changing the call to:

desc = arcpy.Describe(inLine)
sr = desc.spatialReference
sr.linearUnitName = "Foot"
shapefieldname = desc.ShapeFieldName

rows = arcpy.SearchCursor(fc, "", sr)

for row in rows:
    feat = row.getValue(shapefieldname) # this feature should be reprojected.
    # do further manipulations as needed with the feat


That should work, and the documentation for arcpy.SearchCursor states "spatial_reference: When specified, features will be projected on the fly using the spatial_reference provided.".

cheers,
Shaun
0 Kudos
ZachLiu
New Contributor
Thanks for help. It is still not working for some reason.
I tried to test if I can change the linearUnitName of the spatial reference by


 sr = desc.spatialReference
 sr.linearUnitName = "Meter"
 print sr.linearUnitName


but it is not changed at all.
0 Kudos
ZachLiu1
Occasional Contributor II
All right, thanks to the post on Arcpy Cafe, I figured this out.

http://arcpy.wordpress.com/tag/spatialreference/

It looks like changing sr.linearUnitName doesn't change the spatial unit,

but this solution works!
0 Kudos