Utilities Module¶
utils ¶
Utility functions for pyCropWat package.
This module provides utility functions for working with Google Earth Engine, loading geometries from various sources, and managing date ranges.
Functions:
| Name | Description |
|---|---|
initialize_gee |
Initialize Google Earth Engine with optional project ID. |
load_geometry |
Load geometry from local file (shapefile/GeoJSON) or GEE asset. |
load_geometry_from_file |
Load geometry from local shapefile or GeoJSON file. |
load_geometry_from_gee_asset |
Load geometry from GEE FeatureCollection asset. |
is_gee_asset |
Check if a path string represents a GEE asset. |
get_date_range |
Generate date range strings for GEE filtering. |
get_monthly_dates |
Generate list of (year, month) tuples for iteration. |
Example
from pycropwat.utils import initialize_gee, load_geometry
# Initialize GEE with project
initialize_gee(project='my-gee-project')
# Load geometry from local file
geom = load_geometry('study_area.geojson')
# Load geometry from GEE asset
geom = load_geometry(gee_asset='projects/my-project/assets/boundary')
get_date_range ¶
Generate date range strings for filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_year
|
int
|
Start year (inclusive). |
required |
end_year
|
int
|
End year (inclusive). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (start_date, end_date) strings in 'YYYY-MM-DD' format. |
Source code in pycropwat/utils.py
get_monthly_dates ¶
Generate list of (year, month) tuples for the given date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_year
|
int
|
Start year (inclusive). |
required |
end_year
|
int
|
End year (inclusive). |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of (year, month) tuples. |
Source code in pycropwat/utils.py
initialize_gee ¶
Initialize Google Earth Engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project
|
str
|
GEE project ID. If None, uses default authentication. |
None
|
Source code in pycropwat/utils.py
is_gee_asset ¶
Check if a path looks like a GEE asset path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path appears to be a GEE asset path. |
Source code in pycropwat/utils.py
load_geometry ¶
Load a geometry from a local file or GEE FeatureCollection asset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry_source
|
str or Path
|
Path to a local shapefile (.shp) or GeoJSON (.geojson, .json) file, OR a GEE FeatureCollection asset ID. |
required |
gee_asset
|
str
|
Explicit GEE FeatureCollection asset ID. If provided, this takes precedence over geometry_source. |
None
|
Returns:
| Type | Description |
|---|---|
Geometry
|
Earth Engine Geometry object. |
Examples:
Load from local file:
Load from GEE asset:
Auto-detect GEE asset from path:
Source code in pycropwat/utils.py
load_geometry_from_file ¶
Load a geometry from a local shapefile or GeoJSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry_path
|
str or Path
|
Path to the shapefile (.shp) or GeoJSON (.geojson, .json) file. |
required |
Returns:
| Type | Description |
|---|---|
Geometry
|
Earth Engine Geometry object. |
Source code in pycropwat/utils.py
load_geometry_from_gee_asset ¶
Load a geometry from a GEE FeatureCollection asset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_id
|
str
|
GEE FeatureCollection asset ID (e.g., 'projects/my-project/assets/my_boundary'). |
required |
Returns:
| Type | Description |
|---|---|
Geometry
|
Earth Engine Geometry object (dissolved/union of all features). |