1

I'm trying to draw a rectangle polygon on the map having a defined size in meters.

Background is: I want to show the user a rectangle of a defined paper size (e.g. DIN A4) on the map to choose a region to print for a specific scale (e.g. 1:25000).

My math to calculate the size in meters that would be visible on the paper is:

widthInMeters = paperWidthInCm * (scale/100) 

Given the width of A4 landscape (29.7cm) I'd get 7425 meters (29.7 * (25000/100)). Height would be 7425 * 0.7070 = 5249m

Now I want to draw a rectangle of this size (7425m x 5249m) on the map.

My first try was to just set the extent accordingly because the coordinates units (Web Mercator EPSG:3857) are in meters. But that, I think, would only work directly on the equator. Further up north or south this does not work at all because of projection distortion.

Given a defined width and height in meters and a center (or top left) coordinate how can I create an appropriate geometry covering that area?

2

1 Answer 1

1

Ok, I think I figured it out. My question is actually quite simpler than the referenced links, because it's just coordinate calculation and does not have anything to do with zoom levels and pixels. The "missing key" was to set the "resolution" parameter of getPointResolution() to 1.

// input
let centerCoordinate = this.map.getView().getCenter();
let paperWidth = 29.7; // A4 width in cm
let paperHeight = 21;  // A4 height in cm
let scale = 25000;     // 1 cm on map = 25000 cm = 250m in real

// calculation
let widthInMeters = paperWidth *  (scale/100);
let heightInMeters = widthInMeters * (paperHeight / paperWidth);

let pointRes = getPointResolution(this.map.getView().getProjection(), 1, centerCoordinate);
let widthInUnits = widthInMeters / pointRes;
let heightInUnits = heightInMeters / pointRes;

// coordinates of covered region
let mapExtent = [
    centerCoordinate[0] - widthInUnits / 2,
    centerCoordinate[1] - heightInUnits / 2,
    centerCoordinate[0] + widthInUnits / 2,
    centerCoordinate[1] + heightInUnits / 2
];

// and... the geometry
region = fromExtent(mapExtent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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