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:
|
feature_table
|
Table containing features to count (e.g., buildings).
TYPE:
|
zone_table
|
Table containing zone polygons (e.g., admin3 boundaries).
TYPE:
|
feature_geom
|
Geometry column name for features. Auto-detected if None.
TYPE:
|
zone_geom
|
Geometry column name for zones. Auto-detected if None.
TYPE:
|
zone_id
|
Column to identify zones (default: fid).
TYPE:
|
ensure_index
|
Create rtree index if missing (default: True).
TYPE:
|
| 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
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:
|
points_table
|
Table containing point features.
TYPE:
|
polygon_table
|
Table containing polygon features.
TYPE:
|
points_geom
|
Geometry column name for points. Auto-detected if None.
TYPE:
|
polygon_geom
|
Geometry column name for polygons. Auto-detected if None.
TYPE:
|
polygon_id
|
Column to identify polygons (default: fid).
TYPE:
|
ensure_index
|
Create rtree index on points if missing (default: True).
TYPE:
|
| 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
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:
|
table
|
Table to query.
TYPE:
|
bbox
|
Bounding box as (minx, miny, maxx, maxy).
TYPE:
|
geom_col
|
Geometry column name. Auto-detected if None.
TYPE:
|
ensure_index
|
Create rtree index if missing (default: True).
TYPE:
|
| 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")