-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
296 lines (256 loc) · 9.22 KB
/
models.py
File metadata and controls
296 lines (256 loc) · 9.22 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
__copyright__ = "Copyright 2025 TU Dresden / KOMET Project"
__author__ = "Daniel Nüst & KOMET Team"
__license__ = "AGPL v3"
from django.db import models
from django.utils.translation import gettext_lazy as _
from geomet import wkt as geomet_wkt
class AbstractGeometadata(models.Model):
"""
Abstract base model for geospatial and temporal metadata.
Geometry is stored as Well-Known Text (WKT) format, which is a standard
text representation of geometry objects. This allows storage without
requiring GeoDjango/PostGIS while remaining interoperable.
Supported WKT geometry types:
- POINT(lng lat)
- LINESTRING(lng1 lat1, lng2 lat2, ...)
- POLYGON((lng1 lat1, lng2 lat2, ..., lng1 lat1))
- MULTIPOINT, MULTILINESTRING, MULTIPOLYGON
- GEOMETRYCOLLECTION
Example: POLYGON((-10 35, 40 35, 40 70, -10 70, -10 35))
"""
# Spatial metadata
geometry_wkt = models.TextField(
blank=True,
null=True,
verbose_name=_("Geometry (WKT)"),
help_text=_(
"Geographic coverage in Well-Known Text (WKT) format. "
"Example: POLYGON((-10 35, 40 35, 40 70, -10 70, -10 35))"
),
)
# Bounding box for quick spatial queries (extracted from geometry)
bbox_north = models.FloatField(
blank=True,
null=True,
verbose_name=_("Bounding Box North"),
help_text=_("Northern latitude boundary (-90 to 90)"),
)
bbox_south = models.FloatField(
blank=True,
null=True,
verbose_name=_("Bounding Box South"),
help_text=_("Southern latitude boundary (-90 to 90)"),
)
bbox_east = models.FloatField(
blank=True,
null=True,
verbose_name=_("Bounding Box East"),
help_text=_("Eastern longitude boundary (-180 to 180)"),
)
bbox_west = models.FloatField(
blank=True,
null=True,
verbose_name=_("Bounding Box West"),
help_text=_("Western longitude boundary (-180 to 180)"),
)
# Human-readable place name(s)
place_name = models.CharField(
max_length=500,
blank=True,
null=True,
verbose_name=_("Place Name"),
help_text=_(
"Human-readable name(s) of the location(s), e.g., "
"'Vienna, Austria' or 'North Atlantic Ocean'"
),
)
# Administrative units for machine-readable coverage
# Comma-separated list of standardized names
admin_units = models.TextField(
blank=True,
null=True,
verbose_name=_("Administrative Units"),
help_text=_(
"Comma-separated list of administrative units covering this geometry, "
"e.g., 'Austria, Vienna, Wien Stadt'"
),
)
# Temporal metadata — list of [start, end] text pairs stored as JSON
temporal_periods = models.JSONField(
blank=True,
default=list,
verbose_name=_("Temporal Periods"),
help_text=_(
"List of time periods, each as [start, end] text pairs. "
'Example: [["2020-01", "2021-06"], ["Holocene", ""]]'
),
)
# Metadata about the metadata
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
indexes = [
# Composite index for spatial bounding box overlap queries
models.Index(
fields=["bbox_south", "bbox_north", "bbox_west", "bbox_east"],
name="%(class)s_bbox_idx",
),
]
def has_spatial_data(self):
"""Return True if this record has spatial metadata."""
return bool(self.geometry_wkt or self.place_name)
def has_temporal_data(self):
"""Return True if this record has temporal metadata."""
return bool(self.temporal_periods)
def get_temporal_display(self):
"""Return a list of formatted period strings for display.
Each period is rendered as "start – end", or just "start" / "end"
when one bound is empty.
"""
result = []
for period in self.temporal_periods or []:
start = period[0].strip() if period[0] else ""
end = period[1].strip() if period[1] else ""
if start and end:
result.append(f"{start} – {end}")
elif start:
result.append(start)
elif end:
result.append(end)
return result
def get_geometry_type(self):
"""Extract geometry type from WKT string."""
if not self.geometry_wkt:
return None
wkt = self.geometry_wkt.strip().upper()
for gtype in [
"GEOMETRYCOLLECTION",
"MULTIPOLYGON",
"MULTILINESTRING",
"MULTIPOINT",
"POLYGON",
"LINESTRING",
"POINT",
]:
if wkt.startswith(gtype):
return gtype
return None
def get_centroid(self):
"""
Calculate approximate centroid from bounding box.
Returns (lat, lng) tuple or None.
"""
if all(
v is not None
for v in [self.bbox_north, self.bbox_south, self.bbox_east, self.bbox_west]
):
lat = (self.bbox_north + self.bbox_south) / 2
lng = (self.bbox_east + self.bbox_west) / 2
return (lat, lng)
return None
def update_bbox_from_wkt(self):
"""
Parse WKT and update bounding box fields using the geomet library.
"""
if not self.geometry_wkt:
self.bbox_north = None
self.bbox_south = None
self.bbox_east = None
self.bbox_west = None
return
try:
geometry = geomet_wkt.loads(self.geometry_wkt)
coords = self._extract_all_coordinates(geometry)
if coords:
lngs = [c[0] for c in coords if -180 <= c[0] <= 180]
lats = [c[1] for c in coords if -90 <= c[1] <= 90]
if lngs and lats:
self.bbox_north = max(lats)
self.bbox_south = min(lats)
self.bbox_east = max(lngs)
self.bbox_west = min(lngs)
except Exception:
# If parsing fails, clear bbox fields
self.bbox_north = None
self.bbox_south = None
self.bbox_east = None
self.bbox_west = None
def _extract_all_coordinates(self, geometry):
"""
Recursively extract all coordinate pairs from a GeoJSON geometry.
Returns a flat list of [lng, lat] pairs.
"""
coords = []
geom_type = geometry.get("type")
if geom_type == "Point":
coords.append(geometry["coordinates"][:2])
elif geom_type in ("LineString", "MultiPoint"):
for coord in geometry["coordinates"]:
coords.append(coord[:2])
elif geom_type in ("Polygon", "MultiLineString"):
for ring in geometry["coordinates"]:
for coord in ring:
coords.append(coord[:2])
elif geom_type == "MultiPolygon":
for polygon in geometry["coordinates"]:
for ring in polygon:
for coord in ring:
coords.append(coord[:2])
elif geom_type == "GeometryCollection":
for geom in geometry.get("geometries", []):
coords.extend(self._extract_all_coordinates(geom))
return coords
def save(self, *args, **kwargs):
"""Update bounding box before saving."""
self.update_bbox_from_wkt()
super().save(*args, **kwargs)
def to_geojson(self):
"""
Convert geometry to GeoJSON format for use with Leaflet.
Returns a GeoJSON Feature dict or None.
"""
if not self.geometry_wkt:
return None
try:
geometry = geomet_wkt.loads(self.geometry_wkt)
return {
"type": "Feature",
"geometry": geometry,
"properties": {
"place_name": self.place_name or "",
"temporal_periods": self.temporal_periods or [],
},
}
except Exception:
return None
class ArticleGeometadata(AbstractGeometadata):
"""
Geospatial and temporal metadata for journal articles.
"""
article = models.OneToOneField(
"submission.Article",
on_delete=models.CASCADE,
related_name="geometadata",
verbose_name=_("Article"),
)
class Meta:
verbose_name = _("Article Geometadata")
verbose_name_plural = _("Article Geometadata")
def __str__(self):
return f"Geometadata for Article {self.article.pk}"
class PreprintGeometadata(AbstractGeometadata):
"""
Geospatial and temporal metadata for repository preprints.
"""
preprint = models.OneToOneField(
"repository.Preprint",
on_delete=models.CASCADE,
related_name="geometadata",
verbose_name=_("Preprint"),
)
class Meta:
verbose_name = _("Preprint Geometadata")
verbose_name_plural = _("Preprint Geometadata")
def __str__(self):
return f"Geometadata for Preprint {self.preprint.pk}"