Skip to content

Commit

Permalink
version présentable, routes et données dynamiques
Browse files Browse the repository at this point in the history
  • Loading branch information
clement2323 committed Oct 23, 2024
1 parent 4de71b5 commit 892cbb8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 80 deletions.
79 changes: 0 additions & 79 deletions src/data/clusters_statistics.json.py

This file was deleted.

9 changes: 9 additions & 0 deletions src/data/clusters_statistics_mayotte.json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from utils.fonctions import creer_donnees_comparaison

file_paths = {
"2022": "projet-slums-detection/data-prediction/PLEIADES/MAYOTTE/2022/Segmentation/1/statistics_clusters.parquet",
"2023": "projet-slums-detection/data-prediction/PLEIADES/MAYOTTE/2023/Segmentation/1/statistics_clusters.parquet",
}

data = creer_donnees_comparaison(file_paths)
print(data.to_json())
11 changes: 11 additions & 0 deletions src/data/clusters_statistics_reunion.json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from utils.fonctions import creer_donnees_comparaison

file_paths = {
"2022": "projet-slums-detection/data-prediction/PLEIADES/REUNION/2022/Segmentation/1/statistics_clusters.parquet",
"2023": "projet-slums-detection/data-prediction/PLEIADES/REUNION/2023/Segmentation/1/statistics_clusters.parquet",
}

data = creer_donnees_comparaison(file_paths)
print(data.to_json())

data
Empty file added src/data/utils/__init__.py
Empty file.
Binary file added src/data/utils/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
73 changes: 73 additions & 0 deletions src/data/utils/fonctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os
import geopandas as gpd
import s3fs
import numpy as np


def merge_gdfs(gdfs, id_columns, value_columns):
"""Merge multiple GeoDataFrames on specified columns."""
base_gdf = None
for year, gdf in gdfs.items():
year_columns = [*id_columns, *[f"{col}_{year}" for col in value_columns]]
current_gdf = gdf[year_columns]

if base_gdf is None:
base_gdf = current_gdf
else:
base_gdf = base_gdf.merge(current_gdf, on=id_columns)

return base_gdf


def creer_donnees_comparaison(file_paths):

id_columns = ["ident_ilot", "code", "depcom_2018", "ident_up", "dep", "geometry"]
value_columns = ["area_cluster", "area_building", "pct_building"]

# Set up S3 filesystem
fs = s3fs.S3FileSystem(
client_kwargs={"endpoint_url": f"https://{os.environ['AWS_S3_ENDPOINT']}"},
key=os.getenv("AWS_ACCESS_KEY_ID"),
secret=os.getenv("AWS_SECRET_ACCESS_KEY"),
# token=os.environ["AWS_SESSION_TOKEN"],
)

gdfs = {year: gpd.read_parquet(path, filesystem=fs) for year, path in file_paths.items()}

# Rename columns in each GeoDataFrame
gdfs = {
year: gdf.rename(columns={col: f"{col}_{year}" for col in value_columns})
for year, gdf in gdfs.items()
}

# Merge all GeoDataFrames
merged_gdf = merge_gdfs(gdfs, id_columns, value_columns)

merged_gdf.loc[:, "building_2023"] = merged_gdf.loc[
:, "area_building_2023"
]*1e6

merged_gdf.loc[:, "area_building_change_absolute"] = (
merged_gdf.loc[:, "area_building_2023"] - merged_gdf.loc[:, "area_building_2022"]
) * 1e6

merged_gdf.loc[:, "area_building_change_relative"] = (merged_gdf.loc[
:, "area_building_change_absolute"
] / (merged_gdf.loc[:, "area_building_2022"] * 1e6))*100

# Order columns
ordered_columns = (
id_columns[:-1] # All ID columns except geometry
+ [
"pct_building_2023",
"building_2023",
"area_building_change_absolute",
"area_building_change_relative",
] # Value columns
+ ["geometry"] # Put geometry at the end
)

# Remplacer les NaN et Infinity par 0
final_gdf_cleaned = merged_gdf.replace([np.nan, np.inf, -np.inf], 0)

return (final_gdf_cleaned[ordered_columns].set_index("ident_ilot").to_crs("EPSG:4326"))
8 changes: 7 additions & 1 deletion src/departement/[departement].md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ import { quantileProbs, colorScales, departementConfig } from '../utils/config.j
```

```js
const statistics = FileAttachment("../data/clusters_statistics.json").json();
const files = [
{id: "mayotte", file: FileAttachment("../data/clusters_statistics_mayotte.json")},
{id: "reunion", file: FileAttachment("../data/clusters_statistics_reunion.json")},
]
// Trouve le fichier correspondant au département
const selec = files.find(f => f.id === departement);
const statistics = await selec.file.json();
```

```js
Expand Down

0 comments on commit 892cbb8

Please sign in to comment.