0

Is there a function to transform a coordinates length to a pixel length?

I need this function for cropping and scaling map images to be inserted as layers in an OpenLayers Map.

The map images are already adapted to an OSM map and therefore have an "extent" BBOX.

This feature already exists when using ol.source.ImageWMS and a WMS software. The WMS software is too cumbersome for me, also because there is a database needed. Unfortunately the values that are calculated are not retrievable.

I can already calculate the pixel width and height from the extent coordinates, using rule of three. More specific (with rotation) the calculation is also possible with the Pythagorean theorem. For this I only need a default, like for example: 20000 lon = 5000 pixels. But the problem is the projection. For each projection such a default value is needed. Or each projection must be transformed into a default projection. It would be simpler, if it would be transformed into pixels.

Example:

// var extent = [minX,minY,maxX,maxY];
var extent = [1002345.9874,5002345.9875,1102345.9876,6002345.9877];
ol.proj.transformExtent(extent, EPSG:3857, Pixel)
// result: [minX,minY,maxX,maxY]
// result: [50,60,150,120]

With this and together with the function map.getView().calculateExtent(map.getSize()) it can then be calculated whether the extent of the image layer is outside the map viewbox. With this outside pixel length then cropping can be done.

The problem is the zoom. At each zoom level the ratio of coordinate length to pixel length is different.

There is the function ol.source.ImageStatic, but with this the image is not scaled or cropped. With high resolution map images this overloads the browser.

I have already found a somewhat cumbersome solution and am looking for a simpler one like in the example.

12
  • Pixel dimensions in Web Mercator are pretty much useless, as are ground units in 3857. You probably need to re-think this task in terms of meaningful units.
    – Vince
    Commented May 15, 2022 at 20:20
  • What's wrong with ol.source.ImageStatic? See example openlayers.org/en/latest/examples/reprojection-image.html
    – TomazicM
    Commented May 16, 2022 at 9:22
  • @Vince, please visit ol.source.ImageWMS. No Tile or Image Layer works without pixels.
    – Suka
    Commented May 16, 2022 at 19:54
  • @TomazicM, ol.source.ImageStatic → "With high resolution map images this overloads the browser." The Images have a size from 1-10 MB, and i set no limit how much images the user can loaded in the map, by more than 2000 Images. 5x2000=10000MB in the browser. Or 5*100=500MB. The example from OL have only 500KB.
    – Suka
    Commented May 16, 2022 at 19:59
  • And how do you intend to solve this problem with cropping and scaling? Please edit your question with your last comment and then explain you idea of solving the problem.
    – TomazicM
    Commented May 16, 2022 at 20:20

0

Your Answer

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