Wednesday, 4 February 2015

How to set resolution according to a given scale in OpenLayers



This is a followup to this question.


What I know is:




  • given the papersize, I can calculate how many pixels for a given DPI fill a millimeter (dpi * 1/25.4) =pixelpermilimeter





  • the number of pixels needed to represent one meter could be calculated via getPointResolution()*proj.getMetersPerUnit() =pixeltomapmeter




  • the scale is given as 1000 * pixelpermilimeter * pixeltomapmeter




From the issue mentioned above, I learned, that with a given papersize and a given screensize, I could set the resolution to fit the paper as follows:


map.getView().setResolution(resolution / scaling);


where resolution is the map resolution and scaling as the quotient of one map dimension in relation to the paper dimension.


What I am looking for is a way to determine the correct mapsize / resolution / scaling such, that a given scale is printed on paper.


Say the scale is 1:10000, I need the center of the map printed such, that 1 cm represents 100m.


Does that make sense?


I think, I have the dots, but I am not able to connect them in the right way.



Answer



The OpenLayers example is scaling to fit a displayed map extent into a paper size. If you want a specific scale on paper the logic will be slightly different.


You need to set map size from the paper dimensions and dpi. For A4 portrait at 300dpi the paper pixels therefore the mapsize will be [ 210 * 300 / 25.4, 297 * 300 / 25.4 ]


OpenLayers resolutions are meters per pixel so if 10mm on paper represents 100m on the ground the resolution needed at 300 dpi is 100 / ( 10 * 300 / 25.4 )



But if you are using a projection such as web mercator where the scale changes with latitude you also need to adjust for the point resolution because 1 meter in map units isn't really one 1 meter on the ground


100 / getPointResolution(map.getView().getProjection(), 10 * 300 / 25.4, map.getView().getCenter())


No comments:

Post a Comment