Aller au contenu

Update Data

Add new land occupancy data and update SLT + fibre.

Command

Bases: BaseCommand

Source code in back/iarbre_data/management/commands/update_data.py
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
class Command(BaseCommand):
    help = "Update and add new OCS data"

    def handle(self, *args, **options):
        """Update and add new OCS data"""
        for data_config in UPDATES:
            qs = Data.objects.filter(metadata=data_config["name"])
            qs_gdf = load_geodataframe_from_db(qs, fields=[])
            qs_gdf.crs = TARGET_PROJ
            start = time.time()
            gdf = gpd.read_file(
                DATA_DIR / data_config["file"], layer=data_config.get("layer_name")
            )
            gdf["geometry"] = gdf.geometry.force_2d()
            gdf = gdf.to_crs(TARGET_PROJ)
            print("Processing data.")
            datas = process_data(gdf, data_config)
            datas_gdf = gpd.GeoDataFrame(
                datas, geometry=[data["geometry"] for data in datas], crs=TARGET_PROJ
            )
            print("Intersecting with existing data in the DB")
            qs_union = qs_gdf.geometry.unary_union
            datas_gdf_union = datas_gdf.geometry.unary_union
            difference = datas_gdf_union.difference(qs_union)
            difference = gpd.GeoSeries([difference]).explode(index_parts=True)
            valid_differences = [
                {"geometry": geom, "factor": "Réseau Fibre"}
                for geom in difference.geometry
                if geom is not None
            ]

            save_geometries(valid_differences, data_config)
            print(f"Data {data_config['name']} updated in {time.time() - start:.2f}s")

handle(*args, **options)

Update and add new OCS data

Source code in back/iarbre_data/management/commands/update_data.py
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
def handle(self, *args, **options):
    """Update and add new OCS data"""
    for data_config in UPDATES:
        qs = Data.objects.filter(metadata=data_config["name"])
        qs_gdf = load_geodataframe_from_db(qs, fields=[])
        qs_gdf.crs = TARGET_PROJ
        start = time.time()
        gdf = gpd.read_file(
            DATA_DIR / data_config["file"], layer=data_config.get("layer_name")
        )
        gdf["geometry"] = gdf.geometry.force_2d()
        gdf = gdf.to_crs(TARGET_PROJ)
        print("Processing data.")
        datas = process_data(gdf, data_config)
        datas_gdf = gpd.GeoDataFrame(
            datas, geometry=[data["geometry"] for data in datas], crs=TARGET_PROJ
        )
        print("Intersecting with existing data in the DB")
        qs_union = qs_gdf.geometry.unary_union
        datas_gdf_union = datas_gdf.geometry.unary_union
        difference = datas_gdf_union.difference(qs_union)
        difference = gpd.GeoSeries([difference]).explode(index_parts=True)
        valid_differences = [
            {"geometry": geom, "factor": "Réseau Fibre"}
            for geom in difference.geometry
            if geom is not None
        ]

        save_geometries(valid_differences, data_config)
        print(f"Data {data_config['name']} updated in {time.time() - start:.2f}s")