Skip to content

Commit

Permalink
implement admin bounding box (#40) and update tile region contruction…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
KMarkert committed Apr 8, 2022
1 parent b57032b commit 355eac7
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions hydrafloods/geeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,23 @@ def constuctGrid(i):

def contructXGrid(j):
j = ee.Number(j)
box = ee.Feature(
box = (
ee.Geometry.Rectangle(
[j, i, j.add(grid_res), i.add(grid_res)],
"epsg:4326",
geodesic=False,
)
)
if contain_geom is not None:
out = ee.Algorithms.If(
contain_geom.contains(box.geometry(), maxError=500), box, None
)
keep = contain_geom.contains(box, maxError=500)

elif intersect_geom is not None:
out = ee.Algorithms.If(
box.intersects(intersect_geom, maxError=500), box, None
)
keep = box.intersects(intersect_geom, maxError=500)

elif centroid_within is not None:
out = ee.Algorithms.If(
box.geometry().centroid().intersects(centroid_within, maxError=500),
box,
None,
)
else:
out = box
return ee.Feature(out)
keep = box.centroid().intersects(centroid_within, maxError=500)

return ee.Feature(box,{"ul_lat":i.add(grid_res), "ul_lon":j, "keep":keep} )

i = ee.Number(i)
out = ee.List.sequence(west, east.subtract(grid_res), grid_res).map(
Expand Down Expand Up @@ -372,7 +365,7 @@ def contructXGrid(j):
ee.List.sequence(south, north.subtract(grid_res), grid_res)
.map(constuctGrid)
.flatten()
)
).filter(ee.Filter.eq("keep",True))

return grid

Expand All @@ -395,3 +388,24 @@ def country_bbox(country_name, max_error=1000):
.geometry(max_error)
.bounds(max_error)
)

def admin_bbox(admin_name, level=0, max_error=1000):
"""Function to get a bounding box geometry of an administrative area
args:
admin_name (str): US-recognized country name
max_error (float,optional): The maximum amount of error tolerated when
performing any necessary reprojection. default = 100
returns:
ee.Geometry: geometry of country bounding box
"""

if level not in range(0,3):
raise ValueError(f"Administrative level is 0-2, provided level {level} is outside of range")

admin_bounds = ee.FeatureCollection("FAO/GAUL/2015/level2")
return (
admin_bounds.filter(ee.Filter.eq(f"ADM{level}_NAME", admin_name))
.geometry(max_error)
.bounds(max_error)
)

0 comments on commit 355eac7

Please sign in to comment.