How to Access Built-In GeoPandas Datasets

How to Access Built-In GeoPandas Datasets

Building on our previous guide on installing GeoPandas, we now turn to a recent development: the introduction of the geodatasets package. This new package, separated from the main GeoPandas library, focuses on access to and management of spatial datasets. We’ll walk you through installing geodatasets and demonstrate how it complements GeoPandas in handling geospatial information.

Installing Geodatasets

First, make sure you have GeoPandas installed as per our previous guide. Then, install the geodatasets package in the same environment where you installed GeoPandas:

conda install geodatasets -c conda-forge

It’s worth noting that before the introduction of the geodatasets library, geospatial datasets were often accessed and manipulated without needing a specialized library. Users could download these datasets using geopandas.datasets. Along with the release of GeoPandas Version 1.0.0 on June 24th 2024, this was deprecated and removed. Going forth, new sample datasets are made available via the geodatasets package.

Exploring Available Datasets

Once the new package has been installed, we can begin to see what datasets are available:

import geodatasets

# Get a list of all dataset keys
dataset_keys = list(geodatasets.data.flatten().keys())

# Print the number of available datasets
print(f"Total available datasets: {len(dataset_keys)}")

Output: 

Total available datasets: 58

We currently have a total of 58 datasets available within the geodatasets package. Let’s proceed to check out few of them. 

print("Some available datasets:")
for key in dataset_keys[:5]:   #Adjust the slice to show more or fewer examples
    print(f"  - {key}")

This will show you the first five datasets available. 

Some available datasets:
  - geoda.airbnb
  - geoda.atlanta
  - geoda.cars
  - geoda.charleston1
  - geoda.charleston2

Reviewing a Dataset

Let’s pick a dataset and look at its details:

chosen_dataset = 'geoda.airbnb'
dataset_info = geodatasets.data.query_name(chosen_dataset)

print("Dataset Information:")
print(f"Name: {dataset_info['name']}")
print(f"Description: {dataset_info['description']}")
print(f"Geometry Type: {dataset_info['geometry_type']}")
print(f"Number of Rows: {dataset_info['nrows']}")

This gives us an overview of the dataset:

Dataset Information:
Name: geoda.airbnb
Description: Airbnb rentals, socioeconomics, and crime in Chicago
Geometry Type: Polygon
Number of Rows: 77

This information tells us that we’re working with polygon data, likely representing different areas of Chicago, with information about Airbnb rentals, socioeconomic factors, and crime statistics.

Loading and Examining the Data

Now, let’s load the data and take a closer look. We’ll break this down into several steps for easier understanding.

Step 1: Loading the Data

First, we’ll load the dataset and take a look at its columns:

import geopandas as gpd

# Get the path to the dataset
path = geodatasets.get_path('geoda.airbnb')

# Load the dataset
gdf = gpd.read_file(path)

# Display the column names
print("Dataset columns:")
print(gdf.columns)

Output:

Dataset columns:
Index(['community', 'shape_area', 'shape_len', 'AREAID', 'response_r',
       'accept_r', 'rev_rating', 'price_pp', 'room_type', 'num_spots',
       'poverty', 'crowded', 'dependency', 'without_hs', 'unemployed',
       'income_pc', 'harship_in', 'num_crimes', 'num_theft', 'population',
       'geometry'],
      dtype='object')

This output shows us all the columns in our dataset, including both spatial (geometry) and non-spatial attributes.

Step 2: Examining the Data

Next, let’s look at the first few rows of our data:

# Display the first few rows
print("\nFirst few rows:")
print(gdf.head())

Output:

First few rows:
         community     shape_area      shape_len  AREAID  response_r  \
0          DOUGLAS  46004621.1581  31027.0545098      35   98.771429   
1          OAKLAND  16913961.0408  19565.5061533      36   99.200000   
2      FULLER PARK  19916704.8692           None      37   68.000000   
3  GRAND BOULEVARD  48492503.1554  28196.8371573      38   94.037037   
4          KENWOOD  29071741.9283  23325.1679062      39   92.542857   

    accept_r  rev_rating    price_pp  room_type  num_spots  ...  crowded  \
0  94.514286   87.777778   78.157895   1.789474         38  ...      1.8   
1  90.105263   88.812500   53.775000   1.850000         20  ...      1.3   
2        NaN   91.750000   84.000000   1.833333          6  ...      3.2   
3  83.615385   92.750000  119.533333   1.533333         30  ...      3.3   
4  88.142857   90.656250   77.991453   1.615385         39  ...      2.4   

   dependency  without_hs  unemployed  income_pc  harship_in  num_crimes  \
0        30.7        14.3        18.2      23791          47        5013   
1        40.4        18.4        28.7      19252          78        1306   
2        44.9        26.6        33.9      10432          97        1764   
3        39.5        15.9        24.3      23472          57        6416   
4        35.4        11.3        15.7      35911          26        2713   

   num_theft  population                                           geometry  
0       1241       18238  POLYGON ((-87.609 41.845, -87.609 41.845, -87....  
1        311        5918  POLYGON ((-87.592 41.817, -87.592 41.817, -87....  
2        383        2876  POLYGON ((-87.629 41.802, -87.629 41.802, -87....  
3       1428       21929  POLYGON ((-87.607 41.817, -87.607 41.817, -87....  
4        654       17841  POLYGON ((-87.592 41.817, -87.592 41.817, -87....  

[5 rows x 21 columns]

This gives us a glimpse of the data, showing various attributes for each community along with their geometric information.

Step 3: Understanding the Data Structure

To get more detailed information about our dataset, we can use the info() method:

# Display basic information about the dataset
print("\nDataset info:")
print(gdf.info())

Output:

Dataset info:
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 77 entries, 0 to 76
Data columns (total 21 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   community   77 non-null     object  
 1   shape_area  77 non-null     object  
 2   shape_len   76 non-null     object  
 3   AREAID      77 non-null     int64   
 4   response_r  68 non-null     float64 
 5   accept_r    67 non-null     float64 
 6   rev_rating  67 non-null     float64 
 7   price_pp    69 non-null     float64 
 8   room_type   69 non-null     float64 
 9   num_spots   77 non-null     int64   
 10  poverty     77 non-null     float64 
 11  crowded     77 non-null     float64 
 12  dependency  77 non-null     float64 
 13  without_hs  77 non-null     float64 
 14  unemployed  77 non-null     float64 
 15  income_pc   77 non-null     int64   
 16  harship_in  77 non-null     int64   
 17  num_crimes  77 non-null     int64   
 18  num_theft   77 non-null     int64   
 19  population  77 non-null     int64   
 20  geometry    77 non-null     geometry
dtypes: float64(10), geometry(1), int64(7), object(3)
memory usage: 12.8+ KB

This output provides a detailed overview of our dataset, including the number of entries, data types of each column, and memory usage.

Step 4: Examining Spatial Components

Finally, let’s look at the spatial aspects of our GeoDataFrame:

# Display the coordinate reference system (CRS)
print("\nCoordinate Reference System:")
print(gdf.crs)

# Display the geometry column's data type
print("\nGeometry column type:")
print(gdf.geometry.dtype)

# Display a summary of the geometry column
print("\nGeometry column summary:")
print(gdf.geometry.head())

Output:

Coordinate Reference System:
EPSG:4326

Geometry column type:
geometry

Geometry column summary:
0    POLYGON ((-87.609 41.845, -87.609 41.845, -87....
1    POLYGON ((-87.592 41.817, -87.592 41.817, -87....
2    POLYGON ((-87.629 41.802, -87.629 41.802, -87....
3    POLYGON ((-87.607 41.817, -87.607 41.817, -87....
4    POLYGON ((-87.592 41.817, -87.592 41.817, -87....
Name: geometry, dtype: geometry

This final part shows us the spatial components of our GeoDataFrame: the Coordinate Reference System (CRS), the data type of the geometry column, and a summary of the first few geometry entries.

Understanding the Changes

The move from geopandas.datasets to the separate geodatasets package offers several benefits:

  1. Modularity: It allows for more frequent updates to sample datasets without requiring updates to GeoPandas itself.
  2. Richer Metadata: Each dataset comes with detailed information about its contents, source, and usage.
  3. Expanded Collection: The new package offers a wider variety of datasets for different use cases.

Conclusion

While the deprecation of geopandas.datasets might seem disruptive at first, the new geodatasets package offers a more robust and flexible way to access sample geospatial data. By separating the datasets from the main GeoPandas package, the developers have made it easier to maintain and expand the collection of sample data available to users.

This example with the Chicago Airbnb dataset demonstrates how we can use GeoPandas and the new geodatasets package to explore socioeconomic data with a geographical component. We’ve seen how to load the data into a GeoDataFrame and examine its structure, setting the stage for more advanced geospatial analysis and visualization in future posts.

Leave a Reply

Your email address will not be published. Required fields are marked *