NGen
Loading...
Searching...
No Matches
network.hpp
1#ifndef NETWORK_H
2#define NETWORK_H
3
4#include <unordered_map>
5
6#include <boost/graph/adjacency_list.hpp>
7#include <boost/graph/graph_traits.hpp>
8#include <boost/graph/graphviz.hpp>
9#include <boost/graph/visitors.hpp>
10#include <boost/graph/depth_first_search.hpp>
11#include <boost/graph/exception.hpp>
12#include <boost/graph/named_function_params.hpp>
13#include <boost/range/adaptor/filtered.hpp>
14#include <boost/range/adaptor/transformed.hpp>
15#include <boost/range/adaptor/reversed.hpp>
16
17#include <features/Features.hpp>
18#include <FeatureBuilder.hpp>
19
20#include "HY_Features_Ids.hpp"
21
22namespace network {
23
24 const static int DEFAULT_LAYER_ID = 0;
25
48 std::string id;
49 };
50
56 template < typename OutputIterator >
57 struct preorder_visitor : public boost::dfs_visitor<>
58 {
59 preorder_visitor(OutputIterator _iter) : m_iter(_iter) {}
60
61 template < typename Edge, typename Graph >
62 void back_edge(const Edge&, Graph&)
63 {
64 BOOST_THROW_EXCEPTION(boost::not_a_dag());
65 }
66
67 template < typename Vertex, typename Graph >
68 void discover_vertex(const Vertex& u, Graph&)
69 {
70 *m_iter++ = u;
71 //std::cerr << "vdiscover_vertex " << u << std::endl;
72 }
73
74 OutputIterator m_iter;
75 };
76
89 template < typename VertexListGraph, typename OutputIterator, typename P,
90 typename T, typename R >
91 void df_preorder_sort(VertexListGraph& g, OutputIterator result,
92 const boost::bgl_named_params< P, T, R >& params)
93 {
94 typedef preorder_visitor< OutputIterator > PreOrderVisitor;
95 depth_first_search(g, params.visitor(PreOrderVisitor(result)));
96 }
97
103 template < typename VertexListGraph, typename OutputIterator >
104 void df_preorder_sort(VertexListGraph& g, OutputIterator result)
105 {
107 g, result, boost::bgl_named_params< int, boost::buffer_param_t >(0)); // no additional named params
108 }
109
114 typedef boost::property<boost::vertex_index_t, std::size_t> IndexT;
119 typedef boost::property<boost::vertex_name_t, std::string, IndexT> NodeT;
126 typedef boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, NodeT> Graph;
131 typedef std::vector< Graph::vertex_descriptor > NetworkIndexT;
138 using IndexPair = std::pair< NetworkIndexT::const_iterator, NetworkIndexT::const_iterator>;
139
146 class Network {
147 public:
153
159 Network( geojson::GeoJSON fabric );
160
167 Network( geojson::GeoJSON features, std::string* link_key);
168
173 virtual ~Network(){}
174
180 NetworkIndexT::const_reverse_iterator begin();
181
187 NetworkIndexT::const_reverse_iterator end();
188
205 auto filter(std::string type, SortOrder order = SortOrder::Topological) const
206 {
207 //todo need to worry about validating input???
208 //if type isn't found as a prefix, this iterator range should be empty,
209 //which is a reasonable semantic
210 return get_sorted_index(order) | boost::adaptors::reversed
211 | boost::adaptors::transformed([this](int const& i) { return get_id(i); })
212 | boost::adaptors::filtered([type](std::string const& s) {
213 //seperate the prefix from the numeric id
214 std::string id_type = s.substr(0, s.find(hy_features::identifiers::separator) );
215 //Allow subtypes, e.g. inx, tnx, cnx, to be pass the filter for a generic nexus type
217 return hy_features::identifiers::isNexus(id_type);
218 }
219 //Allow subtypes, e.g. wb to be pass the filter for a generic catchment type
222 }
223 //any other subtype filter gets only exact matches
224 return id_type == type;
225 });
226
227 }
228
246 auto filter(std::string type, int target_layer, SortOrder order = SortOrder::Topological) const
247 {
248 //todo need to worry about valivdating input???
249 //if type isn't found as a prefix, this iterator range should be empty,
250 //which is a reasonable semantic
251 return get_sorted_index(order) | boost::adaptors::reversed
252 | boost::adaptors::transformed([this](int const& i) { return get_id(i); })
253 | boost::adaptors::filtered([this,type,target_layer](std::string const& s) {
254 //seperate the prefix from the numeric id
255 std::string id_type = s.substr(0, s.find(hy_features::identifiers::separator) );
256
257 bool type_matches;
258
259 if (type == hy_features::identifiers::nexus) type_matches = hy_features::identifiers::isNexus(id_type);
260 else if (type == hy_features::identifiers::catchment) type_matches = hy_features::identifiers::isCatchment(id_type);
261 else type_matches = (id_type == type);
262
263 if (!type_matches) return false;
264
265 auto iter = this->layer_map.find(s);
266 if (iter == this->layer_map.end()) return false;
267
268 auto const& layer = iter->second;
269 return layer == target_layer;
270 });
271 }
280 std::string get_id( Graph::vertex_descriptor idx) const;
281
288 std::vector<std::string> get_origination_ids(const std::string& id);
289
296 std::vector<std::string> get_destination_ids(const std::string& id);
297
303 std::size_t size();
304
311
318
324 boost::dynamic_properties dp;
325 dp.property("node_id", get(boost::vertex_name, this->graph));
326 boost::write_graphviz_dp(std::cout, graph, dp);
327 }
328
329 protected:
330
331 private:
332
337 void init_indicies();
338
344
346
352
358
359 //There are "classes" of tailwater to consider, "Free" "Coastal" "Internal" etc
360 //Really the same is true for head waters in the "Free" and "Internal" sense where
361 //there are interactions of boundary conditions
362 //TODO Distributary network??? Has topological "wildcard"
363 //Diffusive routing can assume DAG, Dynamic routing cannot
364
370
375 std::unordered_map<std::string, Graph::vertex_descriptor> descriptor_map;
376
381 std::unordered_map<std::string, long> layer_map;
382
388 const NetworkIndexT& get_sorted_index(SortOrder order = SortOrder::Topological, bool cache = true) const;
389
390 };
391}
392
393#endif //NETWORK_H
A lightweight, graph based index of hydrologic features.
Definition network.hpp:146
NetworkIndexT tdfp_order
Definition network.hpp:345
std::vector< std::string > get_origination_ids(const std::string &id)
Get the origination (upstream) ids (immediate neighbors) of all vertices with an edge connecting to i...
Definition network.cpp:172
NetworkIndexT::const_reverse_iterator begin()
Iterator to the first graph vertex descriptor of the topologically ordered graph vertices.
Definition network.cpp:142
std::string get_id(Graph::vertex_descriptor idx) const
Get the string id of a given graph vertex_descriptor idx.
Definition network.cpp:160
NetworkIndexT topo_order
Vector of topologically sorted features.
Definition network.hpp:343
NetworkIndexT::const_reverse_iterator end()
Iterator to the end of the topologically ordered graph vertices.
Definition network.cpp:147
IndexPair tailwaters()
An iterator pair (begin, end) of the network tailwater features.
Definition network.cpp:156
void print_network()
Print a graphviz (text) representation of the network.
Definition network.hpp:323
IndexPair headwaters()
An iterator pair (begin, end) of the network headwater features.
Definition network.cpp:152
auto filter(std::string type, SortOrder order=SortOrder::Topological) const
Provides a boost transform_iterator, filtered by type , to the topologically ordered graph vertex str...
Definition network.hpp:205
std::vector< std::string > get_destination_ids(const std::string &id)
Get the destination (downstream) ids (immediate neighbors) of all vertices with an edge from id.
Definition network.cpp:183
void init_indicies()
Initializes the head/tailwater iterators after the underlying graph is constructed.
Definition network.cpp:83
const NetworkIndexT & get_sorted_index(SortOrder order=SortOrder::Topological, bool cache=true) const
Get an index of the graph in a particular order.
Definition network.cpp:195
std::unordered_map< std::string, long > layer_map
Mapping of identifier to hydrofabric layer.
Definition network.hpp:381
NetworkIndexT headwaters_idx
Vector of headwater features.
Definition network.hpp:351
Graph graph
The underlying boost::Graph.
Definition network.hpp:369
auto filter(std::string type, int target_layer, SortOrder order=SortOrder::Topological) const
Provides a boost transform_iterator, filtered by type , to the topologically ordered graph vertex str...
Definition network.hpp:246
virtual ~Network()
Destroy the Network object.
Definition network.hpp:173
std::size_t size()
The number of features in the network (number of vertices)
Definition network.cpp:168
Network()
Construct a new empty Network object.
Definition network.hpp:152
std::unordered_map< std::string, Graph::vertex_descriptor > descriptor_map
Mapping of identity to graph vertex descriptor.
Definition network.hpp:375
NetworkIndexT tailwaters_idx
Vector of tailwater features.
Definition network.hpp:357
std::shared_ptr< FeatureCollection > GeoJSON
Easy short-hand for a smart pointer to a FeatureCollection.
Definition FeatureBuilder.hpp:21
const bool isCatchment(const std::string &type)
Determine if a provided prefix/type is representative of a Catchment concept.
Definition HY_Features_Ids.hpp:33
static const std::string separator
Namespace constants, define the string prefixes for various hydrofabric identifier types.
Definition HY_Features_Ids.hpp:11
static const std::string nexus
Definition HY_Features_Ids.hpp:12
const bool isNexus(const std::string &type)
Determine if a provided prefix/type is representative of a Nexus concept.
Definition HY_Features_Ids.hpp:24
static const std::string catchment
Definition HY_Features_Ids.hpp:15
Definition network.hpp:22
boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS, NodeT > Graph
Parameterized graph storing network::NodeT vertices.
Definition network.hpp:126
std::pair< NetworkIndexT::const_iterator, NetworkIndexT::const_iterator > IndexPair
A type for holding a pair of network::NetworkIndexT iterators, pair.first=begin, pair....
Definition network.hpp:138
boost::property< boost::vertex_index_t, std::size_t > IndexT
Type used to index network::NodeT types.
Definition network.hpp:114
void df_preorder_sort(VertexListGraph &g, OutputIterator result, const boost::bgl_named_params< P, T, R > &params)
Pre-order depth first sort function.
Definition network.hpp:91
boost::property< boost::vertex_name_t, std::string, IndexT > NodeT
Type used to label node properties in a network::Graph.
Definition network.hpp:119
SortOrder
Selector for using different traversal orders for linear return.
Definition network.hpp:29
@ TransposedDepthFirstPreorder
A depth-first, pre-order recorded traversal on a transposed graph.
@ Topological
Topological order.
static const int DEFAULT_LAYER_ID
Definition network.hpp:24
std::vector< Graph::vertex_descriptor > NetworkIndexT
A vector of network::Graph vertex_descriptors.
Definition network.hpp:131
Definition network.hpp:47
std::string id
The structure defining graph vertex properties.
Definition network.hpp:48
Pre-order recording dfs_visitor.
Definition network.hpp:58
preorder_visitor(OutputIterator _iter)
Definition network.hpp:59
void discover_vertex(const Vertex &u, Graph &)
Definition network.hpp:68
void back_edge(const Edge &, Graph &)
Definition network.hpp:62
OutputIterator m_iter
Definition network.hpp:74