|
| 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 | + |
0 commit comments