Get extent of polygons

7146
19
04-14-2011 05:31 PM
SystemAdministrator
New Contributor
Hello, I would like to know if there is a REST service (and perhaps a JSAPI function) that can get the extent of a set of polygons without having to return the geometry to the browser.

So it would work like this:

1) Make a rest query to ArcGIS server with a where clause like "STATE_CODE in ('TX','CA','NY')"
2) Return a JSON esri.geometry.Extent object.

Does this function, or something similar exist?
0 Kudos
19 Replies
SiqiLi
by Esri Contributor
Esri Contributor
Hi Anaish,

You may use esri.geometry.Polygon.getExtent() method to get the extent of a polygon. Here is its documentation:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/polygon.htm#getExtent
0 Kudos
derekswingley1
Frequent Contributor II
Hi Anaish,

You may use esri.geometry.Polygon.getExtent() method to get the extent of a polygon. Here is its documentation:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/polygon.htm#getExtent


While correct, I don't think this addresses OP's original question.

There's not a way that I know of to only get an extent back from a REST endpoint. You're either getting geometries, or you're not. But it seems like a lot of extra effort to send actual geometries over the wire when you're only interested in their extents.

Are you open to using pre-computed extents and loading those from a .json file? I used this strategy in a dijit I did a while back:  https://github.com/swingley/zoomto_dijit
0 Kudos
SystemAdministrator
New Contributor
Hi swingley, thanks for the reply. I can't use static json files because the geometries in my web application are dynamic (i.e they can be added in an adhoc manner).
0 Kudos
derekswingley1
Frequent Contributor II
Unless you want to write a server object extension to do this, I think you're stuck sending geometries to the client and computing an extent from them.
0 Kudos
HemingZhu
Occasional Contributor III
Hi swingley, thanks for the reply. I can't use static json files because the geometries in my web application are dynamic (i.e they can be added in an adhoc manner).


if you use esri.request to set a url with f=json format. you will get a featurset form server in json text format. Then it is easy to get extent from it. Here is a code you can try:


var urlStr = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapSe...";

var url = esri.urlToObject(urlStr);

var requestHandle = esri.request({
        url: url.path,
        content: url.query,
        handleAs: "text",
        load: requestSucceeded,
        error: requestFailed
      }, {useProxy:true});

....

function requestSucceeded(response, io) {
       var featureSet = eval('(' + response + ')');
       var extent =esri.graphicsExtent(featureSet.features);        
}
0 Kudos
derekswingley1
Frequent Contributor II
@hzhu that still brings all the geometries across the wire, which is what OP is trying to avoid.
0 Kudos
HemingZhu
Occasional Contributor III
@hzhu that still brings all the geometries across the wire, which is what OP is trying to avoid.


Using esri.request all the geometries returns as text or strings (Json text), which is the basic format of the response and fundamentally different form what is return from querytask's callback, which is wraped by server as an object. I don't know what is OP try to avoid if he want something more simple back from the server than a string and convert it to geometries on client browser.
0 Kudos
derekswingley1
Frequent Contributor II
@hzhu As you request more features (using either esri.request or a queryTask), the response size will grow. If the server only returned the extent of the features that satisfy a request, then the response size will be consistent if you ask for 10 or 1000 features. To my knowledge, there's not an out of the box way to get just a bounding box for features that satisfy a request w/o sending all of the geometries to the client.

How is the return value of esri.request different than a queryTask? The JavaScript syntax for working with them differs but both fetch plain text from the server (you can verify this via firebug or chrome's dev tools) that is then turned into JavaScript objects on the client.
0 Kudos
HemingZhu
Occasional Contributor III
@hzhu As you request more features (using either esri.request or a queryTask), the response size will grow. If the server only returned the extent of the features that satisfy a request, then the response size will be consistent if you ask for 10 or 1000 features. To my knowledge, there's not an out of the box way to get just a bounding box for features that satisfy a request w/o sending all of the geometries to the client.

How is the return value of esri.request different than a queryTask? The JavaScript syntax for working with them differs but both fetch plain text from the server (you can verify this via firebug or chrome's dev tools) that is then turned into JavaScript objects on the client.


Handle as "text" (json text in this case) is difinitely differently than handle as "featureset" (Object). Yes there is not way to get the extent directly. But wrap a jason text is different and lighter than wrap featureset object in a http resonse object, and at least, is close to a solution. firebug's debug shows the response object in a text format for display, internally primitive type (string) and object type are delievered and handled differently. There is no point to argue here than try to find a solution to help...
0 Kudos