How to programmatically create a .prj file for a CAD drawing

7945
28
Jump to solution
11-30-2015 02:08 PM
AdrianWelsh
MVP Notable Contributor

I think there must be an ‘easy’ way to do this but I can’t seem to find a comprehensive set of answers for this question.

I am given CAD files from Bentley MicroStation (.dgn files) that are projected in a local coordinate system. I then open a blank ArcMap and create a new Projected Coordinate System (they usually are in a Lambert Conformal Conic system, US Survey Feet, have a Geographic Coordinate System of NAD83, and have a series of inputs for the parallels, easting/northing, scale factor, etc). After that, I load the .dgn file and see if it lines up where it is supposed to.

Some code snippets I have seen show how to get to the false easting/northing, 1st and 2nd standard parallel, and such, but I have not found all of the places to enter in all of the values.

I hope what I am asking makes sense. I am guessing you use the SpatialReference library within arcpy in Python (after I figure out how to program the inputs, I plan on making a form where the user types in certain values, etc). Please show me the direction I need to go for this!

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

You appear to be back to this method, so just open and write the file, without an arcpy SpatialReference whatsoever. To reiterate:

prj = open(r"C:\junk\my_new_prj_file.prj", "w"
crs_string = 'PROJCS["NAD_1983_UTM_Zone_10N",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]'  
prj.write(crs_string)  
prj.close() 

View solution in original post

28 Replies
DanPatterson_Retired
MVP Esteemed Contributor

SpatialReference—Help | ArcGIS for Desktop

Using the spatial reference class—Help | ArcGIS for Desktop

The latter contains geographic and projected coordinate information as well as information on transformations, the former, is the Class

AdrianWelsh
MVP Notable Contributor

Dan, thanks for the help. I had looked at the SpatialReference class site. I looked at the second link and saw where it had information on creating a spatial reference. However, this ends up showing how to create a feature class with a spatial reference. Is there another Pyhon command that would create just the .prj file? That is essentially what I am initially trying to do. Thanks!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Well there is this Projection files for CAD datasets—Help | ArcGIS for Desktop which uses nothing more than a string.  If you are using the same one all the time, basically what you would have to do is create/copy the text contents and make an output filename to match your cad input's filename, with a *.prj extension.  If you have a couple that you use, just get the wkt text contents for each and allow the user to select one.  Saves a lot of loops.  Do be aware, that you will have to create the prj files BEFORE loading the cad data...or at worse, you will have to reload it before the prj takes affect.

AdrianWelsh
MVP Notable Contributor

Dan, this is how the process is for now. Each project that we do in CAD, a new coordinate system is created - a local one. Once that project is complete, we want to convert the CAD to GIS. In doing this, we open a blank ArcMap, create the projection file (which inherently assigns the blank ArcMap project to that projection), then load the CAD data to verify it is in the right place.

From looking at the link that you sent, that text string looks exactly like the parameters that I would want to put in (with using user-inputted variables). What I am really looking for is a Python script that will create this .prj file so that I can use it for the CAD drawing and then eventually I will export the drawing into a GDB or something similar.

Do you know how to utilize the Python script for the SpatialReference class in order to create this .prj file and then throw in the inputs from that CAD text from your link?

Thanks so much for your assistance!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you can either rebuild the car yourself or get one prebuilt

import shutil
src = r"f:\test\2951.prj"
dest = r"f:\test\a_copy_of_2951.prj"
shutil.copy2(src,dest)
print("Copied ... {}\nTo.... {}".format(src,dest))

get it?

DarrenWiens2
MVP Honored Contributor

And as Dan Patterson​ says, you can rebuild the car yourself (well, I guess I'm not sure if this is rebuilding or getting one prebuilt. It makes a prj file, that's all I know):

prj = open(r"C:\junk\my_new_prj_file.prj", "w")
crs_string = 'PROJCS["NAD_1983_UTM_Zone_10N",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]'
prj.write(crs_string)
prj.close()
AdrianWelsh
MVP Notable Contributor

Darren,

I like where this is headed! I took Dan's advice and copied the car into a re-model of the car, so I could have a .prj file that I could manipulate.

I took your code snippet and as long as I use the right values, this seems to work!

However, this feels like cheating. Is it easy to screw stuff up when doing it this way? Or should I just double check each time to make sure it was all entered correctly? Is this the best way to modify/create a .prj file with how I am going through the process?

Thanks guys!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

be cautious of just using a string representation...I have found that the innocent have sometimes split up the projection line to make it easier to read in the script (it is ridiculously long as you have probably noticed) and ended up with nasty newline characters embedded inside...This can be problematic in some python IDE's and/or setups.  You can also generate the necessary string from a list, of lists of elements, but it is ugly and I didn't bother to include it.

AdrianWelsh
MVP Notable Contributor

Dan, I was worried about that which is why I'm hesitant to just use the long string. I have a feeling that it will be more problematic than beneficial.

However, I guess I do not understand how to modify all of the SpatialReference elements within Python. I like using the copy method to make a new (or a copy of a) projection file. But how do you go in to change the elements? For instance, if I wanted to change the name of the projection (which is a read/write property), how do I do that? (Sorry for being so inept in Python!)

When I write it this way, this is what I get

>>> import arcpy
>>> sr = arcpy.SpatialReference(r"C:\Users\AWelsh\Documents\ArcGIS\TempPlaceHolder.prj")
>>> print sr.name
TempPlaceHolder
>>> sr.name = "TempPrj"
>>> print sr.name
TempPlaceHolder
>>> 

I know I am missing something simple but I am not sure what it is.

0 Kudos