Skip to content

Commit

Permalink
Add geopandas and rasterio (#8)
Browse files Browse the repository at this point in the history
* Add geopandas and rasterio

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
giswqs and pre-commit-ci[bot] authored Aug 19, 2024
1 parent 7b3230c commit 4ecd26e
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ __pycache__/
**/.DS_Store
# C extensions
*.so
*.geojson
*.tif

private/
.Python
Expand Down
2 changes: 2 additions & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ parts:
maxdepth: 2
chapters:
- file: book/geospatial/get_started
- file: book/geospatial/geopandas
- file: book/geospatial/rasterio
- file: book/geospatial/leafmap
- file: book/geospatial/vector_viz
- file: book/geospatial/raster_viz
Expand Down
244 changes: 244 additions & 0 deletions book/geospatial/geopandas.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# GeoPandas\n",
"\n",
"## Introduction\n",
"\n",
"[GeoPandas](https://geopandas.org) is an open-source project that makes working with geospatial data in Python easier. It extends the datatypes used by pandas to allow spatial operations on geometric types. GeoPandas combines the capabilities of pandas and Shapely, providing geospatial operations in a pandas-like interface."
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"## Installing and Importing GeoPandas"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"# %pip install geopandas"
]
},
{
"cell_type": "markdown",
"id": "3",
"metadata": {},
"source": [
"Once installed, you can import it like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"import geopandas as gpd\n",
"from shapely.geometry import Point, Polygon"
]
},
{
"cell_type": "markdown",
"id": "5",
"metadata": {},
"source": [
"## Creating GeoDataFrames\n",
"\n",
"A GeoDataFrame is a tabular data structure that contains a 'geometry' column, which holds the geometric shapes. You can create a GeoDataFrame from a list of geometries or from a pandas DataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"# Creating a GeoDataFrame from scratch\n",
"import pandas as pd\n",
"\n",
"data = {\n",
" \"City\": [\"Tokyo\", \"New York\", \"London\", \"Paris\"],\n",
" \"Latitude\": [35.6895, 40.7128, 51.5074, 48.8566],\n",
" \"Longitude\": [139.6917, -74.0060, -0.1278, 2.3522],\n",
"}\n",
"\n",
"df = pd.DataFrame(data)\n",
"gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))\n",
"print(gdf)"
]
},
{
"cell_type": "markdown",
"id": "7",
"metadata": {},
"source": [
"## Reading and Writing Geospatial Data\n",
"\n",
"GeoPandas makes it easy to read and write geospatial data formats like Shapefiles, GeoJSON, and others. Let's read a shapefile and write the GeoDataFrame to a GeoJSON file."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"# Reading a shapefile\n",
"try:\n",
" world = gpd.read_file(gpd.datasets.get_path(\"naturalearth_lowres\"))\n",
" print(world.head())\n",
"except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
"\n",
"# Writing the GeoDataFrame to a GeoJSON file\n",
"output_file = \"world.geojson\"\n",
"try:\n",
" world.to_file(output_file, driver=\"GeoJSON\")\n",
" print(f\"GeoDataFrame has been written to {output_file}\")\n",
"except Exception as e:\n",
" print(f\"An error occurred while writing to {output_file}: {e}\")"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"## Spatial Operations\n",
"\n",
"GeoPandas provides a range of spatial operations, such as buffering, intersections, and spatial joins. Let's explore some of these operations."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"# Example of buffering (creating a buffer zone around points)\n",
"gdf[\"buffer\"] = gdf.buffer(1) # Buffer of 1 degree\n",
"print(gdf[[\"City\", \"buffer\"]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"# Example of spatial join (finding points within a polygon)\n",
"paris = gdf[gdf[\"City\"] == \"Paris\"]\n",
"polygon = Polygon([(2, 48), (2.5, 48), (2.5, 49), (2, 49)])\n",
"polygon_gdf = gpd.GeoDataFrame([1], geometry=[polygon], crs=gdf.crs)\n",
"joined = gpd.sjoin(gdf, polygon_gdf, predicate=\"within\")\n",
"print(joined)"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"## Plotting Geospatial Data\n",
"\n",
"GeoPandas integrates well with Matplotlib, allowing you to easily plot geospatial data. Let's plot the world map with the locations of the cities from our GeoDataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# Plotting the world map and the cities\n",
"world.plot()\n",
"gdf.plot(ax=plt.gca(), color=\"red\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"metadata": {},
"outputs": [],
"source": [
"gdf.crs = \"EPSG:4326\"\n",
"gdf.explore()"
]
},
{
"cell_type": "markdown",
"id": "15",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"1. Create a GeoDataFrame containing a list of countries and their capital cities. Add a geometry column with the locations of the capitals.\n",
"2. Load a shapefile of your choice, filter the data to only include a specific region or country, and save the filtered GeoDataFrame to a new file.\n",
"3. Perform a spatial join between two GeoDataFrames: one containing polygons (e.g., country borders) and one containing points (e.g., cities). Find out which points fall within which polygons.\n",
"4. Plot a map showing the distribution of a particular attribute (e.g., population) across different regions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"metadata": {},
"outputs": [],
"source": [
"# Type your code here"
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"GeoPandas is a powerful tool for geospatial data analysis in Python. It combines the capabilities of pandas with the geometric operations of Shapely, allowing for efficient and intuitive geospatial data manipulation. By practicing with these exercises, you can gain a solid understanding of how to work with geospatial data using GeoPandas."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geo",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 4ecd26e

Please sign in to comment.