You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/manipulation/05_parquet_s3.qmd
+47-10Lines changed: 47 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1279,29 +1279,30 @@ with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:
1279
1279
1280
1280
Dans la partie précédente, on était dans le cas "simple" d'un dataframe, ce qui nous permettait d'utiliser directement les fonctions d'export de `Pandas`. Maintenant, imaginons qu'on ait plusieurs fichiers d'entrée, pouvant chacun avoir des formats différents. Un cas typique de tels fichiers sont les fichiers `ShapeFile`, qui sont des fichiers de données géographiques, et se présentent sous forme d'une combinaison de fichiers (cf. [chapitre sur GeoPandas](/content/manipulation/03_geopandas_intro.qmd#le-format-shapefile-.shp-et-le-geopackage-.gpkg)). Commençons par récupérer un fichier `.shp` pour voir sa structure.
1281
1281
1282
-
On récupère ci-dessous les [contours des départements français](https://www.data.gouv.fr/fr/datasets/contours-des-departements-francais-issus-d-openstreetmap/), sous la forme d'une archive `.zip` qu'on va décompresser en local dans un dossier `departements_fr`.
1282
+
On récupère ci-dessous les contours du département de la Réunion [produits par l'IGN](https://geoservices.ign.fr/telechargement-api/ADMIN-EXPRESS-COG-CARTO), sous la forme d'une archive `.7z` qu'on va décompresser en local dans un dossier `departements_fr`.
1283
1283
::::
1284
1284
1285
1285
:::: {.content-visible when-profile="en"}
1286
1286
#### File-based use case {-}
1287
1287
1288
1288
In the previous section, we dealt with the "simple" case of a DataFrame, which allowed us to use `Pandas`' built-in export functions. Now, let's imagine we have multiple input files, each potentially in a different format. A typical example of such files are `ShapeFile`s, which are geographic data files and are composed of several related files (see [GeoPandas chapter](/content/manipulation/03_geopandas_intro.qmd#le-format-shapefile-.shp-et-le-geopackage-.gpkg)). Let’s start by downloading a `.shp` file to inspect its structure.
1289
1289
1290
-
Below, we retrieve the [boundaries of French departments](https://www.data.gouv.fr/fr/datasets/contours-des-departements-francais-issus-d-openstreetmap/) in the form of a `.zip` archive, which we will unzip locally into a `departements_fr` folder.
1290
+
Below are the outlines of the department of Réunion [produced by the IGN](https://geoservices.ign.fr/telechargement-api/ADMIN-EXPRESS-COG-CARTO), in the form of a `..7z` archive that we will unzip locally into a folder called `departements_fr`.
with py7zr.SevenZipFile(io.BytesIO(response.content), mode='r') as archive:
1305
+
archive.extractall(path="departements_fr")
1305
1306
1306
1307
# Vérification du dossier (local, pas sur S3)
1307
1308
os.listdir("departements_fr")
@@ -1329,7 +1330,14 @@ Vérifions que le dossier a bien été copié :
1329
1330
fs.ls(f"{MY_BUCKET}/diffusion/departements_fr")
1330
1331
```
1331
1332
1332
-
Si tout a bien fonctionné, la commande ci-dessus devrait renvoyer une liste contenant les chemins sur `MinIO` des différents fichiers (`.shp`, `.shx`, `.prj`, etc.) constitutifs du `ShapeFile` des départements.
1333
+
Si le dossier, comme ici, contient des fichiers à plusieurs niveaux, il faudra parcourir la liste de manière récursive pour accéder aux fichiers. Si par exemple, on s'intéresse exclusivement aux découpages des communes, on pourra par exemple utiliser un [`glob`](https://docs.python.org/fr/3/library/glob.html):
Si tout a bien fonctionné, la commande ci-dessus devrait renvoyer une liste contenant les chemins sur `MinIO` des différents fichiers (`.shp`, `.shx`, `.prj`, etc.) constitutifs du `ShapeFile` des communes de la Réunion.
1333
1341
::::
1334
1342
1335
1343
:::: {.content-visible when-profile="en"}
@@ -1340,7 +1348,15 @@ Let’s check that the folder was successfully copied:
1340
1348
fs.ls(f"{MY_BUCKET}/diffusion/departements_fr")
1341
1349
```
1342
1350
1343
-
If everything worked correctly, the command above should return a list of file paths on `MinIO` for the various components of the department `ShapeFile` (`.shp`, `.shx`, `.prj`, etc.).
1351
+
If the folder, as in this case, contains files at multiple levels, you will need to browse the list recursively to access the files. If, for example, you are only interested in municipal divisions, you can use a [`glob`](https://docs.python.org/fr/3/library/glob.html):
If everything worked correctly, the command above should return a list of file paths on `MinIO` for the various components of the Réunion cities `ShapeFile` (`.shp`, `.shx`, `.prj`, etc.).
1344
1360
::::
1345
1361
1346
1362
:::: {.content-visible when-profile="fr"}
@@ -1395,11 +1411,21 @@ Puis on les importe classiquement depuis `Python` avec le *package* approprié.
1395
1411
1396
1412
```{python}
1397
1413
#| eval: false
1414
+
from pathlib import Path
1398
1415
import geopandas as gpd
1399
1416
1400
-
df_dep = gpd.read_file("departements_fr")
1417
+
localpath = Path("departements_fr") #<1>
1418
+
1419
+
1420
+
df_dep = gpd.read_file(
1421
+
list(
1422
+
localpath.glob("**/COMMUNE.shp")
1423
+
)[0] #<2>
1424
+
)
1401
1425
df_dep.head(2)
1402
1426
```
1427
+
1. Comme notre dossier contient beaucoup de fichiers, on va devoir faire une recherche dedans via, à nouveau, un [`glob`](https://docs.python.org/fr/3/library/glob.html)
1428
+
2.`Pathlib` nous donnant une liste, on se restreint au premier écho et on importe avec `GeoPandas`
1403
1429
1404
1430
## Pour aller plus loin
1405
1431
@@ -1458,11 +1484,22 @@ Then, you can import them in the usual way using the appropriate `Python` packag
1458
1484
1459
1485
```{python}
1460
1486
#| eval: false
1487
+
from pathlib import Path
1461
1488
import geopandas as gpd
1462
1489
1463
-
df_dep = gpd.read_file("departements_fr")
1490
+
localpath = Path("departements_fr") #<1>
1491
+
1492
+
1493
+
df_dep = gpd.read_file(
1494
+
list(
1495
+
localpath.glob("**/COMMUNE.shp")
1496
+
)[0] #<2>
1497
+
)
1464
1498
df_dep.head(2)
1465
1499
```
1500
+
1. As our file contains many files, we will need to search through it using, once again, a [`glob`](https://docs.python.org/fr/3/library/glob.html)
1501
+
2.`Pathlib` gives us a list, so we restrict ourselves to the first echo and import it with `GeoPandas`
0 commit comments