• Aucun résultat trouvé

Atelier SAGEO 2021 : Traitements spatiaux avec le plugin Python t4gpd dans le contexte d’un Jupyter Notebook

N/A
N/A
Protected

Academic year: 2021

Partager "Atelier SAGEO 2021 : Traitements spatiaux avec le plugin Python t4gpd dans le contexte d’un Jupyter Notebook"

Copied!
48
0
0

Texte intégral

(1)

HAL Id: hal-03262455

https://hal.archives-ouvertes.fr/hal-03262455

Submitted on 16 Jun 2021

HAL

is a multi-disciplinary open access archive for the deposit and dissemination of sci- entific research documents, whether they are pub- lished or not. The documents may come from teaching and research institutions in France or abroad, or from public or private research centers.

L’archive ouverte pluridisciplinaire

HAL, est

destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des établissements d’enseignement et de recherche français ou étrangers, des laboratoires publics ou privés.

Distributed under a Creative Commons

Attribution| 4.0 International License

Notebook

Thomas Leduc

To cite this version:

Thomas Leduc. Atelier SAGEO 2021 : Traitements spatiaux avec le plugin Python t4gpd dans le

contexte d’un Jupyter Notebook. École thématique. La Rochelle, France. 2021. �hal-03262455�

(2)

Thomas Leduc

[email protected]

UMR 1563 AAU / CRENAU

5 mai 2021

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 1 / 47

(3)

1 Introduction à Pandas

2 Introduction à Shapely

3 Introduction à GeoPandas

4 t4gpd

(4)

Bibliothèque Python de manipulation et analyse de données Les principales structures de données Pandas sont :

I lesSeriespour les données 1D

I lesDataFramepour les données 2D (ou plus)

Ces structures de données sont

indexées

pour plus d’efficacité, couplées à des utilitaires de lecture/écriture de fichiers

csv, tableurs xls(x), fichiersHDF5, bases de donnéesSQL, etc.

Pandas permet la concaténation et la fusion de gros volumes de données (*), l’analyse de séries temporelles (*), facilite la gestion des données manquantes (*), les tableaux croisés dynamiques (*), etc.

Ce qui suit s’inspire largement de

10 minutes to pandas

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 3 / 47

(5)

«

Series

is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index. » Series acts similarly to a

numpy.ndarray

or a fixed-size

dict.

1 from numpy i m p o r t mean ; from p a n d a s i m p o r t S e r i e s 2

3 s 1 = S e r i e s( d a t a =[ ’ a1 ’, ’ b2 ’, ’ c 3 ’, ’ a4 ’ ] ) 4 p r i n t( s 1 [ s 1 .s t r.s t a r t s w i t h(’ a ’) ] )

5

6 s 2 = S e r i e s( d a t a=r a n g e( 0 , 4 0 , 1 0 ) ) 7 p r i n t( s 2 [ s 2 > mean ( s 2 ) ] )

8

9 p r i n t( s 2 .i s _ m o n o t o n i c _ i n c r e a s i n g) 10

11 s 3 = S e r i e s( { c : f’ a { i } ’ f o r i , c i n e n u m e r a t e( [’ a ’,’ b ’,’ c ’] ) } ) 12 p r i n t( s 3 )

13

14 p r i n t( s 3 .i l o c[ 1 ] , s 3 .l o c[’ b ’] ) 15

16 s 4 = S e r i e s( [ 1 , 2 , 3 , 4 , None, 2 ] ) 17 p r i n t( s 4 .h a s n a n s, s 4 .i s _ u n i q u e)

(6)

«

DataFrame

is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. »

1 from p a n d a s i m p o r t DataFrame 2

3 d f 1 = DataFrame( d a t a = [ [’DE ’, ’ A l l e m a g n e ’, ’ B e r l i n ’] , 4 [’ FR ’, ’ F r a n c e ’, ’ P a r i s ’] , [’ IT ’, ’ I t a l i e ’, ’ Rome ’] ] , 5 c o l u m n s=[’ c o d e ’, ’ p a y s ’,’ c a p i t a l e ’]

6 )

7 p r i n t( d f 1 ) 8

9 d f 1 .s e t _ i n d e x( [’ c o d e ’] , d r o p=True, i n p l a c e=True) 10 p r i n t( d f 1 )

11

12 d f 1 .r e s e t _ i n d e x(i n p l a c e=True) 13 p r i n t( d f 1 )

14

15 d f 2 = d f 1 [ ~ d f 1 . c o d e .i s i n( [’ FR ’] ) ] 16 p r i n t( d f 2 )

17

18 d f 2 .r e s e t _ i n d e x(d r o p=True, i n p l a c e=True) 19 p r i n t( d f 2 )

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 5 / 47

(7)

Consulter notamment

pandas.concat(...)

et

pandas.merge(...)

1 from p a n d a s i m p o r t c o n c a t, DataFrame, merge 2

3 d f 3 = DataFrame( d a t a = [ [’ A l l e m a g n e ’, ’ E u r o p e ’] , 4 [’ I t a l i e ’, ’ E u r o p e ’] ] ,

5 c o l u m n s=[’ p a y s ’,’ c o n t i n e n t ’]

6 )

7 p r i n t( d f 3 ) 8

9 d f 4 = DataFrame( d a t a = [ [’ I n d e ’, ’ A s i e ’] ] , 10 c o l u m n s=[’ p a y s ’,’ c o n t i n e n t ’]

11 )

12 p r i n t( d f 4 ) 13

14 d f 5 = c o n c a t( [ d f 3 , d f 4 ] ) 15 p r i n t( d f 5 )

16

17 d f 5 .r e s e t _ i n d e x(d r o p=True, i n p l a c e=True) 18 p r i n t( d f 5 )

19

20 d f 6 = merge( d f 5 , d f 1 [ [’ p a y s ’,’ c a p i t a l e ’] ] , 21 how=’ l e f t ’, on=’ p a y s ’)

22 p r i n t( d f 6 )

(8)

Attention au mécanisme de

vue1

1 from p a n d a s i m p o r t DataFrame 2

3 d f 1 = DataFrame( d a t a = [ [’ a ’, 1 3 ] , [’ d ’, 7 ] , [’ z ’, 4 ] , 4 [’ a ’, 9 ] , [’ z ’, 2 ] ] , c o l u m n s=[ ’ c o l A ’, ’ c o l B ’] ) 5 p r i n t( d f 1 )

6

7 d f 2 = d f 1 [ ( d f 1 . c o l B >= 7 ) & ( d f 1 . c o l B <= 9 ) ] 8 p r i n t( d f 2 )# d f 2 e s t une v u e de d f 1

9

10 # d f 2 . c o l B = 10 ∗ d f 2 . c o l B

11 # A v a l u e i s t r y i n g t o be s e t on a c o p y o f a s l i c e 12 # f r o m a DataFrame .

13 # Try u s i n g . l o c [ r o w _ i n d e x e r , c o l _ i n d e x e r ] = v a l u e 14 # i n s t e a d

15

16 d f 3 = d f 1 .l o c[ d f 1 [ ( d f 1 . c o l B >=7) & ( d f 1 . c o l B < = 9 ) ] .i n d e x] 17 d f 3 . c o l B = 10 ∗ d f 3 . c o l B

18 p r i n t( d f 3 )# d f 3 e s t une c o p i e p r o f o n d e de d f 1 19

20 d f 4 = d f 2 . c o p y ( d e e p=True) 21 d f 4 . c o l B = 10 ∗ d f 4 . c o l B

22 p r i n t( d f 3 )# d f 4 e s t une c o p i e p r o f o n d e de d f 2

1. Une modification de df2 ne sera pas reportée sur df1.

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 7 / 47

(9)

1 Introduction à Pandas

2 Introduction à Shapely

3 Introduction à GeoPandas

4 t4gpd

(10)

Extrait de

https://shapely.readthedocs.io

I Shapely is a Python package for set-theoretic analysis and

manipulation of planar features using (via Python’s ctypes module) functions from the well known and widely deployedGEOSlibrary.

GEOS, a port of theJava Topology Suite (JTS), is the geometry engine of thePostGISspatial extension for the PostgreSQL RDBMS.

The designs of JTS and GEOS are largely guided by theOpen Geospatial Consortium’s Simple Features Access Specification 1 (*) and Shapely adheres mainly to the same set of standard classes and operations. Shapely is thereby deeply rooted in the conventions of the geographic information systems (GIS) world, but aspires to be equally useful to programmers working on non-conventional problems.

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 9 / 47

(11)

Déclaration de géométries — représentation discrète du monde

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t

2 from s h a p e l y.g e o m e t r y i m p o r t L i n e S t r i n g, P o i n t, P o l y g o n 3

4 r e d = P o i n t( 9 0 , 9 0 ) .b u f f e r( 1 0 )

5 b l u e = L i n e S t r i n g( [ ( 1 0 , 8 0 ) , ( 4 0 , 4 0 ) , ( 6 0 , 7 0 ) , ( 8 0 , 4 0 ) ] ) 6 g r e e n = P o l y g o n( [ ( 0 , 0 ) , ( 3 0 , 0 ) , ( 3 0 , 3 0 ) , ( 0 , 3 0 ) ] ) 7

8 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 9 p l t .p l o t( ∗ r e d .e x t e r i o r. xy , ’ r−’, l i n e w i d t h =2) 10 p l t .p l o t( ∗ b l u e . xy , ’ b−. ’, l i n e w i d t h =2)

11 p l t .p l o t( ∗ g r e e n .e x t e r i o r. xy , ’ g−−’, l i n e w i d t h =2) 12 p l t .s a v e f i g(’ . . / img /6 _ 1 _ s h a p e l y . p d f ’)

0 20 40 60 80 100

0 20 40 60 80 100

(12)

Union (efficace) de géométries

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t 2 from s h a p e l y.g e o m e t r y i m p o r t M u l t i P o i n t 3 from s h a p e l y.o p s i m p o r t u n a r y _ u n i o n 4

5 geoms = l i s t( )

6 f o r i i n r a n g e( 1 0 , 1 0 0 , 1 0 ) :

7 geoms .append(M u l t i P o i n t( [ ( 0 , i ) , ( i , 1 0 ) , ( i , 9 0 ) ] ) .b u f f e r( 1 0 ) ) 8

9 r e d = u n a r y _ u n i o n( geoms ) 10

11 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 12 p l t .p l o t( ∗ r e d .e x t e r i o r. xy , ’ r−’, l i n e w i d t h =2) 13 p l t .s a v e f i g(’ . . / img /6 _ 2 _ s h a p e l y . p d f ’)

0 20 40 60 80 100

0 20 40 60 80 100

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 11 / 47

(13)

Transformations affines de géométries

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t

2 from s h a p e l y.a f f i n i t y i m p o r t r o t a t e, t r a n s l a t e, s c a l e 3 from s h a p e l y.g e o m e t r y i m p o r t P o i n t

4 from s h a p e l y.o p s i m p o r t u n a r y _ u n i o n 5

6 r e d = P o i n t( 1 0 , 0 ) .b u f f e r( 1 0 , 1 )

7 b l u e = t r a n s l a t e(r o t a t e( r e d , 4 5 , o r i g i n =’ c e n t e r ’) , x o f f = 4 0 . 0 , y o f f = 0 . 0 )

8 g r e e n = s c a l e(t r a n s l a t e( b l u e , x o f f = 4 0 . 0 , y o f f = 0 . 0 ) , x f a c t =1 , y f a c t =5 , o r i g i n =’ c e n t e r ’) 9

10 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 11 p l t .p l o t( ∗ r e d .e x t e r i o r. xy , ’ r−’, l i n e w i d t h =2) 12 p l t .p l o t( ∗ b l u e .e x t e r i o r. xy , ’ b−. ’, l i n e w i d t h =2) 13 p l t .p l o t( ∗ g r e e n .e x t e r i o r. xy , ’ g−−’, l i n e w i d t h =2) 14 p l t .s a v e f i g(’ . . / img /6 _ 3 _ s h a p e l y . p d f ’)

0 20 40 60 80 100

30 20 10 0 10 20 30

(14)

Création de géométries englobantes

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t 2 from s h a p e l y.g e o m e t r y i m p o r t M u l t i P o i n t 3

4 mpts = M u l t i P o i n t( [ ( 9 0 , 9 0 ) , ( 7 0 , 5 0 ) , ( 5 0 , 2 0 ) , ( 1 0 , 1 0 ) , ( 3 0 , 3 0 ) ] ) 5 r e d = mpts .c o n v e x _ h u l l

6 b l u e = mpts .e n v e l o p e

7 g r e e n = mpts .m i n i m u m _ r o t a t e d _ r e c t a n g l e 8

9 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 10 p l t .p l o t( ∗ r e d .e x t e r i o r. xy , ’ r−’, l i n e w i d t h =2) 11 p l t .p l o t( ∗ b l u e .e x t e r i o r. xy , ’ b−. ’, l i n e w i d t h =2) 12 p l t .p l o t( ∗ g r e e n .e x t e r i o r. xy , ’ g−−’, l i n e w i d t h =2) 13 p l t .s a v e f i g(’ . . / img /6 _ 4 _ s h a p e l y . p d f ’)

20 40 60 80 100

0 20 40 60 80

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 13 / 47

(15)

Prédicats topologiques

1 from s h a p e l y.g e o m e t r y i m p o r t L i n e S t r i n g, P o i n t, P o l y g o n 2

3 r e d = P o i n t( ( 3 0 , 3 0 ) ) .b u f f e r( 2 5 ) 4 g r e e n =P o i n t( ( 7 0 , 3 0 ) ) .b u f f e r( 2 5 ) 5 b l u e = P o i n t( ( 5 0 , 6 0 ) ) .b u f f e r( 3 0 )

6 b l a c k =P o l y g o n( [ ( 4 0 , 8 0 ) , ( 6 0 , 8 0 ) , ( 5 0 , 1 0 0 ) ] ) 7 magenta = P o l y g o n( [ ( 4 0 , 6 0 ) , ( 6 0 , 6 0 ) , ( 5 0 , 8 0 ) ] ) 8 c y a n = L i n e S t r i n g( [ ( 2 0 , 9 0 ) , ( 4 0 , 1 0 0 ) , ( 5 0 , 8 5 ) , ( 6 0 , 1 0 0 ) ] ) 9

10 p r i n t( magenta .w i t h i n( b l u e ) ) #~ True 11 p r i n t( b l u e .c o n t a i n s( magenta ) ) #~ True

12 p r i n t( r e d .d i f f e r e n c e( b l u e ) .i n t e r s e c t s( g r e e n ) ) #~ T rue 13 p r i n t( magenta .t o u c h e s( b l a c k ) ) #~ True

14 p r i n t( b l a c k .o v e r l a p s( b l u e ) ) #~ Tr ue 15 p r i n t( c y a n .c r o s s e s( b l a c k ) ) #~ Tru e

16 p r i n t( b l a c k .u n i o n( magenta ) .d i s j o i n t( g r e e n ) ) #~ Tru e

20 40 60 80

20 40 60 80 100

(16)

A propos du

Dimensionally Extended 9-Intersection Model (DE-9IM)

DE9IM(a,b) =

dim(I(a)I(b)) dim(I(a)B(b)) dim(I(a)E(b))

dim(B(a)I(b)) dim(B(a)B(b)) dim(B(a)E(b)) dim(E(a)I(b)) dim(E(a)B(b)) dim(E(a)E(b))

I oùdimest la dimension de l’intersection (∩) entre l’intérieur (I), la frontière (B) ou le complémentaire (E) des géométries aetb passées en paramètre.

I cette dimension prend la valeurF si l’intersection est l’ensemble vide (∅), 0 si l’intersection est un ensemble de points, 1 si l’intersection est un ensemble de lignes et 2 si l’intersection est un ensemble de

polygones.

I cette matrice peut être sérialisée, ligne après ligne, sous forme d’un DE-9IM string code. Ainsi, à partir de l’exemple précédent, l’instruction Shapely :black .relate(magenta)renvoie la chaîne de caractères :

FF2F01212.

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 15 / 47

(17)

Opérations géométriques

1 g r e e n = P o i n t( ( 5 , 5 ) ) .b u f f e r( 5 ) 2 b l u e = P o i n t( ( 1 0 , 5 ) ) .b u f f e r( 5 )

3 _ i n t e r s e c t i o n = g r e e n .i n t e r s e c t i o n( b l u e ) 4 _ u n i o n = g r e e n .u n i o n( b l u e )

5 _ s y m m e t r i c _ d i f f e r e n c e = g r e e n .s y m m e t r i c _ d i f f e r e n c e( b l u e ) 6 _ d i f f e r e n c e = g r e e n .d i f f e r e n c e( b l u e )

Donnees en entree Intersection Union Difference symetrique Difference

(18)

Simplification de géométrie

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t 2 from s h a p e l y.g e o m e t r y i m p o r t L i n e S t r i n g 3

4 b l a c k = L i n e S t r i n g( [ ( i / / 2 , i−i / / 2 ) f o r i i n r a n g e( 1 9 ) ] ) 5 r e d = b l a c k .s i m p l i f y( 1 . 0 , p r e s e r v e _ t o p o l o g y=True) 6

7 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 8 p l t .p l o t( ∗ b l a c k . xy , ’ k−’, l i n e w i d t h =1) 9 p l t .p l o t( ∗ r e d . xy , ’ r−. ’, l i n e w i d t h =2)

10 p l t .s a v e f i g(’ . . / img /6 _ 7 _ s h a p e l y . p d f ’) 0 2 4 6 8

0 2 4 6 8

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 17 / 47

(19)

Boîte englobante (aussi appelée bounding box)

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t 2 from random i m p o r t r a n d i n t

3 from s h a p e l y.g e o m e t r y i m p o r t box, M u l t i P o l y g o n, P o i n t 4

5 p t s = [P o i n t( 1 , 2 ) .b u f f e r( 0 . 5 ) , P o i n t( 7 , 4 ) .b u f f e r( 0 . 5 ) ] 6 p t s = M u l t i P o l y g o n( p t s )

7

8 p r i n t( p t s .b o u n d s)

9 #~ ( minx , miny , maxx , maxy ) = ( 0 . 5 , 1 . 5 , 7 . 5 , 4 . 5 ) 10 e n v e l o p =box( ∗ p t s .b o u n d s)

11

12 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 13 p l t .x l i m( 0 , 8 )

14 p l t .y l i m( 0 , 8 )

15 p l t .p l o t( ∗ e n v e l o p .e x t e r i o r. xy , ’ r−. ’, l i n e w i d t h =2) 16 f o r p t i n p t s :

17 p l t .p l o t( ∗ p t .e x t e r i o r. xy , ’ k−’, l i n e w i d t h =2) 18 p l t .s a v e f i g(’ . . / img /6 _ 8 _ s h a p e l y . p d f ’)

0 2 4 6 8

0 1 2 3 4 5 6 7 8

(20)

Triangulation via

shapely.ops.triangulate (...)

1 from s h a p e l y.g e o m e t r y i m p o r t M u l t i P o i n t 2 from s h a p e l y.o p s i m p o r t t r i a n g u l a t e 3

4 mpts = M u l t i P o i n t( [ ( 4 , 0 ) , ( 7 , 2 ) , ( 2 , 4 ) , ( 7 , 4 ) , \ 5 ( 5 , 7 ) , ( 4 , 8 ) , ( 2 , 9 ) , ( 5 , 9 ) , ( 1 0 , 9 ) ] )

6 t i n = t r i a n g u l a t e( mpts ) #~ r e t o u r n e une l i s t e de " P o l y g o n "

2 4 6 8 10

0 2 4 6 8

Polygonisation via

shapely.ops.polygonize(...)

1 from s h a p e l y.g e o m e t r y i m p o r t M u l t i L i n e S t r i n g 2 from s h a p e l y.o p s i m p o r t p o l y g o n i z e

3

4 m l s = M u l t i L i n e S t r i n g( [ [ ( 2 , 9 ) , ( 2 , 4 ) ] , [ ( 2 , 4 ) , ( 4 , 8 ) ] , \ 5 [ ( 4 , 8 ) , ( 2 , 9 ) ] , [ ( 4 , 8 ) , ( 5 , 9 ) ] , [ ( 5 , 9 ) , ( 2 , 9 ) ] , [ ( 4 , 8 ) , ( 5 , 7 ) ] , \ 6 [ ( 5 , 7 ) , ( 5 , 9 ) ] , [ ( 5 , 7 ) , ( 1 0 , 9 ) ] , [ ( 1 0 , 9 ) , ( 5 , 9 ) ] , [ ( 5 , 7 ) , ( 7 , 4 ) ] , \ 7 [ ( 7 , 4 ) , ( 1 0 , 9 ) ] , [ ( 7 , 4 ) , ( 7 , 2 ) ] , [ ( 7 , 2 ) , ( 1 0 , 9 ) ] , [ ( 4 , 0 ) , ( 7 , 2 ) ] , \ 8 [ ( 7 , 2 ) , ( 2 , 4 ) ] , [ ( 2 , 4 ) , ( 4 , 0 ) ] , [ ( 7 , 4 ) , ( 2 , 4 ) ] , [ ( 5 , 7 ) , ( 2 , 4 ) ] ] )

9 p o l y g o n s = p o l y g o n i z e( m l s )#~ r e t o u r n e une l i s t e de " P o l y g o n " 0 2 4 6 8 10 2

4 6 8

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 19 / 47

(21)

Dilatation, érosion via

buffer(. . . )

I Point((0, 0)).buffer( distance , resolution =16, cap_style=1, join_style =1, mitre_limit=5.0)

join_style=1 (round) cap_style=1

(round) cap_style=2

(flat) cap_style=3

(square)

join_style=2 (mitre) cap_style=1

(round) cap_style=2

(flat) cap_style=3

(square)

join_style=3 (bevel) cap_style=1

(round) cap_style=2

(flat) cap_style=3

(square)

(22)

Dilatation, érosion via

buffer(. . . )

1 from s h a p e l y.g e o m e t r y i m p o r t L i n e S t r i n g

2 from s h a p e l y.g e o m e t r y i m p o r t CAP_STYLE, JOIN_STYLE 3

4 r e d = L i n e S t r i n g( [ ( 0 , 0 ) , ( 1 0 , 5 ) , ( 1 3 , 0 ) , ( 2 0 , 4 ) ] )

5 #~ DILATATION

6 d i m g r e y = r e d .b u f f e r( 4 , c a p _ s t y l e=CAP_STYLE. f l a t , j o i n _ s t y l e =JOIN_STYLE. m i t r e )

7 #~ EROSION

8 l i g h t g r e y = d i m g r e y .b u f f e r(−2 , j o i n _ s t y l e =JOIN_STYLE.r o u n d)

0 5 10 15 20

6 4 2 0 2 4 6 8 10

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 21 / 47

(23)

linemerge(. . . ),project(. . . )

et

interpolate(. . . )

1 from s h a p e l y.g e o m e t r y i m p o r t L i n e S t r i n g, M u l t i P o i n t 2 from s h a p e l y.o p s i m p o r t l i n e m e r g e

3

4 b l a c k = M u l t i P o i n t( [ ( 2 , 2 ) , ( 7 , 5 ) , ( 6 , 1 ) , ( 1 0 , 3 ) , ( 1 1 , 1 ) , ( 1 5 , 3 ) , ( 1 9 , 2 ) ] ) 5

6 l i n e s = [L i n e S t r i n g( [ ( 0 , 0 ) , ( 1 0 , 5 ) ] ) , L i n e S t r i n g( [ ( 1 0 , 5 ) , ( 1 3 , 0 ) ] ) , 7 L i n e S t r i n g( [ ( 1 3 , 0 ) , ( 2 0 , 4 ) ] ) ]

8 g r e y = l i n e m e r g e( l i n e s ) #~ LINESTRING ( 0 0 , 10 5 , 13 0 , 20 4 ) 9

10 r e d = [ ]

11 f o r _ p o i n t i n b l a c k . geoms :

12 #~ p r o j e c t ( . . . ) r e t o u r n e une a b s c i s s e c u r v i l i g n e 13 #~ i n t e r p o l a t e ( . . . ) r e t o u r n e un P o i n t s h a p e l y 14 r e d .append( g r e y .i n t e r p o l a t e( g r e y .p r o j e c t( _ p o i n t ) ) ) 15 r e d = M u l t i P o i n t( r e d )

0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0

0 1 2 3 4 5

(24)

Import-export

Well-Known Text (WKT)

I Export au format WKT :

fromshapely.geometryimportLineString LineString([ (0,0), (10,0), (10,10) ]).wkt

I Import du format WKT :

fromshapelyimportwkt

wkt.loads(’ LINESTRING (0 0, 10 0, 10 10)’)

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 23 / 47

(25)

Attention, Shapely est une bibliothèque résolument 2D. La composante en Z des géométries est

"décorative".

1 i m p o r t m a t p l o t l i b.p y p l o t a s p l t 2 from s h a p e l y.a f f i n i t y i m p o r t t r a n s l a t e 3 from s h a p e l y.g e o m e t r y i m p o r t P o l y g o n 4

5 r e d = P o l y g o n( [ ( 0 , 0 , 0 ) , ( 1 0 , 0 , 0 ) , ( 1 0 , 1 0 , 0 ) , ( 0 , 1 0 , 0 ) ] ) 6 b l u e = t r a n s l a t e( r e d , x o f f = 5 . 0 , y o f f = 5 . 0 , z o f f = 5 . 0 ) 7 g r e e n = r e d .i n t e r s e c t i o n( b l u e )

8 p r i n t( r e d .i n t e r s e c t s( b l u e ) ) #~ True 9 p r i n t( g r e e n .wkt)

10 #~ POLYGON Z ( ( 5 10 2 . 5 , 10 10 0 , 10 5 2 . 5 , 5 5 5 , 5 10 2 . 5 ) ) 11

12 p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 13 p l t .p l o t( ∗ r e d .e x t e r i o r. xy , ’ r : ’, l i n e w i d t h =2) 14 p l t .p l o t( ∗ b l u e .e x t e r i o r. xy , ’ b : ’, l i n e w i d t h =2) 15 p l t .p l o t( ∗ g r e e n .e x t e r i o r. xy , ’ g−’, l i n e w i d t h =2) 16 p l t .s a v e f i g(’ . . / img /6 _ d _ s h a p e l y . p d f ’)

0.0 2.5 5.0 7.5 10.0 12.5 15.0 0

2 4 6 8 10 12 14

(26)

1 Introduction à Pandas

2 Introduction à Shapely

3 Introduction à GeoPandas

4 t4gpd

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 25 / 47

(27)

Extraits de

https://geopandas.org

I GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandasto allow spatial operations on geometric types. Geometric operations are performed byshapely. Geopandas further depends on fionafor file access anddescartesandmatplotlib for plotting.

I The goal of GeoPandas is to make working with geospatial data in python easier. It combines the capabilities of pandas and shapely, providing geospatial operations in pandas and a high-level interface to multiple geometries to shapely. GeoPandas enables you to easily do operations in python that would otherwise require a spatial database such as PostGIS.

(28)

geopandas.GeoDataFrame

est une sous-classe de

pandas.DataFrame

(de même que

geopandas.GeoSeries

est une sous-classe de

pandas.Series

)

Chaque

GeoSeries

embarque son propre

crs

Un

GeoDataFrame

est une combinaison d’une ou plusieurs

Series

(données attributaires) et d’une ou plusieurs

GeoSeries

(données spatiales), accompagnée d’un mécanisme d’indexation

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 27 / 47

(29)

Mise en carte GeoPandas + Matplotlib

1 i m p o r t g e o p a n d a s a s gpd , m a t p l o t l i b.p y p l o t a s p l t 2

3 c o u n t r i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ l o w r e s ’) ) 4 c i t i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ c i t i e s ’) ) 5

6 a f r i c a = c o u n t r i e s [ c o u n t r i e s . c o n t i n e n t == " A f r i c a " ] 7 c a m e r o o n = a f r i c a .q u e r y(’ name == " Cameroon " ’) 8 y a o u n d e = gpd .s j o i n( c i t i e s , c a m e r o o n ) 9 nHab = c a m e r o o n . p o p _ e s t .s q u e e z e( ) / 1 e 6 10

11 _ , a x = p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) )

12 a x .s e t _ t i t l e(’ Cameroon \ n$ \pm$%.1 f MHab . ’ % nHab , f o n t s i z e =16) 13

14 a f r i c a .p l o t( a x=ax , c o l u m n=’ p o p _ e s t ’, cmap=’ OrRd ’, 15 e d g e c o l o r=’ g r a y ’, l e g e n d=True)

16 c a m e r o o n . b o u n d a r y .p l o t( a x=ax , e d g e c o l o r=’ g r a y ’, h a t c h=’ / ’,

17 l i n e w i d t h =2 , l a b e l =’ Cameroon ’)

18 y a o u n d e .p l o t( a x=ax , c o l o r=’ b l a c k ’, m a r k e r=’+ ’, l a b e l =’ C a p i t a l ’) 19 y a o u n d e .a p p l y(lambda x : a x .a n n o t a t e(

20 s=x . n a m e _ l e f t , x y=x .g e o m e t r y. c o o r d s [ 0 ] , 21 c o l o r=’ b l a c k ’, s i z e =12 , ha=’ c e n t e r ’) , a x i s= 1 ) ; 22

23 minx , miny , maxx , maxy = c a m e r o o n .b u f f e r( 5 . 0 ) .t o t a l _ b o u n d s 24 p l t .a x i s( [ minx , maxx , miny , maxy ] )

25 p l t .l e g e n d(l o c = ’ u p p e r r i g h t ’, f r a m e a l p h a = 0 . 5 ) 26 p l t .s a v e f i g(’ . . / img /7 _1_geopandas . p d f ’)

5 10 15 20

2.5 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5

Yaounde

Cameroon

±25.0 MHab.

Cameroon Capital

0.25 0.50 0.75 1.00 1.25 1.50 1.75 1e8

(30)

Un

GeoDataFrame

GeoPandas est un

DataFrame

pandas (presque) comme les autres. . .

1 i m p o r t g e o p a n d a s a s gpd , numpy a s np 2

3 c o u n t r i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ l o w r e s ’) ) 4

5 c o n t i n e n t s = c o u n t r i e s .g r o u p b y( by=’ c o n t i n e n t ’) . \ 6 p o p _ e s t .a g g r e g a t e( [ np . s i z e , np .sum] ) . \

7 rename(c o l u m n s={’ s i z e ’: ’ NPays ’, ’ sum ’: ’ NHab ’} ) . \ 8 s o r t _ v a l u e s(’ NHab ’, a s c e n d i n g=F a l s e)

9

10 p r i n t( c o n t i n e n t s )

avec un champ geometry qui embarque l’information spatiale avec une méthode plot et d’autres méthodes spécifiques

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 29 / 47

(31)

Table-Oriented Programming : manipulation de données (cf.

Comparison with SQL)

I type( countries . pop_est)#~ pandas.core. series . Series

I type( countries .geometry)#~ geopandas.geoseries.GeoSeries

I SELECT continent, pop_est FROM countries WHERE countries LIKE ’Cameroon’

I countries [ countries . name ==’Cameroon’][[’name’,’pop_est’]]

I SELECT continent, pop_est FROM countries WHERE countries LIKE ’Cameroon’ OR countries LIKE ’France’

I countries [ countries . name.isin([’ Cameroon’,’France’])][[’ name’,’ pop_est’]]

I countries [~ countries . continent .isin([’ Africa ’, ’ Asia ’,’ Europe’, \

’ North America’,’ South America’])][[’ name’,’ continent ’]]

I sum(countries . pop_est)

(32)

Table-Oriented Programming : manipulation de données

I cities [ cities .geometry.y ==max(cities.geometry.y) ]

I cities [abs( cities .geometry.y) ==min(abs(cities.geometry.y)) ]

I cities [ cities . name ==’Paris’].geometry.y.squeeze()

I countries [( countries . continent ==’Europe’) & (countries.bounds.maxx < 5)].name

I north = cities [ cities .geometry.y >= 0]

south = cities [ cities .geometry.y < 0]

lesCapitales = south.append(north)

I countries . continent . unique()

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 31 / 47

(33)

Table-Oriented Programming : manipulation de données

I Jointure attributaire :pandas.merge(...)

1 i m p o r t g e o p a n d a s a s gpd , p a n d a s a s pd 2 from numpy i m p o r t r o u n d

3

4 c o u n t r i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ l o w r e s ’) ) 5

6 c o n t i n e n t s = c o u n t r i e s .g r o u p b y( by=’ c o n t i n e n t ’) .sum( ) . \ 7 rename(c o l u m n s={’ p o p _ e s t ’: ’ NHab ’} )

8

9 r e s u l t = pd .merge( c o u n t r i e s , c o n t i n e n t s , how=’ l e f t ’, \ 10 l e f t _ o n=’ c o n t i n e n t ’, r i g h t _ i n d e x=True, v a l i d a t e =’m: 1 ’) 11 r e s u l t [’ r a t i o ’] = r o u n d( 1 0 0 ∗ r e s u l t . p o p _ e s t / r e s u l t . NHab , 1 ) 12 r e s u l t .s o r t _ v a l u e s(’ r a t i o ’, a s c e n d i n g=F a l s e, i n p l a c e=True) 13

14 p r i n t( r e s u l t [ [’ c o n t i n e n t ’, ’ name ’, ’ r a t i o ’] ] .h e a d( 7 ) )

(34)

Table-Oriented Programming : manipulation de données

I Jointure spatiale :geopandas.sjoin(...)

1 i m p o r t g e o p a n d a s a s gpd 2

3 c o u n t r i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ l o w r e s ’) ) 4 c i t i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ c i t i e s ’) ) 5

6 #~ op : b i n a r y p r e d i c a t e , o n e o f { ’ i n t e r s e c t s ’ , ’ c o n t a i n s ’ , ’ w i t h i n ’ } 7 r e s u l t = gpd .s j o i n( c i t i e s , c o u n t r i e s , how=’ l e f t ’, op=’ i n t e r s e c t s ’, 8 l s u f f i x =’ c i t y ’, r s u f f i x =’ c o u n t r y ’)

9 r e s u l t .s o r t _ v a l u e s(’ p o p _ e s t ’, a s c e n d i n g=True, i n p l a c e=True) 10 r e s u l t .rename(c o l u m n s={’ p o p _ e s t ’: ’ NHab_country ’} , i n p l a c e=True) 11

12 p r i n t( r e s u l t [ [’ n a m e _ c i t y ’,’ c o n t i n e n t ’,’ n a m e _ c o u n t r y ’,’ NHab_country ’] ] .h e a d( 7 ) )

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 33 / 47

(35)

Table-Oriented Programming : manipulation de données

I Jointure spatiale :geopandas.sjoin(...)

1 i m p o r t g e o p a n d a s a s gpd , m a t p l o t l i b.p y p l o t a s p l t 2

3 c o u n t r i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ l o w r e s ’) ) 4 c i t i e s = gpd .r e a d _ f i l e( gpd .d a t a s e t s.g e t _ p a t h(’ n a t u r a l e a r t h _ c i t i e s ’) ) 5

6 r e s u l t = gpd .s j o i n( c i t i e s , c o u n t r i e s , how=’ l e f t ’, op=’ i n t e r s e c t s ’, 7 l s u f f i x =’ c i t y ’, r s u f f i x =’ c o u n t r y ’)

8 p r i n t(’ Nombre de c a p i t a l e s s a n s p a y s de r a t t a c h e m e n t : %d ’ % 9 l e n( r e s u l t [ r e s u l t . n a m e _ c o u n t r y .i s n u l l( ) ] ) )

10 #~ Nombre de c a p i t a l e s s a n s p a y s de r a t t a c h e m e n t : 30 11

12 c o u n t r i e s = c o u n t r i e s [ c o u n t r i e s . name == ’ L i b y a ’] 13 c i t i e s = c i t i e s [ c i t i e s . name == ’ T r i p o l i ’] 14

15 _ , a x = p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 16 a x = c o u n t r i e s .p l o t( a x=ax , c o l o r=’ g r e y ’)

17 c i t i e s .p l o t( a x=ax , c o l o r=’ r e d ’, m a r k e r=’ o ’)

18 minx , miny , maxx , maxy = c i t i e s .b u f f e r( 0 . 5 ) .t o t a l _ b o u n d s 19 p l t .a x i s( [ minx , maxx , miny , maxy ] )

20 p l t .s a v e f i g(’ . . / img /7 _5_geopandas . p d f ’) 32.4 12.8 13.0 13.2 13.4 13.6 32.6

32.8 33.0 33.2

(36)

Table-Oriented Programming : manipulation de données

I Overlay :geopandas.overlay(...)

1 g r e e n 1 = P o l y g o n( [ ( 0 , 0 ) , ( 1 0 , 0 ) , ( 1 0 , 1 0 ) , ( 0 , 1 0 ) ] ) 2 g r e e n 2 = P o l y g o n( [ ( 1 0 , 1 0 ) , ( 2 0 , 1 0 ) , ( 2 0 , 2 0 ) , ( 1 0 , 2 0 ) ] ) 3 b l u e 1 =P o l y g o n( [ ( 5 , 5 ) , ( 1 5 , 5 ) , ( 1 5 , 1 5 ) , ( 5 , 1 5 ) ] )

4 b l u e 2 =P o l y g o n( [ ( 1 5 , 1 5 ) , ( 2 5 , 1 5 ) , ( 2 5 , 2 5 ) , ( 1 5 , 2 5 ) , ( 1 5 , 1 5 ) ] ) 5 g r e e n = gpd .GeoDataFrame( [ {’ g e o m e t r y ’: g r e e n 1 } , {’ g e o m e t r y ’: g r e e n 2 } ] ) 6 b l u e = gpd .GeoDataFrame( [ {’ g e o m e t r y ’: b l u e 1 } , {’ g e o m e t r y ’: b l u e 2 } ] ) 7

8 _ i n t e r s e c t i o n = gpd .o v e r l a y( g r e e n , b l u e , how=’ i n t e r s e c t i o n ’) 9 _ u n i o n = gpd .o v e r l a y( g r e e n , b l u e , how=’ u n i o n ’)

10 _ s y m m e t r i c _ d i f f e r e n c e = gpd .o v e r l a y( g r e e n , b l u e , how=’ s y m m e t r i c _ d i f f e r e n c e ’) 11 _ d i f f e r e n c e = gpd .o v e r l a y( g r e e n , b l u e , how=’ d i f f e r e n c e ’)

0 5 10 15 20 25

0 5 10 15 20

25 Donnees en entree

0 5 10 15 20 25

0 5 10 15 20

25 Intersection

0 5 10 15 20 25

0 5 10 15 20

25 Union

0 5 10 15 20 25

0 5 10 15 20

25Difference symetrique

0 5 10 15 20 25

0 5 10 15 20

25 Difference

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 35 / 47

(37)

Table-Oriented Programming : manipulation de données

I Overlay :geopandas.overlay(...)

1 from g e o p a n d a s i m p o r t GeoDataFrame, o v e r l a y 2 from s h a p e l y.g e o m e t r y i m p o r t P o l y g o n 3

4 g r e e n 1 = P o l y g o n( [ ( 0 , 0 ) , ( 1 0 , 0 ) , ( 1 0 , 1 0 ) , ( 0 , 1 0 ) ] ) 5 g r e e n 2 = P o l y g o n( [ ( 1 0 , 1 0 ) , ( 2 0 , 1 0 ) , ( 2 0 , 2 0 ) , ( 1 0 , 2 0 ) ] ) 6 b l u e 1 =P o l y g o n( [ ( 5 , 5 ) , ( 1 5 , 5 ) , ( 1 5 , 1 5 ) , ( 5 , 1 5 ) ] )

7 b l u e 2 =P o l y g o n( [ ( 1 5 , 1 5 ) , ( 2 5 , 1 5 ) , ( 2 5 , 2 5 ) , ( 1 5 , 2 5 ) , ( 1 5 , 1 5 ) ] ) 8 g r e e n =GeoDataFrame( [ {’ g e o m e t r y ’: g r e e n 1 , ’ i d ’: ’ g1 ’} , 9 {’ g e o m e t r y ’: g r e e n 2 , ’ i d ’: ’ g2 ’} ] )

10 b l u e =GeoDataFrame( [ {’ g e o m e t r y ’: b l u e 1 , ’ i d ’: ’ b1 ’} , 11 {’ g e o m e t r y ’: b l u e 2 , ’ i d ’: ’ b2 ’} ] )

12

13 p r i n t( o v e r l a y( g r e e n , b l u e , how=’ i n t e r s e c t i o n ’) )

(38)

Manipulation de données :

geopandas.clip (...)

1 i m p o r t g e o p a n d a s a s gpd , m a t p l o t l i b.p y p l o t a s p l t 2 from s h a p e l y.g e o m e t r y i m p o r t box

3 from o s i m p o r t p a t h

4 i f p a t h . e x i s t s (’ c : / U s e r s / t l e d u c ’) : 5 HOMEDIR = ’ c : / U s e r s / t l e d u c ’ 6 e l i f p a t h . e x i s t s (’ /home/ t l e d u c ’) : 7 HOMEDIR = ’ /home/ t l e d u c ’ 8 b d t o p o = HOMEDIR + ’ / d a t a / b d t o p o /\

9 BDTOPO_3−0_TOUSTHEMES_SHP_LAMB93_D044_2020−06−15/BDTOPO\

10 /1_DONNEES_LIVRAISON_2020−06−00047\

11 /BDT_3−0_SHP_LAMB93_D044−ED2020−06−15/BATI ’

12

13 r o i = 3 5 5 0 1 9 . 3 , 6 6 8 9 0 7 7 . 3 , 3 5 5 3 1 1 . 4 , 6 6 8 9 5 2 8 . 0

14 r e d = gpd .r e a d _ f i l e( b d t o p o + ’ /BATIMENT . s h p ’, bbox= r o i ) 15

16 r o i = box( ∗ r o i )

17 g r e y = gpd .c l i p( r e d , r o i ) 18

19 _ , basemap = p l t .s u b p l o t s( f i g s i z e = ( 0 . 5 ∗ 8 . 2 6 , 0 . 5 ∗ 8 . 2 6 ) ) 20 p l t .a x i s(’ o f f ’)

21 r e d .p l o t( a x=basemap , c o l o r=’ r e d ’) 22 g r e y .p l o t( a x=basemap , c o l o r=’ g r e y ’)

23 p l t .s a v e f i g(’ . . / img /7 _8_geopandas . p d f ’, b b o x _ i n c h e s=’ t i g h t ’)

T. Leduc (UMR AAU / CRENAU) Atelier Sageo 2021 : t4gpd Mai 2021 37 / 47

Références

Documents relatifs

En Python, pour accéder rapidement à une structure de type liste, on peut utiliser les listes : file = [] crée une file vide. file.append(valeur) permet d’enfiler une valeur

-Préliminaires télécharger et installer une version de Edupython 2.7 (https://edupython.tuxfamily.org/) - Il faut installer le programme (en .py) et le fichier (en .csv) de données

L’algorithme de ”recherche directe du sous-espace” peut ˆetre perc¸u comme un algorithme qui ”saute” les donn´ees manquantes via l’introduction d’une matrice de poids dans

Exemples : TYPE JOURS = (lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche) ; JOUR_TRAVAIL = lundi..

Cette étape est abordée par l’initiation aux fonctionnalités de la librairie pandas et à la classe DataFrame ; lire et écrire des fichiers, gérer une table de données et les

Aide: page 10 du Livret Objectif : Entrer des valeurs dans un tableau NUMPY et les manipuler.. Importer le

Les Environnement de Développement Intégré (EDI ou IDE en anglais – Logiciels fournissant tous les outils pour programmer dans une même interface) pour python donnent accès à

Nous allons donc dans ce cours étudier cette gestion de bases de données, mais au lieu d'utiliser des SGBD (système de gestion de bases de données), nous allons préférer gérer