maps

package
v5.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-http-maps

Go package providing opinionated HTTP middleware for web-based map tiles.

Motivation

The original motivation for the go-http-maps package was to define a handful of top-level methods and net/http middleware handlers to manage the drudegry of setting up maps, with a variety of map providers (Leaflet, Protomaps, Nextzen/Tangramjs), It "worked" but, in the end, it was also not "easy".

Version 2 removes most of the functionality of the go-http-maps package and instead focuses on a handful of methods for providing a dynamic map "config" file (exposed as an HTTP endpoint) which can be retrieved and processed from the browser.

This package no longer provides static asset handlers for Leaflet or Protomaps. It is left up to you to bundle (and serve) them from your own code. You could, if you wanted, define a custom http.Handler instance to load those files from the github.com/aaronland/go-http/v5/maps//static/www.FS embedded filesystem but that's still something you'll need to do on your own.

Documentation

godoc is still incomplete at this time.

Usage

The easiest usage example is the cmd/example tool:

package main

import (
	"flag"
	"fmt"
	"log"
	"log/slog"
	"net/http"

	"github.com/aaronland/go-http/v5/maps
	"github.com/aaronland/go-http/v5/maps/static/www"
	"github.com/sfomuseum/go-flags/multi"	
)

func main() {

	var verbose bool
	var host string
	var port int

	var initial_view string
	var map_provider string
	var map_tile_uri string
	var protomaps_theme string
	var protomaps_max_data_zoom int	
	var leaflet_style string
	var leaflet_point_style string

	flag.StringVar(&host, "host", "localhost", "The host to listen for requests on")
	flag.IntVar(&port, "port", 8080, "The port number to listen for requests on")
	flag.BoolVar(&verbose, "verbose", false, "Enable verbose (debug) logging.")

	flag.StringVar(&map_provider, "map-provider", "leaflet", "Valid options are: leaflet, protomaps")
	flag.StringVar(&map_tile_uri, "map-tile-uri", maps.LEAFLET_OSM_TILE_URL, "A valid Leaflet tile layer URI. See documentation for special-case (interpolated tile) URIs.")
	flag.StringVar(&protomaps_theme, "protomaps-theme", "white", "A valid Protomaps theme label.")
	flag.IntVar(&protomaps_max_data_zoom, "protomaps-max-data-zoom", 0, "The maximum zoom (tile) level for data in a PMTiles database")	
	flag.StringVar(&leaflet_style, "leaflet-style", "", "A custom Leaflet style definition for geometries. This may either be a JSON-encoded string or a path on disk.")
	flag.StringVar(&leaflet_point_style, "leaflet-point-style", "", "A custom Leaflet style definition for points. This may either be a JSON-encoded string or a path on disk.")

	flag.Var(&leaflet_label_properties, "leaflet-label-property", "Zero or more (GeoJSON Feature) properties to use to construct a label for a feature's popup menu when it is clicked on.")
	flag.StringVar(&initial_view, "initial-view", "", "A comma-separated string indicating the map's initial view. Valid options are: 'LON,LAT', 'LON,LAT,ZOOM' or 'MINX,MINY,MAXX,MAXY'.")

	flag.Parse()

	if verbose {
		slog.SetLogLoggerLevel(slog.LevelDebug)
		slog.Debug("Verbose logging enabled")
	}

	mux := http.NewServeMux()

	opts := &maps.AssignMapConfigHandlerOptions{
		MapProvider:            map_provider,
		MapTileURI:             map_tile_uri,
		InitialView:            initial_view,
		LeafletStyle:           leaflet_style,
		LeafletPointStyle:      leaflet_point_style,
		LeafletLabelProperties: leaflet_label_properties,
		ProtomapsTheme:         protomaps_theme,
		ProtomapsMaxDataZoom:    protomaps_max_data_zoom,				
	}

	maps.AssignMapConfigHandler(opts, mux, "/map.json")
	
	www_fs := http.FS(www.FS)
	www_handler := http.FileServer(www_fs)

	mux.Handle("/", www_handler)

	addr := fmt.Sprintf("%s:%d", host, port)
	slog.Info("Listening for requests", "address", addr)

	http.ListenAndServe(addr, mux)
}

Error handling omitted for the sake of brevity.

The "nut" of it being this part:

	opts := &maps.AssignMapConfigHandlerOptions{
		MapProvider:       map_provider,
		MapTileURI:        map_tile_uri,
		InitialView:       initial_view,
		LeafletStyle:      leaflet_style,
		LeafletPointStyle: leaflet_point_style,
		ProtomapsTheme:    protomaps_theme,
		ProtomapsMaxDataZoom:    protomaps_max_data_zoom,		
	}

	maps.AssignMapConfigHandler(opts, mux, "/map.json")

Which populates the maps.AssignMapConfigHandlerOptions with map-specific command line flags and passes those options (along with your http.ServeMux instance) to the AssignMapConfigHandler ethod. This method will validate all the options and create a new map config http.Handler assigning it to the http.ServeMux instance.

If your map provider start with "protomaps" and the corresponding MapTileURI starts with file:// it will be assumed that you are trying to serve a local Protomaps database and a matching http.Handler will assign to the http.ServeMux instance.

And then in your JavaScript code you would write something like this:

window.addEventListener("load", function load(event){

    // Null Island    
    var map = L.map('map').setView([0.0, 0.0], 1);    

    fetch("/map.json")
        .then((rsp) => rsp.json())
        .then((cfg) => {

	    console.debug("Got map config", cfg);
	    
            switch (cfg.provider) {
                case "leaflet":

                    var tile_url = cfg.tile_url;

                    var tile_layer = L.tileLayer(tile_url, {
                        maxZoom: 19,
                    });

                    tile_layer.addTo(map);
                    break;

                case "protomaps":

                    var tile_url = cfg.tile_url;

		    var pm_args = {
                        url: tile_url,
                        flavor: cfg.protomaps.theme,
                    };

		    // Necessary for "over-zooming"
		    
		    if ("max_data_zoom" in cfg.protomaps){
		    	pm_args.maxDataZoom = cfg.protomaps.max_data_zoom;
		    }

                    var tile_layer = protomapsL.leafletLayer(pm_args)
		    
                    tile_layer.addTo(map);
                    break;

                default:
                    console.error("Uknown or unsupported map provider");
                    return;
	    }

	    if (cfg.initial_view) {

		var zm = map.getZoom();

		if (cfg.initial_zoom){
		    zm = cfg.initial_zoom;
		}

		map.setView([cfg.initial_view[1], cfg.initial_view[0]], zm);
		
	    } else if (cfg.initial_bounds){

		var bounds = [
		    [ cfg.initial_bounds[1], cfg.initial_bounds[0] ],
		    [ cfg.initial_bounds[3], cfg.initial_bounds[2] ],
		];

		map.fitBounds(bounds);
	    }
	    
	    console.debug("Finished map setup");
	    
        }).catch((err) => {
	    console.error("Failed to derive map config", err);
	    return;
	});    
    
});

Example

Leaflet

$> make example
go run cmd/example/main.go \
		-initial-view '-122.384292,37.621131,13'
		
2025/03/06 10:00:09 INFO Listening for requests address=localhost:8080
Protomaps (Leaflet)

$> make example-pm
go run cmd/example/main.go \
		-initial-view '-122.384292,37.621131,13' \
		-map-provider protomaps \
		-map-tile-uri 'file:///usr/local/go-http-maps/fixtures/sfo.pmtiles'
		
2025/03/06 09:59:05 INFO Listening for requests address=localhost:8080

Note: The protomaps/protomaps-leaflet JavaScript library has been put in maitainance mode. All new Protomaps features are only being added to the PMTiles for MapLibre GL libraries.

Protomaps (Leaflet with custom paint rules)

$> make example-pm-paint
go run cmd/example/main.go \
		-initial-view '-122.384292,37.621131,13' \
		-map-provider protomaps-paint \
		-protomaps-max-data-zoom 14 \
		-map-tile-uri 'file:///Users/asc/aaronland/go-http-maps/fixtures/sfo.pmtiles'
2026/03/18 10:47:30 INFO Listening for requests address=localhost:8080

Custom paint (or label) rules are not part of the map "config" file and need to be added manually in your JavaScript code. See static/www/javascript/index.init.js for details.

Note: The protomaps/protomaps-leaflet JavaScript library has been put in maitainance mode. All new Protomaps features are only being added to the PMTiles for MapLibre GL libraries.

Protomaps (Leaflet with raster tiles)
$> make example-pm-raster
go run cmd/example/main.go \
		-initial-view '-122.384292,37.621131,13' \
		-map-provider protomaps-raster \
		-protomaps-max-data-zoom 14 \
		-map-tile-uri https://static.sfomuseum.org/aerial/1936.pmtiles
2026/03/18 10:47:59 INFO Listening for requests address=localhost:8080

Note: The protomaps/protomaps-leaflet JavaScript library has been put in maitainance mode. All new Protomaps features are only being added to the PMTiles for MapLibre GL libraries.

Protomaps (MapLibre)

$> make example-pm-ml
go run cmd/example/main.go \
		-initial-view '-122.384292,37.621131,13' \
		-map-provider protomaps-ml \
		-protomaps-max-data-zoom 14 \
		-map-tile-uri 'file:///Users/asc/aaronland/go-http-maps/fixtures/sfo.pmtiles'
2026/03/18 10:48:13 INFO Listening for requests address=localhost:8080

MapLibre styling rules are not part of the map "config" file and need to be added manually in your JavaScript code. See static/www/javascript/index.init.js for details.

Protomaps

Extracting slices from the nightly builds

This is the easiest way to generate Protomaps slices. Simply run the extract command from the go-pmtiles tool with a --bbox flag. For example:

$> cd /usr/local/src/go-pmtiles
$> go run main.go extract \
	--bbox=-122.408061,37.601617,-122.354907,37.640167 \
	https://build.protomaps.com/{YYYYMMDD}.pmtiles \
	sfo-src.pmtiles

fetching 7 dirs, 7 chunks, 7 requests
Region tiles 51, result tile entries 51
fetching 51 tiles, 21 chunks, 17 requests
fetching chunks 100%  (1.3/1.3 MB, 1.1 MB/s)        
Completed in 7.269754041s with 4 download threads (7.015367816594351 tiles/s).
Extract required 27 total requests.
Extract transferred 1.3 MB (overfetch 0.05) for an archive size of 1.3 MB
$> go run main.go show sfo-src.pmtiles
pmtiles spec version: 3
tile type: mvt
bounds: (long: -122.408061, lat: 37.601617) (long: -122.354907, lat: 37.640167)
min zoom: 0
max zoom: 15
center: (long: -122.381484, lat: 37.620892)
center zoom: 0
addressed tiles count: 51
tile entries count: 51
tile contents count: 51
clustered: true
internal compression: gzip
tile compression: gzip
vector_layers <object...>
pgf:devanagari:name NotoSansDevanagari-Regular
pgf:devanagari:version 1
planetiler:osm:osmosisreplicationseq 115029
planetiler:osm:osmosisreplicationtime 2025-10-27T04:00:00Z
planetiler:osm:osmosisreplicationurl https://planet.osm.org/replication/hour/
type baselayer
version 4.12.0
attribution <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap</a>
description Basemap layers derived from OpenStreetMap and Natural Earth
name Protomaps Basemap
planetiler:buildtime 2025-05-06T00:22:16.609Z
planetiler:githash 2c91725f6d048fd60b02d3e7c29bb88838451048
planetiler:version 0.9.0

Documentation

Overview

Package maps provides configuration types and HTTP handlers for web maps using Leaflet and Protomaps.

Index

Constants

View Source
const LEAFLET_OSM_TILE_URL = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"

LEAFLET_OSM_TILE_URL is the URL template for OpenStreetMap's standard raster tile layer.

View Source
const PROTOMAPS_API_TILE_URL = "https://api.protomaps.com/tiles/v3/{z}/{x}/{y}.mvt?key={key}"

PROTOMAPS_API_TILE_URL is the URL template for the Protomaps hosted tiles API, where "{key}" is replaced by an API key.

Variables

This section is empty.

Functions

func AssignMapConfigHandler

func AssignMapConfigHandler(opts *AssignMapConfigHandlerOptions, mux *http.ServeMux, map_cfg_uri string) error

AssignMapConfigHandler derives a new `MapConfig` and corresponding `http.Handler` instance from 'opts' and assigns it to 'mux' at 'map_cfg_uri'. If the config has a custom tile handler it is also assigned to 'mux' at the config's tile URL.

func MapConfigHandler

func MapConfigHandler(cfg *MapConfig) http.Handler

MapConfigHandler returns a new `http.Handler` that will return a JSON-encoded version of 'cfg'.

func ProtomapsFileHandlerFromPath

func ProtomapsFileHandlerFromPath(path string, prefix string) (string, http.Handler, error)

ProtomapsFileHandlerFromPath takes a path and creates a http.FileServer handler instance for the files in its root directory. The handler is returned with a relative URI for the filename in 'path' to be assigned to a net/http ServeMux instance. If 'prefix' is not empty it is stripped from request paths and prepended to the returned URI.

Types

type AssignMapConfigHandlerOptions

type AssignMapConfigHandlerOptions struct {
	// A string label indicating the map provider to use. Valid options are: leaflet, protomaps.
	MapProvider string
	// A valid Leaflet tile layer URI.
	MapTileURI string
	// A comma-separated string indicating the map's initial view. Valid options are: 'LON,LAT', 'LON,LAT,ZOOM' or 'MINX,MINY,MAXX,MAXY'.
	InitialView string
	// A custom Leaflet style definition for geometries. This may either be a JSON-encoded string or a path on disk.
	LeafletStyle string
	// A custom Leaflet style definition for points. This may either be a JSON-encoded string or a path on disk.
	LeafletPointStyle string
	// Zero or more (GeoJSON Feature) properties to use to construct a label for a feature's popup menu when it is clicked on.
	LeafletLabelProperties []string
	// A dictionary defining Leaflet label pane names and their z-index values.
	LeafletPanes map[string]int
	// A valid Protomaps theme label.
	ProtomapsTheme string
	// ProtomapsMaxDataZoom is the maximum zoom (tile) level for data in a PMTiles database.
	ProtomapsMaxDataZoom int
}

AssignMapConfigHandlerOptions defines configuration options for the `AssignMapConfigHandler` and `MapConfigFromOptions` functions.

type InitialBounds

type InitialBounds [4]float64

InitialBounds is the initial (minx, miny, maxx, maxy) bounding box of a map.

func (*InitialBounds) String

func (v *InitialBounds) String() string

String returns the bounds as a comma-separated "minx,miny,maxx,maxy" string.

type InitialView

type InitialView [2]float64

InitialView is the initial (longitude, latitude) centre point of a map.

func (*InitialView) String

func (v *InitialView) String() string

String returns the view as a comma-separated "lon,lat" string.

type LeafletConfig

type LeafletConfig struct {
	// A `LeafletStyle` instance used to style geometries
	Style *LeafletStyle `json:"style,omitempty"`
	// A `LeafletStyle` instance used to style Point geometries
	PointStyle *LeafletStyle `json:"point_style,omitempty"`
	// Zero or more (GeoJSON Feature) properties to use to construct a label for a feature's popup menu when it is clicked on.
	LabelProperties []string `json:"label_properties,omitempty"`
	// A dictionary defining Leaflet label pane names and their z-index values.
	Panes map[string]int `json:"panes,omitempty"`
}

LeafletConfig defines configuration details for maps using Leaflet.

type LeafletStyle

type LeafletStyle struct {
	// Color is the stroke colour.
	Color string `json:"color,omitempty"`
	// FillColor is the fill colour.
	FillColor string `json:"fillColor,omitempty"`
	// Weight is the stroke width in pixels.
	Weight float64 `json:"weight,omitempty"`
	// Opacity is the stroke opacity.
	Opacity float64 `json:"opacity,omitempty"`
	// Radius is the radius of point markers.
	Radius float64 `json:"radius,omitempty"`
	// FillOpacity is the fill opacity.
	FillOpacity float64 `json:"fillOpacity,omitempty"`
	// Custom flags for runtime style configuration
	Custom any `json:"custom,omitempty"`
}

LeafletStyle is a struct containing details for decorating GeoJSON features and markers.

func UnmarshalLeafletStyle

func UnmarshalLeafletStyle(raw string) (*LeafletStyle, error)

UnmarshalLeafletStyle derives a `LeafletStyle` instance from 'raw'. If 'raw' starts with "{" then it is treated as a JSON-encoded string, otherwise it is treated as a local path on disk.

func UnmarshalStyleFromReader

func UnmarshalStyleFromReader(r io.Reader) (*LeafletStyle, error)

UnmarshalStyleFromReader derives a `LeafletStyle` instance from the body of 'r'.

func UnmarshalStyleFromString

func UnmarshalStyleFromString(raw string) (*LeafletStyle, error)

UnmarshalStyleFromString derives a `LeafletStyle` instance from 'raw'.

type MapConfig

type MapConfig struct {
	// A valid map provider label.
	Provider string `json:"provider"`
	// A valid Leaflet tile layer URI.
	TileURL string `json:"tile_url"`
	// A custom http.Handler to use with `TileURL`. Used for PMTiles-based tiles.
	TileURLHandler http.Handler `json:"tile_url_handler,omitempty"`
	// Optional Protomaps configuration details
	Protomaps *ProtomapsConfig `json:"protomaps,omitempty"`
	// Optional Leaflet configuration details
	Leaflet *LeafletConfig `json:"leaflet,omitempty"`
	// The initial view (lon, lat) for the map (optional).
	InitialView *InitialView `json:"initial_view,omitempty"`
	// The initial zoom level for the map (optional).
	InitialZoom int `json:"initial_zoom,omitempty"`
	// The initial bounds (minx, miny, maxx, maxy) for the map (optional).
	InitialBounds *InitialBounds `json:"initial_bounds,omitempty"`
}

MapConfig defines common configuration details for maps.

func MapConfigFromOptions

func MapConfigFromOptions(opts *AssignMapConfigHandlerOptions) (*MapConfig, error)

MapConfigFromOptions derives a new `MapConfig` instance from 'opts'.

type ProtomapsConfig

type ProtomapsConfig struct {
	// A valid Protomaps theme label
	Theme string `json:"theme"`
	// MaxDataZoom is the maximum zoom (tile) level for data in a PMTiles database
	MaxDataZoom int `json:"max_data_zoom,omitempty"`
	// PaintRules are optional custom Protomaps paint rules.
	PaintRules []map[string]any `json:"paintRules"`
	// LabelRules are optional custom Protomaps label rules.
	LabelRules []map[string]any `json:"labelRules"`
}

ProtomapsConfig defines configuration details for maps using Protomaps.

Directories

Path Synopsis
static
www
Package www provides an embedded example web application for displaying maps.
Package www provides an embedded example web application for displaying maps.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL