Skip to content

Commit 67a8d88

Browse files
authored
Merge pull request #15 from terrafloww/patchfix/aws_creds_issues
patch fix aws creds and add uv package manger
2 parents 66c56c4 + 85dce4b commit 67a8d88

File tree

10 files changed

+1889
-197
lines changed

10 files changed

+1889
-197
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ on:
44
branches:
55
- main
66
paths:
7-
- requirements.txt
7+
- pyproject.toml
8+
- uv.lock
89
- 'src/**'
910
- 'examples/**'
1011
pull_request:
1112
branches:
1213
- main
1314
paths:
14-
- requirements.txt
15+
- pyproject.toml
16+
- uv.lock
1517
- 'src/**'
1618
- 'examples/**'
1719

@@ -21,32 +23,33 @@ jobs:
2123

2224
strategy:
2325
matrix:
24-
python-version: ["3.11"]
26+
python-version: ["3.10", "3.11"]
2527

2628
steps:
2729
- name: Checkout repository
2830
uses: actions/checkout@v4
2931

32+
- name: Install uv
33+
uses: astral-sh/setup-uv@v6
34+
3035
- name: Set up Python ${{ matrix.python-version }}
3136
uses: actions/setup-python@v4
3237
with:
3338
python-version: ${{ matrix.python-version }}
3439

3540
- name: Install dependencies
3641
run: |
37-
python -m pip install --upgrade pip
38-
pip install uv
39-
uv pip install -r requirements.txt --system
40-
pip install -e .[dev]
42+
uv sync --all-extras
43+
uv build
4144
4245
- name: Lint with ruff
4346
run: |
44-
ruff check .
47+
uvx ruff check .
4548
4649
- name: Format with black
4750
run: |
48-
black --check .
51+
uvx black --check .
4952
5053
- name: Run tests
5154
run: |
52-
pytest --cov=rasteret
55+
uv run pytest --cov=rasteret

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export AWS_SECRET_ACCESS_KEY='your_secret_key'
167167

168168
## 🔧 Installation
169169
```bash
170-
pip install rasteret
170+
# once cloned
171+
uv sync
171172
```
172173

173174
<br>
@@ -186,6 +187,8 @@ from rasteret import Rasteret
186187
from rasteret.constants import DataSources
187188
from rasteret.core.utils import save_per_geometry
188189

190+
import xarray as xr
191+
189192
aoi1_polygon = Polygon([
190193
(77.55, 13.01),
191194
(77.58, 13.01),
@@ -194,16 +197,8 @@ aoi1_polygon = Polygon([
194197
(77.55, 13.01)
195198
])
196199

197-
aoi2_polygon = Polygon([
198-
(77.56, 13.02),
199-
(77.59, 13.02),
200-
(77.59, 13.09),
201-
(77.56, 13.09),
202-
(77.56, 13.02)
203-
])
204-
205200
# Get the total bounds of all polygons above
206-
bbox = aoi1_polygon.union(aoi2_polygon).bounds
201+
bbox = aoi1_polygon.bounds
207202
# OR
208203
# give even larger AOI bounds that covers all your future analysis areas
209204
# eg., Polygon of a State or a Country
@@ -223,10 +218,10 @@ in your workspace directory, if they were created earlier.
223218
custom_name = "bangalore"
224219

225220
# here we are aiming to write 1 year worth of STAC metadata and COG file headers to local disk
226-
date_range = ("2024-01-01", "2024-12-31")
221+
date_range = ("2025-01-01", "2025-01-31")
227222

228223
# choose from LANDSAT / SENTINEL2
229-
data_source = DataSources.LANDSAT
224+
data_source = DataSources.SENTINEL2
230225

231226
# Set up workspace folder as you wish
232227
workspace_dir = Path.home() / "rasteret_workspace"
@@ -235,6 +230,7 @@ workspace_dir.mkdir(exist_ok=True)
235230
# List existing collections if there are any in the workspace folder
236231
collections = Rasteret.list_collections(workspace_dir=workspace_dir)
237232
for c in collections:
233+
print(f"\nExisting Collection in workspace dir {workspace_dir}:")
238234
print(f"- {c['name']}: {c['data_source']}, {c['date_range']}, {c['size']} scenes")
239235
```
240236

@@ -250,9 +246,9 @@ except ValueError:
250246
# Instantiate the Class
251247
processor = Rasteret(
252248
workspace_dir=workspace_dir,
253-
custom_name="bangalore",
254-
data_source=DataSources.LANDSAT,
255-
date_range=("2024-01-01", "2024-01-31")
249+
custom_name=custom_name,
250+
data_source=data_source,
251+
date_range=date_range
256252
)
257253

258254
# and create a new collection
@@ -263,7 +259,6 @@ except ValueError:
263259
processor.create_collection(
264260
bbox=bbox,
265261
cloud_cover_lt=20,
266-
platform={"in": ["LANDSAT_8"]}
267262
)
268263
```
269264

@@ -273,10 +268,9 @@ except ValueError:
273268
# Now we can query the collection created above, to get the data we want
274269
# in this case 2 geometries, 2 bands, and a few PySTAC search filters are provided
275270
ds = processor.get_xarray(
276-
geometries=[aoi1_polygon,aoi2_polygon],
277-
bands=["B4", "B5"],
271+
geometries=[aoi1_polygon],
272+
bands=["B04", "B08"],
278273
cloud_cover_lt=20,
279-
date_range=["2024-01-10", "2024-01-30"]
280274
)
281275
# this returns an xarray dataset variable "ds" with the data for the geometries and bands specified
282276
# behind the scenes, the library is efficiently filtering the local STAC geoparquet,
@@ -286,7 +280,10 @@ ds = processor.get_xarray(
286280
# and creating a xarray dataset for each geometry and its time series data
287281

288282
# Calculate NDVI
289-
ndvi_ds = (ds.B5 - ds.B4) / (ds.B5 + ds.B4)
283+
ndvi = (ds.B08 - ds.B04) / (ds.B08 + ds.B04)
284+
285+
# for LANDSAT satellite
286+
# ndvi = (ds.B5 - ds.B4) / (ds.B5 + ds.B4)
290287

291288
# give a data variable name for NDVI array
292289
ndvi_ds = xr.Dataset(

examples/basic_workflow_xarray.py renamed to examples/basic_workflow_xarray_landsat_paid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def main():
1414
workspace_dir = Path.home() / "rasteret_workspace"
1515
workspace_dir.mkdir(exist_ok=True)
1616

17-
custom_name = "bangalore"
18-
date_range = ("2024-01-01", "2024-03-31")
17+
custom_name = "bangalore_landsat"
18+
date_range = ("2024-01-01", "2024-01-31")
1919
data_source = DataSources.LANDSAT
2020

2121
# Define area and time of interest
@@ -34,6 +34,7 @@ def main():
3434
# "bangalore_202401-03_landsat", if date_range spans across years, it will be "bangalore_202401-202503_landsat"
3535
collections = Rasteret.list_collections(workspace_dir=workspace_dir)
3636
for c in collections:
37+
print(f"Existing collection in workspace dir {workspace_dir} :")
3738
print(
3839
f"- {c['name']}: {c['data_source']}, {c['date_range']}, {c['size']} scenes"
3940
)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from pathlib import Path
2+
from shapely.geometry import Polygon
3+
4+
from rasteret import Rasteret
5+
from rasteret.constants import DataSources
6+
from rasteret.core.utils import save_per_geometry
7+
8+
import xarray as xr
9+
10+
aoi1_polygon = Polygon(
11+
[(77.55, 13.01), (77.58, 13.01), (77.58, 13.08), (77.55, 13.08), (77.55, 13.01)]
12+
)
13+
14+
15+
# Get the total bounds of all polygons above
16+
bbox = aoi1_polygon.bounds
17+
# OR
18+
# give even larger AOI bounds that covers all your future analysis areas
19+
# eg., Polygon of a State or a Country
20+
# bbox = country_polygon.bounds
21+
22+
# Collection configuration
23+
24+
# give your custom name for local collection, it will be attached to the
25+
# beginning of the collection name for eg., bangalore_202401-12_landsat
26+
custom_name = "bangalore"
27+
28+
# here we are aiming to write 1 year worth of STAC metadata and COG file headers to local disk
29+
date_range = ("2024-01-01", "2024-01-31")
30+
31+
# choose from LANDSAT / SENTINEL2
32+
data_source = DataSources.SENTINEL2
33+
34+
# Set up workspace folder as you wish
35+
workspace_dir = Path.home() / "rasteret_workspace"
36+
workspace_dir.mkdir(exist_ok=True)
37+
38+
# List existing collections if there are any in the workspace folder
39+
collections = Rasteret.list_collections(workspace_dir=workspace_dir)
40+
for c in collections:
41+
print(f"\nExisting Collection in workspace dir {workspace_dir}:")
42+
print(f"- {c['name']}: {c['data_source']}, {c['date_range']}, {c['size']} scenes")
43+
44+
# Try loading existing collection
45+
try:
46+
# example name given here
47+
processor = Rasteret.load_collection(
48+
"bangalore_202401-12_landsat", workspace_dir=workspace_dir
49+
)
50+
except ValueError:
51+
52+
# Instantiate the Class
53+
processor = Rasteret(
54+
workspace_dir=workspace_dir,
55+
custom_name=custom_name,
56+
data_source=data_source,
57+
date_range=date_range,
58+
)
59+
60+
# and create a new collection
61+
62+
# we are giving the BBOX for which STAC items and their COG headers will be fetched
63+
# and also filtering using PySTAC filters for LANDSAT 8 platform specifically
64+
# from LANDSAT USGS STAC, and giving a scene level cloud-cover filter
65+
processor.create_collection(
66+
bbox=bbox,
67+
cloud_cover_lt=20,
68+
)
69+
70+
# Now we can query the collection created above, to get the data we want
71+
# in this case 2 geometries, 2 bands, and a few PySTAC search filters are provided
72+
ds = processor.get_xarray(
73+
geometries=[aoi1_polygon],
74+
bands=["B04", "B08"],
75+
cloud_cover_lt=20,
76+
date_range=["2024-01-10", "2024-01-30"],
77+
)
78+
# this returns an xarray dataset variable "ds" with the data for the geometries and bands specified
79+
# behind the scenes, the library is efficiently filtering the local STAC geoparquet,
80+
# for the LANDSAT scenes that pass the filters and dates provided
81+
# then its getting the tif urls of the requested bands
82+
# then grabbing COG tiles only for the geometries from those tif files
83+
# and creating a xarray dataset for each geometry and its time series data
84+
85+
# Calculate NDVI
86+
ndvi = (ds.B08 - ds.B04) / (ds.B08 + ds.B04)
87+
88+
# for LANDSAT satellite
89+
# ndvi_ds = (ds.B5 - ds.B4) / (ds.B5 + ds.B4)
90+
91+
# give a data variable name for NDVI array
92+
ndvi_ds = xr.Dataset(
93+
{"NDVI": ndvi},
94+
coords=ds.coords,
95+
attrs=ds.attrs,
96+
)
97+
98+
# create a output folder if you wish to
99+
output_dir = Path(f"ndvi_results_{custom_name}")
100+
output_dir.mkdir(exist_ok=True)
101+
102+
# Save results from xarray to geotiff files, each geometry's data will be stored in
103+
# its own folder. We can also give file-name prefix
104+
# and also mention which Xarray varible to save as geotiffs
105+
output_files = save_per_geometry(
106+
ndvi_ds, output_dir, file_prefix="ndvi", data_var="NDVI"
107+
)
108+
109+
for geom_id, filepath in output_files.items():
110+
print(f"Geometry {geom_id}: {filepath}")

examples/example_xarray_nb.ipynb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": 2,
32+
"execution_count": null,
3333
"id": "d6cf7929-caed-44c2-825a-b58d5b7bec94",
3434
"metadata": {},
3535
"outputs": [],
@@ -39,22 +39,18 @@
3939
"workspace_dir.mkdir(exist_ok=True)\n",
4040
"\n",
4141
"# Define a custom name for the stac index\n",
42-
"custom_name = \"bangalore-v2\"\n",
42+
"custom_name = \"bangalore_sentinel2_nb\"\n",
4343
"\n",
4444
"# Define area, time range and data source required in stac search\n",
45-
"date_range = (\"2024-12-01\", \"2025-01-30\")\n",
46-
"data_source = DataSources.LANDSAT # or SENTINEL2\n",
45+
"date_range = (\"2025-01-01\", \"2025-01-30\")\n",
46+
"data_source = DataSources.SENTINEL2 # or SENTINEL2\n",
4747
"\n",
4848
"aoi1_polygon = Polygon(\n",
4949
" [(77.55, 13.01), (77.58, 13.01), (77.58, 13.08), (77.55, 13.08), (77.55, 13.01)]\n",
5050
")\n",
5151
"\n",
52-
"aoi2_polygon = Polygon(\n",
53-
" [(77.56, 13.02), (77.59, 13.02), (77.59, 13.09), (77.56, 13.09), (77.56, 13.02)]\n",
54-
")\n",
55-
"\n",
56-
"# get total bounds of all polygons above for stac search and stac index creation\n",
57-
"bbox = aoi1_polygon.union(aoi2_polygon).bounds\n",
52+
"# get total bounds of all polygons above for STAC search and STAC index creation\n",
53+
"bbox = aoi1_polygon.bounds\n",
5854
"\n"
5955
]
6056
},
@@ -70,6 +66,7 @@
7066
"print(\"--------------------------\")\n",
7167
"collections = Rasteret.list_collections(workspace_dir=workspace_dir)\n",
7268
"for c in collections:\n",
69+
" print(f\"Existing collection in workspace dir {workspace_dir}:\")\n",
7370
" print(f\"- {c['name']}: {c['data_source']}, {c['date_range']}, {c['size']} scenes\")"
7471
]
7572
},
@@ -81,7 +78,7 @@
8178
"outputs": [],
8279
"source": [
8380
"# You can load any existing collection in any folder\n",
84-
"processor = Rasteret.load_collection(\"bangalore_202401-01_landsat\", workspace_dir=workspace_dir)\n"
81+
"#processor = Rasteret.load_collection(\"bangalore_202401-01_landsat\", workspace_dir=workspace_dir)\n"
8582
]
8683
},
8784
{
@@ -102,8 +99,7 @@
10299
"processor.create_collection(\n",
103100
" bbox=bbox,\n",
104101
" date_range=date_range,\n",
105-
" cloud_cover_lt=20,\n",
106-
" platform={\"in\": [\"LANDSAT_8\"]}\n",
102+
" cloud_cover_lt=50,\n",
107103
")"
108104
]
109105
},
@@ -120,8 +116,9 @@
120116
"print(\"\\n3. Processing Data\")\n",
121117
"print(\"-----------------\")\n",
122118
"ds = processor.get_xarray(\n",
123-
" geometries=[aoi1_polygon, aoi2_polygon],\n",
124-
" bands=[\"B4\", \"B5\"],\n",
119+
" geometries=[aoi1_polygon],\n",
120+
" bands=[\"B04\", \"B08\"],\n",
121+
" # give more STAC specific filters if requried\n",
125122
" cloud_cover_lt=20\n",
126123
")\n",
127124
"\n",
@@ -130,13 +127,13 @@
130127
},
131128
{
132129
"cell_type": "code",
133-
"execution_count": 7,
130+
"execution_count": null,
134131
"id": "aa8f4c96-5c5f-4847-a6f3-1a1a06257312",
135132
"metadata": {},
136133
"outputs": [],
137134
"source": [
138135
"# Calculate NDVI\n",
139-
"ndvi = (ds.B5 - ds.B4) / (ds.B5 + ds.B4)\n",
136+
"ndvi = (ds.B08 - ds.B04) / (ds.B08 + ds.B04)\n",
140137
"\n",
141138
"# Create a new dataset with NDVI as a variable\n",
142139
"ndvi_ds = xr.Dataset(\n",

0 commit comments

Comments
 (0)