Executing a Spatial Query on a Feature Layer

853
4
07-26-2019 10:14 AM
MichaelHamsa
Occasional Contributor

Hello,

I was wondering if there were any good examples that would show me how to execute a QueryFeaturesAsync call on a FeatureTable using a SpatialRelationship of Within. I've got some code wired up but it doesn't seem to be returning any results, when I know there should be.

Here's the code I'm currently using. An exception is getting caught - no elements in this sequence when I try to get the First item from the query results.

                QueryParameters qp = new QueryParameters();
                qp.Geometry = new Envelope(Location, Radius, Radius);
                qp.SpatialRelationship = SpatialRelationship.Within;
                var table = layer.FeatureTable as GeodatabaseFeatureTable;
                try
                {
                    FeatureQueryResult queryResult = await table.QueryFeaturesAsync(qp);
                    Feature ftr = queryResult.First();
                }
                catch (Exception ex)
                {
                    string m = ex.Message;
                }

Thanks,

Mike...

0 Kudos
4 Replies
JoeHershman
MVP Regular Contributor

Are you sure it's a Within and not a Contains.  Personally I always play around with those spatial relationships because they never seem to work like I think they should.

Also in general I think using FirstOrDefault and checking for null is a cleaner approach, as opposed to capturing an exception.

Thanks,
-Joe
0 Kudos
ThadTilton
Esri Contributor

Yeah, I agree with Joe, you might want to experiment with some other relationships just to see what you get. Maybe it's a spatial reference issue? Are you sure the geometry you're querying with is coincident with the input features? Perhaps, as a quick test, you could try zooming to the extent of the envelope used for the query.

0 Kudos
MichaelHamsa
Occasional Contributor

Thanks a lot to all of you for the comments. It ended up being the Contains (vs. Within). Once I started using Contains, I got the results I expected.

Thanks again,

Mike Hamsa

Chief Technology Officer - GeoSpatial Innovations, Inc.

P: 512-982-6735

0 Kudos
TonyWakim
Esri Contributor

Also make sure that the SpatialReference of your envelope matches that of your layer.

0 Kudos