Trying to create a spatial reference - crashing

2379
10
Jump to solution
12-09-2013 08:46 AM
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Hey folks,

I'm at a loss as to what's going on. This code (from what I can remember) was working before I left on Friday, yet this morning during testing I'm getting a crash.

Quick explanation, it's part of a larger program to convert a GPX file into a feature class in a file geodatabase. Everything is great except when I try to determine the point's UTM coordinates. For those familiar, I'm in Canada where we use the dominion land survey. Long story short W4M = UTM Zone 12, W5M (and part of 6) is Zone 11. The code loops through the point and determines a bunch of locational information such as decimal degrees, the LSD-SEC-TWP ... etc...

Here's the troublesome code:

             Dim pPCS As IProjectedCoordinateSystem             Dim pSRFactory As ISpatialReferenceFactory2             Dim pSpatialReference As ISpatialReference             Dim pGeometry As IGeometry             Dim pGeogPoint As IPoint              pSRFactory = New SpatialReferenceEnvironment              If iMer = 4 Then                 pPCS = pSRFactory.CreateProjectedCoordinateSystem(ESRI.ArcGIS.Geometry.esriSRProjCSType.esriSRProjCS_NAD1983UTM_12N)                 sZone = "12U"             ElseIf iMer = 5 Or iMer = 6 Then                 pPCS = pSRFactory.CreateProjectedCoordinateSystem(ESRI.ArcGIS.Geometry.esriSRProjCSType.esriSRProjCS_NAD1983UTM_11N)                 sZone = "11U"             Else                 pPCS = Nothing                 sZone = "xxx"             End If              pSpatialReference = pPCS             pSpatialReference.SetFalseOriginAndUnits(200000, 4000000, 100)              pGeogPoint = pFreshPoint             pGeometry = pGeogPoint             pGeometry.Project(pSpatialReference)


From what I can see, it makes it to my If statement, then crashes. I think the problem is the "pPCS = pSRFactory.CreateProjectedCoordinateSystem" line.

I can't see what I'm doing wrong, all the snippets I've seen, have similar code setup. Any sort of help is much appreciated.

Edit: pFreshPoint is a point feature (we're currently in a larger Do loop to run through the points and perform this to each point). Let me know if more info is required.
0 Kudos
1 Solution

Accepted Solutions
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
I put the code from above into a try block with:

             Try                 Dim pPCS As IProjectedCoordinateSystem                 Dim pSRFactory As ISpatialReferenceFactory2                 Dim pSpatialReference As ISpatialReference                 Dim pGeometry As IGeometry                 Dim pGeogPoint As IPoint                  pSRFactory = New SpatialReferenceEnvironment                  If iMer = 4 Then                     pPCS = pSRFactory.CreateProjectedCoordinateSystem(26911)                     sZone = "12U"                 ElseIf iMer = 5 Or iMer = 6 Then                     pPCS = pSRFactory.CreateProjectedCoordinateSystem(26912)                     sZone = "11U"                 Else                     pPCS = Nothing                     sZone = "xxx"                 End If                  pGeogPoint = pFreshPoint                 pSpatialReference = pPCS                 pSpatialReference.SetFalseOriginAndUnits(200000, 4000000, 100)                  pGeometry = pFreshPoint                 pGeometry.Project(pSpatialReference)             Catch ex As Exception                 ' Print generic exception messages.                 Console.WriteLine(ex.Message)              End Try


and it essentially 'skips' the entire try block and proceeds with the remainder of the program.

It finishes the program successfully (no more crash), but no message is printed.

Any thoughts?

Edit: ok I'm successfully able to create a projected coordinate system in a different part of the module (the main sub).

There must be something limiting me within the sub I'm working in...somehow.

Edit 2: Some success finally. I decided to create a simple function of type spatial reference, pass my integer (for Meridian) and create the spatial reference, then return it. This appears to have worked...fingers crossed!

View solution in original post

0 Kudos
10 Replies
NeilClemmons
Regular Contributor III
There's nothing inherently wrong with that particular line.  When you set a breakpoint and step through with the debugger is this the line that throws the exception?  One thing I see is that you're using this spatial reference in the call to Project.  This method will fail if the geometry you're calling it on does not already have a spatial reference or no transformation exists between it's spatial reference and the spatial reference you're projecting.  Unless you can guarantee the geometry already has a valid spatial reference then you should be validating it before calling Project.
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Thanks for the quick reply Neil, you've been my saviour more times than I can count and your help is always extremely appreciated!

Unfortunately, I'm pretty amateur with debugging...and I'm using VS2010 express, so I don't know if there are some limitations.

However, now that you mention it, I think perhaps I should have included a transformation. Even though in my neck of the woods, the WGS84 and NAD83 datum are almost identical, if I do it through the toolbox, ESRI automatically adds a transformation (obviously because datums are switching).

So it's quite possible my code is failing because I'm not telling the code to transform the data? I thought of that earlier, but wasn't sure if it was necessary because no other code snippet included that.

I'm going to try that. I know for a fact that my data is all points and they are part of a WGS84 feature class.
0 Kudos
NeilClemmons
Regular Contributor III
You shouldn't need to provide the transformation for those coordinate systems.  I'm not familiar with the file format you're getting your points from, but have to checked to see if the points already have a spatial reference?  The coordinates may be in WGS84, but unless the spatial reference property is set to WGS84, the Project method will not know that's the coordinates system.  You may need to create the WGS84 spatial reference and use it to set the SpatialReference property of the point before calling Project.
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Interesting point Neil.

The feature class that the point are being "run" from, is the result of some previous code which calls the Geoprocessing tool "GPX to Feature" which converts a standard .gpx file into a feature class (in this case, inside a file geodatabase).

When I examine the result in ArcCatalog (or look at the tools help), it indicates a default spatial reference of WGS84.

However, perhaps telling the code to specify is the answer. Sure it exists...but does the code know that? Perhaps not.

Will try that.

Thanks!
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Spoke too soon.

The "pFreshPoint" is given a spatial reference above all this code. So it specifically knows it has WGS84 as the spatial reference.

I'm sure I'm overlooking something minor, but at this point I'm at least happy you've picked out some things to check.

Anything else you can think of?

We have some similar code in a different program that does essentially the same thing (just for different file input). I'm going to check against that again, maybe I've missed something.

Thanks for your help!
0 Kudos
NeilClemmons
Regular Contributor III
Not sure if this is the problem but you're specifying NAD83 UTM zones.  Would it make more sense to specify WGS84 UTM zones instead?  I would still think it would know which transformation to use but I pretty much work with WGS84 data exclusively so I can't say for certain.

I'm also not familiar with VS Express but if you can't set a breakpoint and step through the code then you can put in message boxes (or write to the output window) to see exactly where the code is failing.  Also, putting in some error handling to give you the exception message might also help.  It's usually just a general COM exception but you never know.
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Thanks for the advice.

Yes, my debugging style is message boxes and it was the pPCS = pSRFactory.CreateProjectedCoordinateSystem() line that seemed to be the problem.

We typically work with just NAD83 which is why I've defaulted to that. My alternative thought to that would be to project the original geographic data into NAD83 and then set up NAD83 UTM Zones since the datums will then match.

Quick check shows failing on UTM Zones for WGS84 as well 😞

Might attempt my alternative in the meantime. Let me know if you come up with any other ideas. I appreciate the help in eliminating some options.

Thanks!
0 Kudos
NeilClemmons
Regular Contributor III
The only reason that I can think of that the call to CreateProjectedCoordinateSystem would fail is if you passed in an invalid constant value.  Try hardcoding the values - NAD83 UTM Zone 11 is 26911 and Zone 12 is 26912.  Is this code running inside of ArcMap or is it a standalone app?
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Hey Neil,

I originally had the integer values, but switched to the longer definition just for some quick testing purposes.

That definitely wasn't the problem, the integer values for both the NAD83 and WGS84 projection definitions still fail.

This is being developed as an Add-In for ArcMap. ArcMap is running, user clicks the button, choose GPX file, and the code does all the rest.

Very strange. I'm sure I had it at least partially working Friday too, can't figure out what happened. I started thinking it might be my logic in the IF statement check. Might just tell it to use Zone 11 (since that's the test data location), and ignore the check for meridian.

Anyways, I'll keep this thread updated as I test different things, maybe a miracle will happen haha.
0 Kudos