Background
Constructing a spatial graph from a spatial grid, also known as a raster, is a fundamental step in spatial analysis and modeling. Spatial graphs provide a powerful representation of relationships and connectivity between different locations within a geographic area. This process involves transforming the discrete and gridded structure of a spatial grid into a network of vertices and edges, where vertices represent spatial entities (such as cells or locations), and edges capture the spatial relationships and connections between these entities.
There are different ways to build such graph.
Graph from raster
The provided code snippet contains the logic for constructing a spatial graph from a quetzal::geography::raster
.
This involves identifying the cells of the raster to map location_type
values to the vertices, and defining edges based on the desired connectivity.
Input
1#include "quetzal/geography.hpp"
10 auto file = std::filesystem::current_path() /
"data/bio1.tif";
13 std::vector<int> times(10);
14 std::iota(times.begin(), times.end(), 2001);
25 std::cout <<
"Graph has " << graph.num_vertices() <<
" vertices, " << graph.num_edges() <<
" edges." << std::endl;
Individuals can not escape the landscape's borders.
Definition bound_policy.hpp:20
Discrete spatio-temporal variations of an environmental variable.
Definition raster.hpp:38
Geospatial data formatting and processing.
Definition geography.hpp:17
boost::no_property no_property
Represents no information carried by vertices or edges of a graph.
Definition concepts.hpp:71
auto from_grid(SpatialGrid const &grid, VertexProperty const &v, EdgeProperty const &e, Vicinity const &vicinity, Directionality dir, Policy const &bounding_policy)
Spatial graph construction method.
Definition from_grid.hpp:36
boost::undirectedS isotropy
Property of a process independent of the direction of movement.
Definition directionality.hpp:18
Definition geography_dispersal_kernel_4.cpp:13
Definition vicinity.hpp:53
Definition coalescence_binary_tree_2.cpp:5
Output
1Graph has 9 vertices, 36 edges.
Graph from landscape
In the parlance of quetzal, a quetzal::geography::landscape
is just multiple aligned quetzal::geography::raster
defined for different spatial quantities: since their grid structure are identical, the logic relating to spatial graph construction is unchanged. The provided code snippet contains the logic for constructing a spatial graph from a quetzal::geography::landscape
.
This involves identifying the cells of the landscape to map location_type
values to the vertices, and defining edges based on the desired connectivity.
Input
1#include "quetzal/geography.hpp"
10 auto file1 = std::filesystem::current_path() /
"data/bio1.tif";
11 auto file2 = std::filesystem::current_path() /
"data/bio12.tif";
14 std::vector<int> times(10);
15 std::iota(times.begin(), times.end(), 2001);
19 auto land = landscape_type::from_file({{
"bio1", file1}, {
"bio12", file2}}, times);
27 std::cout <<
"Graph has " << graph.num_vertices() <<
" vertices, " << graph.num_edges() <<
" edges." << std::endl;
Discrete spatio-temporal variations of a set of environmental variables.
Definition landscape.hpp:42
Output
1Graph has 9 vertices, 36 edges.
Graph from abstract grid
The provided code snippet demonstrates how to construct a spatial graph from a user-defined abstract grid. This approach is useful when you either don't have or prefer not to use a geospatial file alongside your code, for example if you don't care about spatial quantities.
This involves identifying the height and width of the space to define linearly-indexed vertices, and defining edges based on the desired connectivity.
Input
1#include "quetzal/geography.hpp"
9 constexpr int width()
const {
return 30; }
10 constexpr int height()
const {
return 10; }
16 static_assert(geo::two_dimensional<MySpatialGrid>,
"MySpatialGrid does not fulfill the two_dimensional requirements");
25 std::cout <<
"Graph has " << graph.num_vertices() <<
" vertices, " << graph.num_edges() <<
" edges." << std::endl;
Definition geography_graph_4.cpp:7
Output
1Graph has 300 vertices, 44850 edges.
From scratch
The provided code snippet contains the logic for creating a sparse versus dense graph from scratch. This involves dynamically adding vertices and edges based on user-defined conditions, resulting in a graph that represents the desired spatial relationships.
Sparse Graph
Input
1#include "quetzal/geography.hpp"
11 int num_vertices = 100;
12 graph_type graph( num_vertices );
14 graph.add_edge( 0, 1 );
15 graph.add_edge( 0, 2 );
16 graph.add_edge( 0, 3 );
18 std::cout <<
"Graph has " << graph.num_vertices() <<
" vertices, " << graph.num_edges() <<
" edges." << std::endl;
Output
1Graph has 100 vertices, 3 edges.
Dense Graph
Input
1#include "quetzal/geography.hpp"
11 int num_vertices = 100;
12 graph_type graph( num_vertices );
14 graph.add_edge( 0, 1 );
15 graph.add_edge( 0, 2 );
16 graph.add_edge( 0, 3 );
18 std::cout <<
"Graph has " << graph.num_vertices() <<
" vertices, " << graph.num_edges() <<
" edges." << std::endl;
Output
1Graph has 100 vertices, 3 edges.