def gmap_image(fieldminlat,fieldmaxlat,fieldminlong,fieldmaxlong,tileminlat,tilemaxlat,tileminlong,tilemaxlong,fieldid,uid,fieldarea): import urllib.parse import urllib.request from io import StringIO from PIL import Image from math import log, exp, tan, atan, pi, ceil from latlontopixels import latlontopixels from pixelstolatlon import pixelstolatlon import io import math from getmapstaticcoords import getmapstaticcoords from urllib.request import Request zoom = 17 # be careful not to get too many images! ############################################ # Set some important parameters scale = 1 maxsize = 640 # calculate pixel dimensions of each small image bottom = 120 upperleft = str(fieldminlat)+','+str(fieldmaxlong) lowerright= str(fieldmaxlat)+','+str(fieldminlong) print(upperleft) print(lowerright) centerlat = (fieldminlat+fieldmaxlat)/2 centerlong = (fieldminlong+fieldmaxlong)/2 position = str(centerlat)+','+str(centerlong) pixelarea = abs(int(fieldarea)/100) print('pixel area') print(pixelarea) if pixelarea <300: zoom = 16 if pixelarea >300: zoom = 15 if pixelarea > 1200: zoom = 15 if pixelarea >5000: zoom = 14 if pixelarea > 40000: zoom = 13 latlen,longlen = getmapstaticcoords(centerlat,centerlong,zoom) pixellatlen =round(69172*latlen/10) pixellonglen = round(math.cos(centerlat*0.0174533)*69172*longlen/10) if pixellatlen > pixellonglen: width = pixellatlen height = width else: width = pixellonglen height = width if width > 1000: new_width = 1000 new_height = new_width*height/width width = new_width height = new_height print('width'+str(width)+'height'+str(height)) api_key = "AIzaSyBO5HDklsIeFNmKMGDLoImadHH57eQ4c2k" api_key = "AIzaSyDnM_35WfYwhJmTSPxyyiMIcYte65mPitc" urlparams = urllib.parse.urlencode({'center': position, 'zoom': str(zoom), 'size': '600x600', 'maptype': 'satellite', 'sensor': 'false', 'scale': scale}) url = Request('https://maps.googleapis.com/maps/api/staticmap?' + urlparams+'&key=' + api_key, headers={'User-Agent': 'Mozilla/5.0'}) print(url) f=urllib.request.urlopen(url) image_file = io.BytesIO(f.read()) im=Image.open(image_file) width1,height1 = im.size #new_height = height1-25 #width1 = width1-25 #im =im.crop((0,0,width1,new_height)) im = im.resize((width,height)) img_name = uid+"/"+str(fieldid) +'/_static_map.png' im.save(img_name,"PNG") #final.paste(im, (int(x*largura), int(y*altura))) return latlen, longlen, centerlat, centerlong