rslearn.utils.geometry¶
geometry ¶
Spatiotemporal geometry utilities.
Projection ¶
A projection specifies a CRS, x resolution, and y resolution.
The coordinate reference system (CRS) defines the meaning of the coordinates. The resolutions specify the pixels per projection unit, and are used to map pixel coordinates to CRS coordinates.
Source code in rslearn/utils/geometry.py
serialize ¶
Serializes the projection to a JSON-encodable dictionary.
deserialize
staticmethod
¶
deserialize(d: dict) -> Projection
Deserializes a projection from a JSON-decoded dictionary.
Source code in rslearn/utils/geometry.py
ResolutionFactor ¶
Multiplier for the resolution in a Projection.
The multiplier is either an integer x, or the inverse of an integer (1/x).
Factors greater than 1 increase the projection_units/pixel resolution, increasing the resolution (more pixels per projection unit). Factors less than 1 make it coarser (less pixels).
Source code in rslearn/utils/geometry.py
multiply_projection ¶
multiply_projection(projection: Projection) -> Projection
Multiply the projection by this factor.
Source code in rslearn/utils/geometry.py
multiply_bounds ¶
Multiply the bounds by this factor.
When coarsening, the width and height of the given bounds must be a multiple of the denominator.
Source code in rslearn/utils/geometry.py
STGeometry ¶
A spatiotemporal geometry.
Specifiec crs and resolution and corresponding shape in pixel coordinates. Also specifies an optional time range (time range is unlimited if unset).
Source code in rslearn/utils/geometry.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
contains_time ¶
Returns whether this box contains the time.
distance_to_time ¶
Returns the distance from this box to the specified time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
datetime
|
the time to compute distance from |
required |
Returns:
| Type | Description |
|---|---|
timedelta
|
the distance, which is 0 if the box contains the time |
Source code in rslearn/utils/geometry.py
distance_to_time_range ¶
Returns the distance from this geometry to the specified time range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_range
|
tuple[datetime, datetime] | None
|
the time range to compute distance from |
required |
Returns:
| Type | Description |
|---|---|
timedelta
|
the distance, which is 0 if the time ranges intersect |
Source code in rslearn/utils/geometry.py
intersects_time_range ¶
Returns whether this geometry intersects the other time range.
Source code in rslearn/utils/geometry.py
is_global ¶
Returns whether this geometry has global spatial coverage.
Global coverage is indicated by a special geometry with WGS84 projection and corners at (-180, -90, 180, 90) (see get_global_geometry).
Source code in rslearn/utils/geometry.py
is_too_large ¶
Returns whether this geometry's spatial coverage is too large.
This means that it will likely have issues during re-projections and such.
Source code in rslearn/utils/geometry.py
to_wgs84 ¶
to_wgs84() -> STGeometry
Convert to WGS84 with antimeridian splitting handling.
For geometries already in WGS84, this is a no-op.
Source code in rslearn/utils/geometry.py
intersects ¶
intersects(other: STGeometry) -> bool
Returns whether this box intersects the other box.
Source code in rslearn/utils/geometry.py
to_projection ¶
to_projection(projection: Projection) -> STGeometry
Transforms this geometry to the specified projection.
Note that this does not handle antimeridian splitting. Use to_wgs84 when re-projecting to WGS84 to get antimeridian splitting handling.
Source code in rslearn/utils/geometry.py
serialize ¶
Serializes the geometry to a JSON-encodable dictionary.
Source code in rslearn/utils/geometry.py
deserialize
staticmethod
¶
deserialize(d: dict) -> STGeometry
Deserializes a geometry from a JSON-decoded dictionary.
Source code in rslearn/utils/geometry.py
is_same_resolution ¶
shp_intersects ¶
Returns whether the two shapes intersect.
Tries shp.intersects but falls back to shp.intersection which can be more reliable.
Source code in rslearn/utils/geometry.py
get_global_raster_bounds ¶
get_global_raster_bounds(projection: Projection) -> PixelBounds
Get very large pixel bounds for a global raster in the given projection.
This is useful for data sources that cover the entire world and don't want to compute exact bounds in arbitrary projections (which can fail for projections like UTM that only cover part of the world).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projection
|
Projection
|
the projection to get bounds in. |
required |
Returns:
| Type | Description |
|---|---|
PixelBounds
|
Pixel bounds that will intersect with any reasonable window. We assume that the |
PixelBounds
|
absolute value of CRS coordinates is at most 2^32, and adjust it based on the |
PixelBounds
|
resolution in the Projection in case very fine-grained resolutions are used. |
Source code in rslearn/utils/geometry.py
get_global_geometry ¶
get_global_geometry(time_range: tuple[datetime, datetime] | None) -> STGeometry
Gets a geometry that indicates global spatial coverage for the given time range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_range
|
tuple[datetime, datetime] | None
|
the time range for the STGeometry. |
required |
Returns:
| Type | Description |
|---|---|
STGeometry
|
STGeometry with global spatial coverage and specified time range. |
Source code in rslearn/utils/geometry.py
flatten_shape ¶
Flatten the shape into a list of primitive shapes (Point, LineString, and Polygon).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shp
|
Geometry
|
the shape, which could be a primitive shape like polygon or a collection. |
required |
Returns:
| Type | Description |
|---|---|
list[Geometry]
|
list of primitive shapes. |
Source code in rslearn/utils/geometry.py
split_shape_at_antimeridian ¶
Split the given shape at the antimeridian.
The shape must be in WGS84 coordinates.
See split_at_antimeridian for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shp
|
Geometry
|
the shape to split. |
required |
epsilon
|
float
|
the padding in degrees. |
1e-06
|
Returns:
| Type | Description |
|---|---|
Geometry
|
the split shape, in WGS84 projection. |
Source code in rslearn/utils/geometry.py
split_at_antimeridian ¶
split_at_antimeridian(geometry: STGeometry, epsilon: float = 1e-06) -> STGeometry
Split lines and polygons in the given geometry at the antimeridian.
The returned geometry will always be in WGS84 projection.
Small padding is also introduced to ensure coordinates are a bit more than -180 or a bit less than 180.
For example, if the input is a polygon:
Polygon([[-180, 10], [180, 11], [-179, 11], [-179, 10]])
Then it would be converted to:
Polygon([[-179.999999, 10], [-179,999999, 11], [-179, 11], [-179, 10]])
This function may produce unexpected results if the geometries span more than 90 degrees on either dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
STGeometry
|
the geometry to split. |
required |
epsilon
|
float
|
the padding in degrees. It is equivalent to about 1 m at the equator. We ensure no longitude coordinates are within this padding of +/- 180. |
1e-06
|
Returns:
| Type | Description |
|---|---|
STGeometry
|
the padded geometry, in WGS84 projection. |
Source code in rslearn/utils/geometry.py
safely_reproject_within_valid_area ¶
safely_reproject_within_valid_area(src_geoms: Sequence[STGeometry], valid_geom: STGeometry) -> Sequence[STGeometry | None]
Re-project src_geoms into the projection of valid_geom.
Unlike direct to_projection(), this clips each source geometry in WGS84 to a buffered area around valid_geom before reprojecting. This minimizes distortions in case valid_geom is small but src_geoms may be large. It works best if src_geoms are also natively in WGS84; otherwise, there could be distortion issues re-projecting them to WGS84.
Returns a list with one entry per source geometry. The entry is either the re-projected geometry, or it may be (but is not guaranteed to be) None if the source geometry doesn't intersect valid_geom.
Source code in rslearn/utils/geometry.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |