Getting the Latitude of Origin for a spatial reference

4295
10
Jump to solution
06-05-2013 01:12 PM
timloesch
New Contributor
Greetings,

I'm using Python to extract the parameters of projected coordinate systems but I'm finding that the SpatialReference object does not have a .latitudeOfOrigin property that I need to extract.

When I look at the projection parameters in ArcMap the latitude of origin is there but I cannot find where to access that value in the SpatialReference object. I can get all of the other parameters but this seems to be missing.

Can someone point me in the right direction?

thanks

tim
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ShaunWalbridge
Esri Regular Contributor

NOTE: as of Version 10.3, the SpatialReference object contains the "latitudeOfOrigin" property, and the trick below is unncessary.

Hi Tim,

It looks like a property which should be included in the SpatialReference object, but I don't see it either. For a quick and dirty hack, you can pull the relevant string out of the exportToString() results:

import arcpy
import re 
# a projection with a latitude of origin property 
proj = 'NAD 1983 Contiguous USA Albers'  
sr = arcpy.SpatialReference(proj)  
lat_of_origin_regex = "Latitude_Of_Origin',(\d+\.?\d*)]"
match = re.search(lat_of_origin_regex, sr.exportToString())
if match:
 lat_of_origin = float(match.groups()[0])
    print "{0} has latitude of origin {1}".format(proj, lat_of_origin)
else:
    print "no latitude of origin detected."

Set up your own spatial reference object in the code above, and you should be able to use the lat_of_origin variable to get what you need.

cheers, Shaun

View solution in original post

0 Kudos
10 Replies
ShaunWalbridge
Esri Regular Contributor

NOTE: as of Version 10.3, the SpatialReference object contains the "latitudeOfOrigin" property, and the trick below is unncessary.

Hi Tim,

It looks like a property which should be included in the SpatialReference object, but I don't see it either. For a quick and dirty hack, you can pull the relevant string out of the exportToString() results:

import arcpy
import re 
# a projection with a latitude of origin property 
proj = 'NAD 1983 Contiguous USA Albers'  
sr = arcpy.SpatialReference(proj)  
lat_of_origin_regex = "Latitude_Of_Origin',(\d+\.?\d*)]"
match = re.search(lat_of_origin_regex, sr.exportToString())
if match:
 lat_of_origin = float(match.groups()[0])
    print "{0} has latitude of origin {1}".format(proj, lat_of_origin)
else:
    print "no latitude of origin detected."

Set up your own spatial reference object in the code above, and you should be able to use the lat_of_origin variable to get what you need.

cheers, Shaun

0 Kudos
ChrisSnyder
Regular Contributor III
Seems like a bug to not have that property available...
0 Kudos
RhettZufelt
MVP Frequent Contributor
One option would be to open the *.prj file (just a text file) and extract that from there (PARAMETER["Latitude_Of_Origin",45.33333333333334]PARAMETER["Latitude_Of_Origin",45.33333333333334]) for my WKID.

R_

I see this is more or less the same that Shaun mentioned above except he gets it from the sr object, not the file (not sure if one is faster), but the match code is pretty much the same.
0 Kudos
ChrisSnyder
Regular Contributor III
Note there are no more system-based .prj file in v10.1 anymore (!!!!) But you can manufacture one via the SR's exportToString() method and then write it to a file.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Note there are no more system-based .prj file in v10.1 anymore (!!!!) But you can manufacture one via the SR's exportToString() method and then write it to a file.


Had not noticed that.  There are so many bugs introduced to the new python, I only utilize 10.1 version for capabilities that are not in 10.0 version (at least for the commands that work.. (Like updatecursor is now reserved for Enterprise SDE users only.....)).

I see you can copy the v10.0 prj files over and use them if you like.  Was easier for me than to exportToString() method as I didn't need code 😮

R_
0 Kudos
ShaunWalbridge
Esri Regular Contributor
Seems like a bug to not have that property available...


Agreed, I've filed a bug for the problem as NIM092211.
0 Kudos
ShaunWalbridge
Esri Regular Contributor
Note there are no more system-based .prj file in v10.1 anymore (!!!!) But you can manufacture one via the SR's exportToString() method and then write it to a file.


If you'd like a full set of PRJ files, you can generate them fairly easily. I've posted a short script which will populate your 'Coordinate Systems' directory with all internal projections here: https://gist.github.com/scw/5720029
timloesch
New Contributor
Thanks Shawn,

I agree with that this seems to be an error of ommission and really expected it just to be there.

But your solution of exporting the spatial reference to a string and then extracting the latitude of origin using regex was the key.

I had not seen the extractToString prior to now but I can think of other ways to use it.

tim
0 Kudos
MichaelNesius
Occasional Contributor

thanks for this answer!

Note: with 10.3 you'll need to use lat_of_origin_regex = "latitude_of_origin',(\d+\.?\d*)"  with lowercases

also, you can also use the factoryCode in the arcpy.SpatialReference()