Pokazywanie postów oznaczonych etykietą KUCHARSKI. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą KUCHARSKI. Pokaż wszystkie posty

środa, 20 marca 2013

why python for Visum scripting - short answer

that's why

task:
check if UDA exists

code:
def CheckAttr(obj,attr):
       return attr in [e.Code for e in o.Attributes.GetAll]


usage:

CheckAttr(Visum.Net.Links,"ToNodeNo")


with VB, or anything lower level it'd be at least 20 lines of code :P


Update - the same with VB:

Sub UpdateVisumUDAs(ByVal tableName As String _
   , ByVal keyFields As String() _
   , ByRef ds As DataSource _
   , Optional ByVal CreateUDA As Boolean = True _
   , Optional ByVal ValueType As Integer = 5)


Dim o As Object = ds.StringToVisumObject(tableName, ds)
Dim Attributes As Object()
Attributes = o.Attributes.GetAll
Dim AttrCodes As New List(Of String)
For Each Attribute As Object In Attributes
   AttrCodes.Add(Attribute.Code.ToString.ToUpper)
Next
For Each key As String In keyFields
   If CreateUDA AndAlso Not AttrCodes.Contains(key.ToString) Then
   o.AddUserDefinedAttribute(key.ToString, key.ToString, key.ToString, ValueType)
End If
Next


PS. Don't take me as an ignorant - I know more or less pros and cons for python vs. any 'proper' programming language, but I just love the way you can do things in python


czwartek, 24 stycznia 2013

Change Node/Link/Zone Numbers in Visum via COM + limit of Visum

I believe anyone who has created once Visum network out of big extrenal datasets (mainly GIS - .shp) came across problems with numbering duplicates. Here I will show you how to overcome those issues. There are two ways: one is scripting (snippet below), another is via Access database.

Case:
We have model of Kraków (city ca. 1mln inhabitants), we are creating regional model of Małopolska - Kraków is it's capital.
We have detailed GIS network from regional municipality, we have our own Kraków model with Visum network. We want to merge them together.
In most cases we will have problems with duplicate keys in database (i.e. "Node with ID 301 already defined in the network"). That's mainly due to very decent DB architecture behind visum data. Each network object belongs to specific table with well defined 'primary key' and 'foreign key' - which basically cannot have duplicates (http://en.wikipedia.org/wiki/Foreign_key).
It goes like this:
  • each node is identified by it's NO
  • each link is identified by its FromNodeNo and ToNodeNo (those are foreign keys, plus implicitly it has it's own primary key NO - I assume)
  • each turn has its FromNode, ViaNode and ToNode
If during import of any external objects those numbers will get confused we get rubbish results (either all network is mixed up, either it's not imported at all.

Toy-network example:

1. I created following network and exported it to .shp:


Network no 1


2. I created another network like this:
Network no 2
3. I tried to import network 1 into network 2:
a) result without setting "Offset parameter":
No network import at all.
b) when You set "Offset" to a number greater than max NodeNo, you will get something
first you get error for some links (which were already defined in network no 2)

and finally the resulting network will be as follows:
network 1 imported into network 2 (marked added links) all together network makes no sense
As we see there's a lot of confusion.

Solution:

there are two methods: one is scripting, another is via Access DB.


Scripting is easier and faster and can be automated, but it doesn't work for many objects (it works for nodes, doesn't work for links). See examples below(all scripts in python):

a) Bulk (didn't work with older versions of Visum) - much faster:

def get_Nodes(Attr):
       return Visum.Net.Nodes.GetMultiAttValues(Attr)


Nodes=get_Nodes("No")
Nodes=list(Nodes)
Nodes=[list(node) for node in Nodes]
for i,node in enumerate(Nodes):
       node[1]=i+100000000 #put max node no in your network here

# we need to do it twice: once we add big number to all elements and the we can give them ordered numbers starting from whatever
Visum.Net.Nodes.SetMultiAttValues("No",Nodes)

Nodes=get_Nodes("No")
Nodes=list(Nodes)
Nodes=[list(node) for node in Nodes]
for i,node in enumerate(Nodes):
         node[1]=i+1000000 #give your threshold here (nodes will start fron No=1000000

Visum.Net.Nodes.SetMultiAttValues("No",Nodes)


b) element by element - can be very slow:

Nodes=Visum.Net.Nodes.GetMultiAttValues("No")
Nodes=[node[1] for node in Nodes]
for node in Nodes:
       Visum.Net.Nodes.ItemByKey(node).SetAttValue("No",node+
100000000 )
       i+=1
for node in Nodes:
      Visum.Net.Nodes.ItemByKey(node+
100000000 ).SetAttValue("No",i)
      i+=1


2) Access DataBase:
I'm not ver familiar with Access so just a picture of what I do:
a) Export network into Access DB (can take a lot of time)
b) Open access file and edit tables
c) Change keys in tables (for links it's good to change name of "No" column into anything else and create additional column named No, then set this column as a key.
d) make the new column "Autoincrement" - so the number will be integers starting from 1
(for links it needs to be different, links have directed numbers so it shall be like this: 1 1 2 2 3 3 4 4 5 5 6 6 ... - I did it in excel and pasted.
e) you save the file and Import it into Visum

This way we merged four different shapefiles with overlapping numbers and two different Visum networks.
Everything is neatly numbered starting from one onwards.


PS. We came across another issue which seemed strange but we found very easy explanation. During import we got following message that node number exceeds: 2 147 483 648. Which happened to be max of LongInt datatype, so actually that's the limit of Visum :) You cannot go any further. Simply reduce umbers by means of the solutions above.

Hope it helps!




piątek, 20 lipca 2012

Plate number survey add-in for Visum


From raw input files to Visum OD and travel time matrices.



 i2 - Intelligent-infrastructure was working hard last months. We struggled to integrate powerful SQL engine with Visum. We struggled to process errors, to interpret results properly, to create user-friendly gui and we come back even more experienced. Below you can see latest development: From raw plate number results to OD matrix in Visum- that's what was definitely needed. Have a look!

1. Idea

Plate Number survey (APNR) is always a chance to improve your model. However it always comes along with data processing problems - thousands of records stored in numbers of files and all need to be processed to gather information. That's why we integrated data processing within VISUM. Now all standard steps between APNR and OD matrix in Visum are automated in user friendly graphical interface.


2. Functionalities

APNR Support will support you with every step of APNR data processing:
1. Flexible data importer will create SQL database from your records.
2. Powerful database engine will organize results
3. Full Visum integration will import counting points' locations
4. Filtering engine will show data you need (i.e. list of truck crossing two count locations during morning peak hour).
5. Data processing machine will calculate OD matrices with several error detection procedures.
6. You will be able to export travel time skim matrices, paths, OD matrices to Visum.
7. You will see your results on histograms and charts.


fig. 2. flexible filtering engine showing you whatever you need to know

 3. Summary

- take advantage of integrating APNR data and VISUM network model in one flexible Add-In
- speed up your calculations with highly efficient database engine
 - work with user friendly GUI to simply see the data in tables, lists and plots or export ones to Visum and Excel.

APNR created by i2 runs as a script from Visum. It uses data from Visum network and APNR data to provide not only advanced queries to APNR database but also calculation of the characteristics based on Visum network and Count Locations.

fig.3. graphical presentation of observed travel times histogram for OD matrix cell

Contact us at info@intelligent-infrastructure.eu for more details. Visit our website at http://intelligent-infrastructure.eu

PS. Project was done with great help and encouragement from Dipl.-Ing. Timotheus Klein from ARGUS Hamburg, who is co-author of the project.

poniedziałek, 6 lutego 2012

GIS -> Shapefile -> Visum (Transport Network), known issues + snippet for common error

This post summaries problems, that transport modelers experience while importing GIS data as transport network. It contains solutions for known issues, and free python script snippet to solve one particular problem with *.shp import.

We all wish that import of GIS data-base would be as seamless as it seems. However, anyone who has ever imported any GIS transport network into transport modeling software knows, that geographers store data in a way that transport modelers don't. That's why when we import GIS data into transport modeling software, it's in most cases useless. In some cases it's even better to draw network from a scratch, then adjusting imported network.


Among known issues we have:
  1. T junctions without connection (case where dead-end node touches link, but the main link goes "over" T-junction). This issue can be handled (at least in Visum), by means of python script and embedded function (GetNearestLink) - see ready to use code down the page.
  2. Crossing links without node (when two links actually cross,but there's no node to connect them). I found a way to overcome it by means of external libraries, and it works fine for me, however for commercial usage its calculating time is unacceptable. I'll try to shorten the calculations by employing additional conditions.PS. What you need to double check in this case is whether this situation is not a bridge/viaduct – for many external databases you have extra parameter for bridges/viaducts and you can easily filter the data.
  3. Overlapping nodes, and
  4. Almost overlapping nodes (when two dead-end nodes should be a junction,and are either atop of each other, either very close to each other).Two cases above are similar, as you need to  check distance between each pair of nodes, and if its below snap radius, just merge the nodes.
  5. One-way roads/Unblocked turns.Unhandled exception. Even if database provides info on “being one-way”, you still need additional info on what is actual direction. So from my point of view it remains the main unsolved issue when importing GIS, and I struggle with this. The best example for this would be such a junction imported from GIS:
  6. Special case for roundabout. This is really pain in the neck, as you really have not much to do but scream when you see such a case in Visum:
There is a way to handle this issue, however its not straightforward.

Python code for TJunction issue (full code available here , python sythax version available here ):

def TJuction_Error_Handle():
radius=0.1 #specify your snap radius here - in KM
Nodes=Visum.Net.Nodes.GetMultipleAttributes(["NumLinks","No","XCoord","YCoord"])
leng=len(Nodes)
for Node in Nodes:
if Node[0]==1:
Visum.Filters.InitAll()
LinkFilter = Visum.Filters.LinkFilter()
LinkFilter.AddCondition("OP_NONE", False, "FromNodeNo",9,Node[1])
LinkFilter.AddCondition("OP_OR", False, "ToNodeNo",9,Node[1])
LinkFilter.Complement= True
LinkFilter.UseFilter = True
Result=Visum.Net.GetNearestLink(Node[2],Node[3],radius,True)
if Result[0]!=None:
try:
Result[0].SplitViaNode(Node[1])
except:
print Result[0].AttValue("FromNodeNo"),Result[0].AttValue("ToNodeNo"),Node[1]
del Nodes
Visum.Filters.InitAll()

TJuction_Error_Handle()



PS2. For more info check out my website, where full product will be available soon, or simply contact me at info@intelligent-infrastructure.eu

PS3. All cases are from my hometown – Gliwice, Poland. First from surroundings of my high school, others from place where I grown up. My parents see the motorway junction from their windows, and they dislike it a lot