Skip to main content

MBtiles from shapefiles

·2 mins

MBtiles from shapefiles #

  • extract all the zips
for file in *.zip
do
    directory="${file%.zip}"
    unzip "$file" -d "$directory"
done
  • create file to convert shapefiles to geojson
#!/usr/bin/env bash

DIR=$1

find "$DIR" -type f -iname "*.shp" -print0 | while IFS= read -r -d $'\0' line; do
    echo "converting $line to $(echo $line | sed 's/^\(.*\)\.shp$/\1\.geojson/')"
    ogr2ogr -f GeoJSON -t_srs EPSG:4326 -makevalid $(echo "$line" | sed 's/^\(.*\)\.shp$/\1\.geojson/') "$line"
      # && rm "$line" \
      # && rm $(echo "$line" | sed 's/^\(.*\)\.shp$/\1\.dbf/') \
      # && rm $(echo "$line" | sed 's/^\(.*\)\.shp$/\1\.shx/') \
      # && rm $(echo "$line" | sed 's/^\(.*\)\.shp$/\1\.prj/') \
      # && echo "deleted $line" \
      # && echo "deleted $(echo $line | sed 's/^\(.*\)\.shp$/\1\.shx/')" \
      # && echo "deleted $(echo $line | sed 's/^\(.*\)\.shp$/\1\.dbf/')" \
      # && echo "deleted $(echo $line | sed 's/^\(.*\)\.shp$/\1\.prj/')"
done
  • run this file using ogr2ogr through docker
docker run --rm -v $PWD:/data -v $PWD/mbtiles_from_shps.sh:/mbtiles_from_shps.sh osgeo/gdal:ubuntu-full-3.2.1 bash -c "/mbtiles_from_shps.sh /data"
  • move all geojson files into a single directory
find . -type f -iname '*.geojson' -exec mv -t ../output_shps {} \+
  • use tippecanoe through docker to convert geojson to mbtiles
 docker run --rm -it -v $PWD:/data metacollin/tippecanoe tippecanoe -zg -o /data/microsoft-2021-02-15-v1.0.0-us-buildings.mbtiles -l buildings --coalesce-densest-as-needed --extend-zooms-if-still-dropping $(ls *.geojson | sed 's/^\(.*\)/\/data\/\1/')
  • check out these files in the browser using mbview
export MAPBOX_ACCESS_TOKEN=pk.eyJ1IjoiamJhdHRlYXMiLCJhIjoiY2tsNDRmb21qMW5wcTJvdWlwNnpyMHF5bSJ9.F9kRop-f7f_UHJJUXZjcyQ && mbview opendem-2021-02-13-v1.0.1-planet-terrain.mbtiles