import pandas as pd
import shapely
import geopandas as gpd
We also limit the maximum number of printed rows to six, to save space, using the 'display.max_rows'
option of pandas.
'display.max_rows', 6) pd.set_option(
Projects often start by importing an existing vector layer saved as a GeoPackage (.gpkg
) file, an ESRI Shapefile (.shp
), or other geographic file format. The function gpd.read_file
imports a GeoPackage file named world.gpkg
located in the data
directory of Python’s working directory into a GeoDataFrame
named gdf
.
= gpd.read_file('data/world.gpkg') gdf
The result is an object of type (class) GeoDataFrame
with 177 rows (features) and 11 columns, as shown in the output of the following code:
geopandas.geodataframe.GeoDataFrame
geopandas.geodataframe.GeoDataFrame
gdf.shape
(177, 11)
The GeoDataFrame
class is an extension of the DataFrame
class from the popular pandas package (McKinney 2010). This means we can treat non-spatial attributes from a vector layer as a table, and process them using the ordinary, i.e., non-spatial, established function methods. For example, standard data frame subsetting methods can be used. The code below creates a subset of the gdf
dataset containing only the country name and the geometry.
= gdf[['name_long', 'geometry']]
gdf gdf
The following expression creates a subdataset based on a condition, such as equality of the value in the 'name_long'
column to the string 'Egypt'
.
'name_long'] == 'Egypt'] gdf[gdf[