Démonstration de l’écosystème géospatial de Python

Import des bibliothèques

import geopandas as gpd
import rasterio as rio

Ouverture d’un premier jeu de données avec GeoPandas :

communes = gpd.read_file('../data/COMMUNE.shp')
communes.head()
ID INSEE_COM INSEE_ARR INSEE_COL INSEE_DEP INSEE_REG POPULATION SURFACE_HA DATE_CREAT DATE_MAJ ... CL_DEPART CL_REGION CAPITALE DATE_RCT RECENSEUR SIREN_EPCI ID_CH_LIEU ID_AUT_ADM CODE_SIREN geometry
0 COMMUNE_0000000009754200 38359 1 38D 38 84 1197 3622 2006-08-02 08:36:54 2023-01-20 10:57:47 ... Non Non Non 2020-01-01 INSEE 200070431 PAIHABIT0000000007545622 SURFACTI0000000007463791 200054526 POLYGON ((872007.300 6454019.300, 872006.300 6...
1 COMMUNE_0000000009755669 38243 1 38D 38 84 247 1499 2006-08-02 08:36:54 2023-02-14 10:36:40 ... Non Non Non 2020-01-01 INSEE 200030658 PAIHABIT0000000007548437 SURFACTI0000000042499727 213802432 POLYGON ((907510.800 6409545.700, 907506.100 6...
2 COMMUNE_0000000009753737 38239 1 38D 38 84 7495 2006 2006-08-02 08:36:54 2023-01-18 18:39:43 ... Non Non Non 2020-01-01 INSEE 243800984 PAIHABIT0000000007542619 SURFACTI0000000007463524 213802390 POLYGON ((900804.200 6475473.300, 900816.800 6...
3 COMMUNE_0000000009755432 05064 2 05D 05 93 105 10802 2006-08-02 08:36:54 2023-01-18 18:39:43 ... Non Non Non 2020-01-01 INSEE 200068096 PAIHABIT0000000075658095 SURFACTI0000000118484062 210500641 POLYGON ((950358.400 6422049.800, 950358.000 6...
4 COMMUNE_0000000009754591 38237 1 38D 38 84 190 1460 2006-08-02 08:36:54 2023-01-18 18:39:43 ... Non Non Non 2020-01-01 INSEE 243800745 PAIHABIT0000000007546970 SURFACTI0000000007463883 213802374 POLYGON ((946870.000 6443476.500, 946851.600 6...

5 rows × 26 columns

communes.crs
<Projected CRS: EPSG:2154>
Name: RGF93 v1 / Lambert-93
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: France - onshore and offshore, mainland and Corsica (France métropolitaine including Corsica).
- bounds: (-9.86, 41.15, 10.38, 51.56)
Coordinate Operation:
- name: Lambert-93
- method: Lambert Conic Conformal (2SP)
Datum: Reseau Geodesique Francais 1993 v1
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich
communes.plot(edgecolor='black', color="pink")
<Axes: >

arrondissements = communes.dissolve(by='INSEE_ARR')
arrondissements.plot(edgecolor='red')
<Axes: >

centroids = communes.centroid
centroids.plot()
<Axes: >

communes.boundary.plot()
<Axes: >

cours_deau = gpd.read_file('../data/COURS_D_EAU.shp')
cours_deau.head()
ID CODE_HYDRO TOPONYME STATUT_TOP IMPORTANCE DATE_CREAT DATE_MAJ DATE_APP DATE_CONF SOURCE ID_SOURCE STATUT MAREE PERMANENT COMMENT geometry
0 COURDEAU0000002215479238 06C0000002215479238 Canal de Pulivès Validé 5 2020-10-21 09:35:44 2020-11-12 10:46:00 None None None None Validé None None None LINESTRING (847280.500 6505594.500, 847273.400...
1 COURDEAU0000002000792297 06C0000002000792297 Torrent du Clot des Cavales Validé 5 2017-01-24 16:06:18 2022-06-03 22:58:45 None None None None Validé None None None LINESTRING (963060.600 6437049.700, 963068.600...
2 COURDEAU0000002215478774 06C0000002215478774 le Bouveyron Validé 5 2020-10-21 09:35:44 2020-11-12 10:46:00 None None None None Validé None None None LINESTRING (895311.900 6452665.700, 895271.300...
3 COURDEAU0000002000803076 06C0000002000803076 Ruisseau de la Combe de Mouze Validé 5 2017-01-24 16:06:18 2020-09-28 10:34:31 None None None None Validé None None None LINESTRING (878398.000 6459581.500, 878400.900...
4 COURDEAU0000002215480545 06C0000002215480545 le Pachoux Validé 5 2020-10-21 09:35:44 2020-11-12 10:46:00 None None None None Validé None None None LINESTRING (945273.400 6484691.600, 945299.600...
cours_deau.plot()
<Axes: >

cours_deau_intersectés = cours_deau.intersection(communes.dissolve().iloc[0].geometry)
cours_deau_intersectés.plot()
<Axes: >

import contextily as cx

# Les communes, en Web Mercator
ax = communes.to_crs('EPSG:3857').plot(edgecolor='grey', color="lightgrey")

# Les cours d'eau, intersectés avec les communes, en Web mercator
ax = cours_deau_intersectés.to_crs('EPSG:3857').plot(ax = ax, color="blue")

# Ajout du fond de carte
cx.add_basemap(ax, source='https://tile.openstreetmap.org/{z}/{x}/{y}.png')

# On enlève les axes du graphique
ax.set_axis_off()
ax
<Axes: >

cours_deau.crs
<Projected CRS: EPSG:2154>
Name: RGF93 v1 / Lambert-93
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: France - onshore and offshore, mainland and Corsica (France métropolitaine including Corsica).
- bounds: (-9.86, 41.15, 10.38, 51.56)
Coordinate Operation:
- name: Lambert-93
- method: Lambert Conic Conformal (2SP)
Datum: Reseau Geodesique Francais 1993 v1
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich
cours_deau[['TOPONYME', 'IMPORTANCE']].head()
TOPONYME IMPORTANCE
0 Canal de Pulivès 5
1 Torrent du Clot des Cavales 5
2 le Bouveyron 5
3 Ruisseau de la Combe de Mouze 5
4 le Pachoux 5

Sélection de l’entité correspondant au canal du furon et de sa géométrie

cours_deau[cours_deau.TOPONYME == 'Canal du Furon']
ID CODE_HYDRO TOPONYME STATUT_TOP IMPORTANCE DATE_CREAT DATE_MAJ DATE_APP DATE_CONF SOURCE ID_SOURCE STATUT MAREE PERMANENT COMMENT geometry
1236 COURDEAU0000002000796489 06C0000002000796489 Canal du Furon Validé 5 2017-01-24 16:06:18 2021-09-16 13:50:18 None None None None Validé None None None LINESTRING (904717.400 6447571.200, 904712.000...
geom = cours_deau.loc[(cours_deau[cours_deau.TOPONYME == 'Canal du Furon'].index[0], 'geometry')]
geom

mnt = rio.open('../data/DEM_N245E395_l93.tif')
print(mnt.meta)
{'driver': 'GTiff', 'dtype': 'float32', 'nodata': 3.4028234663852886e+38, 'width': 2393, 'height': 2000, 'count': 1, 'crs': CRS.from_epsg(2154), 'transform': Affine(22.625939867401588, 0.0, 876147.6561479433,
       0.0, -27.206653628468978, 6496734.7862259)}
from rasterio.plot import show
show(mnt)

<Axes: >

ax = cours_deau_intersectés.plot(edgecolor="red")
ax = show(mnt, ax=ax)
ax
<Axes: >

# geom est la géométrie récupérée plus tôt
length = geom.length

# combien de points :
nb_points = 50 

# quelle distance entre les points :
step = length / (nb_points - 1)

dists = []
coords = []
for dist in range(0, int(length), int(step)):
    pt = geom.interpolate(dist) # type(pt) -> shapely.geometry.Point
    # récupération des coordonnées du points sous forme d'un tuple :
    c = pt.coords[0]
    coords.append(c)
    dists.append(dist)

result = []
for i, value in enumerate(mnt.sample(coords)):
    result.append((dists[i], value[0]))
import matplotlib.pyplot as plt

plt.plot([i[0] for i in result], [i[1] for i in result])
plt.title('Profil en long du Canal du Furon')
plt.xlabel('Distance à la source')
plt.ylabel('Altitude')
plt.show()

result = []

for dist in range(0, int(length), int(step)):
    pt = geom.interpolate(dist)
    result.append({
        "geometry": pt,
        "distance_source": dist,
        "altitude": list(mnt.sample(pt.coords))[0][0],
    })

# Cette fois nous avons utilisé une liste
# contenant un dictionnaire pour chaque entité
# ... le résultat est le même qu'avec la méthode prédécente
sample_pts = gpd.GeoDataFrame(result, crs=cours_deau.crs)

# Toutes nos nouvelles entités vont prendre cette valeur
sample_pts['TOPONYME_COURS_DEAU'] = 'Canal du Furon'
sample_pts.head()
geometry distance_source altitude TOPONYME_COURS_DEAU
0 POINT (904717.400 6447571.200) 0 1425.737671 Canal du Furon
1 POINT (904696.944 6447996.849) 428 1362.941040 Canal du Furon
2 POINT (904755.972 6448415.457) 856 1316.247681 Canal du Furon
3 POINT (904703.201 6448834.857) 1284 1288.363037 Canal du Furon
4 POINT (904552.212 6449229.865) 1712 1219.130859 Canal du Furon
sample_pts.to_crs('EPSG:4326').to_file('/tmp/sample-pts.geojson')
%cat /tmp/sample-pts.geojson
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "distance_source": 0, "altitude": 1425.7376708984375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.602936388562155, 45.097041248809752 ] } },
{ "type": "Feature", "properties": { "distance_source": 428, "altitude": 1362.9410400390625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.602854817807447, 45.100877769687386 ] } },
{ "type": "Feature", "properties": { "distance_source": 856, "altitude": 1316.2476806640625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.603780310543639, 45.104627343059583 ] } },
{ "type": "Feature", "properties": { "distance_source": 1284, "altitude": 1288.363037109375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.603285507285375, 45.108417233222767 ] } },
{ "type": "Feature", "properties": { "distance_source": 1712, "altitude": 1219.130859375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.601532157195259, 45.112016753315935 ] } },
{ "type": "Feature", "properties": { "distance_source": 2140, "altitude": 1170.004638671875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.601070658466196, 45.115813452064629 ] } },
{ "type": "Feature", "properties": { "distance_source": 2568, "altitude": 1128.1978759765625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.602875015792643, 45.119419528471369 ] } },
{ "type": "Feature", "properties": { "distance_source": 2996, "altitude": 1114.665771484375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.603318600708222, 45.122799392830174 ] } },
{ "type": "Feature", "properties": { "distance_source": 3424, "altitude": 1064.7662353515625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.600993324093485, 45.125765092317096 ] } },
{ "type": "Feature", "properties": { "distance_source": 3852, "altitude": 1028.2977294921875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.596714861215382, 45.127787527323783 ] } },
{ "type": "Feature", "properties": { "distance_source": 4280, "altitude": 1009.6756591796875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.592514319284059, 45.129088181552319 ] } },
{ "type": "Feature", "properties": { "distance_source": 4708, "altitude": 982.3232421875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.588682946240333, 45.131060046928404 ] } },
{ "type": "Feature", "properties": { "distance_source": 5136, "altitude": 970.5863037109375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.587298981190339, 45.134015004202809 ] } },
{ "type": "Feature", "properties": { "distance_source": 5564, "altitude": 966.78436279296875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.587601804540014, 45.137549358350455 ] } },
{ "type": "Feature", "properties": { "distance_source": 5992, "altitude": 961.939453125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.589307549857743, 45.140425187838744 ] } },
{ "type": "Feature", "properties": { "distance_source": 6420, "altitude": 969.871826171875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.592913627728214, 45.141984603742451 ] } },
{ "type": "Feature", "properties": { "distance_source": 6848, "altitude": 976.577880859375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.597445920273993, 45.143742685679122 ] } },
{ "type": "Feature", "properties": { "distance_source": 7276, "altitude": 982.93341064453125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.599646103684857, 45.146560456559669 ] } },
{ "type": "Feature", "properties": { "distance_source": 7704, "altitude": 966.16180419921875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.600427204302822, 45.149846695838583 ] } },
{ "type": "Feature", "properties": { "distance_source": 8132, "altitude": 945.78857421875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.603545080638023, 45.152644680689725 ] } },
{ "type": "Feature", "properties": { "distance_source": 8560, "altitude": 900.91259765625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.606450670072398, 45.155564354564163 ] } },
{ "type": "Feature", "properties": { "distance_source": 8988, "altitude": 887.65020751953125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.608563605707576, 45.158904918695256 ] } },
{ "type": "Feature", "properties": { "distance_source": 9416, "altitude": 902.82696533203125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.609622889418532, 45.162068349970951 ] } },
{ "type": "Feature", "properties": { "distance_source": 9844, "altitude": 887.63275146484375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.611497801481677, 45.165479333457625 ] } },
{ "type": "Feature", "properties": { "distance_source": 10272, "altitude": 878.794921875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.613113398049233, 45.168887338191517 ] } },
{ "type": "Feature", "properties": { "distance_source": 10700, "altitude": 872.40411376953125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.614930460774134, 45.172344289585418 ] } },
{ "type": "Feature", "properties": { "distance_source": 11128, "altitude": 869.51171875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.618104946640242, 45.174794174570977 ] } },
{ "type": "Feature", "properties": { "distance_source": 11556, "altitude": 845.72125244140625, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.619612546351767, 45.177923171302027 ] } },
{ "type": "Feature", "properties": { "distance_source": 11984, "altitude": 841.0703125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.620752381457533, 45.181470353452397 ] } },
{ "type": "Feature", "properties": { "distance_source": 12412, "altitude": 831.24798583984375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.622133643324828, 45.184373138538213 ] } },
{ "type": "Feature", "properties": { "distance_source": 12840, "altitude": 779.9158935546875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.624211779277705, 45.187667608008951 ] } },
{ "type": "Feature", "properties": { "distance_source": 13268, "altitude": 730.93505859375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.62779484719948, 45.190185339903387 ] } },
{ "type": "Feature", "properties": { "distance_source": 13696, "altitude": 695.1026611328125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.6277852170336, 45.193998450489666 ] } },
{ "type": "Feature", "properties": { "distance_source": 14124, "altitude": 661.1397705078125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.629914860357067, 45.197418964335661 ] } },
{ "type": "Feature", "properties": { "distance_source": 14552, "altitude": 574.65057373046875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.633707541744952, 45.199807975440336 ] } },
{ "type": "Feature", "properties": { "distance_source": 14980, "altitude": 554.5118408203125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.636334960225095, 45.202530705404811 ] } },
{ "type": "Feature", "properties": { "distance_source": 15408, "altitude": 537.9266357421875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.639964335327242, 45.20477696452906 ] } },
{ "type": "Feature", "properties": { "distance_source": 15836, "altitude": 488.54095458984375, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.642399549382896, 45.207890545428768 ] } },
{ "type": "Feature", "properties": { "distance_source": 16264, "altitude": 387.07159423828125, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.646623860523144, 45.209265173965186 ] } },
{ "type": "Feature", "properties": { "distance_source": 16692, "altitude": 317.86752319335938, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.651810844049399, 45.208978670733813 ] } },
{ "type": "Feature", "properties": { "distance_source": 17120, "altitude": 216.86354064941406, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.657052766355505, 45.208349663445603 ] } },
{ "type": "Feature", "properties": { "distance_source": 17548, "altitude": 208.27235412597656, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.661201118701395, 45.209238149946053 ] } },
{ "type": "Feature", "properties": { "distance_source": 17976, "altitude": 205.74320983886719, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.663603071168562, 45.212182069309662 ] } },
{ "type": "Feature", "properties": { "distance_source": 18404, "altitude": 207.64234924316406, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.666696966832268, 45.214685949193147 ] } },
{ "type": "Feature", "properties": { "distance_source": 18832, "altitude": 202.63288879394531, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.665296781211094, 45.218376791831325 ] } },
{ "type": "Feature", "properties": { "distance_source": 19260, "altitude": 203.68572998046875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.666779168351665, 45.221590543317554 ] } },
{ "type": "Feature", "properties": { "distance_source": 19688, "altitude": 202.07421875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.664208221363736, 45.224988806681253 ] } },
{ "type": "Feature", "properties": { "distance_source": 20116, "altitude": 200.82586669921875, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.661614069298556, 45.228378651112351 ] } },
{ "type": "Feature", "properties": { "distance_source": 20544, "altitude": 201.16197204589844, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.659027861999523, 45.231771571750315 ] } },
{ "type": "Feature", "properties": { "distance_source": 20972, "altitude": 202.91989135742188, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.656581431065963, 45.235089044362191 ] } },
{ "type": "Feature", "properties": { "distance_source": 21400, "altitude": 198.91233825683594, "TOPONYME_COURS_DEAU": "Canal du Furon" }, "geometry": { "type": "Point", "coordinates": [ 5.656615308778129, 45.238526696492357 ] } }
]
}