How to query MapServer service by polygon?

2382
5
10-22-2021 01:47 PM
EricEagle
Occasional Contributor III

I'm attempting to pull a json response of Point records that fall within an arbitrarily defined polygon from an ArcGIS REST endpoint (MapServer).  The endpoint supports JSON response and querying.  Everything is in EPSG 4326, nothing weird.

 

I'm trying to query it by esriGeometryPolygon.

It is a simple 4-sided polygon built clockwise.  Coords are in Y,X format.

My ring looks like this:

{"rings":[[[10,20],[10,40],[30,40],[30,20],[10,20]]]}

I figure the "spatialReference" element defaults to 4326 for json though couldn't find that explicitly stated in documentation.  However, adding the spatialReference property makes no difference.

I am wrapping this all in a GET call that looks like this (the server is not internet accessible so I'm making it generic):

 

https://server/arcgis/rest/services/PARENT/SERVICENAME/MapServer/0/query?geometry={"rings":[[[10,20],[10,40],[30,40],[30,20],[10,20]]]}&geometryType=esriGeometryPolygon&spatialRel=esriSpatialRelIntersects&outFields=FIELDNAME&returnGeometry=false&returnTrueCurves=false&returnIdsOnly=false&returnCountOnly=false&returnZ=false&returnDistinctValues=false&returnExtentsOnly=false&f=pjson

 

When I go to this area on the service, there are definitely features within the polygon.  However, they will not return via this query.  So I figure I'm doing something wrong in structuring my request.  Even on the REST querying documentation there are no examples showing how to clearly return record properties from a MapServer service based on a Polygon object.

0 Kudos
5 Replies
jcarlson
MVP Esteemed Contributor

Do you have a corresponding feature service? I don't know that spatial queries work on Map Image services. Or at least, I've not been able to do so.

- Josh Carlson
Kendall County GIS
0 Kudos
DavidPike
MVP Frequent Contributor

I'd recommend going to the query? endpoint of the service when dealing with these.  Then looking at the query being set when you press the query button, either in dev tools or the url formed.

geometry={"rings":[[[10,20],[10,40],[30,40],[30,20],[10,20]]],"spatialReference":{"wkid":4326,"latestWkid":4326}}}
0 Kudos
EricEagle
Occasional Contributor III

So, I had taken coordinate pairs I was using with the esriGeometryEnvelope object (YX) and followed the same YX order with the esriGeometryPolygon.

When flipping the pairs to XY, it worked.

Not sure why that should be the case, but if different pair order for different geometry objects in the same API is a thing, I feel like someone on the QA side of the house might've missed something...

 

So, @jcarlson  - yes it does definitely work

And thank you @DavidPike yes that is where I started on this before moving to python

0 Kudos
jcarlson
MVP Esteemed Contributor

Ah, good! I know that spatial filters don't work in Dashboards for whatever reason. I hardly ever use them, but good to know that spatial query parameters will still work.

- Josh Carlson
Kendall County GIS
0 Kudos
TanuHoque
Esri Regular Contributor

Just as FYI: both map service (aka map image layer) and feature service query operations follow the same specs. That is why you can add a feature layer in your web map off a map service or a feature service. When you add a feature layer off a map service, Editing is the only capability you won't have.

here is an example of a map service query request with polygon spatial filter:

https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0/query?geom...

 

0 Kudos