Skip to main content
A FeatureDao provides typed read and write access to a single feature table inside a GeoPackage. Obtain one from an open GeoPackage — do not construct it directly.
const featureDao = geoPackage.getFeatureDao('countries');
FeatureDao extends UserDao, so all generic query helpers from UserDao are also available on a FeatureDao.

Creating a FeatureDao

getFeatureDao()

Call this on an open GeoPackage to get a FeatureDao for the named table.
geoPackage.getFeatureDao(tableName: string): FeatureDao
const featureDao = geoPackage.getFeatureDao('my_features');

Querying rows

queryForAll()

Returns a FeatureResultSet containing every row in the table. Iterate the result set and close it when done.
queryForAll(): FeatureResultSet
const results = featureDao.queryForAll();
try {
  while (results.moveToNext()) {
    const row = results.getRow();
    console.log(row.getId(), row.getGeometry());
  }
} finally {
  results.close();
}
Always close the returned FeatureResultSet — failing to do so leaks the underlying SQLite cursor.

queryForId()

Returns a FeatureResultSet filtered to the single row with the given primary key.
queryForId(id: number): FeatureResultSet
id
number
required
Primary key value of the row to retrieve.
const resultSet = featureDao.queryForId(42);
if (resultSet.moveToNext()) {
  const row = resultSet.getRow();
}
resultSet.close();

queryForIdRow()

Convenience wrapper around queryForId() that returns the first matching FeatureRow directly, or null if not found.
queryForIdRow(id: number): FeatureRow
id
number
required
Primary key value of the row to retrieve.

queryForFieldValues()

Returns a FeatureResultSet for all rows where each field in fieldValues matches.
queryForFieldValues(fieldValues: FieldValues): FeatureResultSet
fieldValues
FieldValues
required
A FieldValues map of column name to expected value.
import { FieldValues } from '@ngageoint/geopackage';

const fields = new FieldValues();
fields.addFieldValue('status', 'active');
const results = featureDao.queryForFieldValues(fields);

query()

Returns a FeatureResultSet using an optional raw SQL WHERE clause and bind arguments.
query(where?: string, whereArgs?: any[]): FeatureResultSet
where
string
SQL WHERE clause, without the WHERE keyword. Omit to return all rows.
whereArgs
any[]
Bind arguments that replace ? placeholders in where.
const results = featureDao.query('population > ?', [1_000_000]);

Counting rows

count()

Returns the total number of rows, optionally filtered by a WHERE clause.
count(where?: string, whereArgs?: any[]): number
const total = featureDao.count();
const active = featureDao.count('status = ?', ['active']);

getCount()

An alternative count helper that accepts the same optional WHERE clause parameters.
getCount(where?: string, whereArgs?: any[]): number

Writing rows

newRow()

Creates and returns a blank FeatureRow backed by this table’s schema. Populate the row then pass it to create().
newRow(): FeatureRow
const row = featureDao.newRow();

create()

Inserts a new row into the table and returns the new row’s id.
create(row: FeatureRow): number
row
FeatureRow
required
A FeatureRow obtained from newRow() with geometry and attribute values set.
return
number
The primary key id of the newly inserted row.
import { GeoPackageGeometryData } from '@ngageoint/geopackage';
import { Point } from '@ngageoint/simple-features-js';

const row = featureDao.newRow();
const geometryData = GeoPackageGeometryData.create(4326, new Point(false, false, -77.03, 38.89));
row.setGeometry(geometryData);
row.setValue('name', 'Washington, D.C.');
const newId = featureDao.create(row);

update()

Updates an existing row in the table. Matches by the row’s primary key.
update(row: FeatureRow): number
row
FeatureRow
required
The modified FeatureRow to persist. The row’s getId() must match an existing record.
return
number
Number of rows updated (should be 1 on success).

deleteRow()

Deletes the row represented by the given FeatureRow.
deleteRow(row: FeatureRow): number
row
FeatureRow
required
The row to delete. Uses the row’s getId() to locate the record.

deleteById()

Deletes the row with the specified primary key.
deleteById(id: number): number
id
number
required
Primary key of the row to delete.
return
number
Number of rows deleted (should be 0 or 1).

Table metadata

getTable()

Returns the underlying FeatureTable schema object describing the table’s columns.
getTable(): FeatureTable

getGeometryColumnName()

Returns the name of the geometry column in this feature table.
getGeometryColumnName(): string

getGeometryType()

Returns the GeometryType enum value declared for this table’s geometry column.
getGeometryType(): GeometryType

FeatureRow

A FeatureRow represents a single row from a feature table. Each row holds geometry data and zero or more attribute columns.

getGeometry()

Returns the GeoPackageGeometryData wrapper containing the encoded geometry, or null if the geometry column is empty.
getGeometry(): GeoPackageGeometryData
const geometryData = row.getGeometry();
const geometry = geometryData?.getGeometry(); // simple-features-js Geometry

setGeometry()

Sets the geometry column value for this row.
setGeometry(geometryData: GeoPackageGeometryData): void
geometryData
GeoPackageGeometryData
required
A GeoPackageGeometryData wrapping the geometry to store.

getGeometryValue()

Convenience method — returns the decoded Geometry object directly from the geometry column, or null.
getGeometryValue(): Geometry

getValue()

Returns the value of the named column for this row.
getValue(columnName: string): any
columnName
string
required
Name of the column to read.
const name = row.getValue('city_name') as string;

setValue()

Sets the value of the named column for this row.
setValue(columnName: string, value: any): void
columnName
string
required
Name of the column to write.
value
any
required
Value to assign. Must be compatible with the column’s declared data type.

getId()

Returns the primary key value of this row.
getId(): number
return
number
The integer primary key for this row.

Full example

import {
  GeoPackageManager,
  GeoPackageGeometryData,
} from '@ngageoint/geopackage';
import { Point } from '@ngageoint/simple-features-js';

const geoPackage = await GeoPackageManager.open('./data.gpkg');
const featureDao = geoPackage.getFeatureDao('cities');

// Read all features
const results = featureDao.queryForAll();
try {
  while (results.moveToNext()) {
    const row = results.getRow();
    console.log(row.getId(), row.getValue('name'), row.getGeometryValue());
  }
} finally {
  results.close();
}

// Write a new feature
const newRow = featureDao.newRow();
newRow.setGeometry(
  GeoPackageGeometryData.create(4326, new Point(false, false, -0.1278, 51.5074))
);
newRow.setValue('name', 'London');
const id = featureDao.create(newRow);
console.log('Created row with id', id);

geoPackage.close();

Build docs developers (and LLMs) love