quadkey_utils
This module contains utility functions to work with quadkey geo indexing for the multimno package.
assign_quadkey(sdf, crs_in, zoom_level)
Assigns a quadkey to each row in a DataFrame based on the centroid of its geometry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdf
|
DataFrame
|
The DataFrame to assign quadkeys to. The DataFrame must contain a geometry column. |
required |
crs_in
|
int
|
The CRS of the dataframe to project to 4326 before assigning quadkeys. |
required |
zoom_level
|
int
|
The zoom level to use when assigning quadkeys. |
required |
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
A DataFrame containing the same rows as the input DataFrame, but with an additional quadkey column. |
Source code in multimno/core/quadkey_utils.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
get_children_quadkeys(quadkey, target_level)
Generates all child quadkeys at a specified resolution level for a given quadkey.
This function takes a parent quadkey and a target level of detail, then returns all child quadkeys that are contained within the parent quadkey's area at the specified target level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
quadkey
|
str
|
The parent quadkey. |
required |
target_level
|
int
|
The target level of detail (zoom level) for the child quadkeys. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: A list of all child quadkeys at the specified target level. |
Raises:
Type | Description |
---|---|
ValueError
|
If target_level is less than the level of the input quadkey. |
Source code in multimno/core/quadkey_utils.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
get_quadkeys_for_bbox(extent, level_of_detail)
Generates a list of quadkeys for a bounding box at a specific zoom level.
This function takes a bounding box defined by its lon min, lat min, lon max, and lat max extents, and a zoom level, and generates a list of quadkeys that cover the bounding box at the specified zoom level. The quadkeys are strings of digits that represent specific tiles in a quadtree-based spatial index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent
|
tuple
|
A tuple representing the bounding box. The tuple contains four elements: (west, south, east, north), which are the western, southern, eastern, and northern extents of the bounding box, respectively. Each extent is a float representing a geographic coordinate in degrees. |
required |
level_of_detail
|
int
|
The zoom level. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List[str]
|
A list of quadkeys that cover the bounding box at the specified zoom level. Each quadkey is a string. |
Source code in multimno/core/quadkey_utils.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
latlon_to_quadkey(latitude, longitude, level_of_detail)
Converts a geographic coordinate to a quadkey at a specific zoom level.
This function takes a latitude and longitude in degrees, and a zoom level, and converts them to a quadkey. The quadkey is a string of digits that represents a specific tile in a quadtree-based spatial index. The conversion process involves first converting the geographic coordinate to tile coordinates, and then converting the tile coordinates to a quadkey.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
latitude
|
float
|
The latitude of the geographic coordinate, in degrees. |
required |
longitude
|
float
|
The longitude of the geographic coordinate, in degrees. |
required |
level_of_detail
|
int
|
The zoom level. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The quadkey representing the geographic coordinate at the specified zoom level. |
Source code in multimno/core/quadkey_utils.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
latlon_to_tilexy(latitude, longitude, level_of_detail)
Converts a geographic coordinate to tile coordinates at a specific zoom level.
This function takes a latitude and longitude in degrees, and a zoom level, and converts them to tile coordinates (tile_x, tile_y) at the specified zoom level. The tile coordinates are in the tile system used by Bing Maps, OpenStreetMap, MapBox and other map providers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
latitude
|
float
|
The latitude of the geographic coordinate, in degrees. |
required |
longitude
|
float
|
The longitude of the geographic coordinate, in degrees. |
required |
level_of_detail
|
int
|
The zoom level. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
int
|
A tuple representing the tile coordinates of the geographic coordinate at the specified |
int
|
zoom level. The tuple contains two elements: (tile_x, tile_y). |
Source code in multimno/core/quadkey_utils.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
quadkey_to_extent(quadkey)
Converts a quadkey to a geographic extent (bounding box).
This function takes a quadkey and converts it to a geographic extent represented as a tuple of (longitude_min, latitude_min, longitude_max, latitude_max).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
quadkey
|
str
|
The quadkey to convert. A quadkey is a string of digits that represents a |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
Tuple[float, float, float, float]
|
A tuple representing the geographic extent of the quadkey. The tuple contains four elements: (longitude_min, latitude_min, longitude_max, latitude_max). |
Source code in multimno/core/quadkey_utils.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
quadkey_to_tile(quadkey)
Converts a quadkey to tile coordinates and zoom level.
This function takes a quadkey and converts it to tile coordinates (tile_x, tile_y) and zoom level. A quadkey is a string of digits that represents a specific tile in a quadtree-based spatial index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
quadkey
|
str
|
The quadkey to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
int
|
A tuple representing the tile coordinates and zoom level of the quadkey. The tuple contains three |
elements |
int
|
(tile_x, tile_y, zoom_level). |
Raises:
Type | Description |
---|---|
ValueError
|
If the quadkey contains an invalid character. |
Source code in multimno/core/quadkey_utils.py
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 |
|
quadkeys_to_extent_dataframe(spark, quadkeys, crs=4326)
Converts a list of quadkeys to a Spark DataFrame with geometry polygons representing their extents.
This function takes a list of quadkeys and creates a DataFrame where each row contains a quadkey and its corresponding geometry (polygon) based on the geographic extent. Uses ST_PolygonFromEnvelope for efficient polygon creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spark
|
The Spark session |
required | |
quadkeys
|
List[str]
|
List of quadkeys to convert to geometries |
required |
crs
|
str
|
Coordinate reference system for the output geometries, defaults to WGS84 |
4326
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
A Spark DataFrame with columns 'quadkey' and 'geometry' |
Source code in multimno/core/quadkey_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
tilexy_to_quadkey(x, y, level_of_detail)
Converts tile coordinates to a quadkey at a specific zoom level.
This function takes tile coordinates (x, y) and a zoom level, and converts them to a quadkey. The quadkey is a string of digits that represents a specific tile in a quadtree-based spatial index. The conversion process involves bitwise operations on the tile coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
The x-coordinate of the tile. |
required |
y
|
int
|
The y-coordinate of the tile. |
required |
level_of_detail
|
int
|
The zoom level. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The quadkey representing the tile at the specified zoom level. |
Source code in multimno/core/quadkey_utils.py
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 |
|