Select to view content in your preferred language

How do you check if a layer in TOC is a vector or a raster/image?  vb.net

1391
2
Jump to solution
07-23-2013 08:49 AM
JJD84
by
Emerging Contributor
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
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Use TypeOf to determine if the layer is an IFeatureLayer (or IRasterLayer)

dim pLayer as ILayer  For i = 0 To LayerCount - 1   pLayer = pMap.Layer(i)     If TypeOf pLayer Is IFeatureLayer Then     'Vector   ElseIf TypeOf pLayer Is IRasterLayer Then     'Raster       End If next i

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
Use TypeOf to determine if the layer is an IFeatureLayer (or IRasterLayer)

dim pLayer as ILayer  For i = 0 To LayerCount - 1   pLayer = pMap.Layer(i)     If TypeOf pLayer Is IFeatureLayer Then     'Vector   ElseIf TypeOf pLayer Is IRasterLayer Then     'Raster       End If next i
0 Kudos
JJD84
by
Emerging Contributor
Ah so simple, this had me bogged for a while tonight .... many thanks Ken. 
J
0 Kudos