Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
Construction

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"
2
3#include <filesystem>
4#include <ranges>
5
6namespace geo = quetzal::geography;
7
8int main()
9{
10 auto file = std::filesystem::current_path() / "data/bio1.tif";
11
12 // The raster has 10 bands that we will assign to 2001 ... 2010.
13 std::vector<int> times(10);
14 std::iota(times.begin(), times.end(), 2001);
15
16 // Initialize the landscape
17 auto land = geo::raster<>::from_file(file, times);
18
19 // Our graph will not store any useful information
22
24
25 std::cout << "Graph has " << graph.num_vertices() << " vertices, " << graph.num_edges() << " edges." << std::endl;
26
27}
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"
2
3#include <filesystem>
4#include <ranges>
5
6namespace geo = quetzal::geography;
7
8int main()
9{
10 auto file1 = std::filesystem::current_path() / "data/bio1.tif";
11 auto file2 = std::filesystem::current_path() / "data/bio12.tif";
12
13 // The raster have 10 bands that we will assign to 2001 ... 2010.
14 std::vector<int> times(10);
15 std::iota(times.begin(), times.end(), 2001);
16
17 // Initialize the landscape: for each var a key and a file, for all a time series.
18 using landscape_type = geo::landscape<>;
19 auto land = landscape_type::from_file({{"bio1", file1}, {"bio12", file2}}, times);
20
21 // Our graph will not store any useful information
24
26
27 std::cout << "Graph has " << graph.num_vertices() << " vertices, " << graph.num_edges() << " edges." << std::endl;
28
29}
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"
2
3namespace geo = quetzal::geography;
4
5// User-defined concept of spatial grid
6struct MySpatialGrid
7{
8 // it is just required to define these two functions
9 constexpr int width() const { return 30; }
10 constexpr int height() const { return 10; }
11};
12
13int main()
14{
15
16 static_assert(geo::two_dimensional<MySpatialGrid>, "MySpatialGrid does not fulfill the two_dimensional requirements");
17 MySpatialGrid land;
18
19 // Our graph will not store any useful information
22
24
25 std::cout << "Graph has " << graph.num_vertices() << " vertices, " << graph.num_edges() << " edges." << std::endl;
26
27}
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"
2
3namespace geo = quetzal::geography;
4
5int main()
6{
10
11 int num_vertices = 100;
12 graph_type graph( num_vertices );
13
14 graph.add_edge( 0, 1 );
15 graph.add_edge( 0, 2 );
16 graph.add_edge( 0, 3 );
17
18 std::cout << "Graph has " << graph.num_vertices() << " vertices, " << graph.num_edges() << " edges." << std::endl;
19
20}
Definition graph.hpp:262

Output

1Graph has 100 vertices, 3 edges.

Dense Graph

Input

1#include "quetzal/geography.hpp"
2
3namespace geo = quetzal::geography;
4
5int main()
6{
10
11 int num_vertices = 100;
12 graph_type graph( num_vertices );
13
14 graph.add_edge( 0, 1 );
15 graph.add_edge( 0, 2 );
16 graph.add_edge( 0, 3 );
17
18 std::cout << "Graph has " << graph.num_vertices() << " vertices, " << graph.num_edges() << " edges." << std::endl;
19
20}

Output

1Graph has 100 vertices, 3 edges.