Skip to content

Instantly share code, notes, and snippets.

@jorelosorio
Last active March 16, 2022 09:14
Show Gist options
  • Save jorelosorio/7042bd27e4b2bb03865215d6a5607266 to your computer and use it in GitHub Desktop.
Save jorelosorio/7042bd27e4b2bb03865215d6a5607266 to your computer and use it in GitHub Desktop.
A couple of functions to download tiles from OpenStreetMaps and concatenate them into one single image. NB! Not recommended for more than 5 in zoom level!
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
var zoom = 2
numberOfTiles := 1 << zoom
for j := 0; j < numberOfTiles; j++ {
for i := 0; i < numberOfTiles; i++ {
path := fmt.Sprintf("http://b.tile.openstreetmap.org/%d/%d/%d.png", zoom, i, j)
curl(path, fmt.Sprintf("./%d/%d_%d.png", zoom, i, j))
}
}
for i := 0; i < numberOfTiles; i++ {
concatColumns(zoom, i, numberOfTiles)
}
concatRows(zoom, numberOfTiles)
}
func curl(url, filepath string) error {
cmd := exec.Command("curl", url, "--output", filepath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func concatColumns(zoom int, index int, numberOfTiles int) error {
images := []string{"-mode", "concatenate", "-tile", "1x"}
for i := 0; i < numberOfTiles; i++ {
images = append(images, fmt.Sprintf("./%d/%d_%d.png", zoom, index, i))
}
images = append(images, fmt.Sprintf("./%d/out_%d.png", zoom, index))
cmd := exec.Command("montage", images...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func concatRows(zoom int, x int) error {
images := []string{"-mode", "concatenate", "-tile", "x1"}
for i := 0; i < x; i++ {
images = append(images, fmt.Sprintf("./%d/out_%d.png", zoom, i))
}
images = append(images, fmt.Sprintf("./zoom_%d.png", zoom))
cmd := exec.Command("montage", images...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
@jorelosorio
Copy link
Author

This tool requires to have ImageMagick to concatenate the images into one big single map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment