Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views35 pages

Module 4-Geopandas

The document provides an overview of Geopandas, an open-source Python library for handling and analyzing geospatial data, including its installation, data structures, and geospatial operations. It covers key features such as the GeoDataFrame, methods for loading and manipulating spatial data, and advanced topics like plotting, working with projections, and interactive maps. The document also includes examples of using Geopandas for various geospatial analyses and visualizations.

Uploaded by

Nande Nangula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

Module 4-Geopandas

The document provides an overview of Geopandas, an open-source Python library for handling and analyzing geospatial data, including its installation, data structures, and geospatial operations. It covers key features such as the GeoDataFrame, methods for loading and manipulating spatial data, and advanced topics like plotting, working with projections, and interactive maps. The document also includes examples of using Geopandas for various geospatial analyses and visualizations.

Uploaded by

Nande Nangula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

GIS Programming

GIP710S

Module 4
Course Lecturer: Dr Oluibukun Ajayi
[email protected]

Oluibukun [email protected]
GEOPANDAS MODULE

Oluibukun [email protected]
Presentation Outline
• Introduction to Geopandas
• Installation and setup
• Data structures
• Loading and working with spatial data
• Geospatial operations
• Advanced Topics
• Recap/ Conclusion

Oluibukun [email protected]
Introduction to Geopandas
• Geopandas is an open-source Python library for
handling and analysing geospatial data.

• It extends the popular Pandas library, which is


widely used for data manipulation and analysis,
to handle and work with geographic data.

Oluibukun [email protected]
Introduction to Geopandas
• Geopandas was first released in 2013 and has
since become a popular library for geospatial
data analysis.

• Its popularity is due to its user-friendly


Application Programming Interface (API),
integration with other popular Python libraries,
and its ability to handle large and complex
geospatial data.
Oluibukun [email protected]
Introduction to Geopandas
• Geopandas extends the Pandas library by
adding a new data structure called the
GeoDataFrame, which is similar to a Pandas
DataFrame but contains additional information
about the geometry and location of the data.

• The GeoDataFrame combines the functionality


of Pandas with the capabilities of popular
geospatial libraries such as Fiona, PyProj, and
Shapely.
Oluibukun [email protected]
Advantages of using Geopandas
Some of the key advantages of using Geopandas include:
1. Handling and analysis of geospatial data in a single
library
2. Integration with other popular Python libraries such as
Matplotlib for plotting and Seaborn for statistical
visualization
3. User-friendly API for handling and manipulating
geospatial data
4. Large community and active development, leading to
frequent updates and new features
5. Support for a wide range of geospatial file formats
including shapefiles, GeoJSON, and more.
Oluibukun [email protected]
Installation and Setup
➢ Required dependencies
• Before installing Geopandas, it is important to have the following
dependencies installed:
1. NumPy, 2.Pandas, 3. Fiona, 4. Shapely, 5. PyProj, 6. Matplotlib
(for visualizing data)

Installing Geopandas using pip


• To install Geopandas, you can use the pip package manager by
running the following command in your terminal or command
prompt:
pip install geopandas

Oluibukun [email protected]
Installation and Setup
➢ Importing Geopandas into your project

• Once installed, you can import Geopandas into your project by


adding the following line of code to your script:
import geopandas as gpd

• Importing Geopandas using the gpd alias allows you to access all
of its functionality and use its classes and methods.

Oluibukun [email protected]
Data structures
➢ Geopandas GeoDataFrame
The primary data structure in Geopandas is the GeoDataFrame.
This is a subclass of the Pandas DataFrame and contains a column
of shapely geometries that represent the location and shape of
geographic features. The GeoDataFrame is used to store and
manipulate geospatial data, and it supports all the functionality of
a Pandas DataFrame.

➢ Geopandas GeoSeries
The GeoSeries is a subclass of the Pandas Series and is used to
store a single shapely geometry. It is used to represent a single
geographic feature, such as a point, line, or polygon.
• The GeoSeries is often used in combination with the
GeoDataFrame to represent complex geospatial data.

Oluibukun [email protected]
➢ Attributes and Methods
Data structures
• GeoDataFrames and GeoSeries have several attributes
and methods that are specific to geospatial data. Some of
the most useful ones include:
• geometry: a column or series that contains shapely
geometries
• plot(): a method for plotting the geospatial data
• centroid: an attribute that returns the center point of a
geometry
• distance(): a method for calculating the distance between
two geometries
• buffer(): a method for creating a buffer around a
geometry. Oluibukun [email protected]
Loading and working with spatial data
➢Loading spatial data
Geopandas provides several methods for loading
geospatial data into a GeoDataFrame. Some of the
most commonly used methods include:
• gpd.read_file(): reads a geospatial file format
such as a shapefile or GeoJSON and returns a
GeoDataFrame
• gpd.from_file(): reads a geospatial file format and
returns a GeoDataFrame
• gpd.GeoDataFrame(): creates a new
GeoDataFrame from scratch.
Oluibukun [email protected]
Loading and working with spatial data
➢ Reading geospatial data
Geopandas provides several methods for reading
geospatial data, including the first two that has been
considered under loading geospatial data:
• gpd.read_file(): reads a geospatial file format such as a
shapefile or GeoJSON and returns a GeoDataFrame
• gpd.from_file(): reads a geospatial file format and
returns a GeoDataFrame
The gpd.read_file() method is the most commonly used
method for reading geospatial data, and it supports
several popular file formats, including shapefiles,
GeoJSON, and more. To read a file, you simply need to
pass the file path to the gpd.read_file() method.
Oluibukun [email protected]
Loading and working with spatial data
➢ Writing geospatial data
Geopandas also provides several methods for writing
geospatial data, including:
• df.to_file(): writes a GeoDataFrame to a geospatial file
format
• df.to_json(): writes a GeoDataFrame to a JSON file
• df.to_csv(): writes a GeoDataFrame to a CSV file
To write a GeoDataFrame to a geospatial file, you simply
need to call the df.to_file() method and pass in the file
path and the file format.

Oluibukun [email protected]
Loading and working with spatial data
➢ Examining spatial data
Once you have loaded your geospatial data into a
GeoDataFrame, you can examine its content and
structure using methods such as:
• df.head(): displays the first few rows of the
GeoDataFrame
• df.info(): displays information about the
GeoDataFrame, including the number of rows, data
types, and memory usage
• df.columns: displays the columns in the GeoDataFrame

Oluibukun [email protected]
Loading and working with spatial data
➢ Plotting spatial data
Geopandas makes it easy to visualize geospatial data using the
built-in plot() method. This method uses Matplotlib to generate
a map of the data, showing the geographic features and their
location.
➢ Manipulating spatial data
Geopandas provides several methods for manipulating
geospatial data, including:
• df.drop(): removes columns or rows from the GeoDataFrame
• df.rename(): renames columns in the GeoDataFrame
• df.sort_values(): sorts the GeoDataFrame by one or more
columns.
• df.set_index(): sets the index of the GeoDataFrame
Oluibukun [email protected]
Geospatial operations

Geospatial operations are a set of operations that allow


you to manipulate and analyze geospatial data.
Geopandas provides several built-in methods for
performing common geospatial operations, including:
• Overlaying geometries: Overlaying geometries is the
process of combining two or more geospatial datasets
based on their spatial relationships. Geopandas
provides the gpd.overlay() method for performing
overlay operations, including union, intersection,
difference, and symmetrical difference.

Oluibukun [email protected]
Geospatial operations

• Measuring distances: Measuring distances is the


process of determining the distance between two or
more geospatial features. Geopandas provides the
geopy library for performing distance calculations,
including the Haversine formula, great-circle distances,
and more.
• Calculating areas: Calculating areas is the process of
determining the area of a geospatial feature.
Geopandas provides the area property for calculating
the area of a polygon.

Oluibukun [email protected]
Geospatial operations

• Calculating centroids: Calculating centroids is the


process of determining the center of a geospatial
feature. Geopandas provides the centroid property for
calculating the centroid of a polygon.
• Performing spatial joins: Performing spatial joins is the
process of combining two or more geospatial datasets
based on their spatial relationships. Geopandas
provides the gpd.sjoin() method for performing spatial
joins, including left, right, inner, and outer joins.

Oluibukun [email protected]
Geospatial operations

• Buffering geometries: Buffering geometries is the


process of creating a new geometry that is a specified
distance away from an existing geometry. Geopandas
provides the buffer method for buffering geometries.
• Clipping geometries: Clipping geometries is the process
of trimming one geometry based on the shape of
another geometry. Geopandas provides the clip
method for clipping geometries.

Oluibukun [email protected]
Advanced Topics

➢ A. Plotting with Geopandas


• Plotting is a powerful way to visualize geospatial data.
Geopandas provides built-in support for plotting
geospatial data using the popular matplotlib library.
• To plot a GeoDataFrame, you simply need to call the
df.plot() method, which will generate a basic plot of
the geospatial data.
• You can also customize the plot by passing in additional
arguments, such as the column argument to specify
which column to plot, the cmap argument to specify
the color map, and more.
Oluibukun [email protected]
Advanced Topics

➢ A. Plotting with Geopandas


Example of plotting GeoDataFrame:
import geopandas as gpd
import matplotlib.pyplot as plt
# Load the GeoDataFrame
df = gpd.read_file("path/to/file.shp")
# Plot the GeoDataFrame
df.plot()
plt.show()

Oluibukun [email protected]
Advanced Topics
➢ Working with projections
• Projections are an important aspect of geospatial data, as
they define how the data is represented on a map.
• Geopandas provides built-in support for working with
projections, including converting between projections
and transforming data from one projection to another.
• To work with projections in Geopandas, you need to be
familiar with the pyproj library, which is used for
performing projection-related operations.
• Here's an example of transforming data from one
projection to another:
Oluibukun [email protected]
Advanced Topics
➢Working with projections: Examples

import geopandas as gpd


from pyproj import CRS

# Load the GeoDataFrame


df = gpd.read_file("path/to/file.shp")

# Define the original and target projections


original_proj = df.crs
target_proj = CRS.from_epsg(4326)

# Transform the data from one projection to another


df = df.to_crs(target_proj)

Oluibukun [email protected]
Advanced Topics
➢Choropleth mapping
• Choropleth mapping is a technique for visualizing the
spatial distribution of a variable.
• In a choropleth map, regions are colored or patterned
in relation to a data value associated with that region.
• In Geopandas, you can create a choropleth map using
the plot() method in combination with the column and
scheme arguments. The column argument specifies
which column in the GeoDataFrame contains the data
values, and the scheme argument specifies the color
scheme to use for the map. See an example below:

Oluibukun [email protected]
Advanced Topics
➢Choropleth mapping
• In this example, the column argument is set to "population",
indicating that the data values are stored in the population
column. The scheme argument is set to "fisher_jenks",
indicating that the Fisher-Jenks classification method should
be used to generate the color scheme for the map.
import geopandas as gpd
import matplotlib.pyplot as plt
# Load the GeoDataFrame
df = gpd.read_file("path/to/file.shp")
# Plot the choropleth map
df.plot(column="population", scheme="fisher_jenks", legend=True)
# Add a title to the plot
plt.title("Population by Region")
# Show the plot
plt.show()
Oluibukun [email protected]
Advanced Topics
➢ Interactive maps
• Interactive maps are digital maps that allow users to
interact with and explore geographical data. They can
be used to display a wide range of information, such
as points of interest, boundaries, and demographic
data, and allow users to pan, zoom, and filter this
data to gain insights and understand patterns.
• Interactive maps can be created using web
technologies such as HTML, CSS, and JavaScript, and
can be embedded in web pages, mobile apps, and
other software applications.
Oluibukun [email protected]
Advanced Topics
➢ Interactive maps
• They can be used for a variety of purposes, including
navigation, visualizing data, and supporting decision-
making.
• There are many libraries and frameworks available for
creating interactive maps, including leaflet, Google Maps
API, and Folium (in the context of Python).
• These libraries provide a range of tools and features for
creating and customizing maps, including markers,
popups, layers, and choropleth visualizations.
• Below is an example using Folium.
Oluibukun [email protected]
import folium
# Load GeoDataFrame Advanced Topics
countries = gpd.read_file("path/to/countries.shp")
# Create a Folium map centered on the mean latitude and longitude of the GeoDataFrame
mean_latitude = countries["geometry"].y.mean()
mean_longitude = countries["geometry"].x.mean()
m = folium.Map(location=[mean_latitude, mean_longitude], zoom_start=3)
# Add the countries GeoDataFrame to the map as a choropleth
folium.Choropleth(
geo_data=countries.to_json(),
name='countries',
data=countries,
columns=['country', 'gdp_per_cap'],
key_on='feature.properties.country',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='GDP per capita').add_to(m)
# Save the map to an HTML file
m.save("countries.html")
This example creates an interactive map using Folium, with a GeoDataFrame of countries
loaded and displayed as a choropleth based on their GDP per capita. The map is saved as
an HTML file.
Oluibukun [email protected]
Advanced Topics
➢ Raster data analysis
Raster data analysis is the process of analysing data stored in a
raster format, which is a grid of cells that represent a continuous
surface. Geopandas provides support for working with raster data
through its integration with the rasterio library.
import rasterio
import numpy as np
# Load a raster dataset
with rasterio.open("path/to/raster.tif") as src:
band = src.read(1)
# Perform a simple calculation on the raster data
result = np.mean(band)
print(result)
In this example, the rasterio.open function is used to open a raster
dataset, and the read method is used to read the data from the first
band of the raster. The numpy library is then used to perform a simple
calculation on the raster data, in this case calculating the mean of the
data. The result of the calculation is then printed to theOluibukun
[email protected]
Potential applications of Geopandas

Geopandas has a wide range of potential


applications due to its ability to handle and
analyze spatial data efficiently. Here are some
examples:
• Geographic data exploration: Geopandas can
be used to load, visualize, and manipulate
geographical data, making it a useful tool for
exploratory data analysis and data
visualization.
Oluibukun [email protected]
Potential applications of Geopandas
• Spatial data analysis: Geopandas provides a
number of spatial analysis methods, such as
intersects, union, and buffer, that allow users to
perform complex spatial analysis tasks, such as
spatial join, proximity analysis, and spatial
clustering.
• GIS applications: Geopandas can be used to
build GIS applications that perform a variety of
tasks, such as route optimization, spatial
planning, and land use management.
Oluibukun [email protected]
Potential applications of Geopandas

• Environmental modeling: Geopandas can be


used to analyze and model environmental
data, such as air and water quality, land use
patterns, and natural resource management.
• Location-based services: Geopandas can be
used to build location-based services, such as
geolocation-based marketing, geotagging, and
location-based recommendations.

Oluibukun [email protected]
Potential applications of Geopandas

• Urban planning and design: Geopandas can


be used to support urban planning and design
by helping to analyse urban growth patterns,
demographic data, and transportation
networks.

Oluibukun [email protected]
THE END!

Oluibukun [email protected]

You might also like