Aller au contenu

C01 Insert Cities And Iris

Insert cities and IRIS from geojson file and BPCE API.

Command

Bases: BaseCommand

Source code in back/iarbre_data/management/commands/c01_insert_cities_and_iris.py
 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
class Command(BaseCommand):
    help = "Insert cities geojson file"

    @staticmethod
    def _insert_iris(qs_city: QuerySet) -> None:
        """Use BPCE API to download IRIS and insert them in a table
        Args:
            qs_city (QuerySet): Query set that correspond to one or multiple cities.
        """
        cities = load_geodataframe_from_db(qs_city, ["id", "name", "code"])
        limit = 100
        for city in cities.itertuples():
            city_GEOS = GEOSGeometry(city.geometry.wkt)
            city_GEOS.srid = TARGET_PROJ
            print(f"Dowloading IRIS for city: {city.name}.")
            if city.name[:4].upper() == "LYON":
                com_code = 69123
            else:
                com_code = city.code
            offset = 0
            params = {
                "where": f"com_code={int(com_code)}",
                "limit": limit,
                "offset": offset,
            }
            response = requests.get(api_url, params=params)
            while response.status_code == 200:
                data = response.json()
                records = data.get("results", [])
                if len(records) == 0:
                    break
                for record in tqdm(records, total=len(records)):
                    geometry = record["geo_shape"]["geometry"]
                    iris_name = record.get("iris_name")[0]
                    iris_code = record.get("iris_code")[0]
                    if geometry and iris_name and iris_code:
                        geom = GEOSGeometry(str(geometry))
                        geom.transform(TARGET_PROJ, clone=False)
                        if geom.intersects(
                            city_GEOS
                        ):  # for Lyon keep only iris for the 'arrondissement' at hand
                            Iris.objects.update_or_create(
                                city_id=city.id,
                                code=iris_code,
                                defaults={
                                    "geometry": geom,
                                    "name": iris_name,
                                },
                            )
                    else:
                        print("No Iris code")
                # Iteration over next fetch
                offset += limit
                params = {
                    "where": f"com_code={int(com_code)}",
                    "limit": limit,
                    "offset": offset,
                }
                response = requests.get(api_url, params=params)

    @staticmethod
    def _insert_cities(data: str) -> None:
        """Insert cities from a GEOJSON file.
        Args:
            data (str): path to the GEOJSON containing cities geometry.
        """
        lm = LayerMapping(City, data=data, mapping=mapping_city)
        lm.save()
        for city in City.objects.all():
            city.tiles_generated = False
            city.tiles_computed = False
            city.save()

    def handle(self, *args, **options):
        """Insert cities from geojson file and IRIS from BPCE API."""
        logger = logging.getLogger(__name__)
        self._insert_cities(data="file_data/communes_gl_2025.geojson")
        print("Removing duplicated cities...")
        remove_duplicates(City)
        logger.info("Duplicate removal complete.")
        qs_city = City.objects.all()
        self._insert_iris(qs_city=qs_city)
        print("Removing duplicated IRIS...")
        remove_duplicates(Iris)

_insert_cities(data) staticmethod

Insert cities from a GEOJSON file. Args: data (str): path to the GEOJSON containing cities geometry.

Source code in back/iarbre_data/management/commands/c01_insert_cities_and_iris.py
84
85
86
87
88
89
90
91
92
93
94
95
@staticmethod
def _insert_cities(data: str) -> None:
    """Insert cities from a GEOJSON file.
    Args:
        data (str): path to the GEOJSON containing cities geometry.
    """
    lm = LayerMapping(City, data=data, mapping=mapping_city)
    lm.save()
    for city in City.objects.all():
        city.tiles_generated = False
        city.tiles_computed = False
        city.save()

_insert_iris(qs_city) staticmethod

Use BPCE API to download IRIS and insert them in a table Args: qs_city (QuerySet): Query set that correspond to one or multiple cities.

Source code in back/iarbre_data/management/commands/c01_insert_cities_and_iris.py
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
@staticmethod
def _insert_iris(qs_city: QuerySet) -> None:
    """Use BPCE API to download IRIS and insert them in a table
    Args:
        qs_city (QuerySet): Query set that correspond to one or multiple cities.
    """
    cities = load_geodataframe_from_db(qs_city, ["id", "name", "code"])
    limit = 100
    for city in cities.itertuples():
        city_GEOS = GEOSGeometry(city.geometry.wkt)
        city_GEOS.srid = TARGET_PROJ
        print(f"Dowloading IRIS for city: {city.name}.")
        if city.name[:4].upper() == "LYON":
            com_code = 69123
        else:
            com_code = city.code
        offset = 0
        params = {
            "where": f"com_code={int(com_code)}",
            "limit": limit,
            "offset": offset,
        }
        response = requests.get(api_url, params=params)
        while response.status_code == 200:
            data = response.json()
            records = data.get("results", [])
            if len(records) == 0:
                break
            for record in tqdm(records, total=len(records)):
                geometry = record["geo_shape"]["geometry"]
                iris_name = record.get("iris_name")[0]
                iris_code = record.get("iris_code")[0]
                if geometry and iris_name and iris_code:
                    geom = GEOSGeometry(str(geometry))
                    geom.transform(TARGET_PROJ, clone=False)
                    if geom.intersects(
                        city_GEOS
                    ):  # for Lyon keep only iris for the 'arrondissement' at hand
                        Iris.objects.update_or_create(
                            city_id=city.id,
                            code=iris_code,
                            defaults={
                                "geometry": geom,
                                "name": iris_name,
                            },
                        )
                else:
                    print("No Iris code")
            # Iteration over next fetch
            offset += limit
            params = {
                "where": f"com_code={int(com_code)}",
                "limit": limit,
                "offset": offset,
            }
            response = requests.get(api_url, params=params)

handle(*args, **options)

Insert cities from geojson file and IRIS from BPCE API.

Source code in back/iarbre_data/management/commands/c01_insert_cities_and_iris.py
 97
 98
 99
100
101
102
103
104
105
106
107
def handle(self, *args, **options):
    """Insert cities from geojson file and IRIS from BPCE API."""
    logger = logging.getLogger(__name__)
    self._insert_cities(data="file_data/communes_gl_2025.geojson")
    print("Removing duplicated cities...")
    remove_duplicates(City)
    logger.info("Duplicate removal complete.")
    qs_city = City.objects.all()
    self._insert_iris(qs_city=qs_city)
    print("Removing duplicated IRIS...")
    remove_duplicates(Iris)