/** * PostGIS Node.js Library — TypeScript Type Definitions * @module postgis/types */ /** A minimal pg.Client-compatible interface */ interface PostgisClient { query(sql: string): Promise<{ rows: Record[]; }>; } /** Options for {@link Postgis.list_tables} */ interface ListTablesOptions { /** Optional SQL WHERE clause fragment (e.g. `"table_type = 'BASE TABLE'"`) */ filter?: string; } /** Options for {@link Postgis.query_table} */ interface QueryTableOptions { /** Columns to select. Defaults to `'*'` */ columns?: string; /** SQL WHERE clause fragment */ filter?: string; /** GROUP BY clause */ group?: string; /** ORDER BY clause */ sort?: string; /** Max rows to return. Defaults to `100`. Pass `null` to remove limit */ limit?: number | null; } /** Options for {@link Postgis.bbox} */ interface BboxOptions { /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** Target SRID for the bounding box. Defaults to `4326` */ srid?: number; /** SQL WHERE clause fragment */ filter?: string; } /** Options for {@link Postgis.centroid} */ interface CentroidOptions { /** Use `ST_PointOnSurface` instead of `ST_Centroid`. Defaults to `false` */ force_on_surface?: boolean; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** Target SRID. Defaults to `'4326'` */ srid?: string | number; /** SQL WHERE clause fragment */ filter?: string; } /** Options for {@link Postgis.intersect_feature} */ interface IntersectFeatureOptions { /** Columns to select. Defaults to `'*'` */ columns?: string; /** Search radius in CRS units. Defaults to `'0'` */ distance?: string | number; /** Geometry column in the first (from) table. Defaults to `'geom'` */ geom_column_from?: string; /** Geometry column in the second (to) table. Defaults to `'geom'` */ geom_column_to?: string; /** SQL WHERE clause fragment */ filter?: string; /** ORDER BY clause */ sort?: string; /** Max rows to return */ limit?: number | null; } /** Options for {@link Postgis.intersect_point} */ interface IntersectPointOptions { /** Columns to select. Defaults to `'*'` */ columns?: string; /** Search radius in CRS units. Defaults to `'0'` */ distance?: string | number; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** SQL WHERE clause fragment */ filter?: string; /** ORDER BY clause */ sort?: string; /** Max rows to return. Defaults to `10` */ limit?: number | null; } /** Options for {@link Postgis.geojson} */ interface GeoJSONOptions { /** * Spatial filter as a comma-separated bounding box. * - 4 values → `xmin,ymin,xmax,ymax` (WGS84 envelope) * - 3 values → `z,x,y` (tile envelope) */ bounds?: string; /** Column to use as the GeoJSON feature `id` */ id_column?: string; /** Coordinate decimal precision. Defaults to `9` */ precision?: number; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** Extra columns to include in feature properties */ columns?: string; /** SQL WHERE clause fragment */ filter?: string; } /** Options for {@link Postgis.geobuf} */ interface GeobufOptions { /** * Spatial filter as a comma-separated bounding box. * - 4 values → `xmin,ymin,xmax,ymax` * - 3 values → `z,x,y` (tile envelope) */ bounds?: string; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** Extra columns to include */ columns?: string; /** SQL WHERE clause fragment */ filter?: string; } /** Options for {@link Postgis.mvt} */ interface MvtOptions { /** Extra attribute columns to embed in the tile */ columns?: string; /** Column to use as the MVT feature ID */ id_column?: string; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** SQL WHERE clause fragment */ filter?: string; } /** Options for {@link Postgis.nearest} */ interface NearestOptions { /** Columns to select. Defaults to `'*'` */ columns?: string; /** Geometry column name. Defaults to `'geom'` */ geom_column?: string; /** SQL WHERE clause fragment */ filter?: string; /** Max rows to return. Defaults to `10` */ limit?: number; } /** Options for {@link Postgis.transform_point} */ interface TransformPointOptions { /** Target SRID. Defaults to `4326` */ srid?: number; } /** GeoJSON FeatureCollection returned by {@link Postgis.geojson} */ interface FeatureCollection { type: 'FeatureCollection'; features: unknown[]; } /** * @packageDocumentation * # postgis * * A lightweight, type-safe Node.js library for interacting with * PostGIS-enabled PostgreSQL databases. * * @example * ```typescript * import Postgis from 'postgis'; * import { Client } from 'pg'; * * const client = new Client({ connectionString: process.env.DATABASE_URL }); * await client.connect(); * * const postgis = new Postgis(client); * const tables = await postgis.list_tables(); * ``` */ /** * Main class for interacting with a PostGIS-enabled PostgreSQL database. * * Wrap any `pg.Client` (or pool client) with this class to get access * to high-level spatial query helpers. * * @example * ```typescript * const postgis = new Postgis(client); * const bbox = await postgis.bbox('my_layer'); * ``` */ declare class Postgis { private readonly client; /** * @param client - A connected `pg.Client` or any object with a `query(sql)` method. * @throws {TypeError} if `client` is missing or does not expose a `query` function. */ constructor(client: PostgisClient); /** * List all user-accessible tables in the database, joined with PostGIS * geometry metadata where available. * * @param options.filter - Optional SQL WHERE clause fragment. * @returns Array of table metadata rows. * * @example * ```typescript * const tables = await postgis.list_tables({ filter: "table_type = 'BASE TABLE'" }); * ``` */ list_tables({ filter }?: ListTablesOptions): Promise[]>; /** * List all columns of a given table using PostgreSQL system catalogs. * * @param table - Table name. * @returns Array of `{ field_name, field_type }` rows. * * @example * ```typescript * const columns = await postgis.list_columns('my_layer'); * ``` */ list_columns(table: string): Promise[]>; /** * Query a table with optional column selection, filtering, grouping, * sorting and limiting. * * @param table - Table name. * @param options - Query options. * @returns Array of result rows. * * @example * ```typescript * const rows = await postgis.query_table('parcels', { * columns: 'id, name', * filter: "status = 'active'", * sort: 'name ASC', * limit: 50, * }); * ``` */ query_table(table: string, { columns, filter, group, sort, limit }?: QueryTableOptions): Promise[]>; /** * Calculate the spatial bounding box (extent) of all geometries in a table. * * @param table - Table name. * @param options.geom_column - Geometry column (default `'geom'`). * @param options.srid - Target SRID (default `4326`). * @param options.filter - Optional SQL WHERE clause fragment. * @returns Array containing a single `{ bbox }` row. * * @example * ```typescript * const [result] = await postgis.bbox('parcels'); * console.log(result.bbox); // BOX(lng1 lat1, lng2 lat2) * ``` */ bbox(table: string, { geom_column, srid, filter }?: BboxOptions): Promise[]>; /** * Calculate the centroid (x, y) of each geometry in a table. * * @param table - Table name. * @param options.force_on_surface - Use `ST_PointOnSurface` instead of `ST_Centroid` (default `false`). * @param options.geom_column - Geometry column (default `'geom'`). * @param options.srid - Target SRID (default `'4326'`). * @param options.filter - Optional SQL WHERE clause fragment. * @returns Array of `{ x, y }` rows. */ centroid(table: string, { force_on_surface, geom_column, srid, filter, }?: CentroidOptions): Promise[]>; /** * Find features from `table_from` that are within `distance` of any feature * in `table_to` (using `ST_DWithin`). * * @param table_from - Source table. * @param table_to - Target table. * @param options - Intersection options. * @returns Array of matching feature rows. */ intersect_feature(table_from: string, table_to: string, { columns, distance, geom_column_from, geom_column_to, filter, sort, limit, }?: IntersectFeatureOptions): Promise[]>; /** * Find features in `table` that are within `distance` of the given point. * * @param table - Table name. * @param point - Point string in `"x,y,srid"` format (e.g. `"73.5,14.9,4326"`). * @param options - Intersection options. * @returns Array of matching feature rows. * @throws {Error} if `point` format is invalid. */ intersect_point(table: string, point: string, { columns, distance, geom_column, filter, sort, limit, }?: IntersectPointOptions): Promise[]>; /** * Export features from `table` as a GeoJSON `FeatureCollection`. * * @param table - Table name. * @param options - GeoJSON export options. * @returns A GeoJSON `FeatureCollection`. * * @example * ```typescript * const fc = await postgis.geojson('parcels', { precision: 6 }); * // { type: 'FeatureCollection', features: [...] } * ``` */ geojson(table: string, { bounds, id_column, precision, geom_column, columns, filter }?: GeoJSONOptions): Promise; /** * Export features from `table` as a Geobuf binary buffer. * * @param table - Table name. * @param options - Geobuf export options. * @returns A `Buffer` containing the Geobuf-encoded data. * * @example * ```typescript * const buf = await postgis.geobuf('parcels'); * res.setHeader('Content-Type', 'application/x-protobuf'); * res.send(buf); * ``` */ geobuf(table: string, { bounds, geom_column, columns, filter }?: GeobufOptions): Promise; /** * Generate a Mapbox Vector Tile (MVT) for the given `z/x/y` tile coordinate. * * @param table - Table name. * @param x - Tile X coordinate. * @param y - Tile Y coordinate. * @param z - Zoom level. * @param options - MVT options. * @returns Array containing the raw MVT binary in a `{ mvt }` row. * * @example * ```typescript * const [{ mvt: tile }] = await postgis.mvt('parcels', 0, 0, 0); * res.setHeader('Content-Type', 'application/vnd.mapbox-vector-tile'); * res.send(tile); * ``` */ mvt(table: string, x: number, y: number, z: number, { columns, id_column, geom_column, filter }?: MvtOptions): Promise[]>; /** * Find the nearest features in `table` to a given point, ordered by distance. * * @param table - Table name. * @param point - Point string in `"x,y,srid"` format (e.g. `"73.5,14.9,4326"`). * @param options - Nearest options. * @returns Array of nearest feature rows (each includes a `distance` column). * @throws {Error} if `point` format is invalid. */ nearest(table: string, point: string, { columns, geom_column, filter, limit }?: NearestOptions): Promise[]>; /** * Transform a point from one coordinate reference system to another. * * @param point - Point string in `"x,y,srid"` format (e.g. `"73.5,14.9,4326"`). * @param options.srid - Target SRID (default `4326`). * @returns Array of `{ x, y }` rows with the transformed coordinates. * @throws {Error} if `point` format is invalid. * * @example * ```typescript * const [pt] = await postgis.transform_point('73.5,14.9,4326', { srid: 3857 }); * ``` */ transform_point(point: string, { srid }?: TransformPointOptions): Promise[]>; /** * Execute a raw SQL string against the client and return result rows. * @internal */ private _executeQuery; } export { type BboxOptions, type CentroidOptions, type FeatureCollection, type GeoJSONOptions, type GeobufOptions, type IntersectFeatureOptions, type IntersectPointOptions, type ListTablesOptions, type MvtOptions, type NearestOptions, Postgis, type PostgisClient, type QueryTableOptions, type TransformPointOptions, Postgis as default };