1

I have a geojson with some data which I loaded into Openlayers. When I try to get the getExtent of the data source I get awkward values not corresponding to the extent of the data. The following image shows the dataset in blue and the extent points, calculated with the gjsonLayer.getSource().getExtent() in red. enter image description here When I click in the polygons in the Chatham islands (on the right side of the image) I get the coordinates [183.49365234375, -43.905808356157394], out of the range -180, 180. Probably that's the reason why the method getExtent() returns messed up values.

Just to show that the problem is the data crossing the dateline, below is an image showing the dataset without those polygons on the right (in the Chatham islands) that pass the dateline. The red points are the result of the getExtent() method and they are correct. enter image description here

Any idea how to make the getExtent() method return correct values when using a dataset in which some data passes the dateline?

  • 1
    If I use a geojson with coordinates in the non-standard format 183.4 (i.e. wrapped across the date line) the extent reflects that. But if your geojson contains normalised coordinates e.g. -176.5 the extent being returned by OpenLayers is correct - but features will be repeated each time the map wraps the date line so see a copy of the Chatham Islands outside that extent. To calculate the smallest (wrapped) extent you would need to either update the geojson or run a transform on all the feature coordinates (to change the range to 0 to 360) in OpenLayers. – Mike May 15 at 10:37
  • The longitudes in the geojson are all between -180 and 180. – Falcoa May 15 at 21:55
  • The longitudes in the geojson are all between -180 and 180. The thing is that the coordinates in Chatham are -176.5 and those in NZ south island are 169.5 (for example). So, when calculating the extent, I get awkward values. @Mike, do you think that converting all coordinates to fit the range 0-360 will fix the problem? – Falcoa May 15 at 22:07
  • Yes, I set a a geojson with a polygon either side of the data line and got a similar result to you, but running the transform below in OpenLayers gives the result you were expecting without having to edit the geojson. – Mike May 15 at 22:12
1

Example of transforming from +/- to positive only x coordinates

var wrapWidth = ol.extent.getWidth(map.getView().getProjection().getExtent());
function wrapTransform(input, opt_output, opt_dimension) {
  var length = input.length;
  var dimension = opt_dimension !== undefined ? opt_dimension : 2;
  var output = opt_output !== undefined ? opt_output : new Array(length);
  var i, j;
  for (i = 0; i < length; i += dimension) {
    output[i] = input[i] < 0 ? input[i] + wrapWidth : input[i];
    for (j = dimension - 1; j >= 1; --j) {
      output[i + j] = input[i + j];
    }
  }
  return output;
}

gjsonLayer.getSource().forEachFeature(function(feature){
  feature.getGeometry().applyTransform(wrapTransform);
});

console.log(gjsonLayer.getSource().getExtent());
  • The width of the projection extent, so it will work regardless of whether the map is in EPSG:4326 or EPSG:3857. It's set in the top line to avoid having to recalculate on each call. – Mike May 15 at 22:17
  • Yes yes, sorry, my bad. The wrapWidth is right there at the top, I didn't see it, sorry. . I will try this function. – Falcoa May 15 at 22:19
  • Awesome!! You saved my day :) Thank you so much. – Falcoa May 15 at 22:26

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.