summaryrefslogtreecommitdiff
path: root/ishtar_common/management/commands/migrate_to_geo_v4.py
blob: 63a07436313f1384a1f33d2ea4d9f664623bc636 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import datetime
from django.db import connection
from multiprocessing import Pool, Process
import os
import sys

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand

from ishtar_common.utils import ugettext_lazy as _, get_log_time, get_percent, get_eta

from ishtar_common import models_common, models
from archaeological_operations.models import Operation, ArchaeologicalSite
from archaeological_context_records.models import ContextRecord
from archaeological_finds.models import BaseFind
from archaeological_warehouse.models import Warehouse, Container


debug = False
log_path = os.sep.join([settings.ROOT_PATH, "logs"])
if not os.path.exists(log_path):
    os.mkdir(log_path, mode=0o770)


town_content_type = ContentType.objects.get(app_label="ishtar_common", model="town")
data_type, __ = models_common.GeoDataType.objects.get_or_create(
    txt_idx="town-limit", defaults={"label": "Limites commune"}
)
provider, __ = models_common.GeoProviderType.objects.get_or_create(
    txt_idx="france-ign", defaults={"label": "IGN"}
)

changed = []


def _process_town(town_id):
    connection.close()
    town = models_common.Town.objects.get(pk=town_id)
    attrs = {
        "name": town._generate_cached_label(),
        "source_content_type": town_content_type,
        "source_id": town.pk,
        "data_type": data_type,
        "provider": provider,
    }
    if town.limit:
        attrs["multi_polygon"] = town.limit
    else:
        attrs["point_2d"] = town.center
    data, created = models_common.GeoVectorData.objects.get_or_create(**attrs)
    town.main_geodata = data
    town._post_save_geo_ok = False
    town.save()
    if created:
        changed.append(["geovectordata", data.name, data.pk, "Création commune"])


model_slug, model_name, model_full_name, model = None, None, None, None
model_content_type, data_type_area, data_type_center = None, None, None

cls_labels = {
    "ContextRecord": ["Context Record", "Unité d'Enregistrement"],
    "BaseFind": ["Base find", "Mobilier d'origine"],
    "Operation": ["Operation", "Opération"],
    "ArchaeologicalSite": ["Entité (EA)", "Entité archéologique",
                           "Archaeological site"],
}


def _process_site_ope(obj):
    connection.close()
    if debug:
        base_debug = "_process_site_ope - l. {} - " + f"{obj} ({obj.pk}) - " + "{}"
    obj._no_move = True
    obj.skip_history_when_saving = True
    obj.save()  # auto manage geo town association
    q_towns = obj.towns.filter(main_geodata__multi_polygon__isnull=False)
    if q_towns.count() > 1:
        changed.append(
            [model_slug, str(obj), obj.pk, "Association géo de zone communale"]
        )
    elif q_towns.count() == 1:
        changed.append(
            [model_slug, str(obj), obj.pk, "Association géo de commune"]
        )
    obj_verbose_names = cls_labels[obj.__class__.__name__]
    if obj.multi_polygon_source == "P" and obj.multi_polygon \
            and obj.multi_polygon_source_item in obj_verbose_names:
        if debug:
            print(base_debug.format(95, "multi_polygon"))
        attrs = {
            "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
            "source_content_type": model_content_type,
            "source_id": obj.pk,
            "multi_polygon": obj.multi_polygon,
            "data_type": data_type_area,
        }
        data = models_common.GeoVectorData.objects.create(**attrs)
        obj.main_geodata = data
        obj._post_save_geo_ok = False
        obj.save()
        changed.append(
            [
                "geovectordata",
                data.name,
                data.pk,
                f"Multi-polygone {model_name}",
            ]
        )
    if obj.point_source == "P" and obj.point_2d \
            and obj.point_source_item in obj_verbose_names:
        if obj.x and obj.y:
            if debug:
                print(base_debug.format(95, "point - coordinates"))
            attrs = {
                "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
                "source_content_type": model_content_type,
                "source_id": obj.pk,
                "data_type": data_type_center,
                "x": obj.x,
                "y": obj.y,
                "z": obj.z,
                "spatial_reference_system": obj.spatial_reference_system
            }
            data = models_common.GeoVectorData.objects.create(**attrs)
            obj.main_geodata = data
            obj.save()
            changed.append(
                [
                    "geovectordata",
                    data.name,
                    data.pk,
                    f"Coordonnées {model_name}",
                ]
            )
        elif obj.point_2d:
            if debug:
                print(base_debug.format(95, "point_2d"))
            attrs = {
                "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
                "source_content_type": model_content_type,
                "source_id": obj.pk,
                "data_type": data_type_center,
            }
            if obj.point:
                attrs["point_3d"] = obj.point
            else:
                attrs["point_2d"] = obj.point_2d
            data = models_common.GeoVectorData.objects.create(**attrs)
            obj.main_geodata = data
            obj._post_save_geo_ok = False
            obj.save()
            changed.append(
                ["geovectordata", data.name, data.pk, f"Point {model_name}"]
            )

data_type_outline = None


def _process_main(obj):
    connection.close()
    obj._no_move = True
    obj.skip_history_when_saving = True
    obj.save()  # auto manage geo town association

    if obj.main_geodata:
        changed.append(
            [model_slug, str(obj), obj.pk, "Association géo de zone communale"]
        )
    obj_verbose_names = cls_labels[obj.__class__.__name__]
    if obj.multi_polygon_source == "P" and obj.multi_polygon \
            and (obj.multi_polygon_source_item in obj_verbose_names or
                 obj.multi_polygon_source_item in (None, "")):
        attrs = {
            "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
            "source_content_type": model_content_type,
            "source_id": obj.pk,
            "multi_polygon": obj.multi_polygon,
            "data_type": data_type_outline,
        }
        data = models_common.GeoVectorData.objects.create(**attrs)
        obj.main_geodata = data
        obj._post_save_geo_ok = False
        obj.save()
        changed.append(
            [
                "geovectordata",
                data.name,
                data.pk,
                f"Multi-polygone {model_name}",
            ]
        )
    if obj.point_source == "P" and obj.point_2d \
            and (obj.point_source_item in obj_verbose_names or
                 obj.point_source_item in (None, "")):
        if obj.x and obj.y:
            attrs = {
                "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
                "source_content_type": model_content_type,
                "source_id": obj.pk,
                "data_type": data_type_center,
                "x": obj.x,
                "y": obj.y,
                "z": obj.z,
                "spatial_reference_system": obj.spatial_reference_system
            }
            data = models_common.GeoVectorData.objects.create(**attrs)
            obj.main_geodata = data
            obj._post_save_geo_ok = False
            obj.save()
            changed.append(
                [
                    "geovectordata",
                    data.name,
                    data.pk,
                    f"Coordonnées {model_name}",
                ]
            )
        elif obj.point_2d:
            attrs = {
                "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}",
                "source_content_type": model_content_type,
                "source_id": obj.pk,
                "data_type": data_type_center,
            }
            if obj.point:
                attrs["point_3d"] = obj.point
            else:
                attrs["point_2d"] = obj.point_2d
            data = models_common.GeoVectorData.objects.create(**attrs)
            obj.main_geodata = data
            obj._post_save_geo_ok = False
            obj.save()
            changed.append(
                ["geovectordata", data.name, data.pk, f"Point {model_name}"]
            )


def _process_simple(obj):
    connection.close()
    obj._post_save_geo_ok = False
    obj._no_move = True
    obj.skip_history_when_saving = True
    obj.save()


idx = 0
total = 0
ref_time = None


def write_output(arg):
    global idx, total, model_name, ref_time, quiet
    if quiet:
        return
    idx = idx + 1
    lbl = f"\r[{get_percent(idx, total)}] Migrate {model_name}s {idx + 1}/{total}"
    if ref_time:
        lbl += f" ({get_eta(idx, total, ref_time, datetime.datetime.now())} left)"
    sys.stdout.write(lbl)
    sys.stdout.flush()


def launch_job(lst, name, process_number, process_func):
    global idx, total, model_name, ref_time
    idx, total, model_name, ref_time = 0, len(lst), name, datetime.datetime.now()
    pool = None
    if process_number > 1:
        pool = Pool(processes=process_number)
    for item in lst:
        if pool:
            pool.apply_async(process_func, (item,), callback=write_output)
        else:
            process_func(item)
            write_output(None)
    if pool:
        pool.close()
        pool.join()


quiet = False


def migrate(log=True, process_number=1):
    global idx
    # create towns
    q = models_common.Town.objects.exclude(
        center__isnull=True, limit__isnull=True
    ).exclude(main_geodata__isnull=False).distinct()
    town_ids = list(q.values_list("id", flat=True))
    idx = 0
    launch_job(town_ids, "town", process_number, _process_town)

    model_list = [
        ("operation", "opération", "de l'opération", Operation),
        ("archaeologicalsite", "site", "du site", ArchaeologicalSite),
    ]

    global model_slug, model_name, model_full_name, model
    global model_content_type, data_type_area, data_type_center, data_type_outline
    for model_slug, model_name, model_full_name, model in model_list:
        connection.close()
        # manage operation vector sources
        model_content_type = ContentType.objects.get(
            app_label="archaeological_operations", model=model_slug
        )
        data_type_area, __ = models_common.GeoDataType.objects.get_or_create(
            txt_idx=f"{model_slug}-area",
            defaults={"label": f"Emprise {model_full_name}"},
        )
        data_type_center, __ = models_common.GeoDataType.objects.get_or_create(
            txt_idx=f"{model_slug}-center",
            defaults={"label": f"Centre {model_full_name}"},
        )
        q = model.objects.exclude(main_geodata__isnull=False)
        launch_job(list(q.all()), model_name, process_number, _process_site_ope)

        if not quiet:
            sys.stdout.write(
                f"\r[{get_log_time()}] {model_name.capitalize()} migrated"
                + " " * 20
                + "\n"
            )
            sys.stdout.flush()

    model_list = [
        (
            "archaeological_context_records",
            "contextrecord",
            "unité d'enregistrement",
            "de l'unité d'enregistrement",
            ContextRecord,
        ),
        (
            "archaeological_finds",
            "basefind",
            "mobilier d'origine",
            "du mobilier d'origine",
            BaseFind,
        ),
    ]
    for app, model_slug, model_name, model_full_name, model in model_list:
        connection.close()
        model_content_type = ContentType.objects.get(app_label=app, model=model_slug)
        data_type_outline, __ = models_common.GeoDataType.objects.get_or_create(
            txt_idx=f"{model_slug}-outline",
            defaults={"label": f"Contour {model_full_name}"},
        )
        data_type_center, __ = models_common.GeoDataType.objects.get_or_create(
            txt_idx=f"{model_slug}-center",
            defaults={"label": f"Centre {model_full_name}"},
        )
        q = model.objects.exclude(main_geodata__isnull=False)
        launch_job(q.all(), model_name, process_number, _process_main)

        if not quiet:
            sys.stdout.write(
                f"\r[{get_log_time()}] {model_name.capitalize()} migrated"
                + " " * 20
                + "\n"
            )
            sys.stdout.flush()
    model_list = [Warehouse, Container]
    for model in model_list:
        connection.close()
        q = model.objects.exclude(main_geodata__isnull=False)
        launch_job(q.all(), model.__name__, process_number, _process_simple)

        if not quiet:
            sys.stdout.write(
                f"\r[{get_log_time()}] {model.__name__.capitalize()} migrated" + " " * 20 + "\n"
            )
            sys.stdout.flush()

    if log and changed:
        filename = f"geo_migration-created-{get_log_time().replace(':', '')}.csv"
        path = os.sep.join([log_path, filename])
        with open(path, "w+") as fle:
            writer = csv.writer(fle)
            writer.writerow(["model", "name", "id", "context"])
            for change in changed:
                writer.writerow(change)
        if not quiet:
            sys.stdout.write(f"[{get_log_time()}] Log: {path} written\n")


class Command(BaseCommand):
    help = "Migrate to new geo data management"

    def add_arguments(self, parser):
        parser.add_argument(
            "--process", dest="process", help="Number of process"
        )
        parser.add_argument(
            "--quiet", dest="quiet", action="store_true", help="Quiet output"
        )
        parser.add_argument(
            "--log", dest="log", action="store_false", help="Log into a file"
        )

    def handle(self, *args, **options):
        log = options["log"]
        global quiet
        quiet = options["quiet"]
        if not quiet:
            sys.stdout.write(f"[{get_log_time()}] Processing migration\n")
        errors = migrate(log=log, process_number=int(options["process"] or 1))
        if not errors:
            if not quiet:
                sys.stdout.write(f"[{get_log_time()}] Migration finished\n")
            sys.exit()
        if not quiet:
            sys.stdout.write("\n".join(errors))
        sys.exit(1)