-1

I would like to connect google satellite map with borders in shp, in order to crop everything outside the city. I have shp file of Wroclaw city borders and code that downloads the satellite map from google maps. The middle of the map is known (lat = 51.1078852, lon = 17.0385376), so maybe it is possible to somehow connect the two?

I found numerous ways to do that using other software like:

But not Python. Here is a code that downloads Wroclaw satellite map (after chenging the API key):

import requests
import shutil

#API
with open('apikey.txt') as f:
    apikey = f.readline()
    f.close 


lat = 51.1078852
lon = 17.0385376    
zoom = 14
size_tile_x, size_tile_y = 1024, 1024
scale = 2
format_image='png'
maptype='satellite'
style=''
url_s = f"https://maps.googleapis.com/maps/api/staticmap?key={apikey}&scale={scale}&center={lat},{lon}&zoom={zoom}&format={format_image}&maptype={maptype}{style}&size={size_tile_x}x{size_tile_y}"

r = requests.get(url_s, stream=True, headers={'User-agent': 'Mozilla/5.0'})
name = 'Map.png'
if r.status_code == 200:
    with open(name, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)
AAAA
  • 461
  • 6
  • 22

1 Answers1

0

If you can download the map as a geo-referenced raster (e.g. a GeoTIFF) you could use GDAL/OGR. I don't think PNG files are typically geo-referenced (I've never seen a geo-referenced PNG) so you might have to first geo-reference the PNG and save it as something other than PNG, like GeoTIFF.

If you can somehow get the file geo-referenced then here is a tutorial from the Python GDAL Cookbook on clipping a raster using a shapefile.

cactus4
  • 128
  • 9