What is wrong with this line?

811
10
06-23-2011 12:57 PM
NathalieNeagle
New Contributor III
Why is this failing...what I am I missing?


  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("*");

//I think it doesn't like this

            FeatureLayer f = Map.Layers[4] as FeatureLayer;

// IT FAILS right after this HERE 
//-------------------------------------------------------??
            query.Geometry = f.Geometry;

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

            queryTask.ExecuteAsync(query);

           
            

        }



        private void QuerySpa_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;

      
            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features retured from query");
                return;
            }

            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;


            if (featureSet != null && featureSet.Features.Count > 0)

               // ResultsDisplay.Visibility = Visibility.Visible;

            {
                foreach (Graphic feature in featureSet.Features)
                {

                    switch (featureSet.GeometryType.ToString())
                    {
                        case "Polygon":
                            feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
                            break;
                        case "Polyline":
                            feature.Symbol = LayoutRoot.Resources["CustomGrowLineSymbol"] as LineSymbol;
                            break;
                        case "Point":
                            feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
                            break;
                    }

                    graphicsLayer.Graphics.Insert(0, feature);

                }
                //ResultsDisplay.Visibility = Visibility.Visible;
                ResultsDisplay.IsExpanded = true;
            }
            MyDrawSurface.IsEnabled = false;
        }

        private void QuerySpa_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }





Please help.
Nathalie
0 Kudos
10 Replies
DominiqueBroux
Esri Frequent Contributor
Why is this failing...what I am I missing?

 
 
//I think it doesn't like this
 
            FeatureLayer f = Map.Layers[4] as FeatureLayer;
 
// IT FAILS right after this HERE 
//-------------------------------------------------------??
           query.Geometry = f.Geometry;

 




Likely, the fifth layer of your map is not a feature layer (or there is less than 5 layers in your map). Using your feature Layer ID instead of an index would be more secure.
0 Kudos
dotMorten_esri
Esri Notable Contributor
An error message and a stack trace would also be a lot more helpful than "doesn't like it" 🙂
0 Kudos
NathalieNeagle
New Contributor III
Dominique and SharpGIS Thanks for the reply,

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 ExecutePointsButton_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 sure may layers have more than one polygon so maybe that my issue.

I'm just trying to use the a basic query to select some points (point layer) that fall within a polygon layer, which is selected in a drop down list by the user. A BASIC SPATIAL SELECTION.

I'm thinking I might have to change my polygon layer to a featureset and then do the query (similar to the spatial selection sdk example) but I'm just having problems and can't wrap my head around this.

Thanks
Nathalie
0 Kudos
dotMorten_esri
Esri Notable Contributor
Your problem is that you assume FullExtent is set once you call Initialize:
FeatureLayer featureLayermodel = new FeatureLayer() { Url = string.Format("{0}/{1}", baseUrl, PolySels.SelectedIndex) };
featureLayermodel.Initialize();
query.Geometry = featureLayermodel.FullExtent;

However, Silverlight does anything webrequest-releated asyncronously, so Initialize is not completed at the time you call FullExtent (the webrequest is still processing). Furthermore for a feature layer, FullExtent returns the extent of the Graphics in the layer, and since you haven't recieved any features (would require a call to Update() and wait for the feature to come back), you wont have a full extent value. Instead you probably want to grab featureLayerModel.LayerInfo.Extent AFTER the Initialized event fires. This value contains the full extent of the service itself and doesn't require you to fetch features as well.
0 Kudos
NathalieNeagle
New Contributor III
Morton,
Thanks for the reply.

I change around my code and I'm still using the full extent (not the featureLayerModel.LayerInfo.Extent per your suggestion)
But now I realized that the Full extent or the LayerInfo extent is not what I'm after because the full or layer extent of graphics will return all the points in the full/layer extent and not necessary just the points that fall within the polygons.  Should have known the full extent is not the same as the a spatial relationship where geometry1.geometry = geomentry2.geometry

Now guess I need to use .Geometry instead of .FullExtent so I need to use a FeatureLayer instead of GraphicsLayer but I'm not entirely sure how.  Here's what I currently have firing, To queries:

        
private void ExecuteSpatialPointWithinPolysButton_Click(object sender, RoutedEventArgs e)
        {

             QueryTask queryTaskm = new QueryTask((Models.SelectedItem as FeatureLayer).Url);
            queryTaskm.ExecuteCompleted += queryTaskmod_ExecuteCompleted;

            Query mquery = new ESRI.ArcGIS.Client.Tasks.Query();
            mquery.OutSpatialReference = Map.SpatialReference;
            mquery.ReturnGeometry = true;
            mquery.Where = "1=1";
            queryTaskm.ExecuteAsync(mquery);

  
        }


        void queryTaskmod_ExecuteCompleted(object sender, QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features retured from query");
                return;
            }

            GraphicsLayer graphicsLayerModPoly = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;

            foreach (Graphic graphic in featureSet.Features)
            {

                //   graphic.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;  // to show the selected Polys
                graphicsLayerModPoly.Graphics.Add(graphic);
            }

            //Clear the Graphics Layer, which will be used to draw the points selected
            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();

            //Begin Query of Points Layer
            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("*");
           
            //Query Point Layer Geometry with Poly layer Geometry
            query.Geometry = graphicsLayerModPoly.FullExtent;
            
            
            // Return geometry with result features
            query.ReturnGeometry = true;

            queryTask.ExecuteAsync(query);


        }


        private void QuerySpa_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;

      
            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features retured from query");
                return;
            }

            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;


            if (featureSet != null && featureSet.Features.Count > 0)

               // ResultsDisplay.Visibility = Visibility.Visible;

            {
                foreach (Graphic feature in featureSet.Features)
                {

                    switch (featureSet.GeometryType.ToString())
                    {
                        case "Polygon":
                            feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
                            break;
                        case "Polyline":
                            feature.Symbol = LayoutRoot.Resources["CustomGrowLineSymbol"] as LineSymbol;
                            break;
                        case "Point":
                            feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
                            break;
                    }

                    graphicsLayer.Graphics.Insert(0, feature);

                }
           
                ResultsDisplay.IsExpanded = true;
            }
     
        }

        private void QuerySpa_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }






So instead of declaring the grahpicLayerModPoly as a graphicsLayer �?�currently it is like this
GraphicsLayer graphicsLayerModPoly = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;

If I do this instead

FeatureLayer graphicsLayerModPoly = new FeatureLayer();


I can then change the line
            query.Geometry = graphicsLayerModPoly.FullExtent;

To:
            query.Geometry = graphicsLayerModPoly.Geometry;


But I get an error message that the where clause is missing or nonexistent and that is the issue I  keep running into no matter how I approach this.

Thanks again for the time and help
Nathalie
0 Kudos
JenniferNery
Esri Regular Contributor
Hi. I would like to help but I'm not sure what you are trying to do here. GraphicsLayer + QueryTask combo is similar to having FeatureLayer. You can use either one. Please correct me if I'm wrong, so you want to use a graphic.Geometry to perform SpatialQuery and you want to make a selection based on the query results? Have you looked into using Editor.Select? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave. This works with both GraphicsLayer or FeatureLayer. When using GraphicsLayer, your symbols need to have a Selected state like the symbols in this sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
Editor has a SelectionMode to which you can specify DrawMode.Polygon http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Editor~Sel...
0 Kudos
NathalieNeagle
New Contributor III
Jennifer,
Thanks for the reply,

Basically all I'm trying to do is select some points that fall within a parcel (a spatial selection).  I have one point layer and several poly layers so I have a drop down /combo box so my users can choose which poly layer they want to work with hit a button and it goes out and selects all the points that fall within the poly.

I can't use the editing tools in the SDK because I don't have my stuff stored in SDE and I don't have FeatureLayer capabilities...so I think I can't use the editing capabilities. 

I've tried to use or do something like the Relation -

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Relation

geometryService.RelationAsync(pointLayer.Graphics, polygonLayer,          GeometryRelation.esriGeometryRelationWithin, null);

but once again all the examples on the SDK and the turtorial in the concepts section focus on user drawn graphiclayers / featurelayers and I can't seem to wrap my head around my issue.

I have tried to go about this task using query.Geometry = g.Geometry; because it seems like I don't need to go the geometryService since the ESRI.ACRGIS.CLIENT.GEOMETRY.GEOMETRY QUERY.GEOMETRY is a spatial relationship query but I guess now I'm unsure.

What is the best route to take for my task?

On a side question/note
I think I have the geometryService running but when it completes I don't know how to or understand how to get the graphic returned out of the List<GeometryRelationshipPair> I want to assign all the graphics in the Graphic1 list (the points) to a new graphics layer but I don't know how.

private void GeometryService_RelationCompleted(object sender, RelationEventArgs args)
       {
  List<GeometryRelationPair> results = args.Results;
            foreach (GeometryRelationPair pair in results)
            {

?????????????????????????????????????????????

}
}




Also I guess I should say that my polygons are very complex and some of them seem to kill the applicaition when I try to simplify them....GeometryService_SimplifyCompleted
Because of this I thought it would be better just to stick with the first option of QUERY.GEOMETRY spatial relationship query
basically just like the SpatialQuery example
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery
but instead of the user drawing a polygon I'm trying to use a sublayer with several polygons or records.

Thanks again for the reply and help. I really need it, I'm lost and really appreciate all the help and trouble.

nathalie
0 Kudos
JenniferNery
Esri Regular Contributor
I think you can make use of SpatialQuery example, but you have to do multiple queries on that layer for each geometry from the polygon layer. When this task completes, you can mark each feature.Selected = true.
0 Kudos
NathalieNeagle
New Contributor III
Oh Jennifer! That was it, I was almost there and then I read your post and it finally clicked. Thank-YOU.

I think I now owe you a drink and I owe Dominique a Drink so I will be at the U.C. on Monday and probably Wed...if I don't see you guys I will send some cash with a co-worker and someone will buy you guys a icy cold beverage. :cool:

O.k one more thing I almost got the Relation working except it does fail on the crazy polygon layers when it runs the simplify but the relationship works. I am struggling with get the attributes to return so I was wondering if you wouldn't mind taking a look at the line that is giving me trouble and possibly providing some insight?

THanks
Nathalie


        private void GeometryService_RelationCompleted(object sender, RelationEventArgs args)
        {

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

            List<GeometryRelationPair> results = args.Results;
            foreach (GeometryRelationPair pair in results)
            {
                Graphic graphic = new Graphic();
                graphic.Geometry = pointLayer.Graphics[pair.Graphic1Index].Geometry;

        //this lines gives the error -Property or indexer 'ESRI.ArcGIS.Client.Graphic.Attributes' cannot be assigned to -- it is read only .. HOW CAN I SEND BACK THE FIELDS 
                //   graphic.Attributes.Add("*", pointLayer.Graphics[pair.Graphic1Index].Attributes);

                graphic.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
                graphicsLayer.Graphics.Insert(0, graphic);

            }

          
            ResultsDisplay.IsExpanded = true;

        }

0 Kudos