Viglino / ol-ext

Cool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animations, canvas filters.
https://viglino.github.io/ol-ext/
Other
1.25k stars 463 forks source link

How to calculate the imageScale of GeoImage dynamically when you have center geo cordinates of image and its height and width in meters. #650

Closed raghavshivi94 closed 3 years ago

raghavshivi94 commented 3 years ago

I have got height and width of the image in meters and its center geo coordinates.

const source = new GeoImage({ url: src, projection: olProj.get('EPSG:3857'), imageCenter: olProj.fromLonLat(centerPoint), imageRotate: Number(45*Math.PI/180), imageScale: ? });

How can I dynamically calculate imageScale based on height and width of the image and center coordinates

Viglino commented 3 years ago

You have to get the pixel size at the center point for the map projection, then compute the scale:

// Pixel resolution 1px = res meter
var res = ol.proj.getPointResolution(map.getView().getProjection(), 1, imageCenter);
// Get image scale
var imageScale = widthInMeters / (widthInPixel * res)
raghavshivi94 commented 3 years ago

For this formula, how can I calculate 'widthInPixel'? And is the height of the image not required while calculating the scale?

Viglino commented 3 years ago

The widthInPixel is the number of pixel columns in your image... The imageScale argument is an ol/size~Size: an array [scaleX,scaleY]. You have to calculate the scaleY if not the same as scaleX using the number of pixel lines in your image and its height in meter (and the projection resolution at center)...

You can also try Map-georeferencer (https://github.com/Viglino/Map-georeferencer) to calculate the params (http://viglino.github.io/Map-georeferencer/)