Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have 2 data frames one which has WGS data in it and the other OSGB1936. I am trying to create a button where the user can input decimal degrees (WGS) and it will zoom them to that location in British National GRid (OSGB1936) I have done the code but when it zooms to OSGB data frame it does not convert the decimal degree to meters. It returns incorrect values for Northing and Eastings in meters.

The code does work only if i have done it as a ToolControl_MouseDown but that uses pMxDoc.CurrentLocation.x, pMxDoc.CurrentLocation.y. which i dont want to do because i want the user to enter the value from a text box. Any ideas please?

Private Sub CommandButton2_Click()


Dim pSREnv As ISpatialReferenceFactory2
Set pSREnv = New SpatialReferenceEnvironment
Set m_PCS = pSREnv.CreateProjectedCoordinateSystem(esriSRProjCS_BritishNationalGrid)

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim x As Double
Dim y As Double

 y = CDbl(Texty.Text)
 x = CDbl(Textx.Text)

Dim pEnvelope As IEnvelope
Dim pMap As IMap
'Set pMap = pMxDox.FocusMap

Dim strName As String
    strName = "Layers"

Set pMap = GetMap(strName)
 If Not pMap Is Nothing Then
    If pMxDoc.ActiveView Is pMxDoc.PageLayout Then
        Set pMxDoc.ActiveView.FocusMap = pMap
    Else
        Set pMxDoc.ActiveView = pMap
    End If

    End If

    Dim pPoint As IPoint
    Set pPoint = New Point


    pPoint.PutCoords x, y     'pMxDoc.CurrentLocation.x, pMxDoc.CurrentLocation.y

    Dim pGeom As IGeometry
    Set pGeom = pPoint
    Set pGeom.SpatialReference = pMxDoc.FocusMap.SpatialReference
    pGeom.Project m_PCS


    Set pEnvelope = pMxDoc.ActiveView.Extent
    pEnvelope.CenterAt pPoint
    pMxDoc.ActiveView.Extent = pEnvelope
    pMap.mapScale = 5000
    pMxDoc.ActiveView.Refresh


        Application.StatusBar.Message(0) = "meters east: " & pPoint.x & ", meters north:" &     pPoint.y

End Sub

The below code is the updated but still does not do what it should. When i enter the decimal degrees 53.47574, -2.22553 it returns a value of -512464, -2174656 which is in the incorrect position. So somewhere in the conversion between WSG and OSGB it is going very wrong.

`Private Sub CommandButton2_Click()


Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pEnvelope As IEnvelope
Dim pMap As IMap

Dim pSREnv As ISpatialReferenceFactory2
Set pSREnv = New SpatialReferenceEnvironment

Dim pProjCoordSystem As ISpatialReference
 Dim pProjCoordSystem2 As ISpatialReference
Dim pGeoTrans_A As IGeoTransformation
'Dim pGeoTrans_B As IGeoTransformation


Set pProjCoordSystem2 =   pSREnv.CreateProjectedCoordinateSystem(esriSRProjCS_BritishNationalGrid)
Set pProjCoordSystem = pSREnv.CreateProjectedCoordinateSystem(esriSRProjCS_WGS1984UTM_14N)



Set pGeoTrans_A = _
pSREnv.CreateGeoTransformation(esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984_1)


 Dim strName As String
    strName = "Layers"

 Set pMap = GetMap(strName)
 If Not pMap Is Nothing Then
    If pMxDoc.ActiveView Is pMxDoc.PageLayout Then
        Set pMxDoc.ActiveView.FocusMap = pMap
    Else
        Set pMxDoc.ActiveView = pMap
    End If

    End If


Dim x As Double
Dim y As Double


 y = CDbl(Texty.Text)
 x = CDbl(Textx.Text)


   Dim pPoint As IPoint
    Set pPoint = New Point
    'Set pPoint = New esriGeometry.Point
    Set pPoint.SpatialReference = pProjCoordSystem2


    pPoint.PutCoords x, y     'pMxDoc.CurrentLocation.x, pMxDoc.CurrentLocation.y

   Dim pGeom As IGeometry2
    Set pGeom = pPoint
    Set pGeom.SpatialReference = pProjCoordSystem2
    pGeom.ProjectEx pProjCoordSystem2, esriTransformForward, pGeoTrans_A, 0, 0, 0


    Set pEnvelope = pMxDoc.ActiveView.Extent
    pEnvelope.CenterAt pPoint
    pMxDoc.ActiveView.Extent = pEnvelope
    pMap.mapScale = 5000
    pMxDoc.ActiveView.Refresh


    Application.StatusBar.Message(0) = "meters east: " & pPoint.x & ", meters north:" & pPoint.y

   End Sub'
share|improve this question
    
It a bit more complex than what your code is showing - see movable-type.co.uk/scripts/latlong-convert-coords.html –  Mapperz Feb 20 '13 at 15:34
1  
See my answer to another very similar question. –  Kirk Kuykendall Feb 20 '13 at 19:05
    
I think it could also be how i define WGS. i used Set pProjCoordSystem = pSREnv.CreateProjectedCoordinateSystem(esriSRProjCS_WGS1984UTM_14N) What should i be using if i just want the standard WGS1984? –  Daz Feb 21 '13 at 15:38
    
Two points strange to me: should the coordinates rather be: -2.22553, 53.47574 (East,North) for a point in GB? And why are you using WGS84 UTM 14N? That's far off Great Britain! And CRS units are in metres, not degrees. –  AndreJ Feb 22 '13 at 12:54
    
The point is being assigned BNG and then projected again to BNG! Use esriSRGeoCS_WGS1984 to create a geographic coordinate system. Assign it to pPoint (and then to pGeom). Change the ProjectEx statement to use esriTransformReverse (you're converting from WGS84 to BNG which is the opposite direction of the transformation). Then assign pProjCoordSystem2 to pGeom afterward. –  mkennedy Feb 22 '13 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.