Select to view content in your preferred language

Using Full Extent or Geometry to query points based on poly

839
6
06-21-2011 02:41 PM
NathalieNeagle
Regular Contributor
I have one combo box (drop down Menu) name PolySels and another combo box (drop down Menu) name PointsSelected

I want my end users to select a Polygon from the PolySels combo box and a Point Layer from the combo box PointsSelected and have my application preform a spatial selection on the points layer. Select all the points within the poly.

I think I have everything set up but I'm having trouble finishing my query selection code and I was wonder if some could help. I've checked out other post and just can't seem to figure it out.


  private void ExecuteSpatialSelectionButton_Click(object sender, RoutedEventArgs e)
        {

            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();

            //My Point layer that will be queried 
            QueryTask queryTask = new QueryTask((PointsSelected.SelectedItem as FeatureLayer).Url);

            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            Query query = new ESRI.ArcGIS.Client.Tasks.Query();

            //Specify fields to return from query...all fields
            query.OutFields.Add("*");


         

//Here's where I'm having problems: I can't figure out how to set the second combo box (the polygon selected layer) to a graphic layer or maybe it needs to be a featurelayer?? 

            GraphicsLayer polyGraphicLayer = new GraphicsLayer((PolySels.SelectedItem as GraphicsLayer).Url);
          

  //FeatureLayer polyFeatureLayer = new FeatureLayer((PolySels.SelectedItem as FeatureLayer).Url);
           

           //Here's the second part that is confusing me based on what I do above should I preform my query with the graphiclayer and use the fullextent or with the feature layer and use the geometry??

            query.Geometry = polyGraphicLayer.FullExtent;
           

           // query.SpatialRelationship = SpatialRelationship.esriSpatialRelContains;

       //     query.Geometry = polyFeatureLayer.Geometry;





            // Return geometry with result features
            query.ReturnGeometry = true;

            queryTask.ExecuteAsync(query);


        }




Thanks in advance.
Nathalie
0 Kudos
6 Replies
NathalieNeagle
Regular Contributor
If I drop the code that I was questions and instead add:

int layernum = PolySels.SelectedIndex;

query.Geometry = Map.Layers[layernum].FullExtent;

It works but it returns every single point...no matter what.  It doesn't matter if the point fall inside or outside of the poly?
0 Kudos
IgressT
Emerging Contributor
If I drop the code that I was questions and instead add:

int layernum = PolySels.SelectedIndex;

query.Geometry = Map.Layers[layernum].FullExtent;

It works but it returns every single point...no matter what.  It doesn't matter if the point fall inside or outside of the poly?


Just a thought... can you use Geometry service relation task
0 Kudos
NathalieNeagle
Regular Contributor
doc21,
Thanks for the reply.
Yes I believe I can use the Geometry service relation task but I guess I'm having the same issue...BRIAN FREEZE.

I can't seem to wrap my head around around this...after looking at the code examples and assigning the graphic layers to my map layers (see code below). I can't seem to figure out how to then send my results (graphics) and set the symbol and then add the results to the map and my featuredatagrid...similar to my query operations

 private void ExecuteSpatialSpeciesNameButton_Click(object sender, RoutedEventArgs e)
        {



            GeometryService geometryService = new GeometryService("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
                "Geometry/GeometryServer");

            geometryService.RelationCompleted += GeometryService_RelationCompleted;
            
            GraphicsLayer pointGraphicsLayer = Map.Layers["MyPointGraphicsLayer"] as GraphicsLayer;
            pointGraphicsLayer = Map.Layers[0] as GraphicsLayer;
            
            GraphicsLayer polygonGraphicsLayer = Map.Layers["MyPolygonGraphicsLayer"] as GraphicsLayer;
            int layernum = PolySels.SelectedIndex;
            polygonGraphicsLayer = Map.Layers[layernum] as GraphicsLayer;


            geometryService.RelationAsync(pointGraphicsLayer.Graphics.ToList(), polygonGraphicsLayer.Graphics.ToList(),
                GeometryRelation.esriGeometryRelationWithin, null);

     

        }



        private void GeometryService_RelationCompleted(object sender, RelationEventArgs args)
        {
                

             List<GeometryRelationPair> results = args.Results;
            foreach (GeometryRelationPair pair in results)
            {
//I'm unsure how to grap the reults (graphics) and set the symbol and then add the results to the map and my featuredatagrid...similar to my query operations

               // results.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
            }

           


Thanks for the help
Nathalie
0 Kudos
NathalieNeagle
Regular Contributor
If I drop the code that I was questions and instead add:

int layernum = PolySels.SelectedIndex;

query.Geometry = Map.Layers[layernum].FullExtent;

It works but it returns every single point...no matter what.  It doesn't matter if the point fall inside or outside of the poly?



So going back to one of my original tries...when I trace the request in fiddler if is sending the extent

{"xmin":-179.999999,"ymin":-89.999999,"xmax":179.999999,"ymax":89.999999,"spatialReference":{"wkid":4326}}

Which is not the extent of the layer...the extent above seems like the full extent of the world so I'm not sure what is happening with this.  WHen I look at my rest endpoints my layer(s) have normal looking extents.

Can anyone get me past this with some help...maybe a snippet of code.

Thanks
Nat
0 Kudos
NathalieNeagle
Regular Contributor
I just can't seem to get this and I'm running into a time-limit/wall.  I'm going to try and handle it just like the spatial selection (except instead of drawing a polygon I will use an existing one) but I think I'm going to run into the same problem that I just can't wrap my head around.

When you run a select by a drawn polygon the parameter args is coming from the drawn polygon so I just want to do something similar but I need to set my polygon layer to

here's how it used in the drawn poly
private void MyDrawSurface_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)

query.Geometry = args.Geometry;



Here's what I would do in trying to grab a poly
FeatureLayer polyLayer = Map.Layers[5] as FeatureLayer;
query.Geometry = polyLayer.Geometry;


But once again when I look at the sent results over fiddle it is giving some coords (Extent) for the entire world and not the extent of my layer that I see at the restendpoint.

Someone has to have done this simple task before...I've seen other post by I can't seem to find code and I'm having trouble piecing everything together.

Please Help.
0 Kudos
NathalieNeagle
Regular Contributor
OK,
I think my first problem was I was setting the query.geometry to a map Layer index and I really wanted to set it to a sublayer?? I have 4 base layers and one dynamic service which has like 20 layers in it and I want to have it work off one of the dynamic layer...based off of the dropdown.

I change my code to this

     private void ExecuteSpatialSpeciesNameButton_Click(object sender, RoutedEventArgs e)
        {

            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();


            QueryTask queryTask = new QueryTask((Layers.SelectedItem as FeatureLayer).Url);

            queryTask.ExecuteCompleted += QuerySpa_ExecuteCompleted;
            queryTask.Failed += QuerySpa_Failed;

            Query query = new ESRI.ArcGIS.Client.Tasks.Query();

            query.OutFields.Add("*");


            GraphicsLayer polygonGraphicsLayer = Map.Layers["MyPolygonGraphicsLayer"] as GraphicsLayer;
            
//--------------HERE"S MY CHANGE INSTEAD OF POINT TO A MAP LAYER I"M SETTING IT TO A SERVICE --------------------------------------------------------------

            string baseUrl = "http://servername/ArcGIS/rest/services/servicename/MapServer";

            FeatureLayer featureLayermodel = new FeatureLayer() { Url = string.Format("{0}/{1}", baseUrl, PolySels.SelectedIndex) };
            
            featureLayermodel.Initialize();
    

  //  query.Geometry = featureLayermodel.Graphics[0].Geometry;


//-----------THIS RETURNS NOTHING -----SEE BELOW FOR ERROR ----//
            query.Geometry = featureLayermodel.FullExtent;

          

            // Return geometry with result features
            query.ReturnGeometry = true;

            queryTask.ExecuteAsync(query);

           
            

        }



So in my code I have the line:
  query.Geometry = featureLayermodel.FullExtent;
and it returns the error

{"error":{"code":400,"message":"Unable to complete operation.","details":["'where' parameter not specified","'objectIds' parameter not specified","'time' parameter not specified","'geometry' parameter not specified","'text' parameter not specified","Unable to complete Query operation."]}}

Looks like for some reason it is not grabbing any geometry...Do not no why.

Per a suggestion on another post (Dominique suggested it)
If I go with this line
query.Geometry = featureLayermodel.Graphics[0].Geometry;
It just errors and I can see my geometry in fiddler is
{"xmin":-180,"ymin":0,"xmax":0,"ymax":90}
Which makes no sense, Dominique mentioned in the other post about having only one polygon in the layer featureLayermodel.Graphics[0].Geometry and I'm not sure how many polys there are so maybe that my issue.
0 Kudos