I am writing code for a button in a windows.form to export to KMZ any selected features from any vector layer in a maps TOC.
Code works well with vector layers but seems to crash if there is any imagery or raster in the TOC.
I understand that Ifeaturelayer is for vector geometries only, so I am guessing I need to include a check if the current layer is a vector, if so continue with code, otherwise skip the code. But being very new to arcobjects, I cant figure out how to do this?
See below:
Public Sub ExportSelectedFeaturesToKML()
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pFeatureLayer As IFeatureLayer
Dim LayerName As String
pMxDoc = My.ArcMap.Document
pMap = pMxDoc.FocusMap
'Counts number of Layers in TOC
Dim LayerCount As Integer
LayerCount = pMap.LayerCount
'******** Loops through layers in TOC and checks if there are any selected features, if so exports them as KMZ.
For i = 0 To LayerCount - 1
pFeatureLayer = pMap.Layer(i)
'Determines the Name of current Layer for KMZ file name
LayerName = pMap.Layer(i).Name
'Calculates current date and time and provides as a string
Dim rightNow As DateTime = DateTime.Now
Dim strCurrentDateTimeString As String
strCurrentDateTimeString = rightNow.ToString("ddd_dd_MMM_yyyy_hh_mm_ss_tt")
'Checks if layer has any selected features
Dim pLayer As ILayer
pLayer = pMap.Layer(i)
Dim pFeatureSelection As IFeatureSelection
pFeatureSelection = pLayer
Dim PSelectionSet As ISelectionSet2
PSelectionSet = pFeatureSelection.SelectionSet
Dim SelectionCount As Integer
SelectionCount = PSelectionSet.Count
If SelectionCount > 0 Then
' Initialize the geoprocessor.
Dim GP As Geoprocessor = New Geoprocessor()
Dim LayerToKMLTool As ESRI.ArcGIS.ConversionTools.LayerToKML = New ESRI.ArcGIS.ConversionTools.LayerToKML()
GP.SetEnvironmentValue("workspace", "C:\Data")
LayerToKMLTool.layer = pFeatureLayer
LayerToKMLTool.layer_output_scale = 1
LayerToKMLTool.out_kmz_file = LayerName & "_" & strCurrentDateTimeString & ".kmz"
GP.Execute(LayerToKMLTool, Nothing)
End If
Next
End Sub
Any suggestions would be greatly appreciated.
Justin
Code works well with vector layers but seems to crash if there is any imagery or raster in the TOC.
I understand that Ifeaturelayer is for vector geometries only, so I am guessing I need to include a check if the current layer is a vector, if so continue with code, otherwise skip the code. But being very new to arcobjects, I cant figure out how to do this?
See below:
Public Sub ExportSelectedFeaturesToKML()
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pFeatureLayer As IFeatureLayer
Dim LayerName As String
pMxDoc = My.ArcMap.Document
pMap = pMxDoc.FocusMap
'Counts number of Layers in TOC
Dim LayerCount As Integer
LayerCount = pMap.LayerCount
'******** Loops through layers in TOC and checks if there are any selected features, if so exports them as KMZ.
For i = 0 To LayerCount - 1
pFeatureLayer = pMap.Layer(i)
'Determines the Name of current Layer for KMZ file name
LayerName = pMap.Layer(i).Name
'Calculates current date and time and provides as a string
Dim rightNow As DateTime = DateTime.Now
Dim strCurrentDateTimeString As String
strCurrentDateTimeString = rightNow.ToString("ddd_dd_MMM_yyyy_hh_mm_ss_tt")
'Checks if layer has any selected features
Dim pLayer As ILayer
pLayer = pMap.Layer(i)
Dim pFeatureSelection As IFeatureSelection
pFeatureSelection = pLayer
Dim PSelectionSet As ISelectionSet2
PSelectionSet = pFeatureSelection.SelectionSet
Dim SelectionCount As Integer
SelectionCount = PSelectionSet.Count
If SelectionCount > 0 Then
' Initialize the geoprocessor.
Dim GP As Geoprocessor = New Geoprocessor()
Dim LayerToKMLTool As ESRI.ArcGIS.ConversionTools.LayerToKML = New ESRI.ArcGIS.ConversionTools.LayerToKML()
GP.SetEnvironmentValue("workspace", "C:\Data")
LayerToKMLTool.layer = pFeatureLayer
LayerToKMLTool.layer_output_scale = 1
LayerToKMLTool.out_kmz_file = LayerName & "_" & strCurrentDateTimeString & ".kmz"
GP.Execute(LayerToKMLTool, Nothing)
End If
Next
End Sub
Any suggestions would be greatly appreciated.
Justin