Saturday, November 7, 2020

Screenshots of Google maps at different zoom levels

 The scenario here is that we have latitude and longitude coordinates of cities that we want their Google maps screenshots at different zoom levels.

As at the time of writing, the construct for google maps url is like this: https://www.google.ng/maps/@latitude,longitude,zoomz >>>> https://www.google.ng/maps/@9.057809,7.4903376,15z

So, at that city coordinate we want to take the screenshots at varying zooms from 6 to 20. The url construct will be:-

  • https://www.google.ng/maps/@9.057809,7.4903376,6z
  • https://www.google.ng/maps/@9.057809,7.4903376,7z
  • https://www.google.ng/maps/@9.057809,7.4903376,8z
  • https://www.google.ng/maps/@9.057809,7.4903376,9z
  • https://www.google.ng/maps/@9.057809,7.4903376,10z
  • https://www.google.ng/maps/@9.057809,7.4903376,11z
  • https://www.google.ng/maps/@9.057809,7.4903376,12z
  • https://www.google.ng/maps/@9.057809,7.4903376,13z
  • https://www.google.ng/maps/@9.057809,7.4903376,14z
  • https://www.google.ng/maps/@9.057809,7.4903376,15z
  • https://www.google.ng/maps/@9.057809,7.4903376,16z
  • https://www.google.ng/maps/@9.057809,7.4903376,17z
  • https://www.google.ng/maps/@9.057809,7.4903376,18z
  • https://www.google.ng/maps/@9.057809,7.4903376,19z
  • https://www.google.ng/maps/@9.057809,7.4903376,20z


As you would have noticed from the url above, only the zoom level changes. The city coordinate remains the same for the fifteen zoom levels (6z - 20z).

Lets write a python script that will handle this for us.


1) Generate the URLs

base_url = 'https://www.google.ng/maps/@9.057809,7.4903376,{}z'

for x in range(6, 21):
    print(base_url.format(str(x)))


2) Take screenshots

Here I used selenium module to open and take screenshot of each map url. I also used the time module to make two second delays to allow the map load completely before taking the screenshots.

Also note that I saved the urls in a list called: url_list

import time
from selenium import webdriver


# Load chrome browser driver
chrome_driver = 'C:\\Users\\Yusuf_08039508010\\Documents\\chromedriver.exe'
driver = webdriver.Chrome(chrome_driver)


for url in url_list:
    driver.get(url)
    time.sleep(2)
    
    img_name = url.split(',')[-1]
    driver.save_screenshot(img_name + ".png")
    
    print('Saving image...', img_name)

That is it!

No comments:

Post a Comment