Shapel y geometries and spatial relationships W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Dani Arribas - Bel Geographic Data Science Lab ( Uni v ersit y of Li v erpool )
Scalar geometr y v al u es cities = geopandas.read_file("ne_110m_populated_places.shp") cities.head() name geometry 0 Vatican City POINT (12.45338654497177 41.90328217996012) 1 San Marino POINT (12.44177015780014 43.936095834768) 2 Vaduz POINT (9.516669472907267 47.13372377429357) 3 Lobamba POINT (31.19999710971274 -26.46666746135247) 4 Luxembourg POINT (6.130002806227083 49.61166037912108) brussels = cities.loc[170, 'geometry'] print(brussels) POINT (4.33137074969045 50.83526293533032) WORKING WITH GEOSPATIAL DATA IN PYTHON
Scalar geometr y v al u es brussels = cities.loc[170, 'geometry'] print(brussels) POINT (4.33137074969045 50.83526293533032) type(brussels) shapely.geometry.point.Point WORKING WITH GEOSPATIAL DATA IN PYTHON
The Shapel y p y thon package type(brussels) shapely.geometry.point.Point Shapel y P y thon Package for the manip u lation and anal y sis of geometric objects Pro v ides the Point , LineString and Polygon objects GeoSeries ( GeoDataFrame ' geometr y' col u mn ) consists of shapel y objects WORKING WITH GEOSPATIAL DATA IN PYTHON
Geometr y objects Accessing from a GeoDataFrame : brussels = cities.loc[170, 'geometry'] paris = cities.loc[235, 'geometry'] belgium = countries.loc[countries['name'] == 'Belgium', 'geometry'].squeeze() france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze() uk = countries.loc[countries['name'] == 'United Kingdom', 'geometry'].squeeze() Creating man u all y: from shapely.geometry import Point p = Point(1, 2) print(p) POINT (1 2) WORKING WITH GEOSPATIAL DATA IN PYTHON
Spatial methods The area of a geometr y: belgium.area 3.8299974609075753 The distance bet w een 2 geometries : brussels.distance(paris) 2.8049127723186214 And man y more ! ( e . g . centroid , simplify , ...) WORKING WITH GEOSPATIAL DATA IN PYTHON
Spatial relationships geopandas.GeoSeries([belgium, france, uk, paris, brussels, line]).plot() WORKING WITH GEOSPATIAL DATA IN PYTHON
Spatial relationships belgium.contains(brussels) belgium.touches(france) True True france.contains(brussels) line.intersects(france) False True brussels.within(belgium) line.intersects(uk) True False WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Spatial relationships w ith GeoPandas W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Dani Arribas - Bel Geographic Data Science Lab ( Uni v ersit y of Li v erpool )
Element -w ise spatial relationship methods brussels.within(france) False paris.within(france) True WORKING WITH GEOSPATIAL DATA IN PYTHON
Element -w ise spatial relationship methods brussels.within(france) False For f u ll GeoDataFrame ? cities.head() name geometry 0 Vatican City POINT (12.45338654497177 41.90328217996012) 1 San Marino POINT (12.44177015780014 43.936095834768) 2 Vaduz POINT (9.516669472907267 47.13372377429357) 3 Lobamba POINT (31.19999710971274 -26.46666746135247) ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Element -w ise spatial relationship methods The within() operation for each geometr y in cities['geometry'][0].within(france) cities : False cities.within(france) cities['geometry'][1].within(france) 0 False 1 False False 2 False ... cities['geometry'][2].within(france) 240 False 241 False 242 False False Length: 243, dtype: bool ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Filtering b y spatial relation Filter cities depending on the within() operation : cities[cities.within(france)] name geometry 10 Monaco POINT (7.406913173465057 43.73964568785249) 13 Andorra POINT (1.51648596050552 42.5000014435459) 235 Paris POINT (2.33138946713035 48.86863878981461) WORKING WITH GEOSPATIAL DATA IN PYTHON
Filtering b y spatial relation Which co u ntries does the Ama z on � o w thro u gh ? rivers = geopandas.read_file("ne_50m_rivers_lake_centerlines.shp") rivers.head() type name geometry 0 Lake Centerline Kama LINESTRING (51.94 55.70, 51.88 55.69... 1 River Kama LINESTRING (53.69 58.21, 53.68 58.27... 2 Lake Centerline Abay LINESTRING (37.11 11.85, 37.15 11.89... ... amazon = rivers[rivers['name'] == 'Amazonas'].geometry.squeeze() mask = countries.intersects(amazon) WORKING WITH GEOSPATIAL DATA IN PYTHON
Filtering b y spatial relation countries[mask] name continent geometry 22 Brazil South America POLYGON ((-57.63 -30.22, -56.29 -28.... 35 Colombia South America POLYGON ((-66.88 1.25, -67.07 1.13, ... 124 Peru South America POLYGON ((-69.53 -10.95, -68.67 -12.... within contains intersects More at h � ps :// shapel y. readthedocs . io / en / latest / WORKING WITH GEOSPATIAL DATA IN PYTHON
Shapel y objects GeoPandas paris.within(france) cities.within(france) True 0 False 1 False 2 False ... france.intersects(amazon) countries.intersects(amazon) False 0 False 1 False 2 False ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
The " spatial join " operation W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Dani Arribas - Bel Geographic Data Science Lab ( Uni v ersit y of Li v erpool )
Spatial relationships I WORKING WITH GEOSPATIAL DATA IN PYTHON
Spatial relationships II Which cities are located w ithin Bra z il ? brazil = countries.loc[22, 'geometry'] cities[cities.within(brazil)] name geometry 169 Brasília POINT (-47.91799814700306 -15.78139437287899) 238 Rio de Janeiro POINT (-43.22696665284366 -22.92307731561596) 239 São Paulo POINT (-46.62696583905523 -23.55673372837896) B u t w hat if w e w ant to kno w for each cit y in w hich co u ntr y it is located ? WORKING WITH GEOSPATIAL DATA IN PYTHON
The Spatial Join SPATIAL JOIN = transferring a � rib u tes from one la y er to another based on their spatial relationship WORKING WITH GEOSPATIAL DATA IN PYTHON
The spatial join w ith GeoPandas joined = geopandas.sjoin(cities, countries[['name', 'geometry']], op="within") joined.head() name_left geometry name_right 0 Vatican City POINT (12.45338654497177 41.90328217996012) Italy 1 San Marino POINT (12.44177015780014 43.936095834768) Italy 226 Rome POINT (12.481312562874 41.89790148509894) Italy 2 Vaduz POINT (9.516669472907267 47.13372377429357) Austria 212 Vienna POINT (16.36469309674374 48.20196113681686) Austria WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Choropleths : Mapping data o v er space W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Dani Arribas - Bel Geographic Data Science Lab ( Uni v ersit y of Li v erpool )
Choropleths countries.plot(column='gdp_per_cap', legend=True) WORKING WITH GEOSPATIAL DATA IN PYTHON
Choropleths Specif y ing a col u mn : locations.plot(column='variable') Choropleth w ith classi � cation scheme : locations.plot(column='variable', scheme='quantiles', k=7, cmap='viridis') Ke y choices : N u mber of classes ( k ) Classi � cation algorithm ( scheme ) Color pale � e ( cmap ) WORKING WITH GEOSPATIAL DATA IN PYTHON
N u mber of classes (" k ") locations.plot(column='variable', scheme='Quantiles', k=7, cmap='viridis') Choropleths necessaril y impl y information loss ( b u t that ' s OK ) Tension bet w een : Maintaining detail and gran u larit y from original v al u es ( higher k ) Abstracting information so it is easier to process and interpret ( lo w er k ) R u le of th u mb : 3 to 12 classes or " bins " WORKING WITH GEOSPATIAL DATA IN PYTHON
Classiffication algorithms (" scheme ") locations.plot(column='variable', scheme='quantiles', k=7, cmap='viridis') Ho w do w e allocate e v er y v al u e in o u r variable into one of the k gro u ps ? T w o ( common ) approaches for contin u o u s v ariables : Eq u al Inter v als ( 'equal_interval' ) Q u antiles ( 'quantiles' ) WORKING WITH GEOSPATIAL DATA IN PYTHON
Eq u al Inter v als locations.plot(column='variable', scheme='equal_interval', k=7, cmap='Purples') WORKING WITH GEOSPATIAL DATA IN PYTHON
Q u antiles locations.plot(column='variable', scheme='quantiles', k=7, cmap='Purples') WORKING WITH GEOSPATIAL DATA IN PYTHON
Color Categories , non - ordered locations.plot(column='variable', categorical=True, cmap='Purples') Grad u ated , seq u ential locations.plot(column='variable', k=5, cmap='RdPu') Grad u ated , di v ergent locations.plot(column='variable', IMPORTANT : Align w ith y o u r p u rpose k=5, cmap='RdYlGn') WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Recommend
More recommend