-
-
Save IgorShadurin/afdc91d2f21cc8154e24da02d1805813 to your computer and use it in GitHub Desktop.
Small script to download OSM GeoJSON tiles
This file contains 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/python | |
from sys import argv | |
import os | |
import math | |
import urllib | |
import random | |
import os.path | |
import urllib.request | |
def deg2num(lat_deg, lon_deg, zoom): | |
lat_rad = math.radians(lat_deg) | |
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 download_url(zoom, xtile, ytile): | |
# Switch between otile1 - otile4 | |
subdomain = random.randint(1, 4) | |
#url = "http://c.tile.openstreetmap.org/%d/%d/%d.png?api_key=NaqqS33fTUmyQcvbuIUCKA" % (zoom, xtile, ytile) | |
url = "https://tile.nextzen.org/tilezen/vector/v1/512/all/%d/%d/%d.json?api_key=YOUR_ACCESS_TOKEN" % (zoom, xtile, ytile) | |
dir_path = "tiles/%d/%d/" % (zoom, xtile) | |
download_path = "tiles/%d/%d/%d.json" % (zoom, xtile, ytile) | |
if not os.path.exists(dir_path): | |
os.makedirs(dir_path) | |
if(not os.path.isfile(download_path)): | |
print("downloading %r" % url) | |
req = urllib.request.Request(url) | |
req.add_header('Referer', '') | |
req.add_header('origin', 'http://localhost:8000') | |
req.add_header('accept', '*/*') | |
req.add_header('accept-language', 'en-US,en;q=0.9,ru-RU;q=0.8,ru;q=0.7') | |
req.add_header('sec-fetch-dest', 'empty') | |
req.add_header('sec-fetch-mode', 'cors') | |
req.add_header('sec-fetch-site', 'cross-site') | |
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36') | |
source = urllib.request.urlopen(req) | |
content = source.read() | |
source.close() | |
destination = open(download_path,'wb') | |
destination.write(content) | |
destination.close() | |
else: print("skipped %r" % url) | |
def usage(): | |
print("Usage: ") | |
print("osm_tiles_downloader <lat> <lon>") | |
def main(argv): | |
try: | |
script, lat, lon = argv | |
except: | |
usage() | |
exit(2) | |
maxzoom = 15 # redefine me if need so | |
# from 0 to 6 download all | |
for zoom in range(0,7,1): | |
for x in range(0,2**zoom,1): | |
for y in range(0,2**zoom,1): | |
download_url(zoom, x, y) | |
# from 6 to 15 ranges | |
for zoom in range(7, int(maxzoom)+1, 1): | |
xtile, ytile = deg2num(float(lat)-0.1, float(lon)-0.05, zoom) | |
final_xtile, final_ytile = deg2num(float(lat)+0.1, float(lon)+0.05, zoom) | |
print("%d:%d-%d/%d-%d" % (zoom, xtile, final_xtile, ytile, final_ytile)) | |
for x in range(xtile, final_xtile + 1, 1): | |
for y in range(ytile, final_ytile - 1, -1): | |
result = download_url(zoom, x, y) | |
main(argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oke lăm