Setting Coordinate System using VBA if it is undefined

973
5
04-14-2010 11:21 PM
CharlesDelahaye
New Contributor
Hello,
Here is my question:
I would like to set the Coordinate system using a sub VBA - (ArcObjects)  (Please not with ArcToolBox !) when the Coordinate System of the Layer is "undefined"
Of course, the Coordinate System is imposed according to my choice.

Thank you for your help...
Kind regards,
Charles.
0 Kudos
5 Replies
MelitaKennedy
Esri Notable Contributor
Hello,
Here is my question:
I would like to set the Coordinate system using a sub VBA - (ArcObjects)  (Please not with ArcToolBox !) when the Coordinate System of the Layer is "undefined"
Of course, the Coordinate System is imposed according to my choice.

Thank you for your help...
Kind regards,
Charles.


I originally cribbed this code from a post made by Kirk Kuykendall many years ago. I have not tried to run it so you may need to make some changes to it.

Melita

' changes spatialref of data in arcmap. coverages not working; see kirk's code
' below this sub.

public sub redefinedatasets()

'On Error GoTo errorhandler:

Dim pMxDoc As IMxDocument
Dim pDataLayer As IDataLayer2
Dim pName As IName
Dim pGxObjectInternalName As IGxObjectInternalName
Dim pGxObject As IGxObject
Dim pmap As IMap

Set pMxDoc = pApp.Document
Set pmap = pMxDoc.FocusMap
Set pDataLayer = pMxDoc.SelectedLayer


If pDataLayer Is Nothing Then
  MsgBox "Please Select a Layer First", vbExclamation, "Select A layer first"
  Exit Sub
End If

Set pName = pDataLayer.DataSourceName
Set pGxObjectInternalName = New GxDataset
Set pGxObjectInternalName.InternalObjectName = pName
Set pGxObject = pGxObjectInternalName

'I also have another problem where the dataset goes outside the spatial envelope
'any suggestions on how to expand the envelope to account for the new projection 
'would be appreciated.
                 
Dim pSpatRefDialog As ISpatialReferenceDialog
Dim pSpatRef As ISpatialReference
Dim pRasterDataset As IRasterDataset
Dim pDataset As IDataset
Dim pGeoDatasetSchemaEdit As IGeoDatasetSchemaEdit
Dim pGXdataset As IGxDataset

'call the spatial reference dialog
Set pSpatRefDialog = New SpatialReferenceDialog
Set pSpatRef = pSpatRefDialog.DoModalCreate(False, False, False, 0)
Set pGXdataset = pGxObject
               
If pGXdataset.Type = esriDTRasterDataset Then
  Set pRasterDataset = pGXdataset.Dataset
  Set pGeoDatasetSchemaEdit = pRasterDataset

'this is for featureclasses
ElseIf pGXdataset.Type <> esriDTRasterDataset Then
  Set pDataset = pGXdataset.Dataset

'COVERAGE DIES RIGHT HERE!!!
'ERROR TYPE MISMATCH
'WORKS FINE FOR SHPS AND GDBS


  Set pGeoDatasetSchemaEdit = pDataset
End If

'this prevents the cancel error
If pSpatRef Is Nothing Then
   Exit Sub
End If

                        
If pGeoDatasetSchemaEdit.CanAlterSpatialReference = True Then
   pGeoDatasetSchemaEdit.AlterSpatialReference pSpatRef
                          
Else

  MsgBox "Cannot Edit the Spatial Reference." & _
    "Perhaps you have a lock somewhere else on the dataset", vbExclamation, "Probably Locked"
End If

'zoom to layer

Dim pactiveview As IActiveView
Set pactiveview = pmap
Dim player As ILayer
Set player = pMxDoc.SelectedLayer

pactiveview.Extent = player.AreaOfInterest
pactiveview.Refresh

'cleanup
Set pMxDoc = Nothing
Set pDataLayer = Nothing
Set pName = Nothing
Set pGxObjectInternalName = Nothing
Set pGxObject = Nothing
Set player = Nothing
Set pactiveview = Nothing

Exit Sub
'errorhandler:
msgbox err.description

exit sub

end sub

Option Explicit
Sub SetPrj()
   Dim pSRF As ISpatialReferenceFactory2
   Set pSRF = New SpatialReferenceEnvironment
   Dim pSR As ISpatialReference
   Set pSR = pSRF.CreateProjectedCoordinateSystem(esriSRProjCS_WGS1972UTM_30S)
                  
   Dim pGxApp As IGxApplication
   Set pGxApp = Application
   If TypeOf pGxApp.SelectedObject Is IGxDataset Then
      Dim pGxDataset As IGxDataset
      Set pGxDataset = pGxApp.SelectedObject
      If TypeOf pGxDataset.DatasetName Is ICoverageName Then
         Dim pGDSE As IGeoDatasetSchemaEdit
         Set pGDSE = pGxDataset.Dataset
         If pGDSE.CanAlterSpatialReference Then
            pGDSE.AlterSpatialReference pSR
         Else
            Debug.Print "cannot alter spatial reference of coverage"
         End If
      End If
   End If
End Sub
0 Kudos
CharlesDelahaye
New Contributor
Hello Melita,

Thanks a lot for your reply. Thanks to it, I find out in the documentation the following code that allows to set the "'Geographic Coordinate System"  directly within ArcGIS (and not from ArcCatalog as you have proposed).

Now, my problem is to find out how to set also the "Projected Coordinate System" (for example: esriSRProjCS_CH1903_LV03)
If anyone has an idea? Thank you in advance for your help
Kind regards
Charles.

Sub Layer_GeographicCoordSyst()

Dim pMxDoc As IMxDocument, pMap As IMap

Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap

Dim pLayer As IFeatureLayer, pFeatureClass As IFeatureClass
Dim pGeoDataset As IGeoDataset, pGeoDatasetEdit As IGeoDatasetSchemaEdit

Set pLayer = pMap.Layer(0)
Set pFeatureClass = pLayer.FeatureClass
Set pGeoDataset = pFeatureClass
Set pGeoDatasetEdit = pGeoDataset

If pGeoDatasetEdit.CanAlterSpatialReference = True Then
    Dim pSpatRefFact As ISpatialReferenceFactory2
    Set pSpatRefFact = New SpatialReferenceEnvironment

    Dim pGeoCoordSys As IGeographicCoordinateSystem
    Set pGeoCoordSys = pSpatRefFact.CreateGeographicCoordinateSystem(esriSRGeoCS_CH1903)
'    Debug.Print pLayer.Name, pGeoCoordSys.Name
    pGeoDatasetEdit.AlterSpatialReference pGeoCoordSys
End If
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing

End Sub

0 Kudos
MelitaKennedy
Esri Notable Contributor
Hello Melita,

Thanks a lot for your reply. Thanks to it, I find out in the documentation the following code that allows to set the "'Geographic Coordinate System"  directly within ArcGIS (and not from ArcCatalog as you have proposed).

Now, my problem is to find out how to set also the "Projected Coordinate System" (for example: esriSRProjCS_CH1903_LV03)
If anyone has an idea? Thank you in advance for your help
Kind regards
Charles.


Hi Charles,

You just need to exchange IGeographicCoordinateSystem and CreateGeographicCoordinateSystem for IProjectedCoordinateSystem and CreateProjectedCoordinateSystem. Please see below.

Melita


Sub Layer_GeographicCoordSyst()

Dim pMxDoc As IMxDocument, pMap As IMap

Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap

Dim pLayer As IFeatureLayer, pFeatureClass As IFeatureClass
Dim pGeoDataset As IGeoDataset, pGeoDatasetEdit As IGeoDatasetSchemaEdit

Set pLayer = pMap.Layer(0)
Set pFeatureClass = pLayer.FeatureClass
Set pGeoDataset = pFeatureClass
Set pGeoDatasetEdit = pGeoDataset

If pGeoDatasetEdit.CanAlterSpatialReference = True Then
    Dim pSpatRefFact As ISpatialReferenceFactory2
    Set pSpatRefFact = New SpatialReferenceEnvironment

    Dim pProjCoordSys As IProjectedCoordinateSystem
    Set pProjCoordSys = pSpatRefFact.CreateProjectedCoordinateSystem(esriSRProjCS_CH1903_LV03)
'    Debug.Print pLayer.Name, pProjCoordSys.Name
    pGeoDatasetEdit.AlterSpatialReference pProjCoordSys
End If
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing

End Sub

0 Kudos
CharlesDelahaye
New Contributor
Hello Melita,
Thank you so much for your help !
Charles.
0 Kudos
BELAYACHISara
New Contributor
hi,
i programmatically call up the dialog box and use it to instantiate a SpatialReference object in two ways :


1.
  Dim pSpaRefDlg As ISpatialReferenceDialog
  Set pSpaRefDlg = New SpatialReferenceDialog
  Dim m_pSpaRef1 As ISpatialReference
  Set m_pSpaRef1 = pSpaRefDlg.DoModalCreate(False, False, False, Form1.ActiveControl) 'Use M and Z



OR
2.

 Dim pSpatialref As IProjectedCoordinateSystem
  Dim pSpaRefDlg As ISpatialReferenceDialog
  Set pSpaRefDlg = New SpatialReferenceDialog
  Set pSpatialref = pSpaRefDlg.DoModalCreate(False, False, False, Form1.ActiveControl) 'Use M and Z



But the projection is not apply, so please how to apply it, i don't want a specific projection, but what the user choose ??

Please Help me
0 Kudos