Get extent of polygons

8294
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
derekswingley1
Frequent Contributor
True, we're not likely to find a solution to OP's question/problem in this thread. But I'd like to continue this discussion as I think we still need to clarify for anyone else who comes across this thread.

A featureSet, in the context of the JavaScript API and as delivered to the client, is a JSON object.

Let's look at this from another angle. How about we talk about this in terms of the REST API? If you want features from a layer in a map service, how do you get them? You use the query endpoint. If you use the endpoint via the JavaScript API's QueryTask (recommended) or through esri.request, you are getting back JSON (as long as you specify f=json when using esri.request).
0 Kudos
HemingZhu
Occasional Contributor III
True, we're not likely to find a solution to OP's question/problem in this thread. But I'd like to continue this discussion as I think we still need to clarify for anyone else who comes across this thread.

A featureSet, in the context of the JavaScript API and as delivered to the client, is a JSON object.

Let's look at this from another angle. How about we talk about this in terms of the REST API? If you want features from a layer in a map service, how do you get them? You use the query endpoint. If you use the endpoint via the JavaScript API's QueryTask (recommended) or through esri.request, you are getting back JSON (as long as you specify f=json when using esri.request).


in my code: function requestSucceeded(response, io) response is a string. u can use alert(response) while in function queryonComplete(featureSet) featureSet is an object..
0 Kudos
derekswingley1
Frequent Contributor
Are you sure response is a string? What does the rest of your esri.request() code look like?

In practice, especially when talking to ArcGIS Server endpoints, I don't think there are many (any?) times you'd want text over json. Why mess around with wrapping a response in parens and calling eval() when the server can return jsonp?
0 Kudos
HemingZhu
Occasional Contributor III
Are you sure response is a string? What does the rest of your esri.request() code look like?

In practice, especially when talking to ArcGIS Server endpoints, I don't think there are many (any?) times you'd want text over json. Why mess around with wrapping a response in parens and calling eval() when the server can return jsonp?


you can test it on my sample code to see if response is a string or not. The point here is that response is a lightweight string instead of an JSON Object. Because I specify url with f=json (or f=pjson), the returned response string is a json text. So you can safely use eval() to convert it in order to get extent. . You can also use other methods to fetch geometres from the response string if you like. Esri.request is efficient to request raw data(.txt, .xml), or, in this case, extent (even thought not exactly) from the server especially if you don't need Json Object to return.
0 Kudos
EricPaitz
Esri Contributor
Anaish, to answer your question...as far as I know there is no way to get ArcGIS Server to return the extent of a query...meaning just returning the xmin, ymin, xmax and ymax of the FeatureSet. Right now you have to call the REST Query Operation which will serialize all of the vertices that make-up the result geometry. In the case of polygons this can be a rather large response. Using Firebug I have seen the response be several megabytes in size. Once you have all of the geometries serialized down to the client you can then loop through each to get an extent, but this is exactly what you are trying to avoided. BTW there is a helper function in the Flex API that will give you and extent for a FeatureSet.

The best approach, until this is part of core, is to write your own ArcGIS Server Object Extension that mimics the REST Query Operation but only returns the extent of the results.

I have added an idea to the ideas page about this very issue.
http://ideas.arcgis.com/ideaView?id=0873000000088OQAAY
0 Kudos
derekswingley1
Frequent Contributor
OK, when specifying "handleAs: 'text'", and using a proxy, yes, the response is a string.

My point is that there is a clearer way to do this and it uses less code.

In this case, I'm not saying you shouldn't use eval() because of security concerns. Rather, I'm saying you shouldn't use eval() because you can just request json using esri.request() and you don't need to do your eval (dojo actually does it for you).

You're saying that the text response is lighter/smaller than the json response but I'm saying they're the same. If you do both and monitor the http traffic using firebug, chrome dev tools or a standalone piece of software like fiddler or charles, you'll see that the responses are the same size (because they are from the same url). The difference is what dojo does once it receives the response. Take a look at the dojo source for the various handlers you can use with xhrPost (esri.request() calls dojo.xhrPost() when you use a proxy):  http://bugs.dojotoolkit.org/browser/dojo/trunk/_base/xhr.js#L255

The text handler simply return responseText while the json handler calls dojo.fromJson which, it turns out, does exactly what you're doing in your callback.

So both of our methods end up with the same result but I think it's clearer to people reading your code (and maybe even you when you come back to this in a year or two) what's happening when you use handleAs: json.
0 Kudos
derekswingley1
Frequent Contributor
@Eric have you experimented with maxAllowableOffset to manage the size of a response sent to the client? The generalize sample shows it in action.
0 Kudos
EricPaitz
Esri Contributor
Hey Swingley, I did play around with the new FeatureLayer in the Flex API...which manages the call to the Query Operation and management of the response and display of the geometries. I did not use the maxAllowableOffset parameter. I will have to go back and see if this will help with our data. Thanks for the hint.

The root issue in this thread is that ArcGIS Server should be able to return the extent (i.e. return where the query results are) of the query rather then the results of the query. Sometimes you need all the result attributes (+ the geometries) and sometimes you just need the attributes and sometimes you only need the extent of the results.

Thanks,
   -eric
0 Kudos
derekswingley1
Frequent Contributor
@Eric I'm not too familiar with the Flex API but I took a quick look at the docs and maxAllowableOffset is there for both Feature Layers and QueryTasks (same as JavaScript).

I understand the underlying issue, hence the reason I suggested a server object extension earlier in this thread 😉
0 Kudos
EricPaitz
Esri Contributor
Oh yeah I totally agree with using an SOE that only returns the extent of the query. Go to the ideas page I linked above and vote for the idea! 🙂
0 Kudos