Fetch extent of the map using REST API

6175
12
12-09-2014 01:24 PM
BhavinSanghani
Occasional Contributor II

How to find extent of the features after querying to the layer?

 

e.g. I query Parcel layer using "where"= "GISID IN (100000)" on <mapservice>/<layerid>/query url and I want to find extent of the result.

 

Is this possible using REST API?

 

In ArcGIS Javascript API, I can use graphicsUtils.graphicsExtent() to find extent of the results. Looking similar while using REST API at java layer.

 

I am exporting map document using print utility service based on the "where" criteria and "mapservice" url I have. I do not instantiate map document. I use ExportWebMapTask to export the map document. I want to make sure that when I export the map document, extent of the result is zoomed-in appropriately.

0 Kudos
12 Replies
OwenEarley
Occasional Contributor III

I don't think this is possible solely within the REST API.

Your original REST API query will return a featureset object, each feature within the featureset will have a geometry. However, the REST API does not seem to have any functions to get the extent of all feature geometries.

This type of function is normally carried out by the client application. For example in the ESRI JS API you can use the client-side graphicsutils.graphicsExtent() method to get the extent of an array of graphics.

0 Kudos
BhavinSanghani
Occasional Contributor II

Can we write logic to calculate extent based on feature geometries? I don't know if its simple or complicated logic.

0 Kudos
NicholasHaney
New Contributor III

This code should do what you want:

var features = <your array of features>

var maxX, maxY, minX, minY;

  for(var i = 0; i < features.length; i++) {

         if(i == 0) {

               maxX = features.geometry.x;

               minX = features.geometry.x;

               maxY = features.geometry.y;

               minY = features.geometry.y;

         }

        else {

        if(features.geometry.x > maxX) {

               maxX = features.geometry.x;

         }

         if(features.geometry.x < minX) {

              minX = features.geometry.x;

         }

         if(features.geometry.y > maxY) {

             maxY = features.geometry.y;

         }

        if(features.geometry.y < minY) {

             minY = features.geometry.y;

        }

      }

   }

   var extent = new Extent(minX, minY, maxX, maxY, new SpatialReference({ wkid:4326 }));

I tested this with a point feature layer. If you are using polygons you will have to use the polygon class's getExtent() method instead of geometry.x or geometry.y. So your code would look something like: features.getExtent().xmin and features.getExtent().ymax

BhavinSanghani
Occasional Contributor II

I will try this approach. Probably, I need to just add padding based on the extent I found for one / multiple features. Then I can pass that as bbox in ExportWebMapTask json. I guess this would work for all types of spatialReference.

0 Kudos
OwenEarley
Occasional Contributor III

You certainly could write your own logic to calculate the combined extent but it may be simpler to use a function from an existing library.

If you are accessing REST via server-side Java code it may be worth looking into some of the Java Spatial libraries such as the ESRI Java ArcObjects SDK‌ (requires ArcGIS license), GeoTools or Spatial4j.

Please note I have not tried the Java GeoTools or Spatial4j as I mostly use JavaScript, python and C#.

0 Kudos
BhavinSanghani
Occasional Contributor II

I avoid using another library my project from maintenance point of view. I have one suggestion from ESRI support to use SOE but trying to avoid it. However, I get your point that it may be worth looking at open source code to validate the logic.

0 Kudos
OwenEarley
Occasional Contributor III

Was just thinking you could do this using:

  1. REST query to get your results (make sure that you request the geometries in the results).
  2. REST call to the Union method on a Geometry service (passing in your geometries from the previous step).

This results in two calls to ArcGIS Server but should be simpler to maintain.

0 Kudos
BhavinSanghani
Occasional Contributor II

This one would probably same as Nicholas's logic.

0 Kudos
NicholasHaney
New Contributor III

I agree with Owen, I can't think of a way to do this using only REST.

0 Kudos