|
20 | 20 | import click |
21 | 21 | import geopandas as gpd |
22 | 22 | import psycopg2.extras |
| 23 | +from flask import current_app |
23 | 24 | from flask.cli import with_appcontext |
24 | 25 | from shapely.geometry import MultiPolygon |
25 | 26 | from shapely.wkt import dumps as wkt_dumps |
|
32 | 33 | REQUIRED_REVISION = "a1b2c3d4e5f6" |
33 | 34 |
|
34 | 35 | # --------------------------------------------------------------------------- |
35 | | -# Chemins des fichiers GPKG |
| 36 | +# Chemins des fichiers GPKG (relatifs à ROOT_DIR, cf. backend/app.py) |
36 | 37 | # --------------------------------------------------------------------------- |
37 | | -GPKG_CADASTRE = "/home/thibaut/appli/app_oeasc/data/ref_geo/cadastres.gpkg" |
38 | | -GPKG_COMMUNES = "/home/thibaut/appli/app_oeasc/data/ref_geo/communes_aoa.gpkg" |
39 | | -GPKG_DGD = "/home/thibaut/appli/app_oeasc/data/ref_geo/forets_dgd.gpkg" |
40 | 38 |
|
41 | 39 |
|
42 | | -# GPKG_FORET_ONF = "/home/thibaut/appli/app_oeasc/data/ref_geo/forets_gestion_onf_aoa.gpkg" |
43 | | -GPKG_FORET_ONF = ( |
44 | | - "/home/thibaut/appli/app_oeasc/data/ref_geo/forets_gestion_onf_aoa.gpkg" |
45 | | -) |
46 | | -# GPKG_PARCELLE_ONF = "/home/thibaut/appli/app_oeasc/data/ref_geo/parcelles_forets_onf.gpkg" |
47 | | -GPKG_PARCELLE_ONF = ( |
48 | | - "/home/thibaut/appli/app_oeasc/data/ref_geo/parcelles_forets_onf_new.gpkg" |
49 | | -) |
50 | | -GPKG_UG_ONF = ( |
51 | | - "/home/thibaut/appli/app_oeasc/data/ref_geo/unites_gestion_forets_onf.gpkg" |
52 | | -) |
| 40 | +def _gpkg_path(filename): |
| 41 | + return os.path.join(current_app.config["ROOT_DIR"], "data", "ref_geo", filename) |
53 | 42 |
|
54 | 43 |
|
55 | 44 | # --------------------------------------------------------------------------- |
@@ -530,7 +519,7 @@ def step_load_communes(): |
530 | 519 | LOG.info("Étape 4a : chargement des communes depuis communes_aoa.gpkg") |
531 | 520 | print("Étape 4a : chargement des communes depuis communes_aoa.gpkg") |
532 | 521 | oeasc_geom = _get_oeasc_geom() |
533 | | - gdf = gpd.read_file(GPKG_COMMUNES) |
| 522 | + gdf = gpd.read_file(_gpkg_path("communes_aoa.gpkg")) |
534 | 523 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
535 | 524 | gdf_4326 = gdf.to_crs(epsg=4326) |
536 | 525 | LOG.info(f" {len(gdf)} communes dans le périmètre OEASC") |
@@ -629,7 +618,7 @@ def step_load_cadastres(): |
629 | 618 | LOG.info("Étape 4b : chargement des cadastres depuis cadastre_pec.gpkg") |
630 | 619 | print("Étape 4b : chargement des cadastres depuis cadastre_pec.gpkg") |
631 | 620 | oeasc_geom = _get_oeasc_geom() |
632 | | - gdf = gpd.read_file(GPKG_CADASTRE) |
| 621 | + gdf = gpd.read_file(_gpkg_path("cadastres.gpkg")) |
633 | 622 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
634 | 623 | gdf_4326 = gdf.to_crs(epsg=4326) |
635 | 624 | LOG.info(f" {len(gdf)} cadastres dans le périmètre OEASC") |
@@ -674,7 +663,7 @@ def step_build_sections(): |
674 | 663 | LOG.info("Étape 4c : construction des sections cadastrales par agrégation") |
675 | 664 | print("Étape 4c : construction des sections cadastrales par agrégation") |
676 | 665 | oeasc_geom = _get_oeasc_geom() |
677 | | - gdf = gpd.read_file(GPKG_CADASTRE) |
| 666 | + gdf = gpd.read_file(_gpkg_path("cadastres.gpkg")) |
678 | 667 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
679 | 668 |
|
680 | 669 | # Groupe par code_section = id_parcelle[:11] |
@@ -721,8 +710,9 @@ def step_load_foret_dgd(): |
721 | 710 | import fiona |
722 | 711 |
|
723 | 712 | oeasc_geom = _get_oeasc_geom() |
724 | | - layers = fiona.listlayers(GPKG_DGD) |
725 | | - gdf = gpd.read_file(GPKG_DGD, layer=layers[0]) |
| 713 | + gpkg_dgd = _gpkg_path("forets_dgd.gpkg") |
| 714 | + layers = fiona.listlayers(gpkg_dgd) |
| 715 | + gdf = gpd.read_file(gpkg_dgd, layer=layers[0]) |
726 | 716 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
727 | 717 | gdf_4326 = gdf.to_crs(epsg=4326) |
728 | 718 | LOG.info(f" {len(gdf)} forêts DGD dans le périmètre OEASC") |
@@ -823,7 +813,7 @@ def step_load_foret_onf(): |
823 | 813 | LOG.info("Étape 4e : chargement des forêts ONF") |
824 | 814 | print("Étape 4e : chargement des forêts ONF") |
825 | 815 | oeasc_geom = _get_oeasc_geom() |
826 | | - gdf = gpd.read_file(GPKG_FORET_ONF) |
| 816 | + gdf = gpd.read_file(_gpkg_path("forets_gestion_onf_aoa.gpkg")) |
827 | 817 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
828 | 818 | gdf_4326 = gdf.to_crs(epsg=4326) |
829 | 819 | LOG.info(f" {len(gdf)} forêts ONF dans le périmètre OEASC") |
@@ -905,7 +895,7 @@ def step_load_parcelle_onf(): |
905 | 895 | LOG.info("Étape 4f : chargement des parcelles ONF") |
906 | 896 | print("Étape 4f : chargement des parcelles ONF") |
907 | 897 | oeasc_geom = _get_oeasc_geom() |
908 | | - gdf = gpd.read_file(GPKG_PARCELLE_ONF) |
| 898 | + gdf = gpd.read_file(_gpkg_path("parcelles_forets_onf_new.gpkg")) |
909 | 899 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
910 | 900 | gdf_4326 = gdf.to_crs(epsg=4326) |
911 | 901 | LOG.info(f" {len(gdf)} parcelles ONF dans le périmètre OEASC") |
@@ -972,7 +962,7 @@ def step_load_ug_onf(): |
972 | 962 | LOG.info("Étape 4g : chargement des unités de gestion ONF") |
973 | 963 | print("Étape 4g : chargement des unités de gestion ONF") |
974 | 964 | oeasc_geom = _get_oeasc_geom() |
975 | | - gdf = gpd.read_file(GPKG_UG_ONF) |
| 965 | + gdf = gpd.read_file(_gpkg_path("unites_gestion_forets_onf.gpkg")) |
976 | 966 | # Exclure les UG dont CCOD_PRF n'est pas un entier (parcelles "U" gérées en 4f) |
977 | 967 | gdf = gdf[gdf["CCOD_PRF"].apply(lambda x: str(x).strip().isdigit())] |
978 | 968 | gdf = _filter_by_oeasc(gdf, oeasc_geom) |
|
0 commit comments