Skip to content

query

count_in_zones

count_in_zones(con: Connection, feature_table: str, zone_table: str, feature_geom: Optional[str] = None, zone_geom: Optional[str] = None, zone_id: str = 'fid', ensure_index: bool = True) -> list[tuple[int, int]]

Count features inside each zone using rtree spatial index.

For each zone polygon, counts how many features from feature_table fall inside it. Uses rtree bbox pre-filter + ST_Contains for speed.

PARAMETER DESCRIPTION
con

SQLite connection with SpatiaLite loaded.

TYPE: Connection

feature_table

Table containing features to count (e.g., buildings).

TYPE: str

zone_table

Table containing zone polygons (e.g., admin3 boundaries).

TYPE: str

feature_geom

Geometry column name for features. Auto-detected if None.

TYPE: Optional[str] DEFAULT: None

zone_geom

Geometry column name for zones. Auto-detected if None.

TYPE: Optional[str] DEFAULT: None

zone_id

Column to identify zones (default: fid).

TYPE: str DEFAULT: 'fid'

ensure_index

Create rtree index if missing (default: True).

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
list[tuple[int, int]]

List of (zone_id_value, feature_count) tuples.

Example

con = connect("mashriq.gpkg") counts = count_in_zones(con, "OSM_Buildings", "lbn_adm3") for zone_id, count in counts[:5]: ... print(f"Zone {zone_id}: {count} buildings")

Source code in geopkgtoolkit/query.py
def count_in_zones(
    con: sqlite3.Connection,
    feature_table: str,
    zone_table: str,
    feature_geom: Optional[str] = None,
    zone_geom: Optional[str] = None,
    zone_id: str = "fid",
    ensure_index: bool = True,
) -> list[tuple[int, int]]:
    """Count features inside each zone using rtree spatial index.

    For each zone polygon, counts how many features from feature_table
    fall inside it. Uses rtree bbox pre-filter + ST_Contains for speed.

    Args:
        con: SQLite connection with SpatiaLite loaded.
        feature_table: Table containing features to count (e.g., buildings).
        zone_table: Table containing zone polygons (e.g., admin3 boundaries).
        feature_geom: Geometry column name for features. Auto-detected if None.
        zone_geom: Geometry column name for zones. Auto-detected if None.
        zone_id: Column to identify zones (default: fid).
        ensure_index: Create rtree index if missing (default: True).

    Returns:
        List of (zone_id_value, feature_count) tuples.

    Example:
        >>> con = connect("mashriq.gpkg")
        >>> counts = count_in_zones(con, "OSM_Buildings", "lbn_adm3")
        >>> for zone_id, count in counts[:5]:
        ...     print(f"Zone {zone_id}: {count} buildings")
    """
    if feature_geom is None:
        feature_geom = _get_geom_col(con, feature_table)
    if zone_geom is None:
        zone_geom = _get_geom_col(con, zone_table)

    rtree_table = _get_rtree_table(feature_table, feature_geom)

    # Ensure rtree index exists
    if ensure_index and not _rtree_exists(con, feature_table, feature_geom):
        _create_rtree(con, feature_table, feature_geom)

    has_rtree = _rtree_exists(con, feature_table, feature_geom)

    # Use SQL ST_MinX/ST_MaxX instead of Python Envelope().bounds
    # because Envelope() returns (0,0,0,0) on GeoPackage blobs through SpatiaLite.
    zones = con.execute(
        f"SELECT [{zone_id}], [{zone_geom}], "
        f"ST_MinX([{zone_geom}]), ST_MinY([{zone_geom}]), "
        f"ST_MaxX([{zone_geom}]), ST_MaxY([{zone_geom}]) "
        f"FROM [{zone_table}]"
    ).fetchall()
    results = []

    for zid, zone_geom_blob, minx, miny, maxx, maxy in zones:
        if zone_geom_blob is None:
            results.append((zid, 0))
            continue

        # Skip degenerate envelopes (empty or null extent)
        if minx is None or miny is None or maxx is None or maxy is None:
            results.append((zid, 0))
            continue
        if minx == maxx and miny == maxy:
            # Degenerate zone (point or line), skip rtree pre-filter
            count = con.execute(
                f"SELECT COUNT(*) FROM [{feature_table}] "
                f"WHERE ST_Contains(?, [{feature_geom}])",
                (zone_geom_blob,),
            ).fetchone()[0]
            results.append((zid, count))
            continue

        if has_rtree:
            count = con.execute(
                f"""
                SELECT COUNT(*) FROM [{feature_table}] ft
                JOIN [{rtree_table}] r ON ft.fid = r.id
                WHERE r.maxx >= ? AND r.minx <= ?
                  AND r.maxy >= ? AND r.miny <= ?
                  AND ST_Contains(?, ft.[{feature_geom}])
                """,
                (minx, maxx, miny, maxy, zone_geom_blob),
            ).fetchone()[0]
        else:
            # Fallback: full table scan with ST_Contains
            count = con.execute(
                f"""
                SELECT COUNT(*) FROM [{feature_table}]
                WHERE ST_Contains(?, [{feature_geom}])
                """,
                (zone_geom_blob,),
            ).fetchone()[0]

        results.append((zid, count))

    return results

points_in_polygons

points_in_polygons(con: Connection, points_table: str, polygon_table: str, points_geom: Optional[str] = None, polygon_geom: Optional[str] = None, polygon_id: str = 'fid', ensure_index: bool = True) -> list[tuple[int, Optional[int]]]

Classify points into containing polygons.

Loads polygon bounding boxes into Python memory first, then uses them as a cheap pre-filter before calling the expensive ST_Contains. This reduces SQL round-trips by ~95% on real datasets.

PARAMETER DESCRIPTION
con

SQLite connection with SpatiaLite loaded.

TYPE: Connection

points_table

Table containing point features.

TYPE: str

polygon_table

Table containing polygon features.

TYPE: str

points_geom

Geometry column name for points. Auto-detected if None.

TYPE: Optional[str] DEFAULT: None

polygon_geom

Geometry column name for polygons. Auto-detected if None.

TYPE: Optional[str] DEFAULT: None

polygon_id

Column to identify polygons (default: fid).

TYPE: str DEFAULT: 'fid'

ensure_index

Create rtree index on points if missing (default: True).

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
list[tuple[int, Optional[int]]]

List of (point_fid, polygon_id_value) tuples. Points not inside

list[tuple[int, Optional[int]]]

any polygon have polygon_id_value of None.

Example

con = connect("data.gpkg") result = points_in_polygons(con, "POIs", "districts") for poi_id, district_id in result[:5]: ... print(f"POI {poi_id} is in district {district_id}")

Source code in geopkgtoolkit/query.py
def points_in_polygons(
    con: sqlite3.Connection,
    points_table: str,
    polygon_table: str,
    points_geom: Optional[str] = None,
    polygon_geom: Optional[str] = None,
    polygon_id: str = "fid",
    ensure_index: bool = True,
) -> list[tuple[int, Optional[int]]]:
    """Classify points into containing polygons.

    Loads polygon bounding boxes into Python memory first, then uses
    them as a cheap pre-filter before calling the expensive ST_Contains.
    This reduces SQL round-trips by ~95% on real datasets.

    Args:
        con: SQLite connection with SpatiaLite loaded.
        points_table: Table containing point features.
        polygon_table: Table containing polygon features.
        points_geom: Geometry column name for points. Auto-detected if None.
        polygon_geom: Geometry column name for polygons. Auto-detected if None.
        polygon_id: Column to identify polygons (default: fid).
        ensure_index: Create rtree index on points if missing (default: True).

    Returns:
        List of (point_fid, polygon_id_value) tuples. Points not inside
        any polygon have polygon_id_value of None.

    Example:
        >>> con = connect("data.gpkg")
        >>> result = points_in_polygons(con, "POIs", "districts")
        >>> for poi_id, district_id in result[:5]:
        ...     print(f"POI {poi_id} is in district {district_id}")
    """
    if points_geom is None:
        points_geom = _get_geom_col(con, points_table)
    if polygon_geom is None:
        polygon_geom = _get_geom_col(con, polygon_table)

    # Load polygons with bounding boxes for Python-level pre-filtering.
    # Bbox check is a cheap float comparison that eliminates ~95% of
    # candidates before the expensive ST_Contains SQL call.
    polygons = con.execute(
        f"SELECT [{polygon_id}], [{polygon_geom}], "
        f"ST_MinX([{polygon_geom}]), ST_MinY([{polygon_geom}]), "
        f"ST_MaxX([{polygon_geom}]), ST_MaxY([{polygon_geom}]) "
        f"FROM [{polygon_table}]"
    ).fetchall()

    # Load points with coordinates
    points = con.execute(
        f"SELECT fid, [{points_geom}], "
        f"ST_X([{points_geom}]), ST_Y([{points_geom}]) "
        f"FROM [{points_table}]"
    ).fetchall()

    results = []
    for pt_fid, pt_geom, ptx, pty in points:
        if pt_geom is None or ptx is None or pty is None:
            results.append((pt_fid, None))
            continue

        matched = None
        for poly_id, poly_geom, minx, miny, maxx, maxy in polygons:
            # Cheap bbox pre-filter: skip polygons whose bbox
            # doesn't contain this point.
            if minx is None or ptx < minx or ptx > maxx or pty < miny or pty > maxy:
                continue
            try:
                if con.execute(
                    "SELECT ST_Contains(?, ?)", (poly_geom, pt_geom)
                ).fetchone()[0]:
                    matched = poly_id
                    break
            except Exception:
                continue

        results.append((pt_fid, matched))

    return results

bbox_filter

bbox_filter(con: Connection, table: str, bbox: tuple[float, float, float, float], geom_col: Optional[str] = None, ensure_index: bool = True) -> list[int]

Return feature IDs within a bounding box.

Uses rtree spatial index for fast bbox filtering.

PARAMETER DESCRIPTION
con

SQLite connection with SpatiaLite loaded.

TYPE: Connection

table

Table to query.

TYPE: str

bbox

Bounding box as (minx, miny, maxx, maxy).

TYPE: tuple[float, float, float, float]

geom_col

Geometry column name. Auto-detected if None.

TYPE: Optional[str] DEFAULT: None

ensure_index

Create rtree index if missing (default: True).

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
list[int]

List of feature IDs (fid values) within the bbox.

Example

con = connect("data.gpkg") fids = bbox_filter(con, "buildings", (35.4, 33.8, 35.6, 33.9)) print(f"{len(fids)} buildings in bbox")

Source code in geopkgtoolkit/query.py
def bbox_filter(
    con: sqlite3.Connection,
    table: str,
    bbox: tuple[float, float, float, float],
    geom_col: Optional[str] = None,
    ensure_index: bool = True,
) -> list[int]:
    """Return feature IDs within a bounding box.

    Uses rtree spatial index for fast bbox filtering.

    Args:
        con: SQLite connection with SpatiaLite loaded.
        table: Table to query.
        bbox: Bounding box as (minx, miny, maxx, maxy).
        geom_col: Geometry column name. Auto-detected if None.
        ensure_index: Create rtree index if missing (default: True).

    Returns:
        List of feature IDs (fid values) within the bbox.

    Example:
        >>> con = connect("data.gpkg")
        >>> fids = bbox_filter(con, "buildings", (35.4, 33.8, 35.6, 33.9))
        >>> print(f"{len(fids)} buildings in bbox")
    """
    if geom_col is None:
        geom_col = _get_geom_col(con, table)

    if ensure_index and not _rtree_exists(con, table, geom_col):
        _create_rtree(con, table, geom_col)

    rtree_table = _get_rtree_table(table, geom_col)
    minx, miny, maxx, maxy = bbox

    has_rtree = _rtree_exists(con, table, geom_col)

    if has_rtree:
        rows = con.execute(
            f"""
            SELECT ft.fid FROM [{table}] ft
            JOIN [{rtree_table}] r ON ft.fid = r.id
            WHERE r.maxx >= ? AND r.minx <= ?
              AND r.maxy >= ? AND r.miny <= ?
            """,
            (minx, maxx, miny, maxy),
        ).fetchall()
    else:
        rows = con.execute(
            f"SELECT fid FROM [{table}] "
            f"WHERE ST_MinX([{geom_col}]) >= ? AND ST_MaxX([{geom_col}]) <= ? "
            f"AND ST_MinY([{geom_col}]) >= ? AND ST_MaxY([{geom_col}]) <= ?",
            (minx, maxx, miny, maxy),
        ).fetchall()

    return [r[0] for r in rows]