Home arrow Site Info arrow Latest Portal Content arrow VBA Point Reproject Example
VBA Point Reproject Example PDF Print E-mail

Written by Bert Granberg,


Quick and dirty VBA code example. This code was written to populate point features with a new UTM NAD83 point based on a latitude and longitude value found in the feature class's attribute fields. Option Explicit

Public Sub ConvertDDtoUTMNAD83Z12N()

    On Error GoTo errorhandler
    
    Dim doc As IMxDocument
    Dim map As IMap
    Dim fl As IFeatureLayer
    Dim fc As IFeatureClass
    
    Set doc = ThisDocument
    Set map = doc.FocusMap
    Set fl = map.Layer(0)
    Set fc = fl.FeatureClass
    
    Dim pPoint As IPoint
    Dim x As Double
    Dim y As Double
    Dim temp As Double
    
    Dim cur As IFeatureCursor
    Dim fea As IFeature

    
    Set cur = fc.Search(Nothing, True)
    Set fea = cur.NextFeature
    
    Do Until fea Is Nothing
        x = 0
        y = 0
        
        'get lat and long values from fields
        x = fea.Value(fc.FindField("Longitude"))
        y = fea.Value(fc.FindField("Latitude"))
        
        If x > y Then
            temp = x
            x = y
            y = temp
        End If
        
        Set pPoint = New Point
        
        If x <> 0 And y <> 0 Then
            
            'make a new point and reproject from DD to UTM
            Dim pSpatialRefFactory As ISpatialReferenceFactory
            Dim pSpatialRef As ISpatialReference
            Dim pUTMSR As ISpatialReference
            
            pPoint.x = x
            pPoint.y = y
            
            Set pSpatialRefFactory = New SpatialReferenceEnvironment
            Set pSpatialRef = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984)
            Set pUTMSR = pSpatialRefFactory.CreateProjectedCoordinateSystem(esriSRProjCS_NAD1983UTM_12N)
            
            Set pPoint.SpatialReference = pSpatialRef
            pPoint.Project pUTMSR
        
        End If
        Set fea.Shape = pPoint
        fea.Store
        Set fea = cur.NextFeature
    Loop

    Exit Sub
    
errorhandler:
    Debug.Print fea.OID
    Resume Next

End Sub


Users' Comments  
 

No comment posted

Add your comment

07, Nov. 2007
Last Updated ( 07, Nov. 2007 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for