Friday, August 1, 2014

Week #11. Network's business logic

At this week I've implemented a support of network's business logic (commit) as I described in my previous posts which is based on the concept of string rules. More general, for the future subclasses of GNMNetwork the method CreateRule(const char *pszRuleStr) can be used to create a some kind of rule/constraint/restriction in the concrete network format. In GNMGdalNetwork it is used to determine how the costs or directions or for the graph edges can be extracted during the connection of features and how the features can be connected with each other. GNMGdalNetwork::CreateRule() parses the given string according to the special syntax (see my first post about rules). I've added to the gnmmanage utility some new commands, so now it can be used like the following:
>gnmmanage --long-usage
Usage: gnmmanage [--help][-q][-quiet][--utility_version][--long-usage]
[create [-f format_name] [-t_srs srs_name] [-dsco NAME=VALUE]... ]
[import src_dataset_name] [-l layer_name]
[connect gfid_src gfid_tgt gfid_con [-c cost] [-ic inv_cost] [-dir dir]]
[rule rule_str]
[autoconnect tolerance]
[remove]
[-nt]
gnm_name

create: create connectivity or the full network, depending on native format support (-nt)
-f format_name: output file format name, possible values are:
[ESRI Shapefile]
-t_srs srs_name: spatial reference input
-dsco NAME=VALUE: dataset creation option (format specific)
import src_dataset_name: dataset name to copy
-l layer_name: layer name in dataset (optional)
connect gfid_src gfid_tgt gfid_con: make a topological connection, where the gfid_src and gfid_tgt are vertexes and gfid_con is edge
-c cost -ic inv_cost -dir dir: manually assign the following values: the cost (weight), inverse cost and direction of the edge (optional)
rule rule_str: creates a rule/constraint in the network by the given rule_str string
autoconnect tolerance: create topology automatically with the given double tolerance
remove: remove connectivity or network, depending on native format support (-nt)
-nt: use native network format (now unavailable)
So lets use the gnmmanage in combination with gnmanalyse to see the new functionality. For example we need to calculate the shortest path between two points. The layer lines has the special field called "L" where the length of the feature is stored. We must create network, import layers and create some rules before the automatic connection will be called in order to put in the graph the "L" field values as costs. For example for bat windows file:
gnmmanage.exe create -f "ESRI Shapefile" -t_srs "EPSG:4326" -dsco "net_name=my_network_1" ..\network_data
gnmmanage.exe import ..\in_data\krasnogorsk -l lines ..\network_data
gnmmanage.exe import ..\in_data\krasnogorsk -l kolodci ..\network_data
gnmmanage.exe import ..\in_data\krasnogorsk -l reshetki ..\network_data
gnmmanage.exe rule "CLASS gnm_lines_line COSTS L" ..\network_data
gnmmanage.exe rule "CLASS gnm_lines_line INVCOSTS L" ..\network_data
gnmmanage.exe rule "CLASS gnm_lines_line DIRECTS my_dir" ..\network_data
gnmmanage.exe autoconnect 0.00005 ..\network_data
The piece of network looks like the following:

The 717 and 744 have not been connected during the automatic connection because they are far from each other. We can connect them manually in order to create the alternative path for routing.
gnmmanage.exe connect 717 744 231 ..\network_data
After that we use Dijkstra algorithm several times to calculate a path between the points: 1) 716->740; 2) 715->740; 3) 711->740. The shortest path is calculated properly, according to the summary length in each case.


During our work with network the connection rules have not been set. The concept of rules considers that if no NETWORK rules for particular classes have been set than the connections of all features with each other are allowed by default. So there were no problems. If we set at least one connection rule for the class, then during the ConnectFeatures() method the permission of the connection will be checked anytime.

We create the following rules and autoconnect the newly created network:
...
gnmmanage.exe rule "NETWORK CONNECTS gnm_reshetki_point WITH gnm_kolodci_point VIA gnm_lines_line" ..\network_data
gnmmanage.exe rule "NETWORK CONNECTS gnm_kolodci_point WITH gnm_kolodci_point VIA gnm_lines_line" ..\network_data
gnmmanage.exe autoconnect 0.00005 ..\network_data
After the automatic connection all kolodci features have been connected with reshetki features and with themselves, as opposite to reshetki & reshetki features because such rule has not been set.
 So the shortest path will not be found between 1100 and 1341 features (1). After the creation of corresponding rule two reshetki features can be connected manually and the path will be found (2). In the picture: kolodci: circle with dots, reshetki: stars, lines: lines.



No comments:

Post a Comment