Sample code
Previous  Top  Next

Most of the sample code provided are some very basic examples, which show how to generate a set of network files and calculate a route. It is not possible to provide sample code for all GIS and programming tools, but over time we are adding more.

For the most comprehensive example on how to perform various functions, you should look at RW Net Demo, which is both available as a compiled application at RouteWare website and the source code is included as part of the Delphi Sample code install.

Step-by-step sample (no specific language)

Below you will find a commented sample, which will show you which RW Net functions to call, to generally setup a street network for routing. All checking of error codes from the functions has been omitted to make it more readable:

Generally you should use the same order (see also here) in your code as shown below, or your calculations may fail !

·Initial setup  
This part has to be performed every time you start using RW Net

Enter the password (not needed in VCL, Free and time-limited versions)
RWnetbase1.init("yourpassword")

Link the calculation object to the network object
OCX/.NET: RWcalc1.SetNet(rwnetbase1.identifier)
VCL: RWcalc1.SetNet(rwnetbase1)
DLL: Not needed

Choose a coordinate unit
RWnetBase1.Coord = LatLong

Choose a GIS output format - we pick SHP
RWnetBase1.GISformat = gf_SHP

·Creating the network  
This part is only needed the first time or when your base street network changes. This is also the only functionality in RW Net, which can not be distributed royalty free.

Actual creation of binary network from a SHP file called streets.shp, streets.shx
Second parameter is true, so the node layer is created for visual inspection
Third parameter means a default coordinate system is generated

RWnetBase1.NWCreateSHP("streets",true,"")

Eventually set property ZlevelFile first.

·Creating attributes  
This part is only needed the first time or when the attributes of your base street network changes

First a field in the DBF file should be prepared according to the description here
Actual creation of attributes from DBF file
RWnetBase1.AttributeCreate2("streets.dbf","attrib",0)

·Creating roadnames  
This part is only needed the first time or when the roadnames of your base street network changes. It is also only needed, if you want to create driving directions or use one of the other functions, that work with roadnames.

It is possibly to read directly from a DBF file:
RWnetBase1.RoadNameCreate2(1,"streets.dbf","name","",0,0)
(see RoadNameCreate for an alternative method)

·Loading the network  

Define the units, when distances are reported. This can be changed at any time.
This setting also applies to the SetSpeed command later
RWnetBase1.Units = miles

We don't need support for turn restrictions. Set this before NWload()
RWnetBase.TurnMode = false

The network is loaded into RAM
RWnetBase1.NWLoad()

We define the speed (km/h or miles/h) for every road class (1..7) in the dataset. This has to be before AttributeLoad
RWnetBase1.SetSpeed(1,1)
RWnetBase1.SetSpeed(2,5)
RWnetBase1.SetSpeed(3,20)
RWnetBase1.SetSpeed(4,25)
RWnetBase1.SetSpeed(5,35)
RWnetBase1.SetSpeed(6,45)
RWnetBase1.SetSpeed(7,55)

We load attribute information and define that we want to apply the one-way information.
RWnetBase1.AttributeLoad(true)

We load roadnames from file 1, but don't cache the actual names (only required for sample 9 below)
RWnetBase1.RoadNameLoad(1,false)

Define fastest path
RWcalc1.SetFastest()

Set alpha to the best possible value. Has to be after AttributeLoad and SetFastest.
RWcalc1.Alpha = RWcalc1.OptimumAlpha()

Include the extra variable which can now hold distance (for the fastest path)
RWcalc1.ExtraVarCreate()

Turn restrictions can also be loaded by calling RWnetBase1.TurnImport2_TXT.


Now that the network has been loaded etc. it is time to do some actual calculations:

1) A route
2) A route - dynamic segmentation
3a) A distance matrix
3b) A distance matrix (Curb Approach)
4) Nearest node in one theme for all nodes in another theme
5) Nearest N nodes in one theme for all nodes in another theme
6) Nearest N locations in one theme for all locations in another theme
7) TSP - travelling salesman
8) TSP - travelling salesman - dynamic segmentation
9) Driving directions