Quantcast
Channel: DotSpatial
Viewing all articles
Browse latest Browse all 3973

New Post: Check to see if added polygon to featureset intersect error

$
0
0
Please can someone help me resolve the error in my code for checking intersect is added polygon overlaps.

if (e.Button == MouseButtons.Left)
                {
                    //left click - fill array of coordinates
                    Coordinate coord = map1.PixelToProj(e.Location);

                    if (polygonmouseClick)
                    {
                        //first time left click - create empty line feature
                        if (firstClick)
                        {
                            //Create a new List called polygonArray.

                            //this list will store the Coordinates
                            //We are going to store the mouse click coordinates into this array.

                            List<Coordinate> polygonArray = new List<Coordinate>();

                            //Create an instance for LinearRing class.
                            //We pass the polygon List to the constructor of this class
                            LinearRing polygonGeometry = new LinearRing(polygonArray);
                            IFeatureLayer pFLayer = (IFeatureLayer)polygonArray; 

                            //Add the polygonGeometry instance to PolygonFeature

                            IFeature polygonFeature = pFLayer.DataSet.AddFeature(polygonGeometry);

                            //add first coordinate to the polygon feature
                            polygonFeature.Coordinates.Add(coord);


                            // Return if no data layers exist
                            if (map1.Layers.Count < 1) return;


                            // Also, mpl can be used to change the selection color.
                            IMapPolygonLayer mpl = map1.Layers[0] as IMapPolygonLayer;

                            // If the first layer is not a polygon layer, exit.
                            if (mpl == null) return;

                            // Get the featureset that contains the actual polygon data for the layer
                            IFeatureSet fs = map1.Layers[0].DataSet as IFeatureSet;

                            // If this is null or not a featureset, exit
                            if (fs == null) return;

                            // Set up an index 
                            int iShape = 0;

                            // Cycle through the shapes
                            foreach (DotSpatial.Data.ShapeRange shape in fs.ShapeIndices)
                            {
                                // Test if the coordinate is in the polygon
                                if (shape.Intersects(coord))
                                {
                                    // Select the polygon if the the coordinate intersects.
                                    mpl.Select(iShape);
                                }
                                iShape++;
                            }

                            firstClick = false;
                        }
                        else
                        {
                            //second or more clicks - add points to the existing feature
                            IFeature existingFeature = (IFeature)polygonF.Features[polygonF.Features.Count - 1];

                            existingFeature.Coordinates.Add(coord);

                            //refresh the map if line has 2 or more points
                            if (existingFeature.Coordinates.Count >= 3)
                            {
                                //refresh the map
                                polygonF.InitializeVertices();
                                map1.ResetBuffer();
                            }
                        }
                    }
                }
                else
                {
                    //right click - reset first mouse click
                    firstClick = true;
                }
                break;


        }

I get the error "Unable to cast object of type 'System.Collections.Generic.List`1[DotSpatial.Topology.Coordinate]' to type 'DotSpatial.Symbology.IFeatureLayer'."

Viewing all articles
Browse latest Browse all 3973

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>