Giter VIP home page Giter VIP logo

natural-earth-tm2's Introduction

Natural Earth for TM2

This project serves as an example for creating a relatively complex vector tile source in TM2 along with a visual style for that source. All data is from Natural Earth.

Requirements

  • PostgreSQL
  • PostGIS >= 2.0
  • GDAL (for ogr2ogr)
  • basic Unix tools: bash, wget, unzip

This project has been tested on Ubuntu Linux and it should also work on Mac OS X. Windows is not yet supported.

Setup

git clone [email protected]:mapbox/natural-earth-tm2.git
cd natural-earth-tm2
./util/setup_db.sh

TODO: configurable database connection. For now you'll need postgres@localhost:5432 to connect, without a password.

Scales

Natural Earth comes in 3 scales. This project uses 1:110,000,000 for zoom levels 0 through 2, 1:50,000,000 for zoom levels 3 & 4, and 1:10,000,000 for zoom levels 5 and up. There are a few exceptions since some layers that are only available at 1:10,000,000 scale - notably roads.

PostgreSQL & PostGIS

For creating anything more than basic vector tile sources, using a PostGIS data source is highly recommended. The flexible queries and spatial functions are very important for creating vector tile sources that are compact and easy to style.

Zoom level conditionals

Mapnik's !scale_denominator! token along with a custom PostgreSQL function called z to let's us include different data at different zoom levels in the same vector tile layer. For example, the following in a WHERE clause will include only the most important objects at lower zoom levels, but include more and more as you zoom in:

CASE WHEN z(!scale_denominator!) = 4 AND scalerank <= 2 THEN TRUE
     WHEN z(!scale_denominator!) = 5 AND scalerank <= 4 THEN TRUE
     WHEN z(!scale_denominator!) >= 6 THEN TRUE  -- includes all data
     END

Multiple tables in one layer

To simplify styling we sometimes combine multiple tables into a single layer query. We do this using a UNION ALL SQL statement. UNION ALL concatenates multiple queries together, but they all must have the same number of columns. If you want a column from one table that the other table does not have, you'll need to include a placeholder column in that query. In this example, that's the '' AS name part in the ocean query:

( SELECT geom, '' AS name FROM ne_10m_ocean
  UNION ALL
  SELECT geom, name FROM ne_10m_lakes
) AS data

In this project you'll also see multiple tables combined with zoom level conditionals in order to handle switching between Natural Earth's 3 data scales at different zoom levels.

Labeling polygons

Deriving points from polygons is especially important for vector tiles. Labeling polygons doesn't work like it did in TileMill 1 - with vector tiles a polygon might be split across many vector tiles, so if you try to label it directly you'll end up with lots of duplicate labels. Using PostGIS's ST_PointOnSurface function to derive a point layer for labeling a separate polygon layer is one way around this.

You can do this on-the-fly in a TM2 SQL query, eg:

( SELECT ST_PointOnSurface(geom) AS geom, name
  FROM ne_10m_lakes
  WHERE geom && !bbox!
) AS data

However for faster exports we've done this as a post-processing step to our import script. It works by adding a new geometry column, geom_point, to polygon layers we'll want to label.

natural-earth-tm2's People

Contributors

ajashton avatar pnorman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

natural-earth-tm2's Issues

Integer out of range error with z() function

With the z() function given, it will get an out of range error for a z0 tile.

Unable to make vector tile: Postgis Plugin: ERROR:  integer out of range

in executeQuery Full sql was: ... WHERE z(5.59082e+08) >= 6 AND z(5.59082e+08) < 8 ...

Zoom level conditionals does not affect data

Hi! I'm trying to display landusage from osm database (imported with imposm). So I want to display simplyfied data on different zoom levels. I used custom function z with mapnik !scale_denominator!, but it seems, that it does not affect on data.

2015-05-06 13 53 06

Here is Mapbox Studio screenshot โ€” there are all 3 landusage layers displayed on 13 zoom level. But I want to display data only from osm_landusages table.

( SELECT * FROM (
    -- landuse_gen0
    SELECT geometry, type, area
    FROM osm_landusages_gen0
    WHERE geometry && !bbox!
    AND CASE
      WHEN z(!scale_denominator!) > 3 THEN TRUE
      WHEN z(!scale_denominator!) <= 9 THEN TRUE
    END

    -- landuse_gen1
    UNION ALL
    SELECT geometry, type, area
    FROM osm_landusages_gen1
    WHERE geometry && !bbox!
    AND CASE
      WHEN z(!scale_denominator!) > 9 THEN TRUE
      WHEN z(!scale_denominator!) <= 12 THEN TRUE
    END

    -- landuse
    UNION ALL
    SELECT geometry, type, area
    FROM osm_landusages
    WHERE geometry && !bbox!
      AND z(!scale_denominator!) > 12
  ) AS QUERY
 ORDER BY area DESC
) AS data

What am I missing? Thanks!

Import Natural Earth to PostGIS report error

FAILURE:
Unable to open datasource `~/Documents/natural_earth_vector.sqlite' with the following drivers.
-> ESRI Shapefile
-> MapInfo File
-> UK .NTF
-> SDTS
-> TIGER
-> S57
-> DGN
-> VRT
-> REC
-> Memory
-> BNA
-> CSV
-> NAS
-> GML
-> GPX
-> LIBKML
-> KML
-> GeoJSON
-> Interlis 1
-> Interlis 2
-> GMT
-> GPKG
-> SQLite
-> DODS
-> WAsP
-> MDB
-> OGDI
-> PostgreSQL
-> PCIDSK
-> OpenFileGDB
-> XPlane
-> AVCBin
-> AVCE00
-> DXF
-> Geoconcept
-> GeoRSS
-> GPSTrackMaker
-> VFK
-> PGDump
-> OSM
-> GPSBabel
-> SUA
-> OpenAir
-> PDS
-> WFS
-> SOSI
-> HTF
-> AeronavFAA
-> EDIGEO
-> GFT
-> GME
-> SVG
-> CouchDB
-> Idrisi
-> ARCGEN
-> SEGUKOOA
-> SEGY
-> XLS
-> ODS
-> XLSX
-> ElasticSearch
-> CartoDB
-> SXF

utils script doesn't get to adding data to the database

When running the utils script it echoes

Setting up PostGIS database...
Downloading Natural Earth Data (213MB)...

Then returns to the bash prompt. Looks like things set up more or less correctly in postgres:

psqlshg

Using postgres 9.3.4. Not sure what I'm doing wrong.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.