Skip to content

Commit

Permalink
Change order of operations for get_tiles to speed things up (#72)
Browse files Browse the repository at this point in the history
* Change order of operations for get_tiles to speed things up

* Fix formatting

* Fix bug with crs

* log python versions

* Remove proj:projjson, which is removed in rio-stac 0.10.0
  • Loading branch information
alexgleith authored Nov 10, 2024
1 parent 028f23a commit 5166849
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
build-essential
pip3 install --upgrade pip setuptools wheel poetry
pip3 install .
pip3 freeze
- name: Run tests
run: |
Expand Down
24 changes: 15 additions & 9 deletions dep_tools/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def get_tiles(

def grid(
resolution: int | float = 30,
simplify_tolerance: float = 0.1,
crs=PACIFIC_EPSG,
return_type: Literal["GridSpec", "GeoSeries", "GeoDataFrame"] = "GridSpec",
intersect_with: GeoDataFrame | None = None,
Expand Down Expand Up @@ -121,16 +122,21 @@ def grid(
return _intersect_grid(full_grid, intersect_with)
else:
gridspec = _gridspec(resolution, crs)
geometry = Geometry(loads(intersect_with.to_json()))
# This is a bit of a hack, but it works. Geometries that are transformed by the tiles_from_geopolygon
# are not valid, but doing the simplification and buffer fixes them.
buffer = 0.0 if buffer_distance is None else buffer_distance
fixed = (
geometry.to_crs(PACIFIC_EPSG, check_and_fix=True, wrapdateline=True)
.simplify(0.01)
.buffer(buffer)
simplified = (
intersect_with.to_crs(PACIFIC_EPSG)
.simplify(simplify_tolerance)
.to_frame()
.to_geo_dict()
)
return gridspec.tiles_from_geopolygon(geopolygon=fixed)
geometry = Geometry(
simplified,
crs=PACIFIC_EPSG,
)
if buffer_distance is not None:
geometry = geometry.buffer(buffer_distance)
else:
geometry = geometry.buffer(0.0)
return gridspec.tiles_from_geopolygon(geopolygon=geometry)

return {
"GridSpec": _gridspec,
Expand Down
2 changes: 1 addition & 1 deletion dep_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def bbox_across_180(region: GeoDataFrame | GeoBox) -> BBOX | tuple[BBOX, BBOX]:
geometry = region.to_crs(4326).geometry.make_valid().explode()
geometry = geometry[
geometry.geom_type.isin(["Polygon", "MultiPolygon"])
].unary_union
].union_all()

geometry = _fix_geometry(geometry)
bbox = antimeridian_bbox(geometry)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_get_stac_item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import rioxarray

from dep_tools.namers import LocalPath
from dep_tools.stac_utils import StacCreator
from dep_tools.writers import StacWriter

from dep_tools.stac_utils import get_stac_item
from pathlib import Path
Expand Down Expand Up @@ -41,7 +39,6 @@ def test_get_stac_item_properties(stac_item):
"proj:bbox",
"proj:shape",
"proj:transform",
"proj:projjson",
]
assert all([key in properties.keys() for key in keys])

Expand Down
2 changes: 0 additions & 2 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_get_gadm():

def test_get_tiles():
# This takes 2 minutes to retrieve all the tiles. Keep it as a generator.
print("a")
tiles = get_tiles(resolution=30)

# Let's just check one
Expand All @@ -23,7 +22,6 @@ def test_get_tiles():
assert type(tile) is tuple
assert geobox.crs == PACIFIC_EPSG

print("b")
# Check the count here, shouldn't take too long
tiles = list(get_tiles(resolution=30, country_codes=["FJI"]))
assert len(tiles) == 27
Expand Down

0 comments on commit 5166849

Please sign in to comment.