Select to view content in your preferred language

Select and Delete feature ,overlapped polygon problem

1872
10
11-09-2011 10:23 AM
ForamParikh
Regular Contributor
I am having trouble in Delete the feature.I am writing this code
Graphic g2 = new Graphic();
            g2.Geometry = graphicsLayer.Graphics[0].Geometry;
         
           
            FeatureLayer fl = new FeatureLayer();
            fl = MyMap.Layers["fl"] as FeatureLayer;
            fl.Graphics.Remove(g2);

but it is not working please help me in delete the feature.

also if any one know about overlapped polygon means if user draw single polygon and then again he draw second overlapped polygon then it should prompt me that overlapped polygon is not allowed.how i can know that user has drawn separate polygons or overlapped polygons?

to combine the multiple polygon i am using below code

ESRI.ArcGIS.Client.Geometry.Polygon a = new ESRI.ArcGIS.Client.Geometry.Polygon();
            foreach (Graphic g in graphicsLayer.Graphics)
            {
              ESRI.ArcGIS.Client.Geometry.Polygon p=g.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
               
                foreach(ESRI.ArcGIS.Client.Geometry.PointCollection pointcell in p.Rings)
                {
                    a.Rings.Add(pointcell);
                   
                }
            }          
          
            graphicsLayer.Graphics.Clear();
            Graphic g1 = new Graphic();
           
            g1.Geometry = a;
            g1.Symbol = LayoutRoot.Resources["DrawFillSymbol1"] as ESRI.ArcGIS.Client.Symbols.Symbol;
            graphicsLayer.Graphics.Add(g1);

and if the polygon is overlapped then i am not able to save it in feature layer that's why i want prompt message after draw or any one know how i can combine overlapped polygon into one?and save it on feature layer.

Please help,
Thanks
Foram
0 Kudos
10 Replies
JenniferNery
Esri Regular Contributor
When doing a remove on any collection. It should be removing the same instance. In your code, you create new instance with the same geometry. Remove() will not find this new instance in the GraphicCollection, unless it's been added to the collection.

How about the following code. Update the layer ID and geometry used for comparison. Add "using System.Linq":
var l = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
if (l != null)
{
 var graphic = l.Graphics.FirstOrDefault(g => g.Geometry != null && g.Geometry.Equals(someGeometry));    
 if (graphic != null)
 l.Graphics.Remove(graphic); //check if this returns true/false.
}


To prevent drawing overlapping polygons, you can probably try to perform a FindGraphicsInHostCoordinates() before adding to layer.Graphics.
0 Kudos
ForamParikh
Regular Contributor
Thanks jenniferdnery,

Delete is working fine now.but still i am confuse in overlapped polygon.I want when user draw first polygon and then user will draw second polygon so if second polygon is overlapped then it should prompt user to not to draw overlapped polygon if user draw separate polygon then it should work fine.do you know any method for that?how i can use FindGraphicsInHostCoordinates?

I will appropriate if you can help me,

Once again thanks
Foram
0 Kudos
JenniferNery
Esri Regular Contributor
Here's some documentation on FindGraphicsInHostCoordinates: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLa.... You can convert your polygon to Rect and pass to this method, the result will be graphics within that rectangle.

You can also try the following using v3.0 Beta 1 http://help.arcgis.com/en/webapi/silverlight/3.0/.Geodesic.Area() will also be available in our 2.3 release.

However, I'm not sure if getting the intersection of their extents will be good enough for you. This may also be expensive since it tries to get intersection with all graphics in the layer.
       private static bool OverlapsExistingFeature(Graphic n, GraphicsLayer l)
        {
            var area = Geodesic.Area(n.Geometry as Polygon);
            foreach (var g in l.Graphics)
            {
                if (g != n)
                {
                    if (g.Geometry.Extent.Intersects(n.Geometry.Extent))
                    {
                        var intersection = GetIntersection(n, g);
                        var intersectionArea = Geodesic.Area(intersection);
                        var target = Geodesic.Area(g.Geometry as Polygon);
                        var coverage = (intersectionArea / target * 100);
                        if (coverage >= 100)
                            return true;
                    }
                }
            }
            return false;
        }

       private static Polygon GetIntersection(Graphic n, Graphic g)
       {
           var env = n.Geometry.Extent.Intersection(g.Geometry.Extent);
           var intersection = new Polygon();
           PointCollection ring = new PointCollection();
           ring.Add(new MapPoint(env.XMin, env.YMin));
           ring.Add(new MapPoint(env.XMin, env.YMax));
           ring.Add(new MapPoint(env.XMax, env.YMax));
           ring.Add(new MapPoint(env.XMax, env.YMin));
           ring.Add(new MapPoint(env.XMin, env.YMin));
           intersection.Rings.Add(ring);
           return intersection;
       }
0 Kudos
ForamParikh
Regular Contributor
Thanks Jennifer I will try and I will let u know.
0 Kudos
ForamParikh
Regular Contributor
Jennifer,

I am not using 3.0 API so i am not able to use Geodesic.Is there any way to implement it?or can i install arcgis silverlight 30 beta 1 API.But it will not affect my existing code?

Please advise,

Thanks
Foram
0 Kudos
JenniferNery
Esri Regular Contributor
You can try 3.0 Beta 1 for now. Our v2.3 will also include Geodesic.Area(), it should go final soon.
http://help.arcgis.com/en/webapi/silverlight/3.0/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Geomet...
0 Kudos
ForamParikh
Regular Contributor
Thanks jennifer for your reply I will try and let u know i am having another issue with my application.

I have two feature layers in my application one is polygon and another one is point.i have set the resolution for both feature layers.i have set in such a manner that one time only one layer will be visible

<esri:FeatureLayer ID="polygonlayer" DisableClientCaching="True"    
                               AutoSave="False"                                Url="http://council4watershedhealth.org/ArcGIS/rest/services/LAWEP/Poly/FeatureServer/0"  
                               OutFields="*"  EndSaveEdits="FeatureLayer_EndSaveEdits"
                               Mode="OnDemand"   MinimumResolution="2.0" MaximumResolution="3.0" />
            <esri:FeatureLayer ID="pointlayer" MinimumResolution="3.0" MaximumResolution="100.0"   OutFields="*"   DisableClientCaching="True"
                               Url="http://council4watershedhealth.org/ArcGIS/rest/services/LAWEP/Point/FeatureServer/0" AutoSave="False"
                               Mode="OnDemand" Visible="True" EndSaveEdits="FeatureLayer_EndSaveEdits_1" />

so suppose user click on point or polygon and then if user hit delete button then both features should remove.

but at a time only one feature i can remove

FeatureLayer featureLayer = MyMap.Layers["polygonlayer"] as FeatureLayer;
            Graphic graphic=new Graphic();
            graphic = featureLayer.Graphics.FirstOrDefault(g => (int)g.Attributes[featureLayer.LayerInfo.Fields[1].Name.ToString()] == 205);
            if (graphic != null)
            {
                featureLayer.Graphics.Remove(graphic);
                featureLayer.SaveEdits();
            }

featureLayer = MyMap.Layers["pointlayer"] as FeatureLayer;
            Graphic graphic=new Graphic();
            graphic = featureLayer.Graphics.FirstOrDefault(g => (int)g.Attributes[featureLayer.LayerInfo.Fields[1].Name.ToString()] == 205);
            if (graphic != null)
            {
                featureLayer.Graphics.Remove(graphic);
                featureLayer.SaveEdits();
            }

but always i am getting one graphic as null..Please let me know how can i solve it?I dont want to show all features on my map but both should delete.

I want same for add the feature and i can add feature sucessfully even only i can see single feature but i can save both features but same thing is not working for delete.

Please let me know
Thanks
foram
0 Kudos
ForamParikh
Regular Contributor
can i delete the feature using query i mean i am using sde database so can i use like
delete from polygon_feature where location_id=205
delete from point_feature where location_id=205 but i am not sure is it good method or not?

Please let me know if i can use the queries then it will be very simple for me.

Thanks
Foram
0 Kudos
JenniferNery
Esri Regular Contributor
Kindly take a look at the editing samples in our SDK: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave. It might save you from writing code that is already available in the API. You can select multiple features from different FeatureLayers/GraphicsLayers using Editor.Select command. You can also delete selected features using Editor.DeleteSelected.

Deleting a feature from feature service requires deleting graphic from FeatureLayer.
0 Kudos