0

I have two feature classes. I'm intersecting both of them. I have to return features which does not intersect with the other. Lets take an example. I have two feature classes: villages and colleges. I'm intersecting both of them. My query is "Select villages where population greater then 500 and not having colleges." So villages "having population greater then" is working finely but then it spatially search for colleges in those villages. what all I need is to select villages which does not have colleges.

public ArrayList Attributespatialquery(String StrTableA, String StrTableB, String Vcode, String GISColumn, String SDE)
    {

        string TableA, TableB;

        ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
        AoInitialize initA = new AoInitialize();
        IWorkspaceFactory pworkspaceFactory = null;
        IWorkspace pworkspace = null;
        ESRI.ArcGIS.Geodatabase.IFeatureWorkspace pfeatureWorkspace = null;
        ESRI.ArcGIS.Geodatabase.IFeatureClass TableBfeatureClass = null;
        ESRI.ArcGIS.Geodatabase.IFeatureClass TableAfeatureClass = null;
        IFeatureCursor pFeatCusr = null;
        IFeatureCursor pFeatCusr1 = null;
        IFeature pFeature = null;
        IFeature pFeature1 = null;

        //System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
        //System.Data.DataSet dataset = new System.Data.DataSet();
        //// MyConnection objCon = new MyConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionGIS"].ToString());
        //con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionGIS"].ToString();
        try
        {
            // initialize an ArcGIS Server license if the license is available
            if (initA.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeStandard) == esriLicenseStatus.esriLicenseAvailable)
                initA.Initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);
            // throw an exception if the license is not available
            else
                throw new Exception("ESRI ArcGIS Server License is unavailable or has failed");

            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            pworkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            pworkspace = pworkspaceFactory.OpenFromFile(SDE, 0);

            pfeatureWorkspace = (IFeatureWorkspace)pworkspace;

            TableA = "dbo." + StrTableA; // Explict Cast
            TableAfeatureClass = pfeatureWorkspace.OpenFeatureClass(TableA);
            TableB = "dbo." + StrTableB; // Explict Cast
            TableBfeatureClass = pfeatureWorkspace.OpenFeatureClass(TableB);

            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.SubFields = "*";
            pQueryFilter.WhereClause = GISColumn + " in (" + Vcode + ")";

            pFeatCusr = TableAfeatureClass.Search(pQueryFilter, false);


            while ((pFeature = pFeatCusr.NextFeature()) != null)

            {
                ISpatialFilter spatialFilter = new SpatialFilterClass();
                spatialFilter.Geometry = pFeature.Shape;
                spatialFilter.GeometryField = TableAfeatureClass.ShapeFieldName;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                pFeatCusr1 = TableBfeatureClass.Search(spatialFilter, false);

                while ((pFeature1 = pFeatCusr1.NextFeature()) != null)

                    if (!arrSpatial.Contains(pFeature1))
                    {
                        arrSpatial.Add(pFeature1.OID);
                        // WriteLog("values added");
                    }

            }
  • 2
    You've tagged this question as arcobjects, what code have you written so far? – Hornbydd May 2 '15 at 11:28
2

You cannot do a 'switch select' with a Search, what you need to use is Select which you can then negate/switch with ISelectionSet.Combine then convert to a cursor using ISelectionSet.Search (cursor must exist first):

// your queries as a SelectionSet
ISelectionSet pSatisfiesQuery = pFeatureClass.Select(pQueryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, null);
ISelectionSet pSatisfiesGeom = pFeatureClass.Select(spatialFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, null);
// some empty sets
ISelectionSet pResultset = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionEmpty, null);
ISelectionSet pDiffSet = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionEmpty, null);
ISelectionSet pEmptySet = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionEmpty, null);
// now select from selected
pSatisfiesQuery.Combine(pSatisfiesGeom, esriSetOperation.esriSetIntersection, out pResultset);
// now switch selection
pResultset.Combine(pEmptySet, esriSetOperation.esriSetDifference, out pDiffSet);
IFeatureCursor pFeatCusr1;
pDiffSet.Search(null, true, out pFeatCusr1); // turn the selection set into a feature cursor

Normally I would put this in the context of your code block but yours is particularly long and I fear that you'd miss the changed lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.