Skip to content

Commit

Permalink
Merge pull request #505 from digitalearthafrica/aoi_module
Browse files Browse the repository at this point in the history
Update AOI function
  • Loading branch information
mpho-sadiki authored May 21, 2024
2 parents fef933f + 43076fb commit 94433b5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Tools/deafrica_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__locales__ = __path__[0] + '/locales'

__version__ = '2.3.9'
__version__ = '2.4.0'

def set_lang(lang=None):
if lang is None:
Expand Down
47 changes: 38 additions & 9 deletions Tools/deafrica_tools/areaofinterest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from shapely.geometry import box
from geojson import Feature, Point, FeatureCollection

def define_area(lat=None, lon=None, buffer=None, vector_path=None):
def define_area(lat=None, lon=None, buffer=None, lat_buffer=None, lon_buffer=None, vector_path=None):
'''
Define an area of interest using either a point and buffer or a vector.
Define an area of interest using either a point and buffer or separate latitude and longitude buffers, or a vector.
Parameters:
-----------
Expand All @@ -24,7 +24,11 @@ def define_area(lat=None, lon=None, buffer=None, vector_path=None):
lon : float, optional
The longitude of the center point of the area of interest.
buffer : float, optional
The buffer around the center point, in degrees.
The buffer around the center point, in degrees. This is used if separate latitude and longitude buffers are not provided.
lat_buffer : float, optional
The buffer around the center point, extending along the latitude, in degrees.
lon_buffer : float, optional
The buffer around the center point, extending along the longitude, in degrees.
vector_path : str, optional
The path to a vector defining the area of interest.
Expand All @@ -33,12 +37,35 @@ def define_area(lat=None, lon=None, buffer=None, vector_path=None):
feature_collection : dict
A GeoJSON feature collection representing the area of interest.
'''
# Check if both buffer and separate lat/lon buffers are specified
if buffer is not None and (lat_buffer is not None or lon_buffer is not None):
raise ValueError("Specify either buffer or separate lat_buffer and lon_buffer, not both.")

# Check if either lat_buffer or lon_buffer is provided without the other
if (lat_buffer is not None and lon_buffer is None) or (lat_buffer is None and lon_buffer is not None):
raise ValueError("Both lat_buffer and lon_buffer must be provided together.")

# Ensure buffer values are positive
# if negative values are provided for buffer, lat_buffer, or lon_buffer, they will be converted to their absolute values without raising an error.
if buffer is not None:
buffer = abs(buffer)
if lat_buffer is not None:
lat_buffer = abs(lat_buffer)
if lon_buffer is not None:
lon_buffer = abs(lon_buffer)

# Define area using point and buffer
if lat is not None and lon is not None and buffer is not None:
lat_range = (lat - buffer, lat + buffer)
lon_range = (lon - buffer, lon + buffer)
box_geom = box(min(lon_range), min(lat_range), max(lon_range), max(lat_range))
aoi = gpd.GeoDataFrame(geometry=[box_geom], crs='EPSG:4326')
if lat is not None and lon is not None:
if buffer is not None and (lat_buffer is None or lon_buffer is None):
lat_buffer = lon_buffer = buffer

if lat_buffer is not None and lon_buffer is not None:
lat_range = (lat - lat_buffer, lat + lat_buffer)
lon_range = (lon - lon_buffer, lon + lon_buffer)
box_geom = box(min(lon_range), min(lat_range), max(lon_range), max(lat_range))
aoi = gpd.GeoDataFrame(geometry=[box_geom], crs='EPSG:4326')
else:
aoi = gpd.GeoDataFrame(geometry=[Point(lon, lat).buffer(buffer)], crs='EPSG:4326')

# Define area using vector
elif vector_path is not None:
Expand All @@ -51,4 +78,6 @@ def define_area(lat=None, lon=None, buffer=None, vector_path=None):
features = [Feature(geometry=row["geometry"], properties=row.drop("geometry").to_dict()) for _, row in aoi.iterrows()]
feature_collection = FeatureCollection(features)

return feature_collection
return feature_collection


2 changes: 1 addition & 1 deletion Tools/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "deafrica-tools"
version = "2.3.9"
version = "2.4.0"
# reflect version changes in deafrica_tools/__init__.py

description = "Functions and algorithms for analysing Digital Earth Africa data."
Expand Down

0 comments on commit 94433b5

Please sign in to comment.