Difference in Spherical Mercator(google/mapbox/supercluster code) calculation and Postgis 3857

As per source code of supercluster, google map utils and mapbox formula to convert (lng, lat) to spherical mercator is as

x = lng/360 + 0.5;
y = 0.5 * Math.log((1 + siny) / (1 - siny)) / -(2 * Math.PI) + .5;

As per the code this is conversion of (lng, lat) to mercator

Result of above formula for spatial point (-122.43499755859375, 37.71860885620117) is {x=0.15990278455946183, y=0.3867186849275602}

If I use PostGIS to convert same point to projection 3857 result is

POINT(-13629401.5834983 4539750.5916923)

Why is there a difference? Leaflet clustering works well with above formula if I have to achieve similar result with PostGIS what kind of projection I must use? Or any more calculations required based on 3857 projection?

• I assumed (and edited) you meant 3857, not 3875 which is a projection for Finland only – JGH Sep 14 '17 at 12:44

The source code you link to seems to be calculating the values in pixels or 0-1 (in one of the comments) as a preparation to multiplying it up to pixels on an image. So the values you have calculated are the proportion of the width/height of the world from the south pole/anti meridian.

To get the Web Mercator coordinates in meters you need to do something like, where C is the circumference of the Earth.

x = C * 0.15990278455946183 - C/2
y = -C * 0.3867186849275602 + C/2

which for C = 40075000m gives

x = -13629395.9088
y = 4539748.70153

which is pretty close!

• Web Mercator uses r = 6378137m, so C is 40075016. – AndreJ Sep 14 '17 at 12:46
• I just used google and took the first answer! – Ian Turton Sep 14 '17 at 12:47
• @iant thanks, its clear now, but there is one more linked question, bringing in the clustering to PostGIS using web mercator. How do I convert zoom levels to web mercator distance so that I can pass details to ST_ClusterWithIn or ST_ClusterDBSCAN – Srinivas Sep 14 '17 at 13:22