Home

Awesome

tilelive-postgis

npm version npm downloads Build Status

Implements the tilelive API for generating mapnik vector tiles from PostGIS.

Installation

npm install @mapbox/tilelive tilelive-postgis

Usage

const tilelive = require('@mapbox/tilelive');
require('tilelive-postgis').registerProtocols(tilelive);

const uri = 'postgis://user:password@localhost:5432/test?table=test_table&geometry_field=geometry&srid=4326';
tilelive.load(uri, (error, source) => {
  if (error) throw error;

  source.getTile(0, 0, 0, (error, tile, headers) => {
    // `error` is an error object when generation failed, otherwise null.
    // `tile` contains the compressed image file as a Buffer
    // `headers` is a hash with HTTP headers for the image.
  });
});

If PostgreSQL server running locally and accepting connections on Unix domain socket, you can connect like this:

const uri = 'postgis:///var/run/postgresql/test?table=test&geometry_field=geom';
tilelive.load(uri, (error, source) => { ... });

You can also use query parameter to specify sub-query like this:

const query = `(select * from schemaName.tableName where st_intersects(geometry, !bbox!)) as query`;
const uri = `postgis://user@localhost/test?table=test_table&geometry_field=geometry&query=${encodeURI(query)}`;
tilelive.load(uri, (error, source) => { ... });

Parameters

The only parameter that is not mapnik specific is:

parametervaluedescriptiondefault
layerNamestringname of the layer in resulting tiles.defaults to the table name for backwards compatibility.

Actual list of parameters you can see here.

parametervaluedescriptiondefault
tablestringname of the table to fetch.
geometry_fieldstringname of the geometry field, in case you have more than one in a single table. This field and the SRID will be deduced from the query in most cases, but may need to be manually specified in some cases.
geometry_tablestringname of the table containing the returned geometry; for determining RIDs with subselects
sridintegersrid of the table, if this is > 0 then fetching data will avoid an extra database query for knowing the srid of the table0
extentstringmaxextent of the geometriesdetermined by querying the metadata for the table
extent_from_subquerybooleanevaluate the extent of the subquery, this might be a performance issuefalse
connect_timeoutintegertimeout is seconds for the connection to take place4
persist_connectionbooleanchoose whether to share the same connection for subsequent queriestrue
row_limitintegermax number of rows to return when querying data, 0 means no limit0
cursor_sizeintegerif this is > 0 then server cursor will be used, and will prefetch this number of features0
initial_sizeintegerinitial size of the stateless connection pool1
max_sizeintegermax size of the stateless connection pool10
multiple_geometriesbooleanwhether to use multiple different objects or a single one when dealing with multi-objects (this is mainly related to how the label are used in the map, one label for a multi-polygon or one label for each polygon of a multi-polygon)false
encodingstringinternal file encodingutf-8
simplify_geometriesbooleanwhether to automatically reduce input vertices. Only effective when output projection matches (or is similar to) input projection. Available from version 2.1.x up.false
max_async_connectionintegermax number of PostGIS queries for rendering one map in asynchronous mode. Default value (1) has no effect.1