ライブラリを読み込む
import numpy as np
from rasterio.io import MemoryFile
import rasterio.plot
import requests
WCS API からデータを取得する
params = {
'service': 'WCS',
'version': '2.0.1',
'request': 'GetCoverage',
'format': 'image/tiff',
'subset': [
'Lon(120.0,160.0)',
'Lat(15.5,50.0)',
't(2021-04-02T01:33:00.000Z)',
],
'coverageid': 'GCOM-C-L2-SST-Day_1km:SST',
}
response = requests.get(
'https://gpwmap.jaxa.jp/examind/api/WS/wcs/default',
params=params,
verify=False, # hotfix
)
/home/takahiro-miyoshi/notebooks/2021-11-11_wcs-user-manual/.venv/lib/python3.8/site-packages/urllib3/connectionpool.py:1013: InsecureRequestWarning: Unverified HTTPS request is being made to host 'gpwmap.jaxa.jp'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings warnings.warn(
レスポンスのバイナリを rasterio のオブジェクトとしてメモリ上に展開する
cf. https://rasterio.readthedocs.io/en/latest/topics/memory-files.html
tiff = MemoryFile(response.content).open()
tiff.profile
{'driver': 'GTiff', 'dtype': 'float64', 'nodata': nan, 'width': 1250, 'height': 1955, 'count': 1, 'crs': CRS.from_epsg(32654), 'transform': Affine(1672.8350443621607, 0.0, -785000.3584931496, 0.0, -1123.503060952663, 5193091.828488017), 'tiled': False, 'interleave': 'band'}
ラスターデータを Numpy N-D array として変数 vals に代入する
vals = tiff.read()
vals
Warning 1: TIFFReadDirectoryCheckOrder:Invalid TIFF directory; tags are not sorted in ascending order
array([[[nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], ..., [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan]]])
nan を除いた平均値、最大値、最小値を表示する
print('mean:', np.nanmean(vals))
print('max:', np.nanmax(vals))
print('min:', np.nanmin(vals))
mean: 9.993937080551728 max: 31.4732 min: -3.0304
rasterio.plot.show(tiff)
<AxesSubplot:>