Skip to content

Commit 6d481f8

Browse files
Merge pull request #6 from charlottecrevier/extract-point-value
Added the read of a single point coordinate to rasterio examples
2 parents 497ac8d + 4d56e88 commit 6d481f8

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

docs/example-cogs.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ For more details on the rasterio library, see the [rasterio] documentation.
4242
```
4343
<!-- END: Read a portion of a cog using rasterio -->
4444

45+
<!-- START: Read pixel with coord using rasterio -->
46+
::: how-to-guides.rasterio-point-example
47+
options:
48+
show_source: false
49+
members: no
50+
show_root_toc_entry: false # To remove the name of the file in the TOC
51+
52+
``` py linenums="1" hl_lines="34-38"
53+
--8<-- "how-to-guides/rasterio-point-example.py:code"
54+
```
55+
<!-- END: Read pixel with coord cog using rasterio -->
56+
4557
[rasterio installation]: https://rasterio.readthedocs.io/en/stable/installation.html
4658
[rasterio]: https://rasterio.readthedocs.io/en/latest/quickstart.html
4759
[Interacting with CCMEO STAC API]: pystac-client.md
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
## Read a portion of a remote COG - Point
3+
4+
For this example, you will need to also install [shapely](https://shapely.readthedocs.io/en/stable/installation.html):
5+
```bash
6+
pip install shapely
7+
```
8+
9+
In this code you will :
10+
11+
- Query a STAC API with pystac-client;
12+
- Read values of remote COG based on coordinates with the [sample()](https://rasterio.readthedocs.io/en/stable/api/rasterio.io.html#rasterio.io.DatasetReader.sample) functionality;
13+
14+
!!! info
15+
This specific example uses the collection **mrdem-30** from CCMEO's datacube
16+
17+
!!! Tip
18+
To perform the same request using gdal, refere to the [gdallocationinfo](https://gdal.org/en/stable/programs/gdallocationinfo.html) utility
19+
20+
"""
21+
# --8<-- [start:code]
22+
import pystac_client
23+
import rasterio
24+
import pyproj
25+
from shapely.geometry import Point
26+
from shapely.ops import transform
27+
28+
29+
# Define the coordinate of the point of interest in EPSG:4326 (for the request to the STAC API)
30+
lonlat = Point(-104.898838, 69.232434)
31+
32+
# Modify the projection of the point (for the COG extraction)
33+
proj = pyproj.Transformer.from_crs(4326, 3979, always_xy=True)
34+
point = transform(proj.transform, lonlat)
35+
x1, y1 = point.coords.xy
36+
37+
38+
# Link to ccmeo datacube stac-api
39+
stac_root = "https://datacube.services.geo.ca/stac/api"
40+
catalog = pystac_client.Client.open(stac_root)
41+
42+
search = catalog.search(
43+
collections=['mrdem-30'],
44+
intersects=lonlat,
45+
)
46+
47+
# Get the link to the data asset for mrdem-30 dtm and dsm
48+
links = {}
49+
for page in search.pages():
50+
for item in page:
51+
links['dtm'] = item.assets['dtm'].href
52+
links['dsm'] = item.assets['dsm'].href
53+
54+
# Read value from coordinates
55+
for asset, href in links.items():
56+
with rasterio.open(href) as src:
57+
for val in src.sample([(x1, y1)]):
58+
print(f'{asset} : {val}')
59+
60+
# --8<-- [end:code]
61+
62+
63+
64+

how-to-guides/rasterio-window-example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
## Read a portion of a remote COG
2+
## Read a portion of a remote COG - Bounding box
33
44
For this example, you will need to also install [shapely](https://shapely.readthedocs.io/en/stable/installation.html):
55
```bash
@@ -10,7 +10,7 @@
1010
1111
- Query a STAC API with pystac-client to get link to a COG;
1212
- Read a portion of a remote COG based on an AOI with the window functionality;
13-
- Optionally write the portion locally inside a .tif file
13+
- Write the portion locally inside a .tif file (Optional)
1414
1515
!!! info
1616
This specific example uses the collection **mrdem-30** from CCMEO's datacube
@@ -73,7 +73,7 @@
7373

7474
# Perform analysis ...
7575

76-
# Write the output array to a tiff file
76+
# (Optional) Write the output array to a tiff file
7777
output_tiff = r"path/to/output.tif"
7878
with rasterio.open(output_tiff, 'w', **metadata) as dst:
7979
dst.write(rst, 1)

0 commit comments

Comments
 (0)