How I can select and zoom to parcel through filter technique

4586
7
Jump to solution
08-18-2014 06:04 AM
NajdALHanahnah
New Contributor III

I am working now on ArcObject (.NET) stand alone application , i need to get the parcel for certain area through filtering technique (4 levels ) district , sub-district , block , parcel

 

each one represent by layer (separate) , what is the code will be use to retrieve the information then focus on the parcel area (target)  , all layers are polygons ....

 

Best Regards

I like GeoNET really i like it ...

 

 

Filter_Engine.jpg

0 Kudos
1 Solution

Accepted Solutions
JohnHauck
Occasional Contributor II

Since this only took a couple of minutes to write I will post an example. However, if you need someone to write solutions for you then we have many people who work in Professional Services that can build applications for you.


using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace EngineWindowsApplication1
{
    public partial class Form1 : Form
    {
        private enum LayerType { Districts, SubDistricts };


        IList<IGeometry> m_districts = null;
        IList<IGeometry> m_subDistricts = null;


        IFeatureLayer m_districtsLayer = null;
        IFeatureLayer m_subDistrictsLayer = null;


        string m_districtsKeyFieldName = "STATE_NAME";
        string m_subDistrictsKeyFieldName = "NAME";


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //could be used to cache the geoms, may not be appropriate for large feature classes
            m_districts = new List<IGeometry>();
            m_subDistricts = new List<IGeometry>();


            m_districtsLayer = (IFeatureLayer)axMapControl1.get_Layer(1);
            m_subDistrictsLayer = (IFeatureLayer)axMapControl1.get_Layer(0);


            //get the districts layer
            IFeatureCursor fCur = m_districtsLayer.FeatureClass.Search(null,false);
            populateCBO(fCur, m_districtsKeyFieldName, false, LayerType.Districts, comboBox1);
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            reset();


            IGeometry g = getGeomAndZoom(LayerType.Districts, comboBox1.SelectedIndex, comboBox1.SelectedItem.ToString());


            ISpatialFilter sf = new SpatialFilter() { Geometry = g, SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects };


            IFeatureCursor fCur = m_subDistrictsLayer.FeatureClass.Search(sf, false);


            comboBox2.Enabled = true;


            populateCBO(fCur, m_subDistrictsKeyFieldName, true, LayerType.SubDistricts, comboBox2);
        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            IGeometry g = getGeomAndZoom(LayerType.SubDistricts, comboBox2.SelectedIndex, comboBox2.SelectedItem.ToString()); 
        }


        private IGeometry getGeomAndZoom(LayerType lt, int i, string val)
        {
            string fieldName = "";
            IGeometry g = null;
            IFeatureLayer fl = null;
            IList<IGeometry> l = null;


            switch (lt)
            {
                case LayerType.Districts:
                    fieldName = m_districtsKeyFieldName;
                    fl = m_districtsLayer;
                    l = m_districts;
                    break;
                case LayerType.SubDistricts:
                    fieldName = m_subDistrictsKeyFieldName;
                    fl = m_subDistrictsLayer;
                    l = m_subDistricts;
                    break;
                default:
                    break;
            }


            if (l.Count > 0)
                g = l;
            else
            {
                IQueryFilter qf = new QueryFilter() { WhereClause = fieldName + " = '" + val + "'"};
                g = fl.FeatureClass.Search(qf, false).NextFeature().Shape;
            }


            axMapControl1.Extent = g.Envelope;
            axMapControl1.Refresh();


            return g;
        }


        private void populateCBO(IFeatureCursor fCur, string fieldName, bool cacheGeoms, LayerType lt, ComboBox cbo)
        {
            IFeature f = fCur.NextFeature();
            while (f != null)
            {
                cbo.Items.Add(f.get_Value(f.Fields.FindField(fieldName)));


                if (cacheGeoms)
                {
                    switch (lt)
                    {
                        case LayerType.Districts:
                            m_districts.Add(f.Shape);
                            break;
                        case LayerType.SubDistricts:
                            m_subDistricts.Add(f.Shape);
                            break;
                        default:
                            break;
                    }
                }


                f = fCur.NextFeature();
            }
        }


        private void reset()
        {
            m_subDistricts = new List<IGeometry>();
            comboBox2.Text = "";
            comboBox2.Items.Clear();
            comboBox2.Enabled = false;
        }
    }
}




View solution in original post

0 Kudos
7 Replies
NajdALHanahnah
New Contributor III

Any help please .....

0 Kudos
JohnHauck
Occasional Contributor II

So, I believe what you are looking for is to create a search cursor with a null QueryFilter on the feature class from the Districts layer and for each feature in the districts class add the field value that contains the districts name for example to the districts ComboBox. Then when the user selects a value from the districts combo box you will need the actual geometry for the district they select. You could either store them (the geometries for the districts) upfront when you loop through looking for the names or you could do an additional search cursor passing in a QueryFillter with a where clause that searches for the user selected name. Once you have the district geometry you can create a search cursor on the sub-districts feature class and pass in a SpatialFilter that contains the districts geometry and the appropriate spatial relationship value. With this you could get all of the sub districts that are coincident with the district. You would then basically start the general steps to the workflow over again by writing out the value for each feature returned by the spatial query on the sub-districts class to the sub districts combo-box. The user selects a sub-district and you again use the actual geometry for that sub-district to perform a spatial query on the next class in line and so on.

For the zoom…each time the user selects a combo-box you will need to find the corresponding geometry for the selected value. At that point you could set the maps extent accordingly.

Let me know if any of that is not clear...

0 Kudos
NajdALHanahnah
New Contributor III

Thanks Mr.John for your clarification

Mainly   this what i need , through the filter  each ComboBox select value will filter based on it and so on , appreciate what you write but i need your help to get the suitable code that i need it to do this function (plz help me ) and in the same time many people will like and they will benefit from this article ..

Note i have 4 layers as shown in the application .(polygon)

Thanks

0 Kudos
JohnHauck
Occasional Contributor II

Since this only took a couple of minutes to write I will post an example. However, if you need someone to write solutions for you then we have many people who work in Professional Services that can build applications for you.


using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace EngineWindowsApplication1
{
    public partial class Form1 : Form
    {
        private enum LayerType { Districts, SubDistricts };


        IList<IGeometry> m_districts = null;
        IList<IGeometry> m_subDistricts = null;


        IFeatureLayer m_districtsLayer = null;
        IFeatureLayer m_subDistrictsLayer = null;


        string m_districtsKeyFieldName = "STATE_NAME";
        string m_subDistrictsKeyFieldName = "NAME";


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //could be used to cache the geoms, may not be appropriate for large feature classes
            m_districts = new List<IGeometry>();
            m_subDistricts = new List<IGeometry>();


            m_districtsLayer = (IFeatureLayer)axMapControl1.get_Layer(1);
            m_subDistrictsLayer = (IFeatureLayer)axMapControl1.get_Layer(0);


            //get the districts layer
            IFeatureCursor fCur = m_districtsLayer.FeatureClass.Search(null,false);
            populateCBO(fCur, m_districtsKeyFieldName, false, LayerType.Districts, comboBox1);
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            reset();


            IGeometry g = getGeomAndZoom(LayerType.Districts, comboBox1.SelectedIndex, comboBox1.SelectedItem.ToString());


            ISpatialFilter sf = new SpatialFilter() { Geometry = g, SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects };


            IFeatureCursor fCur = m_subDistrictsLayer.FeatureClass.Search(sf, false);


            comboBox2.Enabled = true;


            populateCBO(fCur, m_subDistrictsKeyFieldName, true, LayerType.SubDistricts, comboBox2);
        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            IGeometry g = getGeomAndZoom(LayerType.SubDistricts, comboBox2.SelectedIndex, comboBox2.SelectedItem.ToString()); 
        }


        private IGeometry getGeomAndZoom(LayerType lt, int i, string val)
        {
            string fieldName = "";
            IGeometry g = null;
            IFeatureLayer fl = null;
            IList<IGeometry> l = null;


            switch (lt)
            {
                case LayerType.Districts:
                    fieldName = m_districtsKeyFieldName;
                    fl = m_districtsLayer;
                    l = m_districts;
                    break;
                case LayerType.SubDistricts:
                    fieldName = m_subDistrictsKeyFieldName;
                    fl = m_subDistrictsLayer;
                    l = m_subDistricts;
                    break;
                default:
                    break;
            }


            if (l.Count > 0)
                g = l;
            else
            {
                IQueryFilter qf = new QueryFilter() { WhereClause = fieldName + " = '" + val + "'"};
                g = fl.FeatureClass.Search(qf, false).NextFeature().Shape;
            }


            axMapControl1.Extent = g.Envelope;
            axMapControl1.Refresh();


            return g;
        }


        private void populateCBO(IFeatureCursor fCur, string fieldName, bool cacheGeoms, LayerType lt, ComboBox cbo)
        {
            IFeature f = fCur.NextFeature();
            while (f != null)
            {
                cbo.Items.Add(f.get_Value(f.Fields.FindField(fieldName)));


                if (cacheGeoms)
                {
                    switch (lt)
                    {
                        case LayerType.Districts:
                            m_districts.Add(f.Shape);
                            break;
                        case LayerType.SubDistricts:
                            m_subDistricts.Add(f.Shape);
                            break;
                        default:
                            break;
                    }
                }


                f = fCur.NextFeature();
            }
        }


        private void reset()
        {
            m_subDistricts = new List<IGeometry>();
            comboBox2.Text = "";
            comboBox2.Items.Clear();
            comboBox2.Enabled = false;
        }
    }
}




0 Kudos
NajdALHanahnah
New Contributor III

Dear Mr.John

Thanks for your feedback , really i appreciate  what you did , thanks a  , i problem what i face it is i don't have for research because i have many things to do it not this function only ...

I will test it and then i will give the feedback ...

Best regards

0 Kudos
NajdALHanahnah
New Contributor III

Dear Mr.John

Really it's work fine  with me without any problem , many thanks for you >>>>

I am happy

John

0 Kudos
JohnHauck
Occasional Contributor II

Happy to help. Glad it worked out ok and thanks for the follow up!