The following material covers the basics of using spatial data in python. The main goal is to become familiar with the libraries used, and to try a few examples of operations with vector, and raster data, including some basic visualizations.

Vector Data

Note: A GeoDataFrame is a pandas DataFrame with geometries (GeoSeries)
  • How to load and save spatial data with Geopandas
  • General Data Manipulation (Geopandas)
  • Subsetting by Attributes, to select records based on attributes use the techniques from Pandas
  • Projections
  • Intersects
  • Spatial Join
  • Spatial Aggregation
  • Derive Centroids
  • Bounding Box
    • For each row in a GeoDataFrame GeoSeries.bounds if you want to extract the coordinates.
    • For each row in a GeoDataFrame if you want another geodataframe you can do spatial operations with GeoSeries.envelope
    • For a whole GeoDataFrame GeoSeries.total_bounds
import geopandas

url = "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_0_countries.geojson"
countries_gdf = geopandas.read_file(url)
print(countries_gdf.head())
scalerank  labelrank            sovereignt sov_a3  adm0_dif  level  \
0          1          3           Afghanistan    AFG         0      2   
1          1          3                Angola    AGO         0      2   
2          1          6               Albania    ALB         0      2   
3          1          4  United Arab Emirates    ARE         0      2   
4          1          2             Argentina    ARG         0      2   

                type                 admin adm0_a3  geou_dif  ... region_un  \
0  Sovereign country           Afghanistan     AFG         0  ...      Asia   
1  Sovereign country                Angola     AGO         0  ...    Africa   
2  Sovereign country               Albania     ALB         0  ...    Europe   
3  Sovereign country  United Arab Emirates     ARE         0  ...      Asia   
4  Sovereign country             Argentina     ARG         0  ...  Americas   

         subregion                   region_wb name_len long_len  abbrev_len  \
0    Southern Asia                  South Asia       11       11           4   
1    Middle Africa          Sub-Saharan Africa        6        6           4   
2  Southern Europe       Europe & Central Asia        7        7           4   
3     Western Asia  Middle East & North Africa       20       20           6   
4    South America   Latin America & Caribbean        9        9           4   

  tiny homepart     featureclass  \
0  -99        1  Admin-0 country   
1  -99        1  Admin-0 country   
2  -99        1  Admin-0 country   
3  -99        1  Admin-0 country   
4  -99        1  Admin-0 country   

                                            geometry  
0  POLYGON ((61.21082 35.65007, 62.23065 35.27066...  
1  MULTIPOLYGON (((16.32653 -5.87747, 16.57318 -6...  
2  POLYGON ((20.59025 41.85540, 20.46318 41.51509...  
3  POLYGON ((51.57952 24.24550, 51.75744 24.29407...  
4  MULTIPOLYGON (((-65.50000 -55.20000, -66.45000...  

[5 rows x 64 columns]
countries_gdf.total_bounds
[-180.       -90.       180.        83.64513]
countries_gdf.head().bounds
minx       miny       maxx       maxy
0  60.528430  29.318572  75.158028  38.486282
1  11.640096 -17.930636  24.079905  -4.438023
2  19.304486  39.624998  21.020040  42.688247
3  51.579519  22.496948  56.396847  26.055464
4 -73.415436 -55.250000 -53.628349 -21.832310
countries_gdf.head().envelope
0    POLYGON ((60.52843 29.31857, 75.15803 29.31857...
1    POLYGON ((11.64010 -17.93064, 24.07991 -17.930...
2    POLYGON ((19.30449 39.62500, 21.02004 39.62500...
3    POLYGON ((51.57952 22.49695, 56.39685 22.49695...
4    POLYGON ((-73.41544 -55.25000, -53.62835 -55.2...
dtype: geometry

Making Maps

Optional Bonus Material

Raster

Making Maps

Additional References

The majority of lessons come from

Each Library has it's own great documentation

  • Geopandas for vector geometry and attribute handling
  • Shapely for vector geometry operations that Geopandas doesn’t do. Geopandas actually imports Shapely for most operations.
  • Rasterio
  • rio-cogeo
  • folium