I want to export a query to a Shapefile. If the query is empty I get an error and it will not export. How can I do nothing if the query is empty and export next query in the list?
Sample if I exporting Land but it is no Building on the land I get error
Export_with_query("2", "BUILDING", "1")
Export_with_query("2", "LAND", "1")
Public Sub Export_with_query(ByVal ep_layer, ByVal ep_name, ByVal ep_query)
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pFLayer As IFeatureLayer
Dim pFc As IFeatureClass
Dim pINFeatureClassName As IFeatureClassName
Dim pDataset As IDataset
Dim pInDsName As IDatasetName
Dim pFSel As IFeatureSelection
Dim pSelSet As ISelectionSet
Dim pFeatureClassName As IFeatureClassName
Dim pOutDatasetName As IDatasetName
Dim pWorkspaceName As IWorkspaceName
' Dim pExportOp As IExportOperation
Dim pExportOp As ESRI.ArcGIS.GeoDatabaseUI.IExportOperation
Try
pDoc = My.ArcMap.Document
pMap = pDoc.FocusMap
pFLayer = pMap.Layer(ep_layer)
pFc = pFLayer.FeatureClass
'Get the FcName from the featureclass
pDataset = pFc
pINFeatureClassName = pDataset.FullName
pInDsName = pINFeatureClassName
'Get the selection set
pFSel = pFLayer
pSelSet = pFSel.SelectionSet
Dim pQFilter As IQueryFilter
pQFilter = New QueryFilter
pQFilter.WhereClause = "TYPE = '" & ep_query & "'"
If pFc.FeatureCount(pQFilter) = 0 Then
Exit Sub
Else
'Define the output feature class name
pFeatureClassName = New FeatureClassName
pOutDatasetName = pFeatureClassName
pOutDatasetName.Name = ep_name
pWorkspaceName = New WorkspaceName
If Chk_default_path.Checked = True Then
pWorkspaceName.PathName = "c:\map"
Else
pWorkspaceName.PathName = Txt_path.Text
End If
pWorkspaceName.WorkspaceFactoryProgID = _
"esriCore.shapefileworkspacefactory.1"
pOutDatasetName.WorkspaceName = pWorkspaceName
'pFeatureClassName.FeatureType = esriFTSimple
pFeatureClassName.FeatureType = ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple
' pFeatureClassName.ShapeType = esriGeometryAny
pFeatureClassName.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny
pFeatureClassName.ShapeFieldName = "Shape"
'Export
' pExportOp = New ExportOperation
pExportOp = New ESRI.ArcGIS.GeoDatabaseUI.ExportOperation
' pExportOp.ExportFeatureClass(pInDsName, Nothing, pSelSet, Nothing, pOutDatasetName, 0)
pExportOp.ExportFeatureClass(pInDsName, pQFilter, pSelSet, Nothing, pOutDatasetName, 0)
'pExportOp.ExportFeatureClass(pInFClassName, pQFilter, pSelSet, pGeomDef, pOutDatasetName, 0)
' End If
Catch ex As Exception
MessageBox.Show("Error exporting map: " & vbCrLf & ex.ToString)
Finally
End Try
End Sub
2 Answers 2
You get the selection set on the feature layer but then you do a feature count on the feature class, an object that does not support selections. So your current logic makes no sense.
Now do you want to export an EXISTING selection or do you want to SELECT then export?
If you want to export an existing selection then delete the Featurecount code as that is not doing anything for you.
You would need to write:
'Get the selection set
pFSel = pFLayer
pSelSet = pFSel.SelectionSet
If pSelSet.Count = 0 then
Exit Sub
End if
If you want to SELECT and then export I would use code:
Dim pQFilter As IQueryFilter
pQFilter = New QueryFilter
pQFilter.WhereClause = "TYPE = '" & ep_query & "'"
pFSel.SelectFeatures(pQFilter,esriSelectionResultNew,False)
pSelSet = pFSel.SelectionSet
If pSelSet.Count = 0 then
Exit Sub
End if
-
Like it, you've got both ways here: an existing selection and creating a new selection based on a query. Just be aware that the featureselection is iterated on IEnumFeature and the selection set is iterated using an IFeatureCursor - both iterate IFeature objects but their next one approaches are different... you will need to keep track of which one you're using (a boolean works here) and either IFeatureCursor.NextFeature() or IEnumFeature.Next() as appropriate.Michael Stimson– Michael Stimson2014年11月05日 21:39:16 +00:00Commented Nov 5, 2014 at 21:39
-
Thanks for the help I am using select By Graphics to mark the area I want to export so I am finish to Select few layer to export I don't get the error anymore but I export to much outside the area I mark with Select By GraphicsSigster– Sigster2014年11月06日 08:34:52 +00:00Commented Nov 6, 2014 at 8:34
-
The first one works :) Hornbydd, artwork21 Thanks for your help with this ! Regards SigsterSigster– Sigster2014年11月06日 08:43:32 +00:00Commented Nov 6, 2014 at 8:43
You will have to use an if statement to check the feature count using IFeatureClass.FeatureCount Method, before exporting.
-
1I try to use this it just ignore it :) If pFc.FeatureCount(pQFilter) = 0 Then Exit Sub ElseSigster– Sigster2014年11月05日 18:11:51 +00:00Commented Nov 5, 2014 at 18:11
-
Can you update your code snippet with the logic included?artwork21– artwork212014年11月05日 18:45:03 +00:00Commented Nov 5, 2014 at 18:45