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

10002
28
Jump to solution
11-30-2015 02:08 PM
AdrianWelsh
MVP Honored 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
28 Replies
DarrenWiens2
MVP Honored Contributor
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?

I don't think it's cheating to manipulate the prj file directly. You're just getting over the frightfulness of operating outside of what ESRI wants you to believe is the right and only way to do things (i.e. through the GUI or copying a GUI-made prj file).

Granted, there are smart people making these tools and they may have thought of something you haven't, yet, and built in a check for that. And, yes, it is easy to screw things up if you're not careful. And, it's easier still for someone else to screw things up once you've made something and released it into the wild. As a rule, you should always verify user entry because they will find a way to do it wrong.

Whether this is the best way, I don't know. If you're making different custom CRSes, one way or another you need to get those parameters into a prj file, whether you have them do it through the GUI to copy later or enter the values into your custom tool to write a dynamic prj file.

MargaretMaher
Esri Contributor

Hello Adrian, Rather than continually creating custom projection files for your CAD data, I recommend identifying the projection for the DGN files first in ArcMap.  A lot of CAD data is actually being created in a recognized coordinate system that is already supported in ArcGIS Desktop, so that a custom projection file is not even necessary.  To go through this process start by reading Knowledge Base article 29129 from the Support Center.  That will help you identify the type (Geographic, Projected or Local) that the data is actually created in.  The article contains links to additional articles that provide instructions on how to deal with what you find. You might also want to check out my book "Lining Up Data in ArcGIS: a guide to map projections (second edtition)" from Esri Press.  Chapters 4, 5 and 6 deal specifically with projections for CAD files, although the processes also work for other types of data with unknown coordinate systems.  Hope this is helpful and Happy Holidays!

DarrenWiens2
MVP Honored Contributor

Here's a link to the above referenced knowledge base article.

I'd say it's no more than 50/50 whether CAD is being created in a recognized CRS. Perhaps a recognized projection, but in my experience CAD operators prefer to adjust the origin closer to local, which requires adjustment, and thus a custom prj file.

The above knowledge base article does link to this other useful article, which outlines how to

Create a custom projection file to align CAD Data in ArcMap 10.1 and above

... but now we've circled back to creating a new .prj file.

AdrianWelsh
MVP Honored Contributor

I agree with both of you. It would be nice to not have to create a custom .prj file each time (and I'd be curious to check out your book, Margaret Maher​), but I have seen what Darren is mentioning on how surveyors prefer to adjust the origin, etc. And yes, we have circled back around!

0 Kudos
NobbirAhmed
Esri Regular Contributor

If you know the WKID - just the number then it's easy to create one.

# 32145 is the code for:
#  NAD 1983 StatePlane Vermont FIPS 4400 (Meters)
sr = arcpy.SpatialReference(32145)

You can now use this sr object in ArcGIS, for example as:

arcpy.Project_management(r'D:\data\fc.shp', r'D:\data\fc_project', sr)

You can get the WKID from EPSG directory https://www.epsg-registry.org/

0 Kudos
AdrianWelsh
MVP Honored Contributor

That is true - it isn't too hard to create one if you know the WKID. For the projects that I am working on, each prj will have to be a custom made projection file with different values for the parameters (such as false easting, false northing, etc). So, unfortunately, this method will not work for me. Thanks though!

DuncanHall
New Contributor

Hi

I had a similar problem, but fortunately the parameters for official local project grids are available in a spreadsheet, so I wrote some string functions in VBA to create .prj strings from the parameters and export them to .prj files. There were only 2 datums to consider so I extracted them from existing .prj files as string constant parameters.

This was a one off exercise and with these added to my favorites it's a simple matter of testing 2 or 3 likely projections against the drawing to see if it lands in the correct location.

I created polygons from the projection boundaries to see which ones to test knowing the project location

I also did the same for PROJ4 strings for use with other software.

This of course only works if grids are not created for each project. However new .prj files can be created simply by adding the parameters to the spreadsheet and writing to file which is also scripted

0 Kudos
AdrianWelsh
MVP Honored Contributor

Ah VBA. Those were good times!

Thanks for the suggestions.

0 Kudos
MargaretMaher
Esri Contributor

Adrian, I would really like to talk to you about this issue, because I have a great deal of information that I can provide that will help you move forward.  However, when you receive a DGN file that is in a local projection, you do not have any way of knowing what the revised values for the False Easting and False Northing need to be in your custom projection file.  Also, if a rotation is involved, the Lambert Conformal Conic projection does not support a rotation parameter, but DOES support a Scale Factor if scaling is involved.  Also, Microstation uses the International Foot, rather than the US Survey Foot by default so you have to be able to verify the units for the projection.  If you have a maintenance agreement for your software with Esri, please give Support Services a call and ask to talk to me.  Thank you!  Margaret Maher, author of "Lining Up Data in ArcGIS: a guide to map projections".

0 Kudos