-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmodels.py
640 lines (513 loc) · 19.4 KB
/
models.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import calendar
from datetime import timedelta
import logging
import json
import uuid
import re
import os
import math
import time
from hurry.filesize import size
from django.contrib.humanize.templatetags import humanize
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.contrib.gis.db import models
from django.contrib.postgres.fields import ArrayField
from django.db.models.fields import CharField
from django.utils import timezone
from django.core.exceptions import ValidationError
from collections import namedtuple
import mercantile
from utils.aoi_utils import simplify_geom, force2d
from django.contrib import admin
import rasterio
from rasterio import mask
from osm_export_tool.mapping import Mapping
from hdx_exports.hdx_export_set import HDXExportSet
LOG = logging.getLogger(__name__)
MAX_TILE_COUNT = 10000
DIR = os.path.dirname(os.path.abspath(__file__))
RASTER = rasterio.open(os.path.join(DIR, "osm_nodes.tif"))
Group.add_to_class("is_partner", models.BooleanField(null=False, default=False))
def get_geodesic_area(geom):
bbox = geom.envelope
"""
Uses the algorithm to calculate geodesic area of a polygon from OpenLayers 2.
See http://bit.ly/1Mite1X.
Args:
geom (GEOSGeometry): the export extent as a GEOSGeometry.
Returns
area (float): the geodesic area of the provided geometry.
"""
area = 0.0
coords = bbox.coords[0]
length = len(coords)
if length > 2:
for x in range(length - 1):
p1 = coords[x]
p2 = coords[x + 1]
area += math.radians(p2[0] - p1[0]) * (
2 + math.sin(math.radians(p1[1])) + math.sin(math.radians(p2[1]))
)
area = abs(int(area * 6378137 * 6378137 / 2.0 / 1000 / 1000))
return area
MAX_NODES = 15000000
ValidateResult = namedtuple("ValidateResult", ["valid", "message", "params"])
def check_extent(aoi, url):
if not aoi.valid:
return ValidateResult(False, aoi.valid_reason, None)
aoi.srid = 4326
transformed = aoi.transform(3857, clone=True)
masked = mask.mask(RASTER, [json.loads(transformed.json)], all_touched=False)
nodes = masked[0].sum() * 1000
if nodes > MAX_NODES:
return ValidateResult(
False,
"The selected area's bounding box contains about %(nodes)s nodes.\
The maximum is %(maxnodes)s. Please choose a smaller area.",
{"nodes": nodes, "maxnodes": MAX_NODES},
)
return ValidateResult(True, None, None)
def validate_export_formats(value):
if not value:
raise ValidationError("Must choose at least one export format.")
for format_name in value:
if format_name not in [
"shp",
"geojson",
"fgb",
"mvt",
"pmtiles",
"csv",
"sql",
"geopackage",
"garmin_img",
"kml",
"mwm",
"osmand_obf",
"osm_pbf",
"osm_xml",
"bundle",
"mbtiles",
"full_pbf",
]:
raise ValidationError(
"Bad format name: %(format_name)s",
params={"format_name": format_name},
)
def validate_feature_selection(value):
from osm_export_tool.mapping import Mapping
m, errors = Mapping.validate(value)
if not m:
raise ValidationError(errors)
def validate_aoi(aoi):
result = check_extent(aoi, settings.OVERPASS_API_URL)
if not result.valid:
raise ValidationError(result.message, params=result.params)
def validate_mbtiles(job):
if "mbtiles" in job["export_formats"]:
if job.get("mbtiles_source") is None:
raise ValidationError(
"A source is required when generating an MBTiles archive."
)
if job.get("mbtiles_maxzoom") is None or job.get("mbtiles_minzoom") is None:
raise ValidationError(
"A zoom range must be provided when generating an MBTiles archive."
)
bounds = job["the_geom"].extent
tile_count = 0
for z in range(int(job["mbtiles_minzoom"]), int(job["mbtiles_maxzoom"])):
sw = mercantile.tile(*bounds[0:2], zoom=z)
ne = mercantile.tile(*bounds[2:4], zoom=z)
width = 1 + ne[0] - sw[0]
height = 1 + sw[1] - ne[1]
tile_count += width * height
if tile_count > MAX_TILE_COUNT:
raise ValidationError(
"%(tile_count)s tiles would be rendered; please reduce the zoom range, the size of your AOI, or split the export into pieces covering specific areas in order to render fewer than %(max_tile_count)s in each.",
params={"tile_count": tile_count, "max_tile_count": MAX_TILE_COUNT},
)
class Job(models.Model):
"""
Database model for an 'Export'.
Immutable, except in the case of HDX Export Regions.
"""
id = models.AutoField(primary_key=True, editable=False)
uid = models.UUIDField(
unique=True, default=uuid.uuid4, editable=False, db_index=True
)
user = models.ForeignKey(User, related_name="owner", on_delete=models.CASCADE)
name = models.CharField(max_length=100, db_index=True, blank=False)
description = models.CharField(
max_length=1000, db_index=True, default="", blank=True
)
event = models.CharField(max_length=100, db_index=True, default="", blank=True)
export_formats = ArrayField(
models.CharField(max_length=10),
validators=[validate_export_formats],
blank=False,
)
published = models.BooleanField(default=False, db_index=True)
the_geom = models.GeometryField(
verbose_name="Uploaded geometry", srid=4326, blank=False
)
simplified_geom = models.GeometryField(
verbose_name="Simplified geometry", srid=4326, blank=True, null=True
)
feature_selection = models.TextField(
blank=False, validators=[validate_feature_selection]
)
created_at = models.DateTimeField(default=timezone.now, editable=False)
updated_at = models.DateTimeField(default=timezone.now, editable=False)
mbtiles_maxzoom = models.IntegerField(null=True, blank=True)
mbtiles_minzoom = models.IntegerField(null=True, blank=True)
mbtiles_source = models.TextField(null=True, blank=True)
# flags
buffer_aoi = models.BooleanField(default=False)
unlimited_extent = models.BooleanField(default=False)
hidden = models.BooleanField(default=False) # hidden from the list page
expire_old_runs = models.BooleanField(default=True)
pinned = models.BooleanField(default=False)
unfiltered = models.BooleanField(default=False)
userinfo = models.BooleanField(default=False)
class Meta: # pragma: no cover
managed = True
db_table = "jobs"
@property
def last_run_status(self):
if self.runs.count() > 0:
return self.runs.all()[self.runs.count() - 1].status
@property
def last_run_date(self):
if self.runs.count() > 0:
return self.runs.all()[self.runs.count() - 1].started_at
@property
def is_hdx(self):
if HDXExportRegion.objects.filter(job_id=self.id).exists():
return True
return False
@property
def osma_link(self):
bounds = self.the_geom.extent
return "http://osm-analytics.org/#/show/bbox:{0},{1},{2},{3}/buildings/recency".format(
*bounds
)
@property
def area(self):
return get_geodesic_area(self.the_geom)
def save(self, *args, **kwargs):
self.the_geom = force2d(self.the_geom)
self.simplified_geom = simplify_geom(
self.the_geom, force_buffer=self.buffer_aoi
)
super(Job, self).save(*args, **kwargs)
def __str__(self):
return str(self.uid)
class SavedFeatureSelection(models.Model):
"""Mutable database record for a saved YAML configuration."""
created_at = models.DateTimeField(
default=timezone.now, db_index=True, editable=False
)
updated_at = models.DateTimeField(default=timezone.now, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, db_index=True, blank=False)
description = models.CharField(
max_length=1000, db_index=True, default="", blank=True
)
yaml = models.TextField(blank=False, validators=[validate_feature_selection])
public = models.BooleanField(default=False)
deleted = models.BooleanField(default=False)
uid = models.UUIDField(
unique=True, default=uuid.uuid4, editable=False, db_index=True
)
pinned = models.BooleanField(default=False)
def __str__(self):
return str(self.name)
PERIOD_CHOICES = (
("6hrs", "Every 6 hours"),
("daily", "Every day"),
("weekly", "Every Sunday"),
("2wks", "Every two weeks"),
("3wks", "Every three weeks"),
("monthly", "The 1st of every month"),
("quarterly", "Every quarter (90 days)"),
("semiyearly", "Every 6 months"),
("yearly", "Every year"),
("disabled", "Disabled"),
)
HOUR_CHOICES = list(zip(range(0, 24), range(0, 24)))
class RegionMixin:
@property
def last_run(self): # noqa
if self.job.runs.count() > 0:
last_run_time = (
self.job.runs.all()[self.job.runs.count() - 1].finished_at
or self.job.runs.all()[self.job.runs.count() - 1].started_at
or self.job.runs.all()[self.job.runs.count() - 1].created_at
)
return last_run_time
@property
def last_run_status(self):
if self.job.runs.count() > 0:
return self.job.runs.all()[self.job.runs.count() - 1].status
@property
def last_run_duration(self):
if self.job.runs.count() > 0:
i = 1
last_run_duration = None
while (
last_run_duration is None
): # get previous run size if current is running/submitted
if i >= self.job.runs.count() and i != 1:
break
last_run_duration = self.job.runs.all()[
self.job.runs.count() - i
].duration
i += 1
return time.strftime("%H:%M:%S", time.gmtime(last_run_duration))
@property
def last_size(self):
if self.job.runs.count() > 0:
i = 1
last_run_size = 0
while (
last_run_size == 0
): # get previous run size if current is running/submitted
if i >= self.job.runs.count() and i != 1:
break
last_run_size = self.job.runs.all()[self.job.runs.count() - i].size
i += 1
return last_run_size
@property
def last_export_size(self):
if self.last_size:
return size(self.last_size)
@property
def last_run_hdx_sync(self):
if self.job.runs.count() > 0:
return self.job.runs.all()[self.job.runs.count() - 1].hdx_sync_status
@property
def next_run_hum(self):
return humanize.naturaltime(self.next_run)
@property
def last_run_hum(self):
return humanize.naturaltime(self.last_run)
@property
def next_run(self): # noqa
now = timezone.now().replace(minute=0, second=0, microsecond=0)
if self.schedule_period == "6hrs":
delta = 6 - (self.schedule_hour + now.hour % 6)
return now + timedelta(hours=delta)
now = now.replace(hour=self.schedule_hour)
if self.schedule_period == "daily":
anchor = now
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=1)
if self.schedule_period == "weekly":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=7)
if self.schedule_period == "2wks":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=14)
if self.schedule_period == "3wks":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=21)
if self.schedule_period == "monthly":
(_, num_days) = calendar.monthrange(now.year, now.month)
anchor = now.replace(day=1)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=num_days)
if self.schedule_period == "quarterly":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=90)
if self.schedule_period == "semiyearly":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=180)
if self.schedule_period == "yearly":
# adjust so the week starts on Sunday
anchor = now - timedelta((now.weekday() + 1) % 7)
if timezone.now() < anchor:
return anchor
return anchor + timedelta(days=365)
@property
def delta(self): # noqa
if self.schedule_period == "6hrs":
return timedelta(hours=6)
if self.schedule_period == "daily":
return timedelta(days=1)
if self.schedule_period == "weekly":
return timedelta(days=7)
if self.schedule_period == "2wks":
return timedelta(days=14)
if self.schedule_period == "3wks":
return timedelta(days=21)
if self.schedule_period == "monthly":
return timedelta(days=31)
if self.schedule_period == "quarterly":
return timedelta(days=90)
if self.schedule_period == "semiyearly":
return timedelta(days=189)
if self.schedule_period == "yearly":
return timedelta(days=365)
@property
def feature_selection(self): # noqa
return self.job.feature_selection
@property
def the_geom(self):
return self.job.the_geom
@property
def simplified_geom(self): # noqa
return self.job.simplified_geom
@property
def job_uid(self):
return self.job.uid
@property
def created_by(self):
return self.job.user
@property
def export_formats(self): # noqa
return self.job.export_formats
class PartnerExportRegion(models.Model, RegionMixin):
schedule_period = models.CharField(
blank=False, max_length=10, default="disabled", choices=PERIOD_CHOICES
)
schedule_hour = models.IntegerField(blank=False, choices=HOUR_CHOICES, default=0)
job = models.ForeignKey(Job, null=True, on_delete=models.SET_NULL)
# the owning group, which determines access control.
group = models.ForeignKey(Group, on_delete=models.CASCADE)
deleted = models.BooleanField(default=False)
planet_file = models.BooleanField(default=False)
polygon_centroid = models.BooleanField(default=False)
@property
def export_formats(self): # noqa
return self.job.export_formats
@property
def name(self): # noqa
return self.job.name
@property
def description(self):
return self.job.description
@property
def event(self):
return self.job.event
@property
def group_name(self):
return self.group.name
class HDXExportRegion(models.Model, RegionMixin): # noqa
"""Mutable database table for hdx - additional attributes on a Job."""
schedule_period = models.CharField(
blank=False, max_length=10, default="disabled", choices=PERIOD_CHOICES
)
schedule_hour = models.IntegerField(blank=False, choices=HOUR_CHOICES, default=0)
deleted = models.BooleanField(default=False)
# a job should really be required, but that interferes with DRF validation lifecycle.
job = models.ForeignKey(
Job, null=True, related_name="hdx_export_region_set", on_delete=models.SET_NULL
)
is_private = models.BooleanField(default=False)
locations = ArrayField(models.CharField(blank=False, max_length=32), null=True)
license = models.CharField(max_length=32, null=True, blank=True)
subnational = models.BooleanField(default=True)
extra_notes = models.TextField(null=True, blank=True)
planet_file = models.BooleanField(default=False)
class Meta: # noqa
db_table = "hdx_export_regions"
def __str__(self):
return self.name + " (prefix: " + self.dataset_prefix + ")"
def clean(self):
if self.job and not re.match(r"^[a-z0-9-_]+$", self.job.name):
raise ValidationError(
{"dataset_prefix": "Invalid dataset_prefix: {0}".format(self.job.name)}
)
@property
def buffer_aoi(self): # noqa
return self.job.buffer_aoi
@property
def name(self): # noqa
return self.job.description
@property
def dataset_prefix(self): # noqa
return self.job.name
@property
def datasets(self): # noqa
export_set = HDXExportSet(
Mapping(self.feature_selection),
self.dataset_prefix,
self.name,
self.extra_notes,
)
return export_set.dataset_links(settings.HDX_URL_PREFIX)
@property
def update_frequency(self):
"""Update frequencies in HDX form."""
# hdx requirements
# "-2": "As needed",
# "-1": "Never",
# "0": "Live",
# "1": "Every day",
# "7": "Every week",
# "14": "Every two weeks",
# "30": "Every month",
# "90": "Every three months",
# "180": "Every six months",
# "365": "Every year",
# "as needed": "-2",
# "adhoc": "-2",
# "never": "-1",
# "live": "0",
# "every day": "1",
# "every week": "7",
# "every two weeks": "14",
# "every month": "30",
# "every three months": "90",
# "every quarter": "90",
# "every six months": "180",
# "every year": "365",
# "daily": "1",
# "weekly": "7",
# "fortnightly": "14",
# "every other week": "14",
# "monthly": "30",
# "quarterly": "90",
# "semiannually": "180",
# "semiyearly": "180",
# "annually": "365",
# "yearly": "365",
if self.schedule_period == "6hrs":
return 1
if self.schedule_period == "daily":
return 1
if self.schedule_period == "weekly":
return 7
if self.schedule_period == "2wks":
return 14
if self.schedule_period == "3wks":
return 30
if self.schedule_period == "monthly":
return 30
if self.schedule_period == "quarterly":
return 90
if self.schedule_period == "semiyearly":
return 180
if self.schedule_period == "yearly":
return 365
return -2 # returning as needed as default instead of live