copernicus-ems 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- copernicus_ems-0.1.0/LICENSE +202 -0
- copernicus_ems-0.1.0/PKG-INFO +149 -0
- copernicus_ems-0.1.0/README.md +124 -0
- copernicus_ems-0.1.0/pyproject.toml +97 -0
- copernicus_ems-0.1.0/pyproject.toml.orig +71 -0
- copernicus_ems-0.1.0/src/copernicus_ems/__init__.py +62 -0
- copernicus_ems-0.1.0/src/copernicus_ems/catalogue.py +218 -0
- copernicus_ems-0.1.0/src/copernicus_ems/client.py +95 -0
- copernicus_ems-0.1.0/src/copernicus_ems/errors.py +2 -0
- copernicus_ems-0.1.0/src/copernicus_ems/events.py +57 -0
- copernicus_ems-0.1.0/src/copernicus_ems/py.typed +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: copernicus-ems
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Client for Copernicus EMS Rapid Mapping: activations, observation masks, observed flood extents
|
|
5
|
+
Keywords: copernicus,emergency management,rapid mapping,flood,geojson,disaster,remote sensing
|
|
6
|
+
Author: Giuseppe Zileni
|
|
7
|
+
Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: GIS
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Dist: httpx>=0.27
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Project-URL: Homepage, https://github.com/agent-engineering-studio/copernicus-ems
|
|
22
|
+
Project-URL: Issues, https://github.com/agent-engineering-studio/copernicus-ems/issues
|
|
23
|
+
Project-URL: Changelog, https://github.com/agent-engineering-studio/copernicus-ems/blob/main/CHANGELOG.md
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# copernicus-ems
|
|
27
|
+
|
|
28
|
+
Async Python client for [Copernicus EMS Rapid Mapping](https://rapidmapping.emergency.copernicus.eu/):
|
|
29
|
+
activations, their products, and the **observed event layers** (flooded
|
|
30
|
+
areas, burnt areas, landslides…) as GeoJSON, with the observation masks
|
|
31
|
+
that make them usable as ground truth.
|
|
32
|
+
|
|
33
|
+
[](https://github.com/agent-engineering-studio/copernicus-ems/actions/workflows/ci.yml)
|
|
34
|
+
[](https://pypi.org/project/copernicus-ems/)
|
|
35
|
+
|
|
36
|
+
No account and no key: the dashboard API is public. One dependency: `httpx`.
|
|
37
|
+
|
|
38
|
+
## Why a library
|
|
39
|
+
|
|
40
|
+
Rapid Mapping is the best open record of where water actually was after a
|
|
41
|
+
flood in Europe, but the obvious way to read it is the expensive and
|
|
42
|
+
fragile one:
|
|
43
|
+
|
|
44
|
+
| The obvious way | What this does instead |
|
|
45
|
+
|---|---|
|
|
46
|
+
| download `{code}_products.zip` (~100 MB per activation, zips of zips, ortho imagery) and read shapefiles | read each product's vector layer as **GeoJSON** (tens of KB), the same features |
|
|
47
|
+
| some product packages answer **403** (EMSR664, Emilia-Romagna, May 2023) | their GeoJSON layers are open: EMSR664 alone is 6,334 flood polygons |
|
|
48
|
+
| take the **area of interest** as the event | take `observedEventA`: on EMSR762 AOI01 the AOI is ~1,850 km², the flood 1.19 km² |
|
|
49
|
+
| filter activations on category `Flood` | many Italian floods are filed as `Storm`: filter features on `event_type` |
|
|
50
|
+
| keep every feature of a flood activation | the same rain's landslides are in it too (`6-Mass Movement`) |
|
|
51
|
+
|
|
52
|
+
The GeoJSON layers were checked against the shapefiles of the product
|
|
53
|
+
packages on 77 layers from 10 Italian activations: same features, same
|
|
54
|
+
attributes, same area.
|
|
55
|
+
|
|
56
|
+
## Install
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install copernicus-ems
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Use
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import asyncio
|
|
66
|
+
|
|
67
|
+
import httpx
|
|
68
|
+
|
|
69
|
+
from copernicus_ems import fetch_activations, fetch_observed_events, select_activations
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def main() -> None:
|
|
73
|
+
async with httpx.AsyncClient(timeout=60) as client:
|
|
74
|
+
italy = select_activations(
|
|
75
|
+
await fetch_activations(client), country="Italy", categories=["Flood", "Storm"]
|
|
76
|
+
)
|
|
77
|
+
print(len(italy), "activations in Italy, first", italy[0].code, italy[0].event_time.date())
|
|
78
|
+
|
|
79
|
+
events = await fetch_observed_events(client, "EMSR664")
|
|
80
|
+
for e in events[:3]:
|
|
81
|
+
print(e.product.label, e.product.observed_time, len(e.features), "polygons")
|
|
82
|
+
print(sum(len(e.features) for e in events), "flood polygons in", len(events), "products")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
asyncio.run(main())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
14 activations in Italy, first EMSR659 2023-05-02
|
|
90
|
+
AOI01_DEL_MONIT01 2023-05-18 16:55:00+00:00 462 polygons
|
|
91
|
+
AOI01_GRA_PRODUCT 2023-05-22 10:27:00+00:00 92 polygons
|
|
92
|
+
AOI01_DEL_PRODUCT 2023-05-17 16:56:00+00:00 1153 polygons
|
|
93
|
+
6334 flood polygons in 11 products
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Each `Product` carries:
|
|
97
|
+
|
|
98
|
+
- `label` — `AOI01_DEL_PRODUCT`, `AOI02_DEL_MONIT01`: delineation
|
|
99
|
+
(`DEL`), grading (`GRA`) or first estimate (`FEP`), then each
|
|
100
|
+
monitoring update;
|
|
101
|
+
- `observed_time` — the earliest satellite image it was made from. Use
|
|
102
|
+
it, not the activation's `event_time`, to tell *when the water was
|
|
103
|
+
seen*: as an anchor for a forecast lead time, `event_time` would credit
|
|
104
|
+
hours in which the flood was already happening;
|
|
105
|
+
- `aoi_extent_wkt` — the **observation mask**. Inside it, no flood
|
|
106
|
+
polygon means "observed dry"; outside it, nothing was observed, so a
|
|
107
|
+
false alarm there cannot be told from an event nobody looked at;
|
|
108
|
+
- `layers` — every vector layer by kind (`observedEventA`,
|
|
109
|
+
`floodDepthA`, `maximumFloodExtentA`, …), the latest version of each.
|
|
110
|
+
|
|
111
|
+
Features are GeoJSON in CRS84 (lon/lat WGS84), with the Rapid Mapping
|
|
112
|
+
attributes (`event_type`, `notation`, `obj_desc`, `det_method`, …).
|
|
113
|
+
`select_features` keeps `5-Flood` with "Flooded area" and "Flood trace"
|
|
114
|
+
by default; pass `event_types=None, notations=None` for everything.
|
|
115
|
+
|
|
116
|
+
### Limits
|
|
117
|
+
|
|
118
|
+
- The public API lists activations **from 2023**; older ones live on the
|
|
119
|
+
legacy site, with no documented API.
|
|
120
|
+
- Satellite delineation is a lower bound: short urban floods and water
|
|
121
|
+
under canopy may not show. A false alarm measured against it may be an
|
|
122
|
+
event nobody saw.
|
|
123
|
+
|
|
124
|
+
### Bring your own HTTP
|
|
125
|
+
|
|
126
|
+
`activations_url` / `parse_activations`, `detail_url` / `parse_detail`,
|
|
127
|
+
`parse_layer` / `select_features` are pure functions for callers with
|
|
128
|
+
their own HTTP layer.
|
|
129
|
+
|
|
130
|
+
## Data licence
|
|
131
|
+
|
|
132
|
+
© European Union, Copernicus Emergency Management Service. Free to use,
|
|
133
|
+
with attribution: anything you publish from these layers must say so.
|
|
134
|
+
The test fixtures are small Rapid Mapping responses under the same terms.
|
|
135
|
+
|
|
136
|
+
Extracted from [Limen](https://github.com/agent-engineering-studio/limen),
|
|
137
|
+
which uses the observed flood extents as the ground truth of its flood
|
|
138
|
+
backtest.
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
uv sync
|
|
144
|
+
make check
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
Code: [Apache-2.0](./LICENSE).
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# copernicus-ems
|
|
2
|
+
|
|
3
|
+
Async Python client for [Copernicus EMS Rapid Mapping](https://rapidmapping.emergency.copernicus.eu/):
|
|
4
|
+
activations, their products, and the **observed event layers** (flooded
|
|
5
|
+
areas, burnt areas, landslides…) as GeoJSON, with the observation masks
|
|
6
|
+
that make them usable as ground truth.
|
|
7
|
+
|
|
8
|
+
[](https://github.com/agent-engineering-studio/copernicus-ems/actions/workflows/ci.yml)
|
|
9
|
+
[](https://pypi.org/project/copernicus-ems/)
|
|
10
|
+
|
|
11
|
+
No account and no key: the dashboard API is public. One dependency: `httpx`.
|
|
12
|
+
|
|
13
|
+
## Why a library
|
|
14
|
+
|
|
15
|
+
Rapid Mapping is the best open record of where water actually was after a
|
|
16
|
+
flood in Europe, but the obvious way to read it is the expensive and
|
|
17
|
+
fragile one:
|
|
18
|
+
|
|
19
|
+
| The obvious way | What this does instead |
|
|
20
|
+
|---|---|
|
|
21
|
+
| download `{code}_products.zip` (~100 MB per activation, zips of zips, ortho imagery) and read shapefiles | read each product's vector layer as **GeoJSON** (tens of KB), the same features |
|
|
22
|
+
| some product packages answer **403** (EMSR664, Emilia-Romagna, May 2023) | their GeoJSON layers are open: EMSR664 alone is 6,334 flood polygons |
|
|
23
|
+
| take the **area of interest** as the event | take `observedEventA`: on EMSR762 AOI01 the AOI is ~1,850 km², the flood 1.19 km² |
|
|
24
|
+
| filter activations on category `Flood` | many Italian floods are filed as `Storm`: filter features on `event_type` |
|
|
25
|
+
| keep every feature of a flood activation | the same rain's landslides are in it too (`6-Mass Movement`) |
|
|
26
|
+
|
|
27
|
+
The GeoJSON layers were checked against the shapefiles of the product
|
|
28
|
+
packages on 77 layers from 10 Italian activations: same features, same
|
|
29
|
+
attributes, same area.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install copernicus-ems
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Use
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import asyncio
|
|
41
|
+
|
|
42
|
+
import httpx
|
|
43
|
+
|
|
44
|
+
from copernicus_ems import fetch_activations, fetch_observed_events, select_activations
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async def main() -> None:
|
|
48
|
+
async with httpx.AsyncClient(timeout=60) as client:
|
|
49
|
+
italy = select_activations(
|
|
50
|
+
await fetch_activations(client), country="Italy", categories=["Flood", "Storm"]
|
|
51
|
+
)
|
|
52
|
+
print(len(italy), "activations in Italy, first", italy[0].code, italy[0].event_time.date())
|
|
53
|
+
|
|
54
|
+
events = await fetch_observed_events(client, "EMSR664")
|
|
55
|
+
for e in events[:3]:
|
|
56
|
+
print(e.product.label, e.product.observed_time, len(e.features), "polygons")
|
|
57
|
+
print(sum(len(e.features) for e in events), "flood polygons in", len(events), "products")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
asyncio.run(main())
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
14 activations in Italy, first EMSR659 2023-05-02
|
|
65
|
+
AOI01_DEL_MONIT01 2023-05-18 16:55:00+00:00 462 polygons
|
|
66
|
+
AOI01_GRA_PRODUCT 2023-05-22 10:27:00+00:00 92 polygons
|
|
67
|
+
AOI01_DEL_PRODUCT 2023-05-17 16:56:00+00:00 1153 polygons
|
|
68
|
+
6334 flood polygons in 11 products
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Each `Product` carries:
|
|
72
|
+
|
|
73
|
+
- `label` — `AOI01_DEL_PRODUCT`, `AOI02_DEL_MONIT01`: delineation
|
|
74
|
+
(`DEL`), grading (`GRA`) or first estimate (`FEP`), then each
|
|
75
|
+
monitoring update;
|
|
76
|
+
- `observed_time` — the earliest satellite image it was made from. Use
|
|
77
|
+
it, not the activation's `event_time`, to tell *when the water was
|
|
78
|
+
seen*: as an anchor for a forecast lead time, `event_time` would credit
|
|
79
|
+
hours in which the flood was already happening;
|
|
80
|
+
- `aoi_extent_wkt` — the **observation mask**. Inside it, no flood
|
|
81
|
+
polygon means "observed dry"; outside it, nothing was observed, so a
|
|
82
|
+
false alarm there cannot be told from an event nobody looked at;
|
|
83
|
+
- `layers` — every vector layer by kind (`observedEventA`,
|
|
84
|
+
`floodDepthA`, `maximumFloodExtentA`, …), the latest version of each.
|
|
85
|
+
|
|
86
|
+
Features are GeoJSON in CRS84 (lon/lat WGS84), with the Rapid Mapping
|
|
87
|
+
attributes (`event_type`, `notation`, `obj_desc`, `det_method`, …).
|
|
88
|
+
`select_features` keeps `5-Flood` with "Flooded area" and "Flood trace"
|
|
89
|
+
by default; pass `event_types=None, notations=None` for everything.
|
|
90
|
+
|
|
91
|
+
### Limits
|
|
92
|
+
|
|
93
|
+
- The public API lists activations **from 2023**; older ones live on the
|
|
94
|
+
legacy site, with no documented API.
|
|
95
|
+
- Satellite delineation is a lower bound: short urban floods and water
|
|
96
|
+
under canopy may not show. A false alarm measured against it may be an
|
|
97
|
+
event nobody saw.
|
|
98
|
+
|
|
99
|
+
### Bring your own HTTP
|
|
100
|
+
|
|
101
|
+
`activations_url` / `parse_activations`, `detail_url` / `parse_detail`,
|
|
102
|
+
`parse_layer` / `select_features` are pure functions for callers with
|
|
103
|
+
their own HTTP layer.
|
|
104
|
+
|
|
105
|
+
## Data licence
|
|
106
|
+
|
|
107
|
+
© European Union, Copernicus Emergency Management Service. Free to use,
|
|
108
|
+
with attribution: anything you publish from these layers must say so.
|
|
109
|
+
The test fixtures are small Rapid Mapping responses under the same terms.
|
|
110
|
+
|
|
111
|
+
Extracted from [Limen](https://github.com/agent-engineering-studio/limen),
|
|
112
|
+
which uses the observed flood extents as the ground truth of its flood
|
|
113
|
+
backtest.
|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
uv sync
|
|
119
|
+
make check
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
Code: [Apache-2.0](./LICENSE).
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "copernicus-ems"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Client for Copernicus EMS Rapid Mapping: activations, observation masks, observed flood extents"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
keywords = [
|
|
7
|
+
"copernicus",
|
|
8
|
+
"emergency management",
|
|
9
|
+
"rapid mapping",
|
|
10
|
+
"flood",
|
|
11
|
+
"geojson",
|
|
12
|
+
"disaster",
|
|
13
|
+
"remote sensing",
|
|
14
|
+
]
|
|
15
|
+
requires-python = ">=3.12"
|
|
16
|
+
license = "Apache-2.0"
|
|
17
|
+
license-files = ["LICENSE"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 4 - Beta",
|
|
20
|
+
"Intended Audience :: Science/Research",
|
|
21
|
+
"Topic :: Scientific/Engineering :: GIS",
|
|
22
|
+
"Framework :: AsyncIO",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = ["httpx>=0.27"]
|
|
30
|
+
|
|
31
|
+
[[project.authors]]
|
|
32
|
+
name = "Giuseppe Zileni"
|
|
33
|
+
email = "giuseppe.zileni@gmail.com"
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/agent-engineering-studio/copernicus-ems"
|
|
37
|
+
Issues = "https://github.com/agent-engineering-studio/copernicus-ems/issues"
|
|
38
|
+
Changelog = "https://github.com/agent-engineering-studio/copernicus-ems/blob/main/CHANGELOG.md"
|
|
39
|
+
|
|
40
|
+
[build-system]
|
|
41
|
+
requires = ["uv_build>=0.13.6,<0.14"]
|
|
42
|
+
build-backend = "uv_build"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"mypy>=1.13",
|
|
47
|
+
"pytest>=8.3",
|
|
48
|
+
"pytest-cov>=6.0",
|
|
49
|
+
"ruff>=0.8",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.ruff]
|
|
53
|
+
line-length = 100
|
|
54
|
+
target-version = "py312"
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
select = [
|
|
58
|
+
"E",
|
|
59
|
+
"F",
|
|
60
|
+
"W",
|
|
61
|
+
"I",
|
|
62
|
+
"N",
|
|
63
|
+
"UP",
|
|
64
|
+
"B",
|
|
65
|
+
"C4",
|
|
66
|
+
"SIM",
|
|
67
|
+
"ARG",
|
|
68
|
+
"PTH",
|
|
69
|
+
"RUF",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
[tool.ruff.lint.per-file-ignores]
|
|
73
|
+
"tests/**" = [
|
|
74
|
+
"ARG",
|
|
75
|
+
"B018",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[tool.mypy]
|
|
79
|
+
strict = true
|
|
80
|
+
python_version = "3.12"
|
|
81
|
+
files = [
|
|
82
|
+
"src",
|
|
83
|
+
"tests",
|
|
84
|
+
]
|
|
85
|
+
warn_unused_ignores = true
|
|
86
|
+
warn_redundant_casts = true
|
|
87
|
+
|
|
88
|
+
[tool.pytest.ini_options]
|
|
89
|
+
testpaths = ["tests"]
|
|
90
|
+
addopts = "--strict-markers"
|
|
91
|
+
|
|
92
|
+
[tool.coverage.run]
|
|
93
|
+
source = ["copernicus_ems"]
|
|
94
|
+
|
|
95
|
+
[tool.coverage.report]
|
|
96
|
+
fail_under = 90
|
|
97
|
+
show_missing = true
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "copernicus-ems"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Client for Copernicus EMS Rapid Mapping: activations, observation masks, observed flood extents"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
keywords = ["copernicus", "emergency management", "rapid mapping", "flood", "geojson", "disaster", "remote sensing"]
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
|
+
license = "Apache-2.0"
|
|
9
|
+
license-files = ["LICENSE"]
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Giuseppe Zileni", email = "giuseppe.zileni@gmail.com" },
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Science/Research",
|
|
16
|
+
"Topic :: Scientific/Engineering :: GIS",
|
|
17
|
+
"Framework :: AsyncIO",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"httpx>=0.27",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/agent-engineering-studio/copernicus-ems"
|
|
30
|
+
Issues = "https://github.com/agent-engineering-studio/copernicus-ems/issues"
|
|
31
|
+
Changelog = "https://github.com/agent-engineering-studio/copernicus-ems/blob/main/CHANGELOG.md"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["uv_build>=0.13.6,<0.14"]
|
|
35
|
+
build-backend = "uv_build"
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"mypy>=1.13",
|
|
40
|
+
"pytest>=8.3",
|
|
41
|
+
"pytest-cov>=6.0",
|
|
42
|
+
"ruff>=0.8",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.ruff]
|
|
46
|
+
line-length = 100
|
|
47
|
+
target-version = "py312"
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint]
|
|
50
|
+
select = ["E", "F", "W", "I", "N", "UP", "B", "C4", "SIM", "ARG", "PTH", "RUF"]
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint.per-file-ignores]
|
|
53
|
+
"tests/**" = ["ARG", "B018"]
|
|
54
|
+
|
|
55
|
+
[tool.mypy]
|
|
56
|
+
strict = true
|
|
57
|
+
python_version = "3.12"
|
|
58
|
+
files = ["src", "tests"]
|
|
59
|
+
warn_unused_ignores = true
|
|
60
|
+
warn_redundant_casts = true
|
|
61
|
+
|
|
62
|
+
[tool.pytest.ini_options]
|
|
63
|
+
testpaths = ["tests"]
|
|
64
|
+
addopts = "--strict-markers"
|
|
65
|
+
|
|
66
|
+
[tool.coverage.run]
|
|
67
|
+
source = ["copernicus_ems"]
|
|
68
|
+
|
|
69
|
+
[tool.coverage.report]
|
|
70
|
+
fail_under = 90
|
|
71
|
+
show_missing = true
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Client for Copernicus EMS Rapid Mapping."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from copernicus_ems.catalogue import (
|
|
6
|
+
ACTIVATIONS_URL,
|
|
7
|
+
DETAIL_URL,
|
|
8
|
+
Activation,
|
|
9
|
+
ActivationsPage,
|
|
10
|
+
Layer,
|
|
11
|
+
Product,
|
|
12
|
+
activations_url,
|
|
13
|
+
detail_url,
|
|
14
|
+
parse_activations,
|
|
15
|
+
parse_detail,
|
|
16
|
+
parse_time,
|
|
17
|
+
select_activations,
|
|
18
|
+
)
|
|
19
|
+
from copernicus_ems.client import (
|
|
20
|
+
ProductEvents,
|
|
21
|
+
fetch_activations,
|
|
22
|
+
fetch_layer,
|
|
23
|
+
fetch_observed_events,
|
|
24
|
+
fetch_products,
|
|
25
|
+
)
|
|
26
|
+
from copernicus_ems.errors import EmsError
|
|
27
|
+
from copernicus_ems.events import (
|
|
28
|
+
FLOOD_EVENT_TYPE,
|
|
29
|
+
FLOOD_NOTATIONS,
|
|
30
|
+
OBSERVED_EVENT,
|
|
31
|
+
parse_layer,
|
|
32
|
+
select_features,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
__version__ = version("copernicus-ems")
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"ACTIVATIONS_URL",
|
|
39
|
+
"DETAIL_URL",
|
|
40
|
+
"FLOOD_EVENT_TYPE",
|
|
41
|
+
"FLOOD_NOTATIONS",
|
|
42
|
+
"OBSERVED_EVENT",
|
|
43
|
+
"Activation",
|
|
44
|
+
"ActivationsPage",
|
|
45
|
+
"EmsError",
|
|
46
|
+
"Layer",
|
|
47
|
+
"Product",
|
|
48
|
+
"ProductEvents",
|
|
49
|
+
"__version__",
|
|
50
|
+
"activations_url",
|
|
51
|
+
"detail_url",
|
|
52
|
+
"fetch_activations",
|
|
53
|
+
"fetch_layer",
|
|
54
|
+
"fetch_observed_events",
|
|
55
|
+
"fetch_products",
|
|
56
|
+
"parse_activations",
|
|
57
|
+
"parse_detail",
|
|
58
|
+
"parse_layer",
|
|
59
|
+
"parse_time",
|
|
60
|
+
"select_activations",
|
|
61
|
+
"select_features",
|
|
62
|
+
]
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Activations and products of Copernicus EMS Rapid Mapping.
|
|
2
|
+
|
|
3
|
+
The public dashboard API needs no credentials:
|
|
4
|
+
|
|
5
|
+
* ``public-activations-info/`` lists every activation, paginated
|
|
6
|
+
(``count`` / ``next``), newest first;
|
|
7
|
+
* ``public-activations/?code=EMSR762`` details one: its areas of interest
|
|
8
|
+
(AOI), and for each AOI the products (delineation ``DEL``, grading
|
|
9
|
+
``GRA``, first estimate ``FEP``; ``monitoringNumber`` 0 for the first
|
|
10
|
+
product, then 1, 2, ... for each monitoring update), the satellite images
|
|
11
|
+
each product was made from, and its **layers**.
|
|
12
|
+
|
|
13
|
+
Every vector layer of a product is published on its own as GeoJSON (the
|
|
14
|
+
``json`` URL of a ``vt`` layer), tens of kilobytes. There is no need for
|
|
15
|
+
the product package, which is a ~100 MB zip of zips holding the ortho
|
|
16
|
+
imagery, and which for some activations answers 403 (EMSR664) while its
|
|
17
|
+
GeoJSON layers are open.
|
|
18
|
+
|
|
19
|
+
Two things the API does not say:
|
|
20
|
+
|
|
21
|
+
* the **category** describes the cause, not the effect: many Italian floods
|
|
22
|
+
are filed as ``Storm``. Filter on the features' ``event_type`` instead;
|
|
23
|
+
* the list gives ``countries`` as strings, the detail as ``{"name": ...}``.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import json
|
|
29
|
+
import re
|
|
30
|
+
from collections.abc import Iterable
|
|
31
|
+
from dataclasses import dataclass, field
|
|
32
|
+
from datetime import UTC, datetime
|
|
33
|
+
from typing import Any
|
|
34
|
+
|
|
35
|
+
from copernicus_ems.errors import EmsError
|
|
36
|
+
|
|
37
|
+
API_URL = "https://rapidmapping.emergency.copernicus.eu/backend/dashboard-api"
|
|
38
|
+
ACTIVATIONS_URL = f"{API_URL}/public-activations-info/"
|
|
39
|
+
DETAIL_URL = f"{API_URL}/public-activations/"
|
|
40
|
+
|
|
41
|
+
#: ``EMSR762/AOI01/DEL_PRODUCT/EMSR762_AOI01_DEL_PRODUCT_observedEventA_v1_VT``
|
|
42
|
+
_LAYER_RE = re.compile(r"_(?P<kind>[A-Za-z]+)_v(?P<version>\d+)_VT$")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True, slots=True)
|
|
46
|
+
class Activation:
|
|
47
|
+
"""One activation, as listed."""
|
|
48
|
+
|
|
49
|
+
code: str
|
|
50
|
+
name: str
|
|
51
|
+
category: str
|
|
52
|
+
event_time: datetime
|
|
53
|
+
countries: tuple[str, ...]
|
|
54
|
+
activation_time: datetime | None = None
|
|
55
|
+
sub_category: str | None = None
|
|
56
|
+
closed: bool = False
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(frozen=True, slots=True)
|
|
60
|
+
class ActivationsPage:
|
|
61
|
+
activations: list[Activation]
|
|
62
|
+
next_url: str | None
|
|
63
|
+
count: int
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True, slots=True)
|
|
67
|
+
class Layer:
|
|
68
|
+
"""A vector layer of a product, published as GeoJSON."""
|
|
69
|
+
|
|
70
|
+
kind: str
|
|
71
|
+
version: int
|
|
72
|
+
url: str
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass(frozen=True, slots=True)
|
|
76
|
+
class Product:
|
|
77
|
+
"""One map product of one AOI."""
|
|
78
|
+
|
|
79
|
+
activation_code: str
|
|
80
|
+
aoi_number: int
|
|
81
|
+
aoi_name: str
|
|
82
|
+
aoi_extent_wkt: str | None
|
|
83
|
+
product_type: str
|
|
84
|
+
monitoring_number: int
|
|
85
|
+
version: int
|
|
86
|
+
acquisitions: tuple[datetime, ...]
|
|
87
|
+
layers: dict[str, Layer] = field(default_factory=dict)
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def label(self) -> str:
|
|
91
|
+
"""``AOI01_DEL_PRODUCT``, ``AOI02_DEL_MONIT01``: the product's own name."""
|
|
92
|
+
stage = f"MONIT{self.monitoring_number:02d}" if self.monitoring_number else "PRODUCT"
|
|
93
|
+
return f"AOI{self.aoi_number:02d}_{self.product_type}_{stage}"
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def observed_time(self) -> datetime | None:
|
|
97
|
+
"""The earliest image the product was made from: when the satellite saw it."""
|
|
98
|
+
return min(self.acquisitions, default=None)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def parse_time(raw: Any) -> datetime | None:
|
|
102
|
+
"""An ISO time from the API, as aware UTC (the API omits the zone)."""
|
|
103
|
+
if not raw:
|
|
104
|
+
return None
|
|
105
|
+
try:
|
|
106
|
+
parsed = datetime.fromisoformat(str(raw).replace("Z", "+00:00"))
|
|
107
|
+
except ValueError:
|
|
108
|
+
return None
|
|
109
|
+
return parsed if parsed.tzinfo else parsed.replace(tzinfo=UTC)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def activations_url(*, page_size: int = 500) -> str:
|
|
113
|
+
return f"{ACTIVATIONS_URL}?limit={page_size}"
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def detail_url(code: str) -> str:
|
|
117
|
+
return f"{DETAIL_URL}?code={code}"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def parse_activations(body: bytes | str) -> ActivationsPage:
|
|
121
|
+
"""One page of the activation list. Rows without a usable event time are skipped."""
|
|
122
|
+
data = _json(body)
|
|
123
|
+
if not isinstance(data, dict) or "results" not in data:
|
|
124
|
+
raise EmsError(f"unexpected activation list: {str(body)[:200]!r}")
|
|
125
|
+
out: list[Activation] = []
|
|
126
|
+
for row in data["results"] or []:
|
|
127
|
+
when = parse_time(row.get("eventTime"))
|
|
128
|
+
if when is None:
|
|
129
|
+
continue
|
|
130
|
+
out.append(
|
|
131
|
+
Activation(
|
|
132
|
+
code=str(row["code"]),
|
|
133
|
+
name=str(row.get("name") or row["code"]).strip(),
|
|
134
|
+
category=str(row.get("category") or ""),
|
|
135
|
+
event_time=when,
|
|
136
|
+
countries=_countries(row.get("countries")),
|
|
137
|
+
activation_time=parse_time(row.get("activationTime")),
|
|
138
|
+
sub_category=row.get("subCategory") or None,
|
|
139
|
+
closed=bool(row.get("closed")),
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
return ActivationsPage(
|
|
143
|
+
activations=out, next_url=data.get("next") or None, count=int(data.get("count") or 0)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def select_activations(
|
|
148
|
+
activations: Iterable[Activation],
|
|
149
|
+
*,
|
|
150
|
+
country: str | None = None,
|
|
151
|
+
categories: Iterable[str] | None = None,
|
|
152
|
+
) -> list[Activation]:
|
|
153
|
+
"""Activations in ``country`` and ``categories`` (case-insensitive), oldest first."""
|
|
154
|
+
wanted = {c.lower() for c in categories} if categories is not None else None
|
|
155
|
+
return sorted(
|
|
156
|
+
(
|
|
157
|
+
a
|
|
158
|
+
for a in activations
|
|
159
|
+
if (country is None or country in a.countries)
|
|
160
|
+
and (wanted is None or a.category.lower() in wanted)
|
|
161
|
+
),
|
|
162
|
+
key=lambda a: a.event_time,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def parse_detail(body: bytes | str) -> list[Product]:
|
|
167
|
+
"""Every product of an activation, with its vector layers."""
|
|
168
|
+
data = _json(body)
|
|
169
|
+
if not isinstance(data, dict) or "results" not in data:
|
|
170
|
+
raise EmsError(f"unexpected activation detail: {str(body)[:200]!r}")
|
|
171
|
+
out: list[Product] = []
|
|
172
|
+
for activation in data["results"] or []:
|
|
173
|
+
code = str(activation.get("code") or "")
|
|
174
|
+
for aoi in activation.get("aois") or []:
|
|
175
|
+
for product in aoi.get("products") or []:
|
|
176
|
+
out.append(
|
|
177
|
+
Product(
|
|
178
|
+
activation_code=code,
|
|
179
|
+
aoi_number=int(aoi.get("number") or 0),
|
|
180
|
+
aoi_name=str(aoi.get("name") or ""),
|
|
181
|
+
aoi_extent_wkt=aoi.get("extent") or None,
|
|
182
|
+
product_type=str(product.get("type") or "").upper(),
|
|
183
|
+
monitoring_number=int(product.get("monitoringNumber") or 0),
|
|
184
|
+
version=int((product.get("version") or {}).get("number") or 0),
|
|
185
|
+
acquisitions=tuple(
|
|
186
|
+
sorted(
|
|
187
|
+
t
|
|
188
|
+
for image in product.get("images") or []
|
|
189
|
+
if (t := parse_time(image.get("acquisitionTime"))) is not None
|
|
190
|
+
)
|
|
191
|
+
),
|
|
192
|
+
layers=_layers(product.get("layers") or []),
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return out
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _layers(raw: list[dict[str, Any]]) -> dict[str, Layer]:
|
|
199
|
+
out: dict[str, Layer] = {}
|
|
200
|
+
for layer in raw:
|
|
201
|
+
match = _LAYER_RE.search(str(layer.get("name") or ""))
|
|
202
|
+
if layer.get("format") != "vt" or not layer.get("json") or match is None:
|
|
203
|
+
continue
|
|
204
|
+
found = Layer(kind=match["kind"], version=int(match["version"]), url=str(layer["json"]))
|
|
205
|
+
if found.kind not in out or found.version > out[found.kind].version:
|
|
206
|
+
out[found.kind] = found
|
|
207
|
+
return out
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _countries(raw: Any) -> tuple[str, ...]:
|
|
211
|
+
return tuple(str(c["name"] if isinstance(c, dict) else c) for c in raw or [])
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _json(body: bytes | str) -> Any:
|
|
215
|
+
try:
|
|
216
|
+
return json.loads(body)
|
|
217
|
+
except ValueError as exc:
|
|
218
|
+
raise EmsError(f"Rapid Mapping did not return JSON: {str(body)[:200]!r}") from exc
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Async functions over :mod:`copernicus_ems.catalogue` and :mod:`copernicus_ems.events`.
|
|
2
|
+
|
|
3
|
+
They take your ``httpx.AsyncClient``: timeouts, retries and pooling stay
|
|
4
|
+
yours, and HTTP errors propagate.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Iterable
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import TYPE_CHECKING, Any
|
|
12
|
+
|
|
13
|
+
from copernicus_ems.catalogue import (
|
|
14
|
+
Activation,
|
|
15
|
+
Product,
|
|
16
|
+
activations_url,
|
|
17
|
+
detail_url,
|
|
18
|
+
parse_activations,
|
|
19
|
+
parse_detail,
|
|
20
|
+
)
|
|
21
|
+
from copernicus_ems.events import (
|
|
22
|
+
FLOOD_EVENT_TYPE,
|
|
23
|
+
FLOOD_NOTATIONS,
|
|
24
|
+
OBSERVED_EVENT,
|
|
25
|
+
parse_layer,
|
|
26
|
+
select_features,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
if TYPE_CHECKING:
|
|
30
|
+
import httpx
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True, slots=True)
|
|
34
|
+
class ProductEvents:
|
|
35
|
+
"""The selected features of one product's layer, with the product they come from."""
|
|
36
|
+
|
|
37
|
+
product: Product
|
|
38
|
+
features: list[dict[str, Any]]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def fetch_activations(client: httpx.AsyncClient, *, page_size: int = 500) -> list[Activation]:
|
|
42
|
+
"""Every public activation, following the pages."""
|
|
43
|
+
out: list[Activation] = []
|
|
44
|
+
url: str | None = activations_url(page_size=page_size)
|
|
45
|
+
while url:
|
|
46
|
+
response = await client.get(url)
|
|
47
|
+
response.raise_for_status()
|
|
48
|
+
page = parse_activations(response.content)
|
|
49
|
+
out.extend(page.activations)
|
|
50
|
+
url = page.next_url
|
|
51
|
+
return out
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def fetch_products(client: httpx.AsyncClient, code: str) -> list[Product]:
|
|
55
|
+
"""The products of one activation (``EMSR762``)."""
|
|
56
|
+
response = await client.get(detail_url(code))
|
|
57
|
+
response.raise_for_status()
|
|
58
|
+
return parse_detail(response.content)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
async def fetch_layer(
|
|
62
|
+
client: httpx.AsyncClient, product: Product, kind: str
|
|
63
|
+
) -> dict[str, Any] | None:
|
|
64
|
+
"""One vector layer of a product as GeoJSON; ``None`` if the product has none of that kind."""
|
|
65
|
+
layer = product.layers.get(kind)
|
|
66
|
+
if layer is None:
|
|
67
|
+
return None
|
|
68
|
+
response = await client.get(layer.url, follow_redirects=True)
|
|
69
|
+
response.raise_for_status()
|
|
70
|
+
return parse_layer(response.content)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
async def fetch_observed_events(
|
|
74
|
+
client: httpx.AsyncClient,
|
|
75
|
+
code: str,
|
|
76
|
+
*,
|
|
77
|
+
event_types: Iterable[str] | None = (FLOOD_EVENT_TYPE,),
|
|
78
|
+
notations: Iterable[str] | None = FLOOD_NOTATIONS,
|
|
79
|
+
) -> list[ProductEvents]:
|
|
80
|
+
"""The observed events of every product of an activation (floods by default).
|
|
81
|
+
|
|
82
|
+
Products without an ``observedEventA`` layer, and products where no
|
|
83
|
+
feature passes the filter, are left out.
|
|
84
|
+
"""
|
|
85
|
+
types = tuple(event_types) if event_types is not None else None
|
|
86
|
+
marks = tuple(notations) if notations is not None else None
|
|
87
|
+
out: list[ProductEvents] = []
|
|
88
|
+
for product in await fetch_products(client, code):
|
|
89
|
+
collection = await fetch_layer(client, product, OBSERVED_EVENT)
|
|
90
|
+
if collection is None:
|
|
91
|
+
continue
|
|
92
|
+
features = select_features(collection, event_types=types, notations=marks)
|
|
93
|
+
if features:
|
|
94
|
+
out.append(ProductEvents(product=product, features=features))
|
|
95
|
+
return out
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Observed event layers: what the satellite actually saw under water.
|
|
2
|
+
|
|
3
|
+
The flooded extent is the ``observedEventA`` layer, not the AOI. On EMSR762
|
|
4
|
+
AOI01 the AOI covers ~1,850 km² and the observed flood 1.19 km²: taking the
|
|
5
|
+
AOI as the event would call hundreds of dry square kilometres flooded. The
|
|
6
|
+
AOI is the **observation mask** instead: inside it, the absence of a flood
|
|
7
|
+
polygon is an observed "not flooded"; outside it, nothing was observed.
|
|
8
|
+
|
|
9
|
+
A flood activation also maps the landslides the same rain triggered
|
|
10
|
+
(``event_type`` ``6-Mass Movement``). Keep ``5-Flood``, with the notations
|
|
11
|
+
"Flooded area" and "Flood trace" (ground the water left marks on).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
from collections.abc import Iterable
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from copernicus_ems.errors import EmsError
|
|
21
|
+
|
|
22
|
+
OBSERVED_EVENT = "observedEventA"
|
|
23
|
+
FLOOD_EVENT_TYPE = "5-Flood"
|
|
24
|
+
FLOOD_NOTATIONS = ("Flooded area", "Flood trace")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def parse_layer(body: bytes | str) -> dict[str, Any]:
|
|
28
|
+
"""A GeoJSON FeatureCollection (CRS84, i.e. lon/lat WGS84)."""
|
|
29
|
+
try:
|
|
30
|
+
data = json.loads(body)
|
|
31
|
+
except ValueError as exc:
|
|
32
|
+
raise EmsError(f"layer is not GeoJSON: {str(body)[:200]!r}") from exc
|
|
33
|
+
if not isinstance(data, dict) or not isinstance(data.get("features"), list):
|
|
34
|
+
raise EmsError(f"layer is not a FeatureCollection: {str(body)[:200]!r}")
|
|
35
|
+
return data
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def select_features(
|
|
39
|
+
collection: dict[str, Any],
|
|
40
|
+
*,
|
|
41
|
+
event_types: Iterable[str] | None = (FLOOD_EVENT_TYPE,),
|
|
42
|
+
notations: Iterable[str] | None = FLOOD_NOTATIONS,
|
|
43
|
+
) -> list[dict[str, Any]]:
|
|
44
|
+
"""The features of the given event types and notations (``None`` keeps all)."""
|
|
45
|
+
types = set(event_types) if event_types is not None else None
|
|
46
|
+
marks = set(notations) if notations is not None else None
|
|
47
|
+
out = []
|
|
48
|
+
for feature in collection["features"]:
|
|
49
|
+
props = feature.get("properties") or {}
|
|
50
|
+
if feature.get("geometry") is None:
|
|
51
|
+
continue
|
|
52
|
+
if types is not None and props.get("event_type") not in types:
|
|
53
|
+
continue
|
|
54
|
+
if marks is not None and props.get("notation") not in marks:
|
|
55
|
+
continue
|
|
56
|
+
out.append(feature)
|
|
57
|
+
return out
|
|
File without changes
|