Created
September 28, 2018 04:24
-
-
Save filippov70/53bbd21cfe68f401f25939ceda6ef8ee to your computer and use it in GitHub Desktop.
Grab TMS tile script like XYZ (OSM)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Generated by BigMap 2. Permalink: http://bigmap.osmz.ru/bigmap.php?xmin=192934&xmax=192939&ymin=81038&ymax=81042&zoom=18&scale=256&tiles=sputnik | |
# для получения координат тайлов используется сервис http://bigmap.osmz.ru/bigmap.php | |
# для перевода координат тайлов из формата OSM в TMS используется функция getTMSY | |
# полученые из сервиса коодинаты нужно вставит в строку 17 | |
# Важно! Y-овые координаты в строек 17 нудно поменять местами - меншее число в ymax м наоборот | |
import io, urllib2, datetime, time, re, random | |
from PIL import Image, ImageDraw | |
# ^^^^^^ install "python-pillow" package | pip install Pillow | easy_install Pillow | |
def getTMSY(xyz_y, z): | |
print str((2 ** z) - xyz_y - 1), "DEBUG" | |
return (2 ** z) - xyz_y - 1 | |
(zoom, xmin, ymin, xmax, ymax) = (18, 193008, getTMSY(80959, 18), 193039, getTMSY(80936, 18)) # Irkutsky | |
layers = ["https://map.admtomsk.ru/tiles_all/tiles_orto2018_qg_all/!z/!x/!y.png"] | |
attribution = 'Map data (c)' | |
xsize = xmax - xmin + 1 | |
ysize = ymax - ymin + 1 | |
resultImage = Image.new("RGBA", (xsize * 256, ysize * 256), (0,0,0,0)) | |
counter = 0 | |
for x in range(xmin, xmax+1): | |
for y in range(ymin, ymax+1): | |
for layer in layers: | |
url = layer.replace("!x", str(x)).replace("!y", str(y)).replace("!z", str(zoom)) | |
match = re.search("{([a-z0-9]+)}", url) | |
if match: | |
url = url.replace(match.group(0), random.choice(match.group(1))) | |
print url, "... done"; | |
try: | |
req = urllib2.Request(url, headers={'User-Agent': 'BigMap/2.0'}) | |
tile = urllib2.urlopen(req).read() | |
except Exception, e: | |
print "Error", e | |
continue; | |
image = Image.open(io.BytesIO(tile)) | |
resultImage.paste(image, ((x-xmin)*256, (ymax+1-y)*256), image.convert("RGBA")) | |
counter += 1 | |
if counter == 10: | |
time.sleep(2); | |
counter = 0 | |
draw = ImageDraw.Draw(resultImage) | |
draw.text((5, ysize*256-15), attribution, (0,0,0)) | |
del draw | |
now = datetime.datetime.now() | |
outputFileName = "map%02d-%02d%02d%02d-%02d%02d.png" % (zoom, now.year % 100, now.month, now.day, now.hour, now.minute) | |
resultImage.save(outputFileName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment