cnmoro
2/19/2020 - 6:53 PM

## Calcular sub bounds a partir de bounds grande (TopLeft, BottomRight)

Calcular sub bounds a partir de bounds grande (TopLeft, BottomRight)

``````lat_top_left = 5.9657537
lon_top_left = -75.7617188

lat_btm_right =  -34.3797126
lon_btm_right =  -33.8378906

qtdd_split_ratio = 5

piece_lat_div = (lat_top_left - lat_btm_right) / qtdd_split_ratio
piece_lon_div = (lon_btm_right - lon_top_left) / qtdd_split_ratio

done = False
k = 0
num_increments = 0

def incrementar():
global k
global num_increments
if k % 2 == 0: # se for par, deve incrementar
num_increments += 1
k += 1
return True
else:
k += 1
return False

bounds_list = []
j = 0
counter = 0
for i in range(qtdd_split_ratio):
lon = lon_top_left + (i * piece_lon_div)
while num_increments < qtdd_split_ratio + 1:
lat = lat_top_left - (j * piece_lat_div)
lon_aux = lon + piece_lon_div * (counter % 2)
bounds_list.append(str(lon_aux) + ', ' + str(lat))
# print(str(lat) + ' ' + str(lon_aux))
if incrementar():
j += 1
counter += 1
bounds_list = bounds_list[:-1]
j = 0
counter = 0
num_increments = 0
k = 0

for item in bounds_list:
print(item)``````
``````import math
import http.client as httplib
import sys
import tempfile
from PIL import Image
from urllib.request import urlopen as requests
import urllib

if not len(sys.argv) > 6:
raise SystemExit("Usage: upper_lat upper_lon lower_lat lower_lon zoom output_file")

upper_lat = float(sys.argv[1])
upper_lon = float(sys.argv[2])
lower_lat = float(sys.argv[3])
lower_lon = float(sys.argv[4])
zoom = int(sys.argv[5])
output = sys.argv[6]
tmp_location = './tmp/'

def deg2num(lat_deg, lon_deg, zoom):
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)

def writetile(x, y, zoom):
url = 'http://localhost:8787/styles/klokantech-basic/{z}/{x}/{y}.png'
file_name = str(x) + '_' + str(y) + '.png'
url_dl = url.replace("{x}", str(x)).replace("{y}", str(y)).replace("{z}", str(zoom))
m_file = urllib.request.urlretrieve(url_dl, './tmp/' + file_name)
return file_name

top_left = deg2num(upper_lat, upper_lon, zoom)
bottom_right = deg2num(lower_lat, lower_lon, zoom)

height = (bottom_right[1] - top_left[1]) * 256
width = (bottom_right[0] - top_left[0]) * 256
print(width, height)
osm_map = Image.new("RGBA", (width, height))

for x in range(top_left[0], bottom_right[0]):
for y in range(top_left[1], bottom_right[1]):
foo = Image.open(tmp_location + writetile(x, y, zoom))
osm_map.paste(foo, (256 * (x - top_left[0]), 256 * (y - top_left[1])))
writetile(x, y, zoom)

print('Saving map to ./' + output)
osm_map.save('./' + output)``````