Skip to content

Instantly share code, notes, and snippets.

@wipfli
Last active March 24, 2022 06:29
Show Gist options
  • Save wipfli/6c134eb530d2b2408e73199040f72efa to your computer and use it in GitHub Desktop.
Save wipfli/6c134eb530d2b2408e73199040f72efa to your computer and use it in GitHub Desktop.
Shapefile to mbtiles with Planetiler

Shapefile to mbtiles with Planetiler

Example how to generate a .mbtiles file from .shp file with Planetiler. This uses data from Swiss natural protected areas call "Aulav" which are given in the countries EPSG:2056 coordinate system.

Steps

Set up the java environment according to the Planetiler contributing guidelines.

Create AulavOverlay.java and put the following code into it:

// Content of AulavOverlay.java

package com.onthegomap.planetiler.examples;

import com.onthegomap.planetiler.FeatureCollector;
import com.onthegomap.planetiler.Planetiler;
import com.onthegomap.planetiler.Profile;
import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.reader.SourceFeature;
import java.nio.file.Path;

public class AulavOverlay implements Profile {

  public static final String AULAV_SOURCE = "aulav";

  @Override
  public void processFeature(SourceFeature sourceFeature, FeatureCollector features) {
    features.polygon("aulav")
      .setBufferPixels(4)
      .setMinZoom(6)
      .setAttr("name", sourceFeature.getTag("Name"));
  }

  @Override
  public String name() {
    return "Aulav Overlay";
  }

  @Override
  public String description() {
    return "An example overlay showing Aulav";
  }

  @Override
  public boolean isOverlay() {
    return true;
  }

  @Override
  public String attribution() {
    return """
      <a href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\">&copy; OpenStreetMap contributors</a>
      """.trim();
  }

  public static void main(String[] args) throws Exception {
    run(Arguments.fromArgsOrConfigFile(args));
  }

  static void run(Arguments args) throws Exception {
    //String area = args.getString("area", "geofabrik area to download", "monaco");
    Path dataDir = Path.of("data");
    Path sourcesDir = dataDir.resolve("sources");
    Planetiler.create(args)
      .setProfile(new AulavOverlay())
      //.addOsmSource("osm", Path.of("data", "sources", area + ".osm.pbf"), "geofabrik:" + area)
      .addShapefileSource("EPSG:2056", AULAV_SOURCE,
        sourcesDir.resolve("aulav/AuLaV_Jagdbanngebiete_LV95/AuLaVJagdbanngebiete20171101_1.shp"),
        "")
      .overwriteOutput("mbtiles", Path.of("data", "Aulav.mbtiles"))
      .run();
  }
}

Download the planetiler jar file:

wget https://github.com/onthegomap/planetiler/releases/latest/download/planetiler.jar

Make a data folder and download the shapefile:

mkdir -p data/sources/aulav
cd data/sources/aulav
wget https://data.geo.admin.ch/ch.bafu.schutzgebiete-aulav_jagdbanngebiete/data.zip
unzip data.zip
cd ../../../

Run planetiler:

java -cp planetiler.jar AulavOverlay.java
ls data/
# Aulav.mbtiles  sources  tmp

Inspect the generated .mbtiles file with:

sudo docker run --rm -it -v "$(pwd)/data":/data -p 8080:8080 maptiler/tileserver-gl -p 8080

Open a web browser and go to http://[::]:8080/data/Aulav/#8.6/46.7229/8.7673

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