According to the API:

ol.extent.containsExtent(extent1, extent2) 

checks to see whether extent1 is contained by extent2.

With the following variables:

extent1 = [-93, 44, -94, 45];

extent2 = [-95, 43, -92, 46];

ol.extent.containsExtent(extent1, extent2) returns false, though extent1 is clearly within extent2. What am I doing wrong?


tl;dr; You just need to switch the order of your parameters.

There are two issues here:

  1. Your extents are invalid
  2. The API documentation is wrong

(In retrospect the order of your extents does not affect the outcome, but it is important to have standards..)

Further to point 1 above, extent1 contains the lower right, and upper left coordinates respectively. Whereas extent2 contains the lower left and upper right coordinates.

The proper format for extents is [minx, miny, maxx, maxy]. (API)

var extent1 = [-94, 44, -93, 45];
var extent2 = [-95, 43, -92, 46]; 
ol.extent.containsExtent(extent1, extent2); // returns false still?

Looking at the ol.extent.containsExtent function on GitHub, we see the implementation as:

 return extent1[0] <= extent2[0] && extent2[2] <= extent1[2] &&
   extent1[1] <= extent2[1] && extent2[3] <= extent1[3];

Initially, we can see that the first condition is not satisfied for your example:

/* -94    <=   -95   (false) */
extent[0] <= extent2[0]

This function will simply not work for coordinates in the negative hemisphere. But, looking further into the code we see that the latest version has updated the API documentation: (via). So clearly, the documentation is wrong - not the code.

 * Check if one extent contains another.
 * An extent is deemed contained if it lies completely within the other extent,
 * including if they share one or more edges.
 * @param {ol.Extent} extent1 Extent 1.
 * @param {ol.Extent} extent2 Extent 2.
 * @return {boolean} The second extent is contained by or on the edge of the
 *     first.
 * @api stable

So, if you execute ol.extent.containsExtent(extent2, extent1) it will give you a valid response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.