Skip to content

Commit 643927b

Browse files
committed
Fix reading of json file
1 parent c1b33df commit 643927b

File tree

12 files changed

+99
-108
lines changed

12 files changed

+99
-108
lines changed

pystac_monty/sources/desinventar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ def __init__(self, data_source: DesinventarDataSource) -> None:
238238
super().__init__("desinventar")
239239
self.data_source = data_source
240240
self.events_collection_id = "desinventar-events"
241-
self.events_collection_url = "../../monty-stac-extension/examples/desinventar-events/desinventar-events.json" # noqa: E501
241+
self.events_collection_url = "./monty-stac-extension/examples/desinventar-events/desinventar-events.json" # noqa: E501
242242

243243
self.impacts_collection_id = "desinventar-impacts"
244-
self.impacts_collection_url = "../../monty-stac-extension/examples/desinventar-impacts/desinventar-impacts.json" # noqa: E501
244+
self.impacts_collection_url = "./monty-stac-extension/examples/desinventar-impacts/desinventar-impacts.json" # noqa: E501
245245

246246
def create_datetimes(self, row: DataRow) -> datetime | None:
247247
start_year = strtoi(row["year"], None)

pystac_monty/sources/emdat.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pandas as pd
77
import pytz
8-
import requests
98
from pystac import Collection, Item, Link
109
from shapely.geometry import Point, mapping
1110

@@ -52,13 +51,13 @@ class EMDATTransformer:
5251
"""
5352

5453
emdat_events_collection_id = "emdat-events"
55-
emdat_events_collection_url = ("../../monty-stac-extension/examples/emdat-events/emdat-events.json")
54+
emdat_events_collection_url = ("./monty-stac-extension/examples/emdat-events/emdat-events.json")
5655

5756
emdat_hazards_collection_id = "emdat-hazards"
58-
emdat_hazards_collection_url = ("../../monty-stac-extension/examples/emdat-hazards/emdat-hazards.json")
57+
emdat_hazards_collection_url = ("./monty-stac-extension/examples/emdat-hazards/emdat-hazards.json")
5958

6059
emdat_impacts_collection_id = "emdat-impacts"
61-
emdat_impacts_collection_url = ("../../monty-stac-extension/examples/emdat-impacts/emdat-impacts.json")
60+
emdat_impacts_collection_url = ("./monty-stac-extension/examples/emdat-impacts/emdat-impacts.json")
6261

6362
hazard_profiles = MontyHazardProfiles()
6463

@@ -296,21 +295,21 @@ def _get_row_by_disno(self, disno: str) -> Optional[pd.Series]:
296295

297296
def get_event_collection(self) -> Collection:
298297
"""Get event collection"""
299-
response = open(self.emdat_events_collection_url)
300-
collection_dict = json.loads(response.text)
301-
return Collection.from_dict(collection_dict)
298+
with open(self.emdat_events_collection_url, "r", encoding="utf-8") as f:
299+
response = json.load(f)
300+
return Collection.from_dict(response)
302301

303302
def get_hazard_collection(self) -> Collection:
304303
"""Get hazard collection"""
305-
response = open(self.emdat_hazards_collection_url)
306-
collection_dict = json.loads(response.text)
307-
return Collection.from_dict(collection_dict)
304+
with open(self.emdat_hazards_collection_url, "r", encoding="utf-8") as f:
305+
response = json.load(f)
306+
return Collection.from_dict(response)
308307

309308
def get_impact_collection(self) -> Collection:
310309
"""Get impact collection"""
311-
response = open(self.emdat_impacts_collection_url)
312-
collection_dict = json.loads(response.text)
313-
return Collection.from_dict(collection_dict)
310+
with open(self.emdat_impacts_collection_url, "r", encoding="utf-8") as f:
311+
response = json.load(f)
312+
return Collection.from_dict(response)
314313

315314
def _create_title_from_row(self, row: pd.Series) -> str | None:
316315
"""Create a descriptive title from row data when Event Name is missing"""

pystac_monty/sources/gdacs.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any, List
77

88
import pytz
9-
import requests
109
from markdownify import markdownify as md
1110
from pystac import Asset, Collection, Item, Link
1211
from shapely import simplify, to_geojson
@@ -60,13 +59,13 @@ class GDACSTransformer:
6059
"""
6160

6261
gdacs_events_collection_id = "gdacs-events"
63-
gdacs_events_collection_url = ("../../monty-stac-extension/examples/gdacs-events/gdacs-events.json")
62+
gdacs_events_collection_url = ("./monty-stac-extension/examples/gdacs-events/gdacs-events.json")
6463

6564
gdacs_hazards_collection_id = "gdacs-hazards"
66-
gdacs_hazards_collection_url = ("../../monty-stac-extension/examples/gdacs-hazards/gdacs-hazards.json")
65+
gdacs_hazards_collection_url = ("./monty-stac-extension/examples/gdacs-hazards/gdacs-hazards.json")
6766

6867
gdacs_impacts_collection_id = "gdacs-impacts"
69-
gdacs_impacts_collection_url = ("../../monty-stac-extension/examples/gdacs-impacts/gdacs-impacts.json")
68+
gdacs_impacts_collection_url = ("./monty-stac-extension/examples/gdacs-impacts/gdacs-impacts.json")
7069

7170
data: list[GDACSDataSource] = []
7271
hazard_profiles = MontyHazardProfiles()
@@ -92,19 +91,19 @@ def make_items(self) -> list[Item]:
9291
return items
9392

9493
def get_event_collection(self) -> Collection:
95-
response = open(self.gdacs_events_collection_url)
96-
collection_dict = json.loads(response.text)
97-
return Collection.from_dict(collection_dict)
94+
with open(self.gdacs_events_collection_url, "r", encoding="utf-8") as f:
95+
response = json.load(f)
96+
return Collection.from_dict(response)
9897

9998
def get_hazard_collection(self) -> Collection:
100-
response = open(self.gdacs_hazards_collection_url)
101-
collection_dict = json.loads(response.text)
102-
return Collection.from_dict(collection_dict)
99+
with open(self.gdacs_hazards_collection_url, "r", encoding="utf-8") as f:
100+
response = json.load(f)
101+
return Collection.from_dict(response)
103102

104103
def get_impact_collection(self) -> Collection:
105-
response = open(self.gdacs_impacts_collection_url)
106-
collection_dict = json.loads(response.text)
107-
return Collection.from_dict(collection_dict)
104+
with open(self.gdacs_impacts_collection_url, "r", encoding="utf-8") as f:
105+
response = json.load(f)
106+
return Collection.from_dict(response)
108107

109108
def check_and_get_event_data(self) -> GDACSDataSource:
110109
# first check that the event data is present in the data

pystac_monty/sources/gfd.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Any, List
44

55
import pytz
6-
import requests
76
from pystac import Collection, Item
87

98
from pystac_monty.extension import (
@@ -36,13 +35,13 @@ class GFDTransformer:
3635
"""Transform the source data into the STAC items"""
3736

3837
gfd_events_collection_id = "gfd-events"
39-
gfd_events_collection_url = "../../monty-stac-extension/examples/gfd-events/gfd-events.json" # noqa
38+
gfd_events_collection_url = "./monty-stac-extension/examples/gfd-events/gfd-events.json" # noqa
4039

4140
gfd_hazards_collection_id = "gfd-hazards"
42-
gfd_hazards_collection_url = "../../monty-stac-extension/examples/gfd-hazards/gfd-hazards.json" # noqa
41+
gfd_hazards_collection_url = "./monty-stac-extension/examples/gfd-hazards/gfd-hazards.json" # noqa
4342

4443
gfd_impacts_collection_id = "gfd-impacts"
45-
gfd_impacts_collection_url = "../../monty-stac-extension/examples/gfd-impacts/gfd-impacts.json" # noqa
44+
gfd_impacts_collection_url = "./monty-stac-extension/examples/gfd-impacts/gfd-impacts.json" # noqa
4645
hazard_profiles = MontyHazardProfiles()
4746

4847
def __init__(self, data: GFDDataSource):
@@ -63,23 +62,23 @@ def make_items(self) -> List[Item]:
6362

6463
return items
6564

66-
def get_event_collection(self, timeout: int = 30) -> Collection:
65+
def get_event_collection(self) -> Collection:
6766
"""Get Event collection"""
68-
response = open(self.gfd_events_collection_url)
69-
collection_dict = json.loads(response.text)
70-
return Collection.from_dict(collection_dict)
67+
with open(self.gfd_events_collection_url, "r", encoding="utf-8") as f:
68+
response = json.load(f)
69+
return Collection.from_dict(response)
7170

72-
def get_hazard_collection(self, timeout: int = 30) -> Collection:
71+
def get_hazard_collection(self) -> Collection:
7372
"""Get Hazard collection"""
74-
response = open(self.gfd_hazards_collection_url)
75-
collection_dict = json.loads(response.text)
76-
return Collection.from_dict(collection_dict)
73+
with open(self.gfd_hazards_collection_url, "r", encoding="utf-8") as f:
74+
response = json.load(f)
75+
return Collection.from_dict(response)
7776

78-
def get_impact_collection(self, timeout: int = 30) -> Collection:
77+
def get_impact_collection(self) -> Collection:
7978
"""Get Impact collection"""
80-
response = open(self.gfd_impacts_collection_url)
81-
collection_dict = json.loads(response.text)
82-
return Collection.from_dict(collection_dict)
79+
with open(self.gfd_impacts_collection_url, "r", encoding="utf-8") as f:
80+
response = json.load(f)
81+
return Collection.from_dict(response)
8382

8483
def _get_bounding_box(self, polygon: list):
8584
"""Get the bounding box from the polygon"""

pystac_monty/sources/gidd.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import Any, Dict, List
55

66
import pytz
7-
import requests
87
from pystac import Asset, Collection, Item, Link
98

109
from pystac_monty.extension import (
@@ -34,10 +33,10 @@ class GIDDTransformer:
3433
"""Transforms GIDD event data into STAC Items"""
3534

3635
gidd_events_collection_id = "gidd-events"
37-
gidd_events_collection_url = ("../../monty-stac-extension/examples/idmc-events/idmc-events.json")
36+
gidd_events_collection_url = ("./monty-stac-extension/examples/idmc-gidd-events/idmc-gidd-events.json")
3837
# TODO: Make these events collection id consistent
3938
gidd_hazards_collection_id = "idmc-gidd-impacts"
40-
gidd_hazards_collection_url = ("../../monty-stac-extension/examples/idmc-gidd-impacts/idmc-gidd-impacts.json")
39+
gidd_hazards_collection_url = ("./monty-stac-extension/examples/idmc-gidd-impacts/idmc-gidd-impacts.json")
4140
hazard_profiles = MontyHazardProfiles()
4241

4342
def __init__(self, data: GIDDDataSource) -> None:
@@ -65,20 +64,23 @@ def make_items(self) -> List[Item]:
6564

6665
return items
6766

68-
def get_event_collection(self, timeout: int = 30) -> Collection:
67+
def get_event_collection(self) -> Collection:
6968
"""Get the event collection"""
70-
response = open(self.gidd_events_collection_url)
69+
with open(self.gidd_events_collection_url, "r", encoding="utf-8") as f:
70+
response = json.load(f)
71+
7172
if response.status_code == 200:
72-
collection_dict = json.loads(response.text)
73+
collection_dict = json.loads(response)
7374
return Collection.from_dict(collection_dict)
7475
return Collection.from_dict({})
7576

76-
def get_impact_collection(self, timeout: int = 30) -> Collection:
77+
def get_impact_collection(self) -> Collection:
7778
"""Get the hazard collection"""
78-
response = open(self.gidd_hazards_collection_url)
79+
with open(self.gidd_hazards_collection_url, "r", encoding="utf-8") as f:
80+
response = json.load(f)
81+
7982
if response.status_code == 200:
80-
collection_dict = json.loads(response.text)
81-
return Collection.from_dict(collection_dict)
83+
return Collection.from_dict(response)
8284
return Collection.from_dict({})
8385

8486
def make_source_event_items(self) -> List[Item]:

pystac_monty/sources/glide.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from datetime import datetime
44
from typing import Any, List
55

6-
import requests
76
from pystac import Asset, Collection, Item, Link
87
from shapely.geometry import Point, mapping
98

@@ -28,9 +27,9 @@ class GlideTransformer:
2827

2928
hazard_profiles = MontyHazardProfiles()
3029

31-
glide_events_collection_url = ("../../monty-stac-extension/examples/glide-events/glide-events.json")
30+
glide_events_collection_url = ("./monty-stac-extension/examples/glide-events/glide-events.json")
3231

33-
glide_hazard_collection_url = ("../../monty-stac-extension/examples/glide-hazards/glide-hazards.json")
32+
glide_hazard_collection_url = ("./monty-stac-extension/examples/glide-hazards/glide-hazards.json")
3433

3534
def __init__(self, data: GlideDataSource) -> None:
3635
self.data = data
@@ -176,17 +175,17 @@ def make_date(self, event_date: dict) -> datetime:
176175
date = datetime.fromisoformat(formatted_date.replace("Z", "+00:00"))
177176
return date
178177

179-
def get_event_collection(self, timeout: int = 30) -> Collection:
178+
def get_event_collection(self) -> Collection:
180179
"""Get event collection"""
181-
response = open(self.glide_events_collection_url)
182-
collection_dict = json.loads(response.text)
183-
return Collection.from_dict(collection_dict)
180+
with open(self.glide_events_collection_url, "r", encoding="utf-8") as f:
181+
response = json.load(f)
182+
return Collection.from_dict(response)
184183

185-
def get_hazard_collection(self, timeout: int = 30) -> Collection:
184+
def get_hazard_collection(self) -> Collection:
186185
"""Get hazard collection"""
187-
response = open(self.glide_hazard_collection_url)
188-
collection_dict = json.loads(response.text)
189-
return Collection.from_dict(collection_dict)
186+
with open(self.glide_hazard_collection_url, "r", encoding="utf-8") as f:
187+
response = json.load(f)
188+
return Collection.from_dict(response)
190189

191190
def check_and_get_glide_events(self) -> list[Any]:
192191
"""Validate the source fields"""

pystac_monty/sources/idu.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any, Dict, List
66

77
import pytz
8-
import requests
98
from markdownify import markdownify as md
109
from pystac import Asset, Collection, Item, Link
1110
from shapely.geometry import Point, mapping
@@ -46,10 +45,9 @@ class IDUTransformer:
4645
"""Transform the source data into the STAC items"""
4746

4847
idu_events_collection_id = "idu-events"
49-
idu_events_collection_url = "../../monty-stac-extension/examples/idmc-events/idmc-events.json" # noqa
48+
idu_events_collection_url = "./monty-stac-extension/examples/idmc-idu-events/idmc-idu-events.json" # noqa
5049
idu_impacts_collection_id = "idu-impacts"
51-
# TODO: Update idu collection url after the PR is merged in monty-stac-extension
52-
idu_impacts_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/feature/update-idu-documentation/examples/idu-impacts/idu-impacts.json" # noqa
50+
idu_impacts_collection_url = "./monty-stac-extension/examples/idmc-idu-impacts/idmc-idu-impacts.json" # noqa
5351

5452
hazard_profiles = MontyHazardProfiles()
5553

@@ -68,21 +66,20 @@ def make_items(self) -> List[Item]:
6866

6967
return items
7068

71-
def get_event_collection(self, timeout: int = 30) -> Collection:
69+
def get_event_collection(self) -> Collection:
7270
"""Get the event collection"""
73-
response = open(self.idu_events_collection_url)
71+
with open(self.idu_events_collection_url, "r", encoding="utf-8") as f:
72+
response = json.load(f)
7473
if response.status_code == 200:
75-
collection_dict = json.loads(response.text)
76-
return Collection.from_dict(collection_dict)
74+
return Collection.from_dict(response)
7775
return Collection.from_dict({})
7876

79-
def get_impact_collection(self, timeout: int = 30) -> Collection:
77+
def get_impact_collection(self) -> Collection:
8078
"""Get the impact collection"""
81-
# TODO: Update this when the collection url is updated
82-
response = requests.get(self.idu_events_collection_url, timeout=timeout)
79+
with open(self.idu_events_collection_url, "r", encoding="utf-8") as f:
80+
response = json.load(f)
8381
if response.status_code == 200:
84-
collection_dict = json.loads(response.text)
85-
return Collection.from_dict(collection_dict)
82+
return Collection.from_dict(response)
8683
return Collection.from_dict({})
8784

8885
def make_source_event_items(self) -> List[Item]:

pystac_monty/sources/ifrc_events.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ def get_data(self) -> dict:
3333

3434
class IFRCEventTransformer:
3535
ifrcevent_events_collection_id = "ifrcevent-events"
36-
# TODO: Update collection url after IFRC DREF PR is merged
37-
ifrcevent_events_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/feature/collection-ifrc-event/examples/ifrcevent-events/ifrcevent_events.json" # noqa: E501
36+
ifrcevent_events_collection_url = "./monty-stac-extension/examples/ifrcevent-events/ifrcevent_events.json" # noqa: E501
3837
ifrcevent_impacts_collection_id = "ifrcevent-impacts"
39-
ifrcevent_impacts_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/feature/collection-ifrc-event/examples/ifrcevent-impacts/ifrcevent_impacts.json" # noqa: E501
38+
ifrcevent_impacts_collection_url = "./monty-stac-extension/examples/ifrcevent-impacts/ifrcevent_impacts.json" # noqa: E501
4039

4140
hazard_profiles = MontyHazardProfiles()
4241

@@ -48,17 +47,17 @@ def __init__(self, data: IFRCEventDataSource, geocoder: MontyGeoCoder):
4847

4948
def get_event_collection(self) -> Collection:
5049
"""Get event collection"""
51-
# TODO: Update request after collection url is updated
52-
response = requests.get(self.ifrcevent_events_collection_url)
53-
collection_dict = json.loads(response.text)
54-
return Collection.from_dict(collection_dict)
50+
with open(self.ifrcevent_events_collection_url, "r", encoding="utf-8") as f:
51+
response = json.load(f)
52+
53+
return Collection.from_dict(response)
5554

5655
def get_impact_collection(self) -> Collection:
5756
"""Get event collection"""
58-
# TODO: Update request after collection url is updated
59-
response = requests.get(self.ifrcevent_impacts_collection_url)
60-
collection_dict = json.loads(response.text)
61-
return Collection.from_dict(collection_dict)
57+
with open(self.ifrcevent_impacts_collection_url, "r", encoding="utf-8") as f:
58+
response = json.load(f)
59+
60+
return Collection.from_dict(response)
6261

6362
def make_items(self) -> List[Item]:
6463
"""Create items"""

0 commit comments

Comments
 (0)