+ Reply to Thread
Results 1 to 15 of 15

Thread: Polygon intersection selects unselected graphics on user drawn polygon.

  1. #1
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Polygon intersection selects unselected graphics on user drawn polygon.

    Hi
    I have a small problem. i have some graphics on my base map. Now I do a Intersect for the graphics using polygon.Extent.Intersects(graphic.Geometry.Extent). This selects the different shapes on the map on which the polygon is drawn. But this also selects the graphics nearby with that comes around that selected graphic features which is not selected. I need to avoid this. Can anybody provide some light on it. I am using

    VS 2010.
    Silverlight 4.

    I am using the code

    GraphicsLayer graphicLayer = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicCollection graphicCollection = new GraphicCollection();
    graphicCollection = graphicLayer.Graphics;
    foreach (Graphic graphic in graphicCollection)
    {
    if (polygon.Extent.Intersects(graphic.Geometry.Extent))
    {
    ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol FillSymbol;
    FillSymbol = (ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)(LayoutRoot.Resources ["PolygonSelectSymbol"]);
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)FillSymbol;
    }
    }
    My xaml code is

    <esriSymbols:SimpleFillSymbol x:Key="PolygonSelectSymbol" Fill="#66BB0000" BorderBrush="#88CC0000" BorderThickness="2" />

    Any code snippet will be good
    Please find the attached screen shot along with.

    Regards,
    Saurabh.
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	select1.JPG‎
Views:	82
Size:	49.9 KB
ID:	13836   Click image for larger version

Name:	Select.JPG‎
Views:	95
Size:	35.9 KB
ID:	13834  
    Last edited by e_saurabh; 04-26-2012 at 06:08 AM.

  2. #2
    Demin Hu
    Join Date
    Apr 2010
    Posts
    136
    Points
    2
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    I might not understand your case well, but I think you could look GeometryService such as relation or intersect.

  3. #3
    Morten Nielsen

    Join Date
    Oct 2009
    Posts
    1,166
    Points
    44
    Answers Provided
    7


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    What you are doing is intersecting envelopes with each other. You are not intersecting polygons with an envelope (but the extent of the envelope), so naturally you are going to get a lot of false positives.
    Envelope/Envelope intersection is very fast and a good way to remove a lot of candidates, but you will have to do a full on intersection test, and unfortunately the API doesn't come with this out of the box, but you can use the geometry service for this, or google for an algorithm that will do the trick.
    If the polygons you want to intersect against are visible on the screen, you can use hit testing to accomplish the same thing (and this is btw extremely fast too). See GraphicsLayer.FindGraphicsInHostCoordinates(Rectangle);
    /Morten Nielsen
    ArcGIS Runtime SDK for .NET
    silverlight team blog - twitter - personal blog

  4. #4
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Hi Mortein,
    Thanks for your answer. I finally managed to use the geometry service to perform the intersection query. Now I am able to select the graphics on the map and it gets selected but still have one step to go as this selects all the graphics on the map. Can you just tell me the way to select the selected graphics at a time. Below is my new code.

    void spatialQueryPolygon(Object sender, DrawEventArgs e)
    {

    GraphicsLayer graphicLayer1 = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicsLayer graphicLayer = edmsMap.Layers["graphicsLayer"] as GraphicsLayer;
    //GraphicCollection _theGraphicsCollection = graphicLayer.Graphics;
    geometryService.RelationCompleted += new EventHandler<RelationEventArgs>(geometryService_RelationCompleted);

    string sclRelationship = "dim(g1.interior, g2) != null";
    geometryService.RelationAsync(graphicLayer1.Graphics.ToList(), graphicLayer1.Graphics.ToList(),
    GeometryRelation.esriGeometryRelationRelation, sclRelationship);
    }

    void geometryService_RelationCompleted(object sender, RelationEventArgs e)
    {
    string results = "";
    for (int i = 0; i < e.Results.Count; i++)
    {
    results += string.Format("Point {0} is within polygon {1}.\n", e.Results[i].Graphic1Index,
    e.Results[i].Graphic2Index);
    GraphicsLayer graphicLayer1 = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicCollection graphicCollection = graphicLayer1.Graphics;

    foreach (Graphic graphic in graphicCollection)
    {
    ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol SimpleLineSymbol;
    SimpleLineSymbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)(LayoutRoot.Resources["DefaultLineSymbol"]);
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)SimpleLineSymbol;
    }
    }
    }



    Regards,
    Saurabh.
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	process.JPG‎
Views:	44
Size:	52.4 KB
ID:	14046   Click image for larger version

Name:	select.JPG‎
Views:	41
Size:	43.0 KB
ID:	14047  
    Last edited by e_saurabh; 05-03-2012 at 04:53 AM.

  5. #5
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Quote Originally Posted by e_saurabh View Post
    Hi Mortein,
    Thanks for your answer. I finally managed to use the geometry service to perform the intersection query. Now I am able to select the graphics on the map and it gets selected but still have one step to go as this selects all the graphics on the map. Can you just tell me the way to select the selected graphics at a time. Below is my new code.

    void spatialQueryPolygon(Object sender, DrawEventArgs e)
    {

    GraphicsLayer graphicLayer1 = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicsLayer graphicLayer = edmsMap.Layers["graphicsLayer"] as GraphicsLayer;
    //GraphicCollection _theGraphicsCollection = graphicLayer.Graphics;
    geometryService.RelationCompleted += new EventHandler<RelationEventArgs>(geometryService_RelationCompleted);

    string sclRelationship = "dim(g1.interior, g2) != null";
    geometryService.RelationAsync(graphicLayer1.Graphics.ToList(), graphicLayer1.Graphics.ToList(),
    GeometryRelation.esriGeometryRelationRelation, sclRelationship);
    }

    void geometryService_RelationCompleted(object sender, RelationEventArgs e)
    {
    string results = "";
    for (int i = 0; i < e.Results.Count; i++)
    {
    results += string.Format("Point {0} is within polygon {1}.\n", e.Results[i].Graphic1Index,
    e.Results[i].Graphic2Index);
    GraphicsLayer graphicLayer1 = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicCollection graphicCollection = graphicLayer1.Graphics;

    foreach (Graphic graphic in graphicCollection)
    {
    ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol SimpleLineSymbol;
    SimpleLineSymbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)(LayoutRoot.Resources["DefaultLineSymbol"]);
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)SimpleLineSymbol;
    }
    }
    }



    Regards,
    Saurabh.
    Can anybody provide me some help on this..

  6. #6
    Morten Nielsen

    Join Date
    Oct 2009
    Posts
    1,166
    Points
    44
    Answers Provided
    7


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Are you saying you don't want to select all the graphics? Because your code loops through ALL the graphics in your layer and selects them one by one. Not sure why you do that, if you don't want to do that.
    /Morten Nielsen
    ArcGIS Runtime SDK for .NET
    silverlight team blog - twitter - personal blog

  7. #7
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Quote Originally Posted by SharpGIS View Post
    Are you saying you don't want to select all the graphics? Because your code loops through ALL the graphics in your layer and selects them one by one. Not sure why you do that, if you don't want to do that.
    Yes Morten.. That exactly what I want... I dont want to select all the graphics, just want to select the selected graphics for the user drawn polygon; but not finding a proper way to do the loop for selecting the single graphics.May be my loop is wrongly typed. Can u provide me a better way..

    Regards,
    saurabh.

  8. #8
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Quote Originally Posted by e_saurabh View Post
    Yes Morten.. That exactly what I want... I dont want to select all the graphics, just want to select the selected graphics for the user drawn polygon; but not finding a proper way to do the loop for selecting the single graphics.May be my loop is wrongly typed. Can u provide me a better way..

    Regards,
    saurabh.
    Hi Mortein,
    Finally I am able to select the graphics as required graphics from the user drawn polygon. But still this selects unwanted graphics and most importantly in my case this selects only the line graphics even if I draw to select polygon or point. Below is my code which I am performing using geometry service.

    public void spatialQueryPolygon(Object sender, DrawEventArgs e)
    {
    GeometryService geometryService =
    new GeometryService("http://sgdc1w8ahva09/ArcGIS/rest/services/BaseMap1/GeometryServer");
    GraphicsLayer programTypeGraphicLayer = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicCollection sourceGraphicCollection = programTypeGraphicLayer.Graphics;
    Graphic queryGraphic = new Graphic();
    // e.Geometry.SpatialReference = new SpatialReference(ArcConstants.SRID);
    queryGraphic.Geometry = e.Geometry;
    //queryGraphic.Geometry.SpatialReference = new SpatialReference(ArcConstants.SRID);
    GraphicCollection queryGraphicCollection = new GraphicCollection();
    queryGraphicCollection.Add(queryGraphic);
    geometryService.RelationCompleted += new EventHandler<RelationEventArgs>(geometryService_RelationCompleted);
    geometryService.RelationAsync(sourceGraphicCollection, queryGraphicCollection, GeometryRelation.esriGeometryRelationIntersection, null);
    }

    void geometryService_RelationCompleted(object sender, RelationEventArgs e)
    {
    GraphicsLayer sourceGraphicLayer = edmsMap.Layers[selectedProgramTypeName] as GraphicsLayer;
    GraphicCollection sourceGraphicCollection = sourceGraphicLayer.Graphics;
    GraphicCollection targetGraphicCollection = new GraphicCollection();
    List<GeometryRelationPair> geometryRelationPairList = new List<GeometryRelationPair>();
    List<string> collectionEventResultIdList = new List<string>();
    int datasetActivityID = layerIDs[layerNameslist.IndexOf(selectedProgramTypeName)];
    geometryRelationPairList = e.Results;

    foreach (GeometryRelationPair geometryRelationPair in geometryRelationPairList)
    {
    targetGraphicCollection.Add(sourceGraphicCollection[geometryRelationPair.Graphic1Index]);
    }

    foreach (Graphic graphic in targetGraphicCollection)
    {
    ESRI.ArcGIS.Client.Symbols.MarkerSymbol MarkerSymbol;
    MarkerSymbol = (ESRI.ArcGIS.Client.Symbols.MarkerSymbol)(LayoutRoot.Resources["DefaultMarkerSymbol"]);

    ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol FillSymbol;
    FillSymbol = (ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)(LayoutRoot.Resources["PolygonSelectSymbol"]);

    ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol SimpleLineSymbol;
    SimpleLineSymbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)(LayoutRoot.Resources["DefaultLineSymbol"]);

    string shape = graphic.Geometry.ToString();
    if (shape.Contains("Polygon"))
    {
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)FillSymbol;
    }
    else if (shape.Contains("Polyline"))
    {
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol)SimpleLineSymbol;
    }
    else if (shape.Contains("Point"))
    {
    graphic.Symbol = (ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol)MarkerSymbol;
    }


    Can u provide me a better way to select the desired graphics as wanted.

    Regards,
    Saurabh.
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	Process.JPG‎
Views:	36
Size:	53.6 KB
ID:	14133   Click image for larger version

Name:	Intersect_Complete.JPG‎
Views:	32
Size:	53.4 KB
ID:	14134  

  9. #9
    Jennifer Nery

    Join Date
    May 2010
    Posts
    2,449
    Points
    289
    Answers Provided
    51


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Have you tried using Editor.Select? This SDK sample demonstrates how you can do this by simply having an Editor as your Button's DataContext and then using Select command as Button.Command property. Editor SelectionMode can be set to Polygon if you are working with FeatureLayers. You can include/exclude layers from your map by setting Editor.LayerIDs property.
    Jennifer Nery
    Software Developer
    ArcGIS API for SL/WPF/WP7

  10. #10
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Quote Originally Posted by jenniferdnery View Post
    Have you tried using Editor.Select? This SDK sample demonstrates how you can do this by simply having an Editor as your Button's DataContext and then using Select command as Button.Command property. Editor SelectionMode can be set to Polygon if you are working with FeatureLayers. You can include/exclude layers from your map by setting Editor.LayerIDs property.
    Hi Jennefer,
    Thanks for the reply. But when I searched through the silverlight api it clearly says that "The SelectionMode specifies whether the user selects using a point, polygon, polyline or rectangle. Only FeatureLayer supports polygon and polyline selection modes. If any of these modes are selected but no FeatureLayers are referenced by the editor, this command will be disabled.
    "
    http://help.arcgis.com/en/webapi/sil...or~Select.html
    and in my caase I am usng graphics layer.

    Can u let me know in case of graphics layer how I can achieve this.

    Regards,
    Saurabh.
    Last edited by e_saurabh; 05-08-2012 at 01:20 AM.

  11. #11
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Still waiting for any suitable approach for this.

    ~Saurabh.

  12. #12
    Jennifer Nery

    Join Date
    May 2010
    Posts
    2,449
    Points
    289
    Answers Provided
    51


    0
    This post is marked as the answer

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Oh I see. If your GraphicsLayer contain features from a service, you can do a spatial query by setting Query.Geometry: http://help.arcgis.com/en/webapi/sil...m#SpatialQuery. If your GraphicsLayer contain features that only reside in the client, you can use FindGraphicsInHostCoordinates() but this only accepts either Point or Rect. You can also tweak this SDK sample: http://help.arcgis.com/en/webapi/sil....htm#Intersect. You can skip the QueryTask section where it adds the results to the GraphicsLayer, since your features are drawn and not retrieved from service.
    Jennifer Nery
    Software Developer
    ArcGIS API for SL/WPF/WP7

  13. #13
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Hi Jennifer,
    Thanks a lot for your reply and applogise for the dealy. I got my problem resolved using the FindGraphicsInHostCoordinates() method. Now I am able to select the required graphics on the map layer and no more false positives are returned. Just for an query, is there any way out to do this process using the polygon selection which should return only the true positives. Thanks again.

    ~Saurabh.

  14. #14
    Morten Nielsen

    Join Date
    Oct 2009
    Posts
    1,166
    Points
    44
    Answers Provided
    7


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    You can do the findgraphicsinhostcoordinates to get a good set of candidates, then use the geometry service to perform a full analysis on the remaining features.
    /Morten Nielsen
    ArcGIS Runtime SDK for .NET
    silverlight team blog - twitter - personal blog

  15. #15
    Saurabh Dasgupta
    Join Date
    Feb 2011
    Posts
    80
    Points
    1
    Answers Provided
    0


    0

    Default Re: Polygon intersection selects unselected graphics on user drawn polygon.

    Thanks Mortein. I will try the option.

    ~Saurabh.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts