-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine_points_yearly.py
37 lines (32 loc) · 1.18 KB
/
combine_points_yearly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import json
from collections import defaultdict
# Define directories
base_dir = '.'
points_dir = os.path.join(base_dir, 'points')
yearly_dir = os.path.join(base_dir, 'points_yearly')
os.makedirs(yearly_dir, exist_ok=True)
# Introduce the overwrite variable
overwrite = False
# Collect points by year
yearly_points = defaultdict(list)
for filename in os.listdir(points_dir):
if filename.endswith('_points.geojson'):
# Extract year from the filename
year = filename[:4]
if int(year) >= 2020:
with open(os.path.join(points_dir, filename), 'r') as file:
data = json.load(file)
yearly_points[year].extend(data.get('features', []))
# Write combined points for each year
for year, features in yearly_points.items():
yearly_geojson = {
"type": "FeatureCollection",
"features": features
}
# Check if the yearly file exists and overwrite is False
yearly_file_path = os.path.join(yearly_dir, f'{year}_points.geojson')
if os.path.exists(yearly_file_path) and not overwrite:
continue
with open(yearly_file_path, 'w') as outfile:
json.dump(yearly_geojson, outfile, indent=2)