3.0.0 • Published 7 years ago

pg-postgis-types v3.0.0

Weekly downloads
121
License
BSD-3-Clause
Repository
github
Last release
7 years ago

pg-postgis-types Build Status

Use PostGIS geometry types with node-postgres.

This module registers parsers for the PostGIS geometry types. You can also plug in your own WKB parser.

Installation

npm install pg-postgis-types

Documentation

postgis(fetcher, key, callback)

Fetches the OIDs for the given types.

Parameters

parametertypedescription
fetcherFunctionThe function to query of the form `function (sql, callback)
keyStringA unique string to key the type map with (e.g. the connection string)
callbackFunctionThe callback to call after the types are fetched

Callback is called with an error argument.

postgis.isGeometryType(oid, key)

Returns true if the given OID is a geometry or geography type

Parameters

parametertypedescription
oidNumberThe oid of a column
keyStringA unique string to key the type map with (e.g. the connection string)

postgis.setGeometryParser(parser)

Setup a custom parser for geometry/geography columns. The parser is a function that accepts one argument for the string value to parse. The library uses wkx by default to parse goemetries. You can use this if you want to use your own WKB parser.

Parameters

parametertypedescription
parserFunctionThe custom parser to use for geometry columns

Example

var pg = require('pg');
var postgis = require('pg-postgis-types');
var pgtypes = require('pg-custom-types');

var connection = 'some connection string';

postgis(pgtypes.fetcher(pg, connection), null, (err, oids) {
  if (err) {
    throw err;
  }

  pg.connect(connection, function (err, client, done) {
    if (err) {
      return callback(err);
    }

    var sql = "SELECT ST_GeomFromText('POINT(1 2)') AS geom";

    client.query(sql, null, function (err, results) {
      done();

      if (err) {
        throw err;
      }

      var geojson = results.rows[0].geom.toGeoJSON();

      // do something cool with geojson
    });
  });
});