Skip to content

API Reference

Constructor

typescript
const postgis = new Postgis(client);
ParameterTypeDescription
clientPostgisClientA connected pg.Client or any object with a query(sql) method

Throws TypeError if client is not provided or does not have a query function.


Schema Introspection

list_tables(options?)

Lists all user-accessible tables with PostGIS geometry metadata.

typescript
const tables = await postgis.list_tables({ filter: "table_type = 'BASE TABLE'" });
OptionTypeDefaultDescription
filterstringSQL WHERE clause fragment

list_columns(table)

Lists all columns of a given table.

typescript
const cols = await postgis.list_columns('parcels');
// [{ field_name: 'id', field_type: 'int4' }, { field_name: 'geom', field_type: 'geometry' }]

General Querying

query_table(table, options?)

Flexible SELECT with optional filter, group, sort, limit.

typescript
const rows = await postgis.query_table('parcels', {
  columns: 'id, name, area',
  filter: "status = 'active'",
  sort: 'area DESC',
  limit: 50,
});
OptionTypeDefaultDescription
columnsstring'*'Columns to SELECT
filterstringWHERE clause
groupstringGROUP BY clause
sortstringORDER BY clause
limitnumber|null100LIMIT (pass null to remove)

Spatial Operations

bbox(table, options?)

Returns the spatial extent of all geometries.

typescript
const [{ bbox }] = await postgis.bbox('parcels', { srid: 4326 });
OptionTypeDefault
geom_columnstring'geom'
sridnumber4326
filterstring

centroid(table, options?)

Returns the centroid (x, y) of each row's geometry.

typescript
const centroids = await postgis.centroid('parcels', { force_on_surface: true });
OptionTypeDefault
force_on_surfacebooleanfalse
geom_columnstring'geom'
sridstring|number'4326'
filterstring

intersect_feature(table_from, table_to, options?)

Finds cross-table intersections using ST_DWithin.

typescript
const results = await postgis.intersect_feature('roads', 'parcels', {
  distance: '50',
  sort: 'name ASC',
});
OptionTypeDefault
columnsstring'*'
distancestring|number'0'
geom_column_fromstring'geom'
geom_column_tostring'geom'
filterstring
sortstring
limitnumber|null

intersect_point(table, point, options?)

Finds features within distance of a point.

typescript
const nearby = await postgis.intersect_point('restaurants', '73.5,14.9,4326', {
  distance: '500',
  limit: 20,
});
ParameterTypeDescription
pointstring"x,y,srid" format

nearest(table, point, options?)

Returns features ordered by distance to a point.

typescript
const closest = await postgis.nearest('hospitals', '73.5,14.9,4326', { limit: 5 });
// Each row includes a `distance` column

Geometry Export

geojson(table, options?)

Exports features as a GeoJSON FeatureCollection.

typescript
const fc = await postgis.geojson('parcels', {
  bounds: '72.8,18.9,73.2,19.2',
  precision: 6,
  columns: 'name, area',
});
// { type: 'FeatureCollection', features: [...] }
OptionTypeDefaultDescription
boundsstring"xmin,ymin,xmax,ymax" or "z,x,y"
id_columnstringGeoJSON feature ID column
precisionnumber9Coordinate decimal places
geom_columnstring'geom'
columnsstringExtra properties columns
filterstringWHERE clause

geobuf(table, options?)

Exports features as a Geobuf Buffer (compact binary GeoJSON).

typescript
const buf = await postgis.geobuf('parcels');
res.setHeader('Content-Type', 'application/x-protobuf');
res.send(buf);

mvt(table, x, y, z, options?)

Generates a Mapbox Vector Tile for the given tile coordinate.

typescript
const [{ mvt: tile }] = await postgis.mvt('parcels', 0, 0, 0);
res.setHeader('Content-Type', 'application/vnd.mapbox-vector-tile');
res.send(tile);
ParameterTypeDescription
xnumberTile X coordinate
ynumberTile Y coordinate
znumberZoom level

transform_point(point, options?)

Transforms a point between coordinate systems.

typescript
const [{ x, y }] = await postgis.transform_point('73.5,14.9,4326', { srid: 3857 });
OptionTypeDefault
sridnumber4326

Released under the MIT License.