Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created May 20, 2023 17:00
Show Gist options
  • Save roman01la/35171dfcf2b64983486dcb1c2f844be9 to your computer and use it in GitHub Desktop.
Save roman01la/35171dfcf2b64983486dcb1c2f844be9 to your computer and use it in GitHub Desktop.
Export large scale map from OSM
  1. Go to https://www.openstreetmap.org/export#map=11/48.6129/38.2221
  2. Choose your area for export
  3. Put export bounding box longitude and latitude into calc_tiles.py and run it to calculate which tiles to has to be downloaded
  4. Create tiles directory
  5. Put calculated coordinates into download_tiles.js and run it do download tiles (couldn't make downloading work in Python)
  6. Put calculated coordinates into stitch_tiles.py and run it
  7. The output will be stored in map.png file
import math
zoom = 14
lon1 = 48.7752
lon2 = 48.4675
lat1 = 37.8500
lat2 = 38.5984
def lat_to_x(lat, zoom):
return int((lat + 180.0) / 360.0 * (1 << zoom))
def lon_to_y(lon, zoom):
return int((1.0 - math.log(math.tan(math.radians(lon)) + 1 /
math.cos(math.radians(lon))) / math.pi) / 2.0 * (1 << zoom))
x1 = lat_to_x(lat1, zoom)
x2 = lat_to_x(lat2, zoom)
y1 = lon_to_y(lon1, zoom)
y2 = lon_to_y(lon2, zoom)
print(x1, x2, y1, y2)
const fs = require("fs");
const axios = require("axios");
const coords = [];
for (let idx = x1; idx <= x2; idx++) {
for (let jdx = y1; jdx <= y2; jdx++) {
coords.push([idx, jdx]);
}
}
urls.map(([idx, jdx], iidx) =>
axios(`https://tile.openstreetmap.org/14/${idx}/${jdx}.png`, {
responseType: "arraybuffer",
}).then((response) => {
console.log(iidx, urls.length);
const buff = Buffer.from(response.data, "binary");
fs.writeFileSync(`tiles/${idx}_${jdx}.png`, buff);
})
);
from PIL import Image
x1 = 0
x2 = 0
y1 = 0
y2 = 0
width = (x2 - x1 + 1) * 256
height = (y2 - y1 + 1) * 256
result_image = Image.new('RGB', (width, height))
for x in range(x1, x2 + 1):
for y in range(y1, y2 + 1):
tile_image = Image.open(f"tiles/{x}_{y}.png")
result_image.paste(tile_image, ((x - x1) * 256, (y - y1) * 256))
result_image.save("map.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment