Export to SHP with WhereClause

2422
5
Jump to solution
10-17-2015 02:43 AM
Highlighted
New Contributor

Hi

I use this code to export to SHP but if the WhereClause  is null it still make empty SHP file

How can I stop the export if pQFilter.WhereClause is notthing

       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 ESRI.ArcGIS.GeoDatabaseUI.IExportOperation

        pDoc = My.ArcMap.Document

        pMap = pDoc.FocusMap

        pFLayer = pMap.Layer(ep_layer)

        pFc = pFLayer.FeatureClass

        pDataset = pFc

        pINFeatureClassName = pDataset.FullName

        pInDsName = pINFeatureClassName

        Dim pQFilter As IQueryFilter

        pQFilter = New QueryFilter

        pQFilter.WhereClause = "TYPE '" & ep_query & "'"

        pFSel = pFLayer

        pSelSet = pFSel.SelectionSet

        If pSelSet.Count = 0 Then

            Exit Sub

        End If

        If pFc.FeatureCount(pQFilter) <= 1 Then

          

            Exit Sub

        ElseIf pFc.FeatureCount(pQFilter) >= 1 Then

            pFeatureClassName = New FeatureClassName

            pOutDatasetName = pFeatureClassName

            pOutDatasetName.Name = ep_name

            pWorkspaceName = New WorkspaceName

            pWorkspaceName.PathName = path.txt

            pWorkspaceName.WorkspaceFactoryProgID = esriCore.shapefileworkspacefactory.1"

            pOutDatasetName.WorkspaceName = pWorkspaceName

            pFeatureClassName.FeatureType = ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple

            pFeatureClassName.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny

            pFeatureClassName.ShapeFieldName = "Shape"

            pExportOp = New ESRI.ArcGIS.GeoDatabaseUI.ExportOperation

            pExportOp.ExportFeatureClass(pInDsName, pQFilter, pSelSet, Nothing, pOutDatasetName, 0)

        Else

          

            Exit Sub

        End If

0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor

It appears you have an issue where you are never applying your non-spatial filter to your spatial selection or your spatial filter to your non-spatial selection until the ExportFeatureClass call at the very end of your script, hence the empty shape file.  I can't verify my hunch right now because I don't have VBA Compatibility installed.

As an aside, please use code formatting/syntax highlighting as described in Posting Code blocks in the new GeoNet​.  It is much easier for community member to comment when the code has line numbers.

After this line:

pSelSet = pFSel.SelectionSet

Try adding and changing the following to see if it makes any difference:

pSelSet1 = pSelSet.Select(pQFilter)

If pSelSet1.Count = 0 Then

    Exit Sub

End If

View solution in original post

0 Kudos
5 Replies
Highlighted
MVP Esteemed Contributor

You might want to specify which... of numerous .... places in Developers you wish this to move this..  Your tage should include things like the development language, the development environment etc.  This will facilitate getting an answer  check the (24) subspaces or at least one of the three main branches here....

Developers

Highlighted
MVP Esteemed Contributor

Can you elaborate on "the WhereClause  is null?"  A SQL WHERE clause can be absent/present or defined/undefined, but I would argue the clause itself cannot be NULL.  A SQL WHERE clause can contain search conditions that check for NULL conditions, but that is different than saying the clause itself is NULL.

You ask, "How can I stop the export if pQFilter.WhereClause is nothing?"  Is that question meant to imply that the line in the script, pQFilter.WhereClause = ..., doesn't exist sometimes or does it mean that ep_query isn't always defined or is an empty string?  My guess is that your issue is with ep_query, so it would be helpful if you could elaborate on what data type it is, where it is getting defined, and how it is getting defined.

0 Kudos
Highlighted
New Contributor

do this help

When I export

1. "Draw a Graphic" to mark the area I want to export

2. go to Selection and select By Graphich

if 31 are inside the area I select it export 31 so that is ok

but if 32 and 33 are not inside the area it still export empty file for 32 and 33

ep_query is newer empty string

Arcmap

Field Details, Data TYPE is Long

   Export_with_query("1", "box1", "31")

   Export_with_query("1", "box2", "32")

   Export_with_query("1", "box3", "33")  

  

       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 ESRI.ArcGIS.GeoDatabaseUI.IExportOperation

        pDoc = My.ArcMap.Document

        pMap = pDoc.FocusMap

        pFLayer = pMap.Layer(ep_layer)

        pFc = pFLayer.FeatureClass

        pDataset = pFc

        pINFeatureClassName = pDataset.FullName

        pInDsName = pINFeatureClassName

        Dim pQFilter As IQueryFilter

        pQFilter = New QueryFilter

        pQFilter.WhereClause = "BOXTYPE =  '" & ep_query & "'"

        pFSel = pFLayer

        pSelSet = pFSel.SelectionSet

        If pSelSet.Count = 0 Then

            Exit Sub

        End If

        If pFc.FeatureCount(pQFilter) <= 1 Then

        Exit Sub

        ElseIf pFc.FeatureCount(pQFilter) >= 1 Then

            pFeatureClassName = New FeatureClassName

            pOutDatasetName = pFeatureClassName

            pOutDatasetName.Name = ep_name

            pWorkspaceName = New WorkspaceName   

            pWorkspaceName.PathName = path.Text

            pWorkspaceName.WorkspaceFactoryProgID = "esriCore.shapefileworkspacefactory.1"

            pOutDatasetName.WorkspaceName = pWorkspaceName

            pFeatureClassName.FeatureType = ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple

            pFeatureClassName.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny

            pFeatureClassName.ShapeFieldName = "Shape"

            pExportOp = New ESRI.ArcGIS.GeoDatabaseUI.ExportOperation

            pExportOp.ExportFeatureClass(pInDsName, pQFilter, pSelSet, Nothing, pOutDatasetName, 0)

        Else

           

            Exit Sub

        End If

    End Sub

0 Kudos
Highlighted
MVP Esteemed Contributor

It appears you have an issue where you are never applying your non-spatial filter to your spatial selection or your spatial filter to your non-spatial selection until the ExportFeatureClass call at the very end of your script, hence the empty shape file.  I can't verify my hunch right now because I don't have VBA Compatibility installed.

As an aside, please use code formatting/syntax highlighting as described in Posting Code blocks in the new GeoNet​.  It is much easier for community member to comment when the code has line numbers.

After this line:

pSelSet = pFSel.SelectionSet

Try adding and changing the following to see if it makes any difference:

pSelSet1 = pSelSet.Select(pQFilter)

If pSelSet1.Count = 0 Then

    Exit Sub

End If

View solution in original post

0 Kudos
Highlighted
New Contributor

Thanks Joshua this works

thanks for the help

0 Kudos