Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions pystac_monty/sources/common.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import abc
import json
import tempfile
import typing
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Literal, Tuple, Union
from typing import List, Literal, Optional, Tuple, Union

import requests
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from pystac import Collection, Item

from pystac_monty.geocoding import MontyGeoCoder
Expand Down Expand Up @@ -42,14 +43,18 @@ def success_rows(self) -> int:
return self.total_rows - self.failed_rows


class BaseModelWithExtra(BaseModel):
model_config = ConfigDict(extra="ignore", arbitrary_types_allowed=True)


class DataType(Enum):
FILE = "file"
MEMORY = "memory"


class File(BaseModel):
class File(BaseModelWithExtra):
data_type: Literal[DataType.FILE]
path: str
path: str | tempfile._TemporaryFileWrapper


class Memory(BaseModel):
Expand Down Expand Up @@ -84,6 +89,13 @@ class USGSDataSourceType(BaseModel):
loss_data: Union[File, Memory, None] = None


class DesInventarData(BaseModel):
tmp_zip_file: File
country_code: str
iso3: str
source_url: str | None = None


@dataclass
class MontyDataSource:
source_url: str
Expand Down Expand Up @@ -123,14 +135,15 @@ def get_data(self) -> typing.Any:

@dataclass
class MontyDataSourceV3:
root: Union[GenericDataSource, GdacsDataSourceType, USGSDataSourceType]
root: Union[GenericDataSource, GdacsDataSourceType, USGSDataSourceType, DesInventarData]
source_url: Optional[str] = field(init=False)

def __post_init__(self):
self.source_url = self.root.source_url
if isinstance(self.root, GenericDataSource):
self.input_data = self.root.input_data

def get_source_url(self) -> str:
def get_source_url(self) -> Optional[str]:
"""Get the Source URL"""
return self.source_url

Expand Down
15 changes: 7 additions & 8 deletions pystac_monty/sources/desinventar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
MontyImpactType,
)
from pystac_monty.hazard_profiles import MontyHazardProfiles
from pystac_monty.sources.common import MontyDataTransformer
from pystac_monty.sources.common import DesInventarData, MontyDataSourceV3, MontyDataTransformer
from pystac_monty.validators.desinventar import (
STAC_EVENT_ID_PREFIX,
STAC_HAZARD_ID_PREFIX,
Expand Down Expand Up @@ -164,17 +164,16 @@ def parse_row_data(


# FIXME: cleanup named temporary file
class DesinventarDataSource:
class DesinventarDataSource(MontyDataSourceV3):
tmp_zip_file: tempfile._TemporaryFileWrapper
source_url: str | None
country_code: str
iso3: str

def __init__(self, tmp_zip_file: tempfile._TemporaryFileWrapper, country_code: str, iso3: str, source_url: str | None = None):
self.tmp_zip_file = tmp_zip_file
self.country_code = country_code
self.iso3 = iso3
self.source_url = source_url
def __init__(self, data: DesInventarData):
super().__init__(root=data)
self.tmp_zip_file = data.tmp_zip_file.path
self.country_code = data.country_code
self.iso3 = data.iso3

@classmethod
def from_zip_file(cls, zip_file: ZipFile, country_code: str, iso3: str):
Expand Down
1 change: 0 additions & 1 deletion pystac_monty/sources/emdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class EMDATDataSource(MontyDataSourceV3):

df: pd.DataFrame
file_path: str
source_url: str
data_source: Union[File, Memory]

def __init__(self, data: GenericDataSource):
Expand Down
2 changes: 2 additions & 0 deletions pystac_monty/sources/usgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import typing
from dataclasses import dataclass
from datetime import datetime
from enum import Enum

Expand Down Expand Up @@ -47,6 +48,7 @@ class USGSLossData(BaseModel):
data: GenericDataSource


@dataclass
class USGSDataSource(MontyDataSourceV3):
type: USGSSourceType
source_url: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ interactions:
Access-Control-Allow-Origin:
- '*'
Age:
- '0'
- '398'
Cache-Control:
- max-age=600
Connection:
Expand All @@ -147,11 +147,11 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 30 May 2025 06:34:39 GMT
- Thu, 05 Jun 2025 06:30:26 GMT
ETag:
- '"68362516-231e"'
- '"68362515-231e"'
Last-Modified:
- Tue, 27 May 2025 20:48:22 GMT
- Tue, 27 May 2025 20:48:21 GMT
Server:
- GitHub.com
Vary:
Expand All @@ -161,17 +161,17 @@ interactions:
X-Cache:
- HIT
X-Cache-Hits:
- '0'
- '3'
X-Fastly-Request-ID:
- 9f9f09ad2221afd85d14852d6401eb4f2c12feb6
- e4d6e8043d19816a287408e60606f714666e4b51
X-GitHub-Request-Id:
- 5F40:10F1EC:134EF0:172ED1:68394806
- 4E22:64F80:87F27:A7133:684137F4
X-Served-By:
- cache-maa10225-MAA
- cache-maa10226-MAA
X-Timer:
- S1748586879.013310,VS0,VE233
- S1749105027.752903,VS0,VE1
expires:
- Fri, 30 May 2025 06:04:15 GMT
- Thu, 05 Jun 2025 06:33:49 GMT
x-proxy-cache:
- MISS
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interactions:
Host:
- ifrcgo.org
User-Agent:
- Python-urllib/3.13
- Python-urllib/3.12
method: GET
uri: https://ifrcgo.org/monty-stac-extension/v1.0.0/schema.json
response:
Expand Down Expand Up @@ -137,7 +137,7 @@ interactions:
Access-Control-Allow-Origin:
- '*'
Age:
- '0'
- '402'
Cache-Control:
- max-age=600
Connection:
Expand All @@ -147,11 +147,11 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 30 May 2025 08:09:35 GMT
- Thu, 05 Jun 2025 06:30:31 GMT
ETag:
- '"68362516-231e"'
- '"68362515-231e"'
Last-Modified:
- Tue, 27 May 2025 20:48:22 GMT
- Tue, 27 May 2025 20:48:21 GMT
Server:
- GitHub.com
Vary:
Expand All @@ -163,15 +163,15 @@ interactions:
X-Cache-Hits:
- '0'
X-Fastly-Request-ID:
- 080038baf25e880038eff8ebe24c59d0029b2905
- 0979feb4560e90b0ff7cda2945f2dae7293b3f36
X-GitHub-Request-Id:
- 5F40:10F1EC:134EF0:172ED1:68394806
- 4E22:64F80:87F27:A7133:684137F4
X-Served-By:
- cache-maa10227-MAA
- cache-maa10222-MAA
X-Timer:
- S1748592576.517644,VS0,VE229
- S1749105032.564379,VS0,VE1
expires:
- Fri, 30 May 2025 06:04:15 GMT
- Thu, 05 Jun 2025 06:33:49 GMT
x-proxy-cache:
- MISS
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interactions:
Host:
- ifrcgo.org
User-Agent:
- Python-urllib/3.13
- Python-urllib/3.12
method: GET
uri: https://ifrcgo.org/monty-stac-extension/v1.0.0/schema.json
response:
Expand Down Expand Up @@ -137,7 +137,7 @@ interactions:
Access-Control-Allow-Origin:
- '*'
Age:
- '0'
- '403'
Cache-Control:
- max-age=600
Connection:
Expand All @@ -147,11 +147,11 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 30 May 2025 08:09:35 GMT
- Thu, 05 Jun 2025 06:30:31 GMT
ETag:
- '"68362516-231e"'
- '"68362515-231e"'
Last-Modified:
- Tue, 27 May 2025 20:48:22 GMT
- Tue, 27 May 2025 20:48:21 GMT
Server:
- GitHub.com
Vary:
Expand All @@ -163,15 +163,15 @@ interactions:
X-Cache-Hits:
- '1'
X-Fastly-Request-ID:
- d9b2de839b64e00a1b827aaf9f5f3be4c965eeca
- 60b661c66a2e62c55c4f3fd8c1a4186f37601210
X-GitHub-Request-Id:
- 5F40:10F1EC:134EF0:172ED1:68394806
- 4E22:64F80:87F27:A7133:684137F4
X-Served-By:
- cache-maa10236-MAA
- cache-maa10246-MAA
X-Timer:
- S1748592576.976088,VS0,VE2
- S1749105032.850369,VS0,VE2
expires:
- Fri, 30 May 2025 06:04:15 GMT
- Thu, 05 Jun 2025 06:33:49 GMT
x-proxy-cache:
- MISS
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interactions:
Host:
- ifrcgo.org
User-Agent:
- Python-urllib/3.13
- Python-urllib/3.12
method: GET
uri: https://ifrcgo.org/monty-stac-extension/v1.0.0/schema.json
response:
Expand Down Expand Up @@ -137,7 +137,7 @@ interactions:
Access-Control-Allow-Origin:
- '*'
Age:
- '0'
- '403'
Cache-Control:
- max-age=600
Connection:
Expand All @@ -147,11 +147,11 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 30 May 2025 08:09:36 GMT
- Thu, 05 Jun 2025 06:30:32 GMT
ETag:
- '"68362516-231e"'
- '"68362515-231e"'
Last-Modified:
- Tue, 27 May 2025 20:48:22 GMT
- Tue, 27 May 2025 20:48:21 GMT
Server:
- GitHub.com
Vary:
Expand All @@ -161,17 +161,17 @@ interactions:
X-Cache:
- HIT
X-Cache-Hits:
- '1'
- '2'
X-Fastly-Request-ID:
- f324a7226f606c69b43ad983df6bbb6235085e5e
- 468580308f9a6ef5698d0ee1e07b944b6e1134d2
X-GitHub-Request-Id:
- 5F40:10F1EC:134EF0:172ED1:68394806
- 4E22:64F80:87F27:A7133:684137F4
X-Served-By:
- cache-maa10229-MAA
- cache-maa10234-MAA
X-Timer:
- S1748592576.182067,VS0,VE1
- S1749105032.112576,VS0,VE1
expires:
- Fri, 30 May 2025 06:04:15 GMT
- Thu, 05 Jun 2025 06:33:49 GMT
x-proxy-cache:
- MISS
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ interactions:
Access-Control-Allow-Origin:
- '*'
Age:
- '6'
- '403'
Cache-Control:
- max-age=600
Connection:
Expand All @@ -147,11 +147,11 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- Fri, 30 May 2025 06:34:45 GMT
- Thu, 05 Jun 2025 06:30:32 GMT
ETag:
- '"68362516-231e"'
- '"68362515-231e"'
Last-Modified:
- Tue, 27 May 2025 20:48:22 GMT
- Tue, 27 May 2025 20:48:21 GMT
Server:
- GitHub.com
Vary:
Expand All @@ -161,17 +161,17 @@ interactions:
X-Cache:
- HIT
X-Cache-Hits:
- '1'
- '4'
X-Fastly-Request-ID:
- 7d28007237f6bc60f24a1a8b01656708bf608f24
- cd6f5bc3db7a0b1d45012b300aad504352f34fd1
X-GitHub-Request-Id:
- 5F40:10F1EC:134EF0:172ED1:68394806
- 4E22:64F80:87F27:A7133:684137F4
X-Served-By:
- cache-maa10237-MAA
- cache-maa10226-MAA
X-Timer:
- S1748586886.542049,VS0,VE2
- S1749105032.388272,VS0,VE1
expires:
- Fri, 30 May 2025 06:04:15 GMT
- Thu, 05 Jun 2025 06:33:49 GMT
x-proxy-cache:
- MISS
status:
Expand Down
Loading
Loading