+ Reply to Thread
Results 1 to 20 of 20

Thread: Get extent of polygons

  1. #1
    Andrew Naish
    Join Date
    Aug 2010
    Posts
    9
    Points
    1
    Answers Provided
    0


    0

    Default Get extent of polygons

    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?

  2. #2
    Shuping Li
    Join Date
    May 2010
    Posts
    61
    Points
    26
    Answers Provided
    5


    0

    Default Re: Get extent of polygons

    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/jav....htm#getExtent

  3. #3
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by Shuping View Post
    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/jav....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
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  4. #4
    Andrew Naish
    Join Date
    Aug 2010
    Posts
    9
    Points
    1
    Answers Provided
    0


    0

    Default Re: Get extent of polygons

    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).

  5. #5
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    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.
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  6. #6
    Heming Zhu

    Join Date
    May 2010
    Posts
    514
    Points
    47
    Answers Provided
    7


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by anaish View Post
    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/MapServer/1/query?where=STATE_NAME='Florida'&f=json";

    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);
    }

  7. #7
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    @hzhu that still brings all the geometries across the wire, which is what OP is trying to avoid.
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  8. #8
    Heming Zhu

    Join Date
    May 2010
    Posts
    514
    Points
    47
    Answers Provided
    7


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by swingley View Post
    @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.

  9. #9
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    @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.
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  10. #10
    Heming Zhu

    Join Date
    May 2010
    Posts
    514
    Points
    47
    Answers Provided
    7


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by swingley View Post
    @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...

  11. #11
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    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).
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  12. #12
    Heming Zhu

    Join Date
    May 2010
    Posts
    514
    Points
    47
    Answers Provided
    7


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by swingley View Post
    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..

  13. #13
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    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?
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  14. #14
    Heming Zhu

    Join Date
    May 2010
    Posts
    514
    Points
    47
    Answers Provided
    7


    0

    Default Re: Get extent of polygons

    Quote Originally Posted by swingley View Post
    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.

  15. #15
    Eric Paitz
    Join Date
    Nov 2009
    Posts
    41
    Points
    2
    Answers Provided
    0


    0

    Default Re: Get extent of polygons

    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

  16. #16
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    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/...se/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.
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  17. #17
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    @Eric have you experimented with maxAllowableOffset to manage the size of a response sent to the client? The generalize sample shows it in action.
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  18. #18
    Eric Paitz
    Join Date
    Nov 2009
    Posts
    41
    Points
    2
    Answers Provided
    0


    0

    Default Re: Get extent of polygons

    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

  19. #19
    Derek Swingley

    Join Date
    May 2010
    Posts
    1,859
    Points
    892
    Answers Provided
    165


    0

    Default Re: Get extent of polygons

    @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
    --
    @derekswingley | ArcGIS API for JavaScript Team Member.

    If you're posting a code or html sample, please wrap it in code tags.

  20. #20
    Eric Paitz
    Join Date
    Nov 2009
    Posts
    41
    Points
    2
    Answers Provided
    0


    0

    Default Re: Get extent of polygons

    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!

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts