ThinkGeo.com    |     Documentation    |     Premium Support

Calculating Scale Using Degrees

I am using leaflet.js - which doesn’t have a setting like WpfMap.CurrentScale for example.
Unfortunatly in the web server we have some code that requires “Scale” as input (MapSuiteTileMatrix).
The question is how to calculate that scale.

In the JS client I managed to calculate a scale as meter per pixel - but the map data is in degrees.

So I would like to understand how scale is used in combination with degrees (e.g. in WPF edition), so that I can implement a formula.

I also have seen the comment on ZoomLevel.Scale:

/// The scale is the ratio of a single unit of distance on the map to the equivalent
/// distance on the ground. For example, a scale of 1:1 means that one foot on the ground
/// would be displayed as one foot on the screen. A scale of 1:1,000 means that one foot on
/// the screen is equal to 1,000 feet on the ground.

I made up a sample map that shows approx. 1 km (in WPF edition). CurrentScale is ~ 4500.
Following the formula 1 degree on the screen would be 4500 degree on the ground.
But then I still need to calculate how much degree 1px is. Probably 0.000…1 something.
How to get there?

To use the projection I need Lng and Lat values. If I use Lat=0 it might work, but would this be accurate?

Hi Andreas,

We use the following formula to calculate the scale:

screenDpi = 96;
inchPerDecimalDegree = 4374754;
Scale = worldExtent.Width * inchPerDecimalDegree * screenDpi / screenWidth;

You can refer the following test code to get scale by the JavaScript in client side:

var screenDpi = 96;
var inchPerDecimalDegree = 4374754;
var mapExtent = map.getBounds();
var worldWidth = mapExtent._northEast.lng - mapExtent._southWest.lng;
var screenWidth = map.getSize().x;
var scale = worldWidth * inchPerDecimalDegree * screenDpi / screenWidth;

The scale value is different in different latitude. It calculates the scale width hard code latitude, which isn’t accurate.

If you have any questions, please feel free to let us know.

Thanks,

The solution above works. The calculated scale is different than in the ZoomLevelSet on the web server (about 1%), from my observation the standard leaflet.js zoom factors are either equal or pretty close to SphericalMercatorZoomLevelSet.
One can probably just use (leaflet) zoom + 1 and use that as the ThinkGeo zoom level.

For our problem both the equation above or the alternative zoom + 1 are accurate enough.

1 Like

Hi Andreas,

Thanks for your information.

The different maps use different zoom levels.

On Leaflet the default CRS(Coordinate Reference System) is EPSG 3857, it’s Spherical Mercator projection. Please refer http://leafletjs.com/reference.html#map-crs for detail.

So the zoom factors of leaflet are matched our SphericalMercatorZoomLevelSet. You should want to use SphericalMercatorZoomLevelSet on the web server instead of ZoomLevelSet .

You can get zoom level number at client end by JavaScript, then pass that back to server side and get the accurate scale by this level number in SphericalMercatorZoomLevelSet.

Note: ZoomLevel starts form 0 on Leaflet, but starts form 1 for SphericalMercatorZoomLevelSet.

Help it’s helpful. If you have any questions, please feel free to let us know.

Thanks,