Creating custom projected coordinate systems using ArcPy

3715
7
Jump to solution
01-09-2017 01:01 PM
PaulGosling1
New Contributor II

I am trying to write an ArcPy script which takes a user input feature class polygon and creates a custom version of a projected coordinate system based on the Orthographic projection (which has the WKID 43041) and the centroid of the polygon. All of the help pages are based on creating spatial references which are already defined Projected Coordinate Systems, but none which show how to create a new custom PCS.

I have tried various versions of creating an instance of the SpatialReference class, setting properties then using the create method, such as the following snippet of my code but I keep getting the 'Error executing function' message below.

Runtime error

Traceback (most recent call last):

File "<string>", line 1, in <module>

File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\arcobjects.py", line 1226, in create

return convertArcObjectToPythonObject(self._arc_object.Create(*gp_fixargs(args)))

RuntimeError: ERROR 999999: Error executing function.

This is something which I find straightforward using the Data Frame Coordinate Reference System properties in ArcMap but is proving beyond me using ArcPy, so any assistance or examples of the correct code to define a custom projection gratefully received.

0 Kudos
1 Solution

Accepted Solutions
NeilAyres
MVP Alum

I have done this in the past by using a "template" string. You can copy this from your known, existing projection.

Then use the string formatting in python to modify the parameters to what you want.

A bit like this :

prjTemplate = 'PROJCS["My_local_projection",GEOGCS["GCS_Cape",DATUM["D_Cape",SPHEROID["Clarke_1880_Arc",6378249.145,293.466307656]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Local"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Scale_Factor",{0}],PARAMETER["Azimuth",{1}],PARAMETER["Longitude_Of_Center",16.661764589799],PARAMETER["Latitude_Of_Center",-28.407083714978],UNIT["Meter",1.0]]'

srLocalString = prjTemplate.format(1.0, 40.0) # set new parameters here, see placeholders in prjTemplate
srLocal = arcpy.SpatialReference()
srLocal.loadFromString(srLocalString)

View solution in original post

7 Replies
MelitaKennedy
Esri Notable Contributor

Try also setting: 

PCSName

GCS (perhaps by the GCSCode instead, 4326 for WGS84, 4269 for NAD83)

There also might be a problem with the parameters. Orthographic uses Lon/Lat of Center and parameters exposed here might not be converting to the correct ones internally. So if you do get a valid SR, make sure the results make sense.

Melita

DanPatterson_Retired
MVP Emeritus

is there some code missing? like the import arcpy line and the line where you define the input_fc?  because the script fails at line 1, and doesn't have to do with anything else by all appearances.  If you have the rest of the code, please update your intial post with the full code

0 Kudos
NeilAyres
MVP Alum

I have done this in the past by using a "template" string. You can copy this from your known, existing projection.

Then use the string formatting in python to modify the parameters to what you want.

A bit like this :

prjTemplate = 'PROJCS["My_local_projection",GEOGCS["GCS_Cape",DATUM["D_Cape",SPHEROID["Clarke_1880_Arc",6378249.145,293.466307656]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Local"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Scale_Factor",{0}],PARAMETER["Azimuth",{1}],PARAMETER["Longitude_Of_Center",16.661764589799],PARAMETER["Latitude_Of_Center",-28.407083714978],UNIT["Meter",1.0]]'

srLocalString = prjTemplate.format(1.0, 40.0) # set new parameters here, see placeholders in prjTemplate
srLocal = arcpy.SpatialReference()
srLocal.loadFromString(srLocalString)
NeilAyres
MVP Alum

But I see this for wkid 43041

>>> import arcpy
>>> sr = arcpy.SpatialReference(43041)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects\mixins.py", line 962, in __init__
    self._arc_object.createFromFile(item)
RuntimeError: ERROR 999999: Error executing function.
the input is not a geographic or projected coordinate system
>>> 

So, what sort of Orthographic projection are we talking about here?

0 Kudos
PaulGosling1
New Contributor II

Thanks for all of the useful comments, and my original script is getting quite long and complicated which is why I just cut out the bit relating to creating the SpatialReference. Now that I have had a go using the String representation it looks like Melita is right that my definition was incomplete and using the wrong properties for that projection, but in any case the string method appears to be the best way to go. I have successfully run my script by copying the string from a PRJ file of an Orthographic projection saved from ArcMap, and now just need to turn it into the template suggested by Neil to read the centroid lat/long from my input polygons.

I think an example in the ArcGIS Help pages for creating custom projected coordinate systems in ArcPy would still be really useful though.

PaulGosling1
New Contributor II

Just to answer your question Neil, Orthographic is a Projection Name and not a Projected Coordinate System so this error is understandable. PCS are built using a specific projection and the other parameters required to define its use for a particular part of the world. For example, UTM grid zones are based on the Transverse Mercator projection, which is WKID 43006 but in that case you would use the WKID for the UTM zone you want in the SpatialReference class.

Orthographic is an azimuthal perspective projection which has the effect of viewing the Earth from an infinite distance in space thereby making the map appear as a globe, not dissimilar to the perspective projection used by Google Earth but in that case you can obviously alter the viewing distance. Like a number of map projections Orthographic does not form the basis for any PCS and therefore it requires a custom version with user-defined central latitude and longitude.

0 Kudos
NeilAyres
MVP Alum

Yes, I realise that wkid 43041 is a projection type rather than a fully formed PCS.

So, you need to set one up perhaps centred on the middle of your project area. If you save it in favourites, you can find the prj in userbanme/appdata etc etc.

Open that up with a text editor, then copy it into your python script. Becareful to surround the entire string with single quotes (all the internal ones are double quotes).

Then edit the string to put the formatting placeholders "{}" at the places you require.

Then you're good to go.