'==================================================================
'如何修改层的坐标系统
'当加载一个Shape 文件的时候,会检查有没有跟Shape 文件相关的
'prj 文件,如果有就根据该prj 文件内的参数设置层的坐标系统,反之就会提示
'该Shape 文件没有相关的空间坐标系,并且把层的坐标系统设置为“Unknown”。
'本例要实现的功能是改变一个已加载进mapcontrol的
'Feature Layer 的坐标系统。
'==================================================================
Sub AlterSpatialReference()
Dim pMap As IMap
Dim pFeatureLayer As IFeatureLayer
Dim pGeoDataset As IGeoDataset
Dim pSpatialReference As ISpatialReference
Dim pFeatureClass As IFeatureClass
Dim pGeoDatasetEdit As IGeoDatasetSchemaEdit
Dim pSpatialReferenceF As ISpatialReferenceFactory2
Dim pProjectedCoordinateS As IProjectedCoordinateSystem
On Error GoTo ErrorHandler
Set pMap = MapControl1.ActiveView.FocusMap
'Assume that europeEquidistant is added last and that it is at the top of the map.
Set pFeatureLayer = pMap.Layer(0)
'This is how we get the current spatial reference for a layer
'QI for the geodatset from the layer
Set pGeoDataset = pFeatureLayer
Set pSpatialReference = pGeoDataset.SpatialReference
'Note that ArcMap sets the SR as "Unknown"
MsgBox pFeatureLayer.Name + " SpatialReference is " + pSpatialReference.Name
If (pSpatialReference.Name = "Unknown") Then
'Get the FeatureClass from the Layer
Set pFeatureClass = pFeatureLayer.FeatureClass
'QI for the Geodataset from the FeatureClass
Set pGeoDataset = pFeatureClass
'QI for GeoDatasetSchemaEdit from the Geodataset
Set pGeoDatasetEdit = pGeoDataset
'Test if we can alter the spatialreference, if we can then we create a factory
'and use that to create a projected coordinate system.
If (pGeoDatasetEdit.CanAlterSpatialReference = True) Then
Set pSpatialReferenceF = New SpatialReferenceEnvironment
'Use a SpatialReferenceFactory to create the Projected Coordinate System.
'Here we are using a Factory Code for the Two Point Equidistant
'coordinate system.
Set pProjectedCoordinateS = _
pSpatialReferenceF.CreateProjectedCoordinateSystem _
(esriSRProjCS_Beijing1954_3_Degree_GK_CM_102E)
'Now alter the layers spatial reference
pGeoDatasetEdit.AlterSpatialReference pProjectedCoordinateS
'Now get the updated SpatialReference and its name
Set pSpatialReference = pGeoDataset.SpatialReference
MsgBox pFeatureLayer.Name + " SpatialReference is " + pSpatialReference.Name
'Force a full refresh
MapControl1.ActiveView.Refresh
End If
End If
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
(本文已被浏览 次) | | |