Converting to map coordinates in 10.1

437
2
Jump to solution
08-29-2012 12:06 PM
MarkGolebiewski
New Contributor
We have a bounding polygon in WGS 84 coordinates that we need to draw on the map.  The users map data could be anything.  The following code works in ArcMAP 10.0 (and prior) but does not work in 10.1 if the factory code is zero.  Unfortunately a lot of shape files our customers use seem to have valid projection files but no factory codes.  They show up in the dataframe properties a "custom" coordinate systems. 
In 10.1 is there anyway to create a coordinate system if you have a spatial reference but no factory code? 
Or be able to convert a WGS84 point to a "custom" coordinate?
Perhaps I should ask the high level generic question as well.  How can I convert a WGS84 coordinate into the maps coordinate system which can be anything ArcMAP supports.

Thanks
-Mark

 Public Function ConvertToMap(ByVal lat As Double, ByVal lon As Double, ByVal elev As Double, ByVal layer As ILayer) As IPoint         'Convert x and y to map units. m_pApp is set in ICommand_OnCreate.         Dim pMxApp As IMxApplication         Dim mapPoint As IPoint         Dim pPoint As Point      ' = m_mapPoint         Dim sMessage As String = ""         pMxApp = m_pApp          mapPoint = pMxApp.Display.DisplayTransformation.ToMapPoint(0, 0)         Dim pSpatialRefFactory As ISpatialReferenceFactory         pSpatialRefFactory = New SpatialReferenceEnvironment         Dim pGeographicCoordinateSystem As IGeographicCoordinateSystem         pGeographicCoordinateSystem = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984)         pPoint = mapPoint          ' Set the Spacial Ref. to WGS 84         pPoint.Project(pGeographicCoordinateSystem)         pPoint.X = lon         pPoint.Y = lat         Dim code As Integer         Dim pProjectedCoordinateSystem As IProjectedCoordinateSystem         code = pMxApp.Display.DisplayTransformation.SpatialReference.FactoryCode()         ' Dont' know if map is in Geographic or Projected coordinate system so try both.        Try            pProjectedCoordinateSystem = pSpatialRefFactory.CreateProjectedCoordinateSystem(code)            pPoint.Project(pProjectedCoordinateSystem)        Catch ex As Exception            pGeographicCoordinateSystem = pSpatialRefFactory.CreateGeographicCoordinateSystem(code)            pPoint.Project(pGeographicCoordinateSystem)        End Try          Return pPoint     End Function
0 Kudos
1 Solution

Accepted Solutions
PeterYurkosky1
Occasional Contributor
You can always create the spatial reference from a string (that is, the contents of a .prj file) using ISpatialReferenceFactory.CreateESRISpatialReferenceFromPRJ. By the way, at the end of your code sample there, couldn't you just call pPoint.Project(pMxApp.Display.DisplayTransformation.SpatialReference) and not be bothered about whether the map's got a geographic or projected CS?

View solution in original post

0 Kudos
2 Replies
PeterYurkosky1
Occasional Contributor
You can always create the spatial reference from a string (that is, the contents of a .prj file) using ISpatialReferenceFactory.CreateESRISpatialReferenceFromPRJ. By the way, at the end of your code sample there, couldn't you just call pPoint.Project(pMxApp.Display.DisplayTransformation.SpatialReference) and not be bothered about whether the map's got a geographic or projected CS?

View solution in original post

0 Kudos
MarkGolebiewski
New Contributor
couldn't you just call pPoint.Project(pMxApp.Display.DisplayTransformation.SpatialReference) ?


Thank you.  This turned out to by the answer I was looking for.  I can project the point to the maps spatial reference so I don't need a coordinate system so I don't need a factory code so I don't need a PRJ.   Lots of thing I no longer need.
So far so good in my limited testing.

Thanks again
-Mark
0 Kudos