• Stars
    star
    367
  • Rank 116,257 (Top 3 %)
  • Language
    JavaScript
  • Created almost 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dirt Simple PostGIS HTTP API

Dirt-Simple PostGIS HTTP API

The Dirt-Simple PostGIS HTTP API, or dirt, exposes PostGIS functionality to your applications over HTTP.

Important Note!

Dirt is now optimized for Postgis 3. If you're using Postgis 2.x, use the postgis2x branch.

Getting started

Requirements

  • Node
  • PostgreSQL with PostGIS 3
  • A PostgreSQL login for the service that has select rights to any tables or views you want to expose to dirt.

Step 1: get the goodies

Note: if you don't have git, you can download a zip file of the project instead.

git clone https://github.com/tobinbradley/dirt-simple-postgis-http-api.git dirt
cd dirt
npm install

Step 2: add your configuration

Dirt is configured via environmental variables. These variables can be placed in a .env file in the project's root folder, via the command line at run time, or however you set environmental variables on your operating system. The only environmental variable that must be set is POSTGRES_CONNECTION, which contains your postgres login information.

.env file

POSTGRES_CONNECTION="postgres://user:password@server/database"

command line, linux and mac

POSTGRES_CONNECTION="postgres://user:password@server/database" npm start

This is the complete complete list of environmental variables that can be set.

Variable Required Default Description
POSTGRES_CONNECTION Yes N/A Postgres connection string
SERVER_LOGGER No undefined Turn on Fastify's error logger. Options are true (same as info), fatal, error, warn, info, debug, trace or silent.
SERVER_LOGGER_PATH No undefined Log to file instead of console, ex: /path/to/file
SERVER_HOST No 0.0.0.0 IP to listen on, default is all
SERVER_PORT No 3000 Port to listen on
BASE_PATH No / The base path on which the API is served, which is relative to the host
CACHE_PRIVACY No private Cache response directive
CACHE_EXPIRESIN No 3600 Max age in seconds
CACHE_SERVERCACHE No undefined Max age in seconds for shared cache (i.e. CDN)
RATE_MAX No undefined Requests per minute rate limiter (limiter not used if RATE_LIMIT not set)

Step 3: fire it up!

npm start

To view interactive documentation, head to http://127.0.0.1:3000/.

Running via Docker

To build a Docker image:

docker build -t dirt .

To run the Docker image:

docker run -dp 3000:3000 -e POSTGRES_CONNECTION=<connection string> dirt

Architecture

Due credit

The real credit for this project goes to the great folks behind the following open source software:

How it works

The core of the project is Fastify.

Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

Fastify is written by some of the core Node developers, and it's awesome. A number of Fastify plugins (fastify-autoload, fastify-caching, fastify-compress, fastify-cors, fastify-postgres, and fastify-swagger) are used to abstract away a lot of boilerplate. If you're looking for additional functionality, check out the Fastify ecosystem.

All routes are stored in the routes folder and are automatically loaded on start. Check out the routes readme for more information.

Tips and Tricks

Database

Your Postgres login will need select rights to any tables or views it should be able to access. That includes the geometry_columns view for the list_layers end point to work.

For security, it should only have select rights unless you plan to specifically add a route that writes to a table.

Dirt uses connection pooling, minimizing database connections.

SQL Functions

If a query parameter looks like it should be able to handle SQL functions, it probably can. For example, the columns parameter for most queries can use the count(*) function. You can use any function in the database, including user defined functions.

Mapbox vector tiles

The mvt route serves Mapbox Vector Tiles. The layer name in the returned protobuf will be the same as the table name passed as input. Here's an example of using both geojson and mvt routes with MapLibre GL JS.

map.on('load', function() {
  map.addLayer({
    id: 'dirt-mvt',
    source: {
      type: 'vector',
      tiles: ['http://localhost:3000/v1/mvt/voter_precinct/{z}/{x}/{y}'],
      maxzoom: 14,
      minzoom: 5
    },
    'source-layer': 'voter_precinct',
    type: 'fill',
    minzoom: 5,
    paint: {
      'fill-color': '#088',
      'fill-outline-color': '#333'
    }
  })

  map.addLayer({
    id: 'dirt-geojson',
    type: 'circle',
    source: {
      type: 'geojson',
      data: 'http://localhost:3000/v1/geojson/voter_polling_location'
    },
    paint: {
      'circle-radius': 2,
      'circle-color': '#bada55'
    }
  })
})

Changes require a Restart

If you modify code or add a route, dirt will not see it until dirt is restarted.

More Repositories

1

Mecklenburg-County-GeoPortal

The source code for Mecklenburg County's GeoPortal.
Svelte
101
star
2

quality-of-life-dashboard

Neighborhood indicators dashboard built with Vue and Mapbox GL JS
Vue
63
star
3

Mecklenburg-County-Quality-of-Life-Dashboard

Neighborhood quality of life web site using Leaflet and D3.
JavaScript
60
star
4

leaflet-bootstrap-mapping-template

This is just a personal template I use for new mapping projects. Thought it might come in handy for others.
JavaScript
34
star
5

mapbox-gl-style-editor

Simple, code based Mapbox GL style editor
JavaScript
24
star
6

GeoPortal-Project

GeoPortal mapping template created by Mecklenburg County GIS.
JavaScript
19
star
7

mapbox-gl-pitch-toggle-control

Simple pitch on/off button in Mapbox GL JS style.
JavaScript
16
star
8

pg-vector-tile-server

PostGIS vector tile (pbf) server written in node.
JavaScript
15
star
9

map_widget_maker

Create at embed-able map iFrame using WMS services and leaflet.
JavaScript
15
star
10

svelte-template

Svelte template using Rollup, PostCSS, Babel, Tailwind CSS, and PWA
JavaScript
9
star
11

sitechangecheck

A little NodeJS Puppeteer action to see if a page or part of a page changed.
JavaScript
8
star
12

quality-of-life-explorer

Mecklenburg County Quality of Life Explorer
Svelte
8
star
13

docker-mapbox-tools

Dockerized fontnik, spritezero, and tippecanoe
JavaScript
7
star
14

quality-of-life-embed

Embed/Print map for the Quality of Life Project
JavaScript
6
star
15

etl-with-ogr_fdw

Simple NodeJS ETL tooling using ogr_fdw.
PLpgSQL
5
star
16

mecklenburg-quality-of-life-data

Mecklenburg County data for the Quality of Life Dashboard
JavaScript
5
star
17

geokettle-etl-workflow

ESRI SDE to PostGIS workflow using ArcPy and GeoKettle
Python
5
star
18

timemachine

Mecklenburg County historical aerials viewer
Svelte
5
star
19

quality-of-life-report

Report for the Quality of Life project
Vue
3
star
20

community_compass

multi-variate mapping tool
Svelte
2
star
21

dotfiles

Dot files
Vim Script
2
star
22

leaflet-workshop

Leaflet workshop I did for Triangle Area GIS
HTML
2
star
23

quality-of-life-data-uscensus

automated quality of life data from US Census to be used with the Quality of Life Dashboard
JavaScript
2
star
24

fuzzy-tolerance-octopress-theme

Fuzzy Tolerance Blog Octopress Theme
JavaScript
2
star
25

css-slideshow

CSS Only Slideshow
CSS
1
star
26

buildcharmeck

Building CharMeck website
HTML
1
star
27

redistricting_2020_demo

Svelte
1
star
28

maplibre-3d-terrain

Experiments with Maplibre GL JS 3D terrain coming in the 2.2 release.
JavaScript
1
star
29

quality-of-life-annual-report

Annual scrolly telling report for Quality of Life
HTML
1
star
30

connected-government

CSS
1
star
31

open-mapping

Mecklenburg County Open Mapping website.
Svelte
1
star