0

I'm working on a project that hopes to convert Google maps static images into larger, stitched together maps.

I have created an algorithm that given a starting and ending, latitude and longitude, it will fetch the Google Maps Static image for that lat,lng pair, then increment lng by the pixel width of the fetched image, in this case 700px, using an algorithm for determining pixel to longitude ratio using a couple of formulas.

The formulas seem to be working perfectly for latitude, as you can see in my attachment, the vertically tiling images line up almost perfectly.

Yet horizontally, the longitude increment seems to be off by a factor of 2, or slightly more.

To increment latitude, I use a constant metres to latitude ratio

const metresToLatRatio = 0.001 / 111 // Get lat value from metres
const metresPerPxLat = getMetresPerPxLng(currentLat, zoom)
const latIncrement = metresToLatRatio * (metresPerPxLat * 700)

But for longitude, I replaces the metresToLngRatio constant with a variable derived from this formula

const metresToLngRatio = getMetresToLngRatio(currentLat)
const metresPerPxLng = getMetresPerPxLng(currentLat, zoom)
lngIncrement = (metresToLngRatio * (metresPerPxLng * 700))

Where getMetresToLngRatio and getMetresPerPxLng are

function getMetresPerPxLng(lat, zoom = 19, scale = 2) {
    return Math.abs((156543.03392 * Math.cos(lat * Math.PI / 180) / (2 ** zoom)) / scale)
}

function getMetresToLngRatio(lat) {
    return 1 / Math.abs(111111 * Math.cos(lat))
}

The getMetresPerPxLng function is derived from this post and this answer: https://groups.google.com/g/google-maps-js-api-v3/c/hDRO4oHVSeM / https://gis.stackexchange.com/questions/7430/what-ratio-scales-do-google-maps-zoom-levels-correspond-to

What I noticed is that if I change getMetresToLng Ratio to return (1 / Math.abs(111111 * Math.cos(lat))) * 2, the stitching appears more accurate, only off by a few tens of pixels, instead of almost half the image.

With * 2 With times 2

Without * 2 Without times 2

Am I doing something wrong with my longitude equation? I know that 111111*cos(lat) is a rough estimate, so I'm wondering if there's a more accurate formula

lopu
  • 175
  • 1
  • 18

1 Answers1

0

You are using the wrong earth radius. From the answer in https://gis.stackexchange.com/questions/7430/what-ratio-scales-do-google-maps-zoom-levels-correspond-to, you need to update the equation using a radius of 6371010 instead of 6378137 meters.

Please note that stitching tiles together for some other use is likely a violation of the Terms of Service.

Justin Poehnelt
  • 2,992
  • 1
  • 19
  • 23
  • Sorry to sound dumb but where am I using an earth radius amount such as 6378137? Is that related to the 156543.03392 number? Kindly :) And yes I know it's likely against TOS, technically I'm not storing the maps for later use, just going to be analyzing them, and then storing some information, but I guess I'm storing data for later use so ¯\_(ツ)_/¯ – lopu Jan 28 '22 at 23:03