-
Notifications
You must be signed in to change notification settings - Fork 29
Add vector data analysis tutorial #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Valyrian-Code
wants to merge
14
commits into
OSGeo:main
Choose a base branch
from
Valyrian-Code:add-vector-analysis-tutorial
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1f658fb
Add vector data analysis tutorial
Valyrian-Code 5a96f47
Address review: fix images, add attribute table and area computation
Valyrian-Code 4d64256
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 18aa6f9
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 59b1578
Merge branch 'main' into add-vector-analysis-tutorial
Valyrian-Code 7e8b287
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 27e0c77
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 0353b80
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code f490d7b
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 06c12c7
Show table extracts for schools and reach-per-ZIP
Valyrian-Code 9d4815b
Address review: cleaner Tools API idioms, scale bar placement
Valyrian-Code 776fd28
Update content/tutorials/vector_analysis/vector_analysis.qmd
Valyrian-Code 27309c7
Merge branch 'main' into add-vector-analysis-tutorial
Valyrian-Code f227d84
Fix scale bar cropped at the lower border
Valyrian-Code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,324 @@ | ||
| --- | ||
| title: "Basic Vector Data Analysis with GRASS" | ||
| author: Rajveer Bishnoi | ||
| date: 2026-07-05 | ||
| date-modified: today | ||
| lightbox: true | ||
| image: images/thumbnail.webp | ||
| categories: [vector, beginner, intermediate, Python] | ||
| description: > | ||
| Learn the core vector operations in GRASS: exploring attributes, selecting by | ||
| attribute and by location, buffering, overlay, and counting features per area. | ||
| format: | ||
| ipynb: default | ||
| html: | ||
| toc: true | ||
| code-tools: true | ||
| code-copy: true | ||
| code-fold: false | ||
| engine: jupyter | ||
| execute: | ||
| eval: false | ||
| jupyter: python3 | ||
| --- | ||
|
|
||
| # Introduction | ||
|
|
||
| Vector data represents geographic features as **points**, **lines**, and **areas**, | ||
| each linked to a table of **attributes**. GRASS has a large family of `v.*` tools for | ||
| working with them. This tutorial introduces the operations you will reach for most | ||
| often, using a single worked example: analyzing how schools in Wake County, North | ||
| Carolina relate to major roads and administrative areas. | ||
|
|
||
| Along the way we will: | ||
|
|
||
| - explore a vector map and its attribute table, | ||
| - **select by attribute** with [v.extract](https://grass.osgeo.org/grass-stable/manuals/v.extract.html), | ||
| - **buffer** features with [v.buffer](https://grass.osgeo.org/grass-stable/manuals/v.buffer.html), | ||
| - **select by location** with [v.select](https://grass.osgeo.org/grass-stable/manuals/v.select.html), | ||
| - perform spatial operations with [v.overlay](https://grass.osgeo.org/grass-stable/manuals/v.overlay.html), and | ||
| - count features per area with [v.vect.stats](https://grass.osgeo.org/grass-stable/manuals/v.vect.stats.html). | ||
|
|
||
| ::: {.callout-note title="Demo dataset"} | ||
| This tutorial uses the standard GRASS | ||
| [North Carolina sample dataset](https://grass.osgeo.org/sampledata/north_carolina/nc_spm_08_grass7.zip) | ||
| (`nc_spm_08_grass7`), which includes `schools_wake` (points), `roadsmajor` (lines), | ||
| and `zipcodes_wake` (areas). | ||
| ::: | ||
|
|
||
| ::: {.callout-note title="How to run this tutorial"} | ||
| The code uses the GRASS Python API in a Jupyter notebook. If you are new to running | ||
| GRASS from Python, see the | ||
| [Get started with GRASS & Python](https://grass-tutorials.osgeo.org/content/tutorials/get_started/fast_track_grass_and_python.html) | ||
| tutorial. Every step also works from the GRASS GUI or command line; just use the | ||
| tool name (for example `v.buffer`) with the same parameters. | ||
| ::: | ||
|
|
||
| # Setup and exploration | ||
|
|
||
| Start a GRASS session and set the | ||
| [computational region](https://grass.osgeo.org/grass-stable/manuals/g.region.html) to | ||
| the extent of the ZIP code areas, which cover the whole county. For vector-only work | ||
| the region mainly controls what the maps display. | ||
|
|
||
| ```{python} | ||
| import sys | ||
| import subprocess | ||
|
|
||
| sys.path.append( | ||
| subprocess.check_output(["grass", "--config", "python_path"], text=True).strip() | ||
| ) | ||
|
|
||
| import grass.script as gs | ||
| import grass.jupyter as gj | ||
| from grass.tools import Tools | ||
|
|
||
| session = gj.init("~/grassdata/nc_spm_08_grass7/PERMANENT") | ||
| tools = Tools() | ||
|
|
||
| tools.g_region(vector="zipcodes_wake") | ||
| ``` | ||
|
|
||
| ::: {.callout-note title="GRASS version"} | ||
| The examples use the [grass.tools API](https://grass.osgeo.org/grass-stable/manuals/python_intro.html) | ||
| (the `Tools` class), introduced in GRASS 8.5. On earlier versions you can run the | ||
| same tools with `gs.run_command("v.buffer", ...)`. | ||
| ::: | ||
|
|
||
| Before analyzing a layer, it helps to know what it contains. | ||
| [v.info](https://grass.osgeo.org/grass-stable/manuals/v.info.html) reports the feature | ||
| types and counts, and its `-c` flag lists the attribute columns. | ||
|
|
||
| ```{python} | ||
| # Feature summary (points, lines, areas ...) | ||
| print(tools.v_info(map="schools_wake", flags="t").text) | ||
|
|
||
| # Attribute columns | ||
| print(tools.v_info(map="schools_wake", flags="c").text) | ||
| ``` | ||
|
|
||
| `schools_wake` has 167 point features. Let's look at the attribute table itself. | ||
| [v.db.select](https://grass.osgeo.org/grass-stable/manuals/v.db.select.html) can | ||
| return the table as JSON, which [pandas](https://pandas.pydata.org/) turns into a tidy | ||
| table: | ||
|
|
||
| ```{python} | ||
|
veroandreo marked this conversation as resolved.
|
||
| import pandas as pd | ||
|
|
||
| pd.DataFrame( | ||
| tools.v_db_select( | ||
| map="schools_wake", | ||
| columns="NAMESHORT,GLEVEL,ADDRCITY,CORECAPACI", | ||
| format="json", | ||
| )["records"] | ||
| ).head() | ||
| ``` | ||
|
|
||
| | NAMESHORT | GLEVEL | ADDRCITY | CORECAPACI | | ||
| |---|---|---|---| | ||
| | SWIFT CREEK | E | Raleigh | 448.0 | | ||
| | BRIARCLIFF | E | Cary | 540.0 | | ||
| | FARMINGTON WOODS | E | Cary | 523.0 | | ||
| | CARY | H | Cary | 2287.0 | | ||
| | ADAMS | E | Cary | 722.0 | | ||
|
|
||
| The `GLEVEL` column records each school's level (`E` for elementary, `M` for | ||
| middle, `H` for high, and so on), which we use next to select schools by | ||
| attribute. Let's first draw an overview map: the ZIP code areas, the major roads, and | ||
| the schools. In [grass.jupyter](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.jupyter.html), | ||
| a `Map` object collects display layers and renders them together. | ||
|
|
||
| ```{python} | ||
| overview = gj.Map(width=500) | ||
| overview.d_vect(map="zipcodes_wake", type="area", | ||
| fill_color="235:235:235", color="180:180:180") | ||
|
veroandreo marked this conversation as resolved.
|
||
| overview.d_vect(map="roadsmajor", color="90:90:90", width=1) | ||
| overview.d_vect(map="schools_wake", icon="basic/circle", size=7, | ||
| fill_color="200:30:30", color="white") | ||
| overview.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| overview.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| # Selecting by attribute | ||
|
|
||
| To keep just the **elementary schools** (`GLEVEL='E'`), use | ||
| [v.extract](https://grass.osgeo.org/grass-stable/manuals/v.extract.html) with a SQL | ||
| `where` clause. | ||
|
|
||
| ```{python} | ||
| tools.v_extract(input="schools_wake", output="elementary", where="GLEVEL='E'") | ||
| print(tools.v_info(map="elementary", flags="t").text) # points=95 | ||
| ``` | ||
|
|
||
| ```{python} | ||
| attr_map = gj.Map(width=500) | ||
| attr_map.d_vect(map="zipcodes_wake", type="area", | ||
| fill_color="235:235:235", color="180:180:180") | ||
| attr_map.d_vect(map="roadsmajor", color="90:90:90", width=1) | ||
| attr_map.d_vect(map="schools_wake", icon="basic/circle", size=6, | ||
| fill_color="180:180:180", color="none") | ||
| attr_map.d_vect(map="elementary", icon="basic/circle", size=8, | ||
| fill_color="30:120:200", color="white") | ||
| attr_map.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| attr_map.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| # Buffering | ||
|
|
||
| A **buffer** is an area of a given radius around features. Here we buffer the major | ||
| roads by 500 m with [v.buffer](https://grass.osgeo.org/grass-stable/manuals/v.buffer.html) | ||
| to represent an "along a major road" corridor. The result is an area map. | ||
|
|
||
| ```{python} | ||
| tools.v_buffer(input="roadsmajor", output="road_buffer", distance=500) | ||
| ``` | ||
|
|
||
| ```{python} | ||
| buffer_map = gj.Map(width=500) | ||
| buffer_map.d_vect(map="zipcodes_wake", type="area", | ||
| fill_color="235:235:235", color="180:180:180") | ||
| buffer_map.d_vect(map="road_buffer", type="area", fill_color="255:200:120", color="none") | ||
| buffer_map.d_vect(map="roadsmajor", color="120:70:20", width=1) | ||
| buffer_map.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| buffer_map.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| # Selecting by location | ||
|
|
||
| Now we combine two layers spatially: | ||
| [v.select](https://grass.osgeo.org/grass-stable/manuals/v.select.html) keeps features | ||
| of one map based on their spatial relationship to another. With `operator=overlap`, | ||
| we keep the elementary schools that fall inside the road buffer. | ||
|
|
||
| ```{python} | ||
| tools.v_select(ainput="elementary", binput="road_buffer", | ||
| output="schools_near", operator="overlap") | ||
| print(tools.v_info(map="schools_near", flags="t").text) # points=15 | ||
| ``` | ||
|
|
||
| Only **15 out of the 95 elementary schools** lie within 500 m of a major road, a | ||
| reminder that major roads here are highways, while most schools sit on local streets. | ||
|
|
||
| ```{python} | ||
| near_map = gj.Map(width=500) | ||
| near_map.d_vect(map="zipcodes_wake", type="area", | ||
| fill_color="235:235:235", color="180:180:180") | ||
| near_map.d_vect(map="road_buffer", type="area", fill_color="255:230:200", color="none") | ||
| near_map.d_vect(map="roadsmajor", color="150:110:60", width=1) | ||
| near_map.d_vect(map="elementary", icon="basic/circle", size=7, | ||
| fill_color="150:150:150", color="none") | ||
| near_map.d_vect(map="schools_near", icon="basic/circle", size=9, | ||
| fill_color="20:150:60", color="white") | ||
| near_map.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| near_map.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| # Overlaying layers | ||
|
|
||
| [v.overlay](https://grass.osgeo.org/grass-stable/manuals/v.overlay.html) combines two | ||
| area maps with set operations. With `operator=and` we get the **intersection**, the | ||
| part of the road corridor that falls within each ZIP code area. The output keeps the | ||
| attributes of both inputs (prefixed `a_` and `b_`). | ||
|
|
||
| ```{python} | ||
| tools.v_overlay(ainput="road_buffer", binput="zipcodes_wake", | ||
| operator="and", output="buffer_by_zip") | ||
| ``` | ||
|
|
||
| ```{python} | ||
| overlay_map = gj.Map(width=500) | ||
| overlay_map.d_vect(map="zipcodes_wake", type="area", | ||
| fill_color="235:235:235", color="180:180:180") | ||
| overlay_map.d_vect(map="buffer_by_zip", type="area", | ||
| fill_color="120:180:220", color="80:80:80", width=1) | ||
| overlay_map.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| overlay_map.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| Because each piece now carries its ZIP code, we can measure how much of each ZIP code | ||
| is within reach of a major road. | ||
| [v.to.db](https://grass.osgeo.org/grass-stable/manuals/v.to.db.html) computes the area | ||
| of every polygon, and a grouped SQL query with | ||
| [db.select](https://grass.osgeo.org/grass-stable/manuals/db.select.html) sums it per | ||
| ZIP code: | ||
|
|
||
| ```{python} | ||
| tools.v_to_db(map="buffer_by_zip", option="area", columns="reach_area", units="kilometers") | ||
|
|
||
| # db.select also returns JSON, so the same pandas pattern works here | ||
| pd.DataFrame( | ||
| tools.db_select( | ||
| sql="SELECT b_ZIPNAME, ROUND(SUM(reach_area), 1) AS reach_km2 " | ||
| "FROM buffer_by_zip GROUP BY b_ZIPNAME ORDER BY reach_km2 DESC", | ||
| format="json", | ||
| )["records"] | ||
| ).head(6) | ||
| ``` | ||
|
|
||
|
veroandreo marked this conversation as resolved.
|
||
| | b_ZIPNAME | reach_km2 | | ||
| |---|---| | ||
| | RALEIGH | 158.3 | | ||
| | WAKE FOREST | 59.1 | | ||
| | ZEBULON | 42.0 | | ||
| | GARNER | 33.4 | | ||
| | CARY | 32.2 | | ||
| | APEX | 29.5 | | ||
|
|
||
| Raleigh leads by a wide margin because it is the largest ZIP code and is crossed by | ||
| the most major roads. | ||
|
|
||
| # Counting features per area | ||
|
|
||
| A common summary is "how many points fall in each area?" | ||
| [v.vect.stats](https://grass.osgeo.org/grass-stable/manuals/v.vect.stats.html) counts | ||
| the points of one map within the areas of another and writes the result to the area | ||
| map's attribute table. We first create a copy of the ZIP codes map with | ||
| [g.copy](https://grass.osgeo.org/grass-stable/manuals/g.copy.html) so the original is | ||
| untouched. | ||
|
|
||
| ```{python} | ||
| tools.g_copy(vector=("zipcodes_wake", "zip_counts")) | ||
| tools.v_vect_stats(points="schools_wake", areas="zip_counts", count_column="n_schools") | ||
| ``` | ||
|
|
||
| Each ZIP code now has an `n_schools` value (ranging from 0 to 17 here). We map it as a | ||
| choropleth with | ||
| [d.vect.thematic](https://grass.osgeo.org/grass-stable/manuals/d.vect.thematic.html), | ||
| using quantile classes so each color holds a similar number of areas. | ||
|
|
||
| ```{python} | ||
| choropleth = gj.Map(width=500) | ||
| choropleth.d_vect_thematic( | ||
| map="zip_counts", column="n_schools", algorithm="qua", nclasses=5, | ||
| colors="255:245:215,255:200:120,240:140:60,200:70:30,140:20:20", | ||
| ) | ||
| choropleth.d_vect(map="roadsmajor", color="120:120:120", width=1) | ||
| choropleth.d_barscale(flags="n", at=(58, 9), bgcolor="white") | ||
| choropleth.show() | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| # Summary | ||
|
|
||
| Using the North Carolina schools, roads, and ZIP codes, we have worked through the | ||
| core vector toolkit in GRASS: | ||
|
|
||
| - exploring features and attributes with `v.info` and `v.db.select`, | ||
| - **selecting by attribute** (`v.extract`) and **by location** (`v.select`), | ||
| - **buffering** (`v.buffer`) and **overlaying** (`v.overlay`) layers, and | ||
| - **counting** points per area (`v.vect.stats`) for a thematic map. | ||
|
|
||
| The operations described here are common in most vector workflows. To turn results | ||
| like the choropleth into a finished, annotated map, continue with | ||
| [Making Thematic Maps](../thematic_maps/thematic_maps.qmd). | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All scale bars are cropped in the lower border now. Fix all of them.