Skip to content

Commit

Permalink
Support customizing map name (#3)
Browse files Browse the repository at this point in the history
* Add `-map` option flag to correspond to map layer name in renderd.conf

* Add `map_name` value to `data_dir` tile path

* Update `README.md`
  • Loading branch information
hummeltech authored May 31, 2022
1 parent 09d9b07 commit f3c0556
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ If you prefer to run the binary directly you have the following options:
Usage of /tmp/go-build2340306719/b001/exe/osm-tileserver:
-data string
Path to directory containing tiles (default "./data")
-map string
Name of map. This value is also used to determine the metatile subdirectory (default "ajt")
-port string
Listening port (default ":8080")
-renderd-timeout int
Expand All @@ -58,7 +60,7 @@ Usage of /tmp/go-build2340306719/b001/exe/osm-tileserver:
2. Follow the [breadcrumbs](https://github.com/Overv/openstreetmap-tile-server/issues/15) about how to pregenerate tiles for the desired area and zoom-level.
1. Get a shell in the container: `docker exec -it $CONTAINER_NAME bash`
2. Download a script that makes it easier to specify GPS coordinates: `curl -o render_list_geo.pl https://raw.githubusercontent.com/alx77/render_list_geo.pl/master/render_list_geo.pl`
3. Pregenerate the tiles to your liking: `perl ./render_list_geo.pl -m ajt -n 3 -z 6 -Z 16 -x 2.5 -X 6.5 -y 49.4 -Y 51.6`
3. Pregenerate the tiles to your liking: `perl ./render_list_geo.pl -m ajt -n 3 -z 6 -Z 16 -x 2.5 -X 6.5 -y 49.4 -Y 51.6`
3. Cheers, you now have tiles in the `$YOUR_TILE_FOLDER`.

## Performance
Expand Down Expand Up @@ -102,5 +104,5 @@ Percentage of the requests served within a certain time (ms)
100% 9 (longest request)
```

This benchmark doesn't access the disk, as the tile has obviously been cached in memory.
This benchmark doesn't access the disk, as the tile has obviously been cached in memory.
Anyways it should give you an indication of whether this is fast enough for your use-case.
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
* along with this program; If not, see http://www.gnu.org/licenses/.
*/

func findPath(baseDir string, z, x, y uint32) (metaPath string, offset uint32) {
func findPath(baseDir, mapName string, z, x, y uint32) (metaPath string, offset uint32) {
var mask uint32
var hash [5]byte

Expand All @@ -47,7 +47,7 @@ func findPath(baseDir string, z, x, y uint32) (metaPath string, offset uint32) {
x >>= 4
y >>= 4
}
metaPath = fmt.Sprintf("%s/%d/%d/%d/%d/%d/%d.meta", baseDir, z, hash[4], hash[3], hash[2], hash[1], hash[0])
metaPath = fmt.Sprintf("%s/%s/%d/%d/%d/%d/%d/%d.meta", baseDir, mapName, z, hash[4], hash[3], hash[2], hash[1], hash[0])
return
}

Expand Down Expand Up @@ -122,23 +122,23 @@ func parsePath(path string) (z, x, y uint32, err error) {
return
}

func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir *string, renderd_sock_path string, renderd_timeout time.Duration) {
func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir, map_name, renderd_sock_path string, renderd_timeout time.Duration) {
z, x, y, err := parsePath(req.URL.Path)
if err != nil {
resp.WriteHeader(http.StatusBadRequest)
resp.Write([]byte(err.Error()))
return
}
resp.Header().Add("Content-Type", "image/png")
metatile_path, metatile_offset := findPath(*data_dir, z, x, y)
metatile_path, metatile_offset := findPath(data_dir, map_name, z, x, y)
fileInfo, statErr := os.Stat(metatile_path)
if statErr != nil {
if errors.Is(statErr, os.ErrNotExist) {
if len(renderd_sock_path) == 0 {
resp.WriteHeader(http.StatusNotFound)
return
}
renderErr := requestRender(x, y, z, renderd_sock_path, renderd_timeout)
renderErr := requestRender(x, y, z, map_name, renderd_sock_path, renderd_timeout)
if renderErr != nil {
fmt.Printf("Could not generate tile for coordinates %d, %d, %d (x,y,z). '%s'\n", x, y, z, renderErr)
// Not returning as we are hoping and praying that rendering did nonetheless produce a file
Expand Down Expand Up @@ -169,6 +169,7 @@ func main() {
static_dir := flag.String("static", "./static/", "Path to static file directory")
renderd_sock_path := flag.String("socket", "/var/run/renderd/renderd.sock", "Path to renderd socket. Set to '' to disable rendering")
renderd_timeout := flag.Int("renderd-timeout", 60, "time in seconds to wait for renderd before returning an error to the client. Set negative to disable")
map_name := flag.String("map", "ajt", "Name of map. This value is also used to determine the metatile subdirectory")
var renderd_timeout_duration time.Duration = time.Duration(*renderd_timeout) * time.Second
flag.Parse()
http.HandleFunc("/tile/", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -177,7 +178,7 @@ func main() {
w.Write([]byte("Only GET requests allowed"))
return
}
handleRequest(w, r, data_dir, *renderd_sock_path, renderd_timeout_duration)
handleRequest(w, r, *data_dir, *map_name, *renderd_sock_path, renderd_timeout_duration)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(*static_dir)).ServeHTTP(w, r)
Expand Down
4 changes: 2 additions & 2 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Request struct {
Map [44]byte
}

func requestRender(x, y, z uint32, renderd_sock_path string, renderd_timeout time.Duration) error {
func requestRender(x, y, z uint32, map_name, renderd_sock_path string, renderd_timeout time.Duration) error {
c, err := net.Dial("unix", renderd_sock_path)
if err != nil {
return err
Expand All @@ -38,7 +38,7 @@ func requestRender(x, y, z uint32, renderd_sock_path string, renderd_timeout tim
Y: y,
Z: z,
}
copy(request.Map[:], []byte("ajt"))
copy(request.Map[:], []byte(map_name))
if err := binary.Write(c, binary.LittleEndian, request); err != nil {
return err
}
Expand Down

0 comments on commit f3c0556

Please sign in to comment.