geopackage-ts
    Preparing search index...

    Class FeatureDao

    Data Access Object for GeoPackage feature tables.

    Extends UserDao with spatial query capabilities including bounding box queries (with R-tree index support), GeoJSON conversion, and geometry metadata access. This is the primary class for reading and writing spatial features in a GeoPackage.

    const featureDao = new FeatureDao(conn, featureTable, geometryColumns);

    // Iterate over all features as GeoJSON
    for (const feature of featureDao.queryForGeoJSON()) {
    console.log(feature.properties);
    }

    // Spatial query with bounding box
    const bbox = { minX: -180, minY: -90, maxX: 180, maxY: 90 };
    for (const feature of featureDao.queryForGeoJSONWithBoundingBox(bbox)) {
    console.log(feature.geometry);
    }

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    The underlying database connection.

    table: UserTable

    The user table schema that this DAO operates on.

    Accessors

    • get geometryColumnName(): string

      Gets the name of the geometry column in this feature table.

      Returns string

      The geometry column name (e.g., "geometry" or "geom").

    • get geometryType(): string

      Gets the geometry type name for this feature table.

      Returns string

      The geometry type as defined in the gpkg_geometry_columns table (e.g., "POINT", "LINESTRING", "POLYGON", "GEOMETRY").

    • get tableName(): string

      Gets the name of the table managed by this DAO.

      Returns string

      The table name string.

    Methods

    • Counts rows in the table, optionally filtered by a WHERE clause.

      Parameters

      • Optionalwhere: string

        An optional SQL WHERE clause (without the WHERE keyword).

      • ...params: unknown[]

        Bind parameters for placeholders in the WHERE clause.

      Returns number

      The count of matching rows.

      const total = dao.count();
      const active = dao.count('"status" = ?', 1);
    • Deletes a single row by its primary key value.

      Parameters

      • id: number

        The primary key value of the row to delete.

      Returns number

      The number of rows deleted (0 or 1).

      const deleted = dao.deleteById(42);
      console.log(deleted ? 'Row deleted' : 'Row not found');
    • Deletes rows matching a SQL WHERE clause.

      Parameters

      • where: string

        A SQL WHERE clause (without the WHERE keyword).

      • ...params: unknown[]

        Bind parameters for placeholders in the WHERE clause.

      Returns number

      The number of rows deleted.

      const deleted = dao.deleteWhere('"status" = ?', 0);
      console.log(`Deleted ${deleted} row(s)`);
    • Retrieves the bounding box for the entire feature table from the gpkg_contents metadata.

      Returns BoundingBox | null

      The BoundingBox of the table's spatial extent, or null if the bounding box is not defined in gpkg_contents.

      const bbox = featureDao.getBoundingBox();
      if (bbox) {
      console.log(`Extent: [${bbox.minX}, ${bbox.minY}] to [${bbox.maxX}, ${bbox.maxY}]`);
      }
    • Gets the Spatial Reference System ID for the geometry column.

      Returns number

      The SRS ID (e.g., 4326 for WGS 84).

    • Inserts a new row into the table.

      The row's values are inserted into the corresponding columns. The primary key value from the row is typically omitted to allow AUTOINCREMENT to assign an ID automatically.

      Parameters

      • row: UserRow

        The UserRow to insert. The values record determines which columns are set.

      Returns number

      The last inserted row ID (the auto-generated primary key).

      const row = createUserRow(table, { name: 'New Feature' });
      const newId = dao.insert(row);
      console.log(`Inserted row with ID: ${newId}`);
    • Queries a single row by its primary key value.

      Type Parameters

      • T = Record<string, unknown>

        The expected row type. Defaults to Record<string, unknown>.

      Parameters

      • id: number

        The primary key value to look up.

      Returns T | undefined

      The matching row, or undefined if no row with the given ID exists.

      const row = dao.queryById(42);
      if (row) {
      console.log('Found:', row);
      }
    • Queries all rows from the table.

      Returns a lazy iterable iterator that yields rows one at a time, which is memory-efficient for large tables.

      Type Parameters

      • T = Record<string, unknown>

        The expected row type. Defaults to Record<string, unknown>.

      Returns IterableIterator<T>

      An iterable iterator over all rows in the table.

      for (const row of dao.queryForAll()) {
      console.log(row);
      }
    • Queries all features and yields them as GeoJSON Feature objects.

      Iterates over every row in the feature table, converting each row's geometry blob to a GeoJSON geometry and mapping non-geometry, non-primary-key columns to the feature's properties.

      Returns IterableIterator<Feature<Geometry, GeoJsonProperties>>

      A GeoJSON Feature for each row in the table.

      const features: GeoJSON.Feature[] = [];
      for (const feature of featureDao.queryForGeoJSON()) {
      features.push(feature);
      }
    • Queries features within a bounding box and yields them as GeoJSON Feature objects.

      Combines the spatial filtering of queryWithBoundingBox with the GeoJSON conversion of queryForGeoJSON, yielding GeoJSON Features for all rows whose geometries intersect the given bounding box.

      Parameters

      • bbox: BoundingBox

        The bounding box to filter features against.

      Returns IterableIterator<Feature<Geometry, GeoJsonProperties>>

      A GeoJSON Feature for each matching row.

      const bbox: BoundingBox = { minX: -74.1, minY: 40.6, maxX: -73.9, maxY: 40.8 };
      for (const feature of featureDao.queryForGeoJSONWithBoundingBox(bbox)) {
      console.log(feature.geometry.type, feature.properties);
      }
    • Queries rows matching a SQL WHERE clause.

      Returns a lazy iterable iterator that yields matching rows one at a time.

      Type Parameters

      • T = Record<string, unknown>

        The expected row type. Defaults to Record<string, unknown>.

      Parameters

      • where: string

        A SQL WHERE clause (without the WHERE keyword).

      • ...params: unknown[]

        Bind parameters for placeholders in the WHERE clause.

      Returns IterableIterator<T>

      An iterable iterator over the matching rows.

      for (const row of dao.queryWhere('"status" = ?', 1)) {
      console.log(row);
      }
    • Queries features that intersect a bounding box.

      If an R-tree spatial index exists for the feature table (named rtree_{tableName}_{geometryColumnName}), it is used for efficient spatial filtering via an index join. Otherwise, a full table scan is performed with envelope-based intersection checks on each geometry blob.

      Parameters

      • bbox: BoundingBox

        The bounding box to filter features against.

      Returns IterableIterator<Record<string, unknown>>

      Raw row records that intersect the bounding box.

      const bbox: BoundingBox = { minX: -74.1, minY: 40.6, maxX: -73.9, maxY: 40.8 };
      for (const row of featureDao.queryWithBoundingBox(bbox)) {
      console.log(row);
      }
    • Updates an existing row in the table, identified by its primary key.

      All columns present in row.values (except the primary key column itself) are updated. The primary key column value is used in the WHERE clause to identify the target row.

      Parameters

      • row: UserRow

        The UserRow containing updated values. Must include the primary key value.

      Returns number

      The number of rows affected by the update (typically 0 or 1).

      const row = createUserRow(table, { id: 42, name: 'Updated Name' });
      const affected = dao.update(row);
      console.log(`Updated ${affected} row(s)`);