5

What would be the best way to take current extent and rotate it for 90 degrees around center point and it would work for any projection.

Let's say I have a map with n-layers enabled and landscape view. Meaning I can get extent of it. How can I get extent of a rotated view (90° rotation). Sketch of rotation: illustration of desired effect _

Center is fixed and should stay the same and aspect ration should be same as before.

  • Just use rotate function from ol.View & it should work (if I've understood your requirement) See related API doc openlayers.org/en/latest/apidoc/ol.View.html#rotate – ThomasG77 Jan 31 '18 at 23:15
  • My goal is not to rotate the view, but just to get rotated extent of rotated view. It could be done by rotating, getting the extent and rotating it back, but it seems very unpractical to me. – pc9529 Feb 1 '18 at 6:21
  • You could maybe create a GeoJSON feature for the current extent then use turf-transform-rotate to rotate it. – walkermatt Oct 14 '18 at 11:41
  • map.getView().calculateExtent(map.getSize().slice().reverse()) slice() is important otherwise you update the live map – Mike Dec 8 '18 at 0:04
2

This should do it:

  const get_rotated_extent = (map, degrees) => {
    const view = map.getView()
    const extent = view.calculateExtent(map.getSize())
    const geom = ol.geom.Polygon.fromExtent(extent)
    const map_center = view.getCenter()
    const rotation_in_radians = degrees * Math.PI / 180
    geom.rotate(rotation_in_radians, map_center) 
    return geom.getExtent()
  }
  const rotated_extent = get_rotated_extent(map, 90)

To test it visually:

  const polygon_source = new ol.source.Vector()
  const polygon_layer = new ol.layer.Vector({ source: polygon_source })
  map.addLayer(polygon_layer)
  const polygon = new ol.Feature(ol.geom.Polygon.fromExtent(rotated_extent))
  polygon_source.addFeature(polygon)

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.