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.
ol.source.ImageStatic
? See example openlayers.org/en/latest/examples/reprojection-image.html