This project is read-only.

Selecting features from shapefile using extent

May 26, 2011 at 6:37 AM

Trying to select all featues within a given extent using the following

features = new FeatureSet(features.Select(bound.ClipToExtent));

But what I get back is only the features intersected by the extent.

Any ideas how I maybe able to achieve selecting all polygons either intersected or within extent.

Jul 7, 2011 at 3:06 PM

If you want to select all Features of a layer, you can use the SelectByAttribute method and give the filter: "" wichi select all the Features of a Layer.

Hope it can help you.

LordTj

Jul 7, 2011 at 9:09 PM

Hi,

If I understand your problem, you can use IFeatureSource as shown in the following method.  The following code is actually overkill because it uses a search geometry instead of just an extent.  My GCSearchMethod is my own definition, but should be intuitive.  I had to populate the z values in the geometry coordinates with Nans to workaround some issues in the Topology engine.  If you have 3D features, that step may not be necessary.  You can ignore the returned array and the code at the end of the for loop.  Anything that makes it past the switch statement has passed the tests.

To create an IFeatureSource from a shapefile, you can use the static method DotSpatial.Data.ShapefileFeatureSource.Open(<shapefile path>, false, false);  If you are going to do more than 3 or so searches on the FeatureSource, you may want to pass true for 2nd argument so spatial indexing is turned on (it takes about the time of 3 searches to build the index and uses up a little memory, but not much).

HTH,

Kyle

        public static GCClientEntityInfo[] SearchAndModifyEntities(bool bReturnGeometry, IFeatureSource ifs, IGeometry searchGeometry, GCSearchMethod searchMethod, bool bSetSpecifiedBits, int nSetBits, int nSetLoadSetBits, int nClearLoadSetBits, ref List<KeyValuePair<int, DataRow>> rowsChanged)
        {
            const int chunkSize = 50000;

            List<GCClientEntityInfo> listEntityInfo = new List<GCClientEntityInfo>();
            int index = 0;
            IFeatureSet ifsSelected = null;

            if (null != searchGeometry && GCSearchMethod.LineCross == searchMethod)
                searchGeometry = new LineString(searchGeometry.Coordinates);

            IEnvelope ise = null == searchGeometry ? null : searchGeometry.Envelope;
            do
            {
                ifsSelected = ifs.Select(null, ise, ref index, chunkSize);
                foreach (IFeature feature in ifsSelected.Features)
                {
                    if (null != searchGeometry && searchMethod != GCSearchMethod.ExtentIntersect)
                    {
                        IGeometry fg = Geometry.FromBasicGeometry(feature.BasicGeometry);
                        foreach (Coordinate c in fg.Coordinates)
                            c.Z = double.NaN;

                        switch (searchMethod)
                        {
                            case GCSearchMethod.AreaIntersect:
                                if (!searchGeometry.Intersects(fg))
                                    continue;
                                break;
                            case GCSearchMethod.Contains:
                                if (!searchGeometry.Contains(fg))
                                    continue;
                                break;
                            case GCSearchMethod.ContainedBy:
                                if (!fg.Contains(searchGeometry))
                                    continue;
                                break;
                            case GCSearchMethod.LineCross:
                                if (!searchGeometry.Crosses(fg))
                                    continue;
                                break;
                            default:
                                throw new ArgumentOutOfRangeException("searchMethod");
                        }
                    }

                    GCClientEntityInfo ei = new GCClientEntityInfo();
                    DataRow dr = feature.DataRow;
                    ei.EntityID = (int)(double)dr["ID"];
                    if (bReturnGeometry)
                        ei.Shape = Geometry.FromBasicGeometry(feature.BasicGeometry);
                    listEntityInfo.Add(ei);

                    if (bSetSpecifiedBits)
                    {
                        SetSpecifiedBits(dr, nSetBits, 0, nSetLoadSetBits, nClearLoadSetBits);
                        int nRowNumber = feature.RecordNumber - 1;
                        rowsChanged.Add(new KeyValuePair<int, DataRow>(nRowNumber, dr));
                    }
                }
            } while (ifsSelected.NumRows() == chunkSize);

            return listEntityInfo.ToArray();
        }