I am trying to calculate the cost of a route based on the propietaries of the segments of road.
I am looking for a way to calculate the distance traveled for attribute and I have the next codelines, that works but not like I need.
dim costone
dim costwo
dim total
If [owner] = "FERRONorth" And [owner] = "KNS" Then
costone = [Shape_Length] * 1500
ElseIf [owner] = "FERROSouth" And [owner] = "MAYAB"
costwo = [Shape_Length] * 2800
End If
total = costone + costwo
Solved! Go to Solution.
Thank you @EduardoDiaz for the clarification.
From my understanding of what you wrote, it sounds like each road segment has a different cost per unit distance based on the value of the [owner] field. So revising your original code, I would suggest the following making use of the Select Case ... End Select statement:
Dim cost
Select Case [owner]
Case "FERRONorth"
cost = [Shape_Length] * 1500
Case "KNS"
cost = [Shape_Length] * 1500
Case "FERROSouth"
cost = [Shape_Length] * 2800
Case "MAYAB"
cost = [Shape_Length] * 2800
End Select
It looked like you were trying to combine multiple [owner] values that have the same multiplier. The above code can be simplified down as follows:
Dim cost
Select Case [owner]
Case "FERRONorth", "KNS"
cost = [Shape_Length] * 1500
Case "FERROSouth", "MAYAB"
cost = [Shape_Length] * 2800
End Select
Note that each road segment has its own cost. When you construct the route of multiple road segments, the cost values of these road segments get summed together to yield the total cost of the route.
Let me know if you have any further questions. Thanks!
Alan
Hi @EduardoDiaz,
It looks like you have some logic errors -- in the code you provided, both the If and ElseIf statements will always evaluate to False, as it is never possible for the value of the [owner] field to be two different values at the same time (like being both "FERRONorth" And "KNS" at the same time, or being both "FERROSouth" And "MAYAB" at the same time). So that means that costone and costwo will never have a value set.
Also note that since you're using an If and ElseIf statement, it is never possible for costone and costwo to both be populated, since only either the If or the ElseIf statement (or neither), but never both, will get executed.
Can you provide details of what you're intending the values to be? Thanks!
Alan
Hello, thanks for the replie, I am trying to get the cost of the route based on the distance that the vehicle travel in different segments of road.
For example, if the vehicle travel 100 meters on KNS´s road and 500 on FERRONorth´s road I want to calculate:
costone = 100 * $50
costwo = 500 * $25
cost = costone + costwo
I think the main requirement is know the distance what is traveled according the owner of the road, for example, first 50 meters on KNS, 100 m on FERRONorth, then another 50 meters on KNS, etc.
Example:
distance_on_kns = 340 m
distance_on_FERRONorth = 1500 m (what i try to calculate but like you say, is not the way to)
Then knowing the distance do...
costone = distance_on_kns * $50
costwo = distance_om_FERRONorth * $ 25
cost = costone + costwo
So that I get the final cost adding up the distance and apply their costs.
Within the data there is a field called "Owner" which I know it is the filter to add the distance but I haven´t figured it out yet.
Thank you @EduardoDiaz for the clarification.
From my understanding of what you wrote, it sounds like each road segment has a different cost per unit distance based on the value of the [owner] field. So revising your original code, I would suggest the following making use of the Select Case ... End Select statement:
Dim cost
Select Case [owner]
Case "FERRONorth"
cost = [Shape_Length] * 1500
Case "KNS"
cost = [Shape_Length] * 1500
Case "FERROSouth"
cost = [Shape_Length] * 2800
Case "MAYAB"
cost = [Shape_Length] * 2800
End Select
It looked like you were trying to combine multiple [owner] values that have the same multiplier. The above code can be simplified down as follows:
Dim cost
Select Case [owner]
Case "FERRONorth", "KNS"
cost = [Shape_Length] * 1500
Case "FERROSouth", "MAYAB"
cost = [Shape_Length] * 2800
End Select
Note that each road segment has its own cost. When you construct the route of multiple road segments, the cost values of these road segments get summed together to yield the total cost of the route.
Let me know if you have any further questions. Thanks!
Alan
Hello.
Select Case statement works perfectly but I have some doubts. Inside the select case I did costone, costwo but is it necessary? using just "cost" the results get summed automatically?
Finally, with the Select Case Statement I have been having some problems using the Count method, I used it as:
Dim segment
Select Case [owner]
Case "FERRONorth"
segment = [OBJECTID].Count
The network dataset is built whitout errors but throws the warning: "Network object evaluator error".
In this case, to count the number of segments by [owner] Field is the Count method the right way to do?
Thank you so much, I was working in other things but I kept on mind this problem.
Hi @EduardoDiaz,
A network cost attribute can only return one cost value per edge segment, and this cost value gets summed up across all edge segments in the route. If you want to track multiple cost values, then you need to set up multiple cost attributes where each cost attribute tracks its own cost value on the segment (e.g., cost for each Owner, number of segments for each Owner, etc.).
Also, the .Count method in VB/VBA is used to get the number of items in an array or other structure. The [OBJECTID] field returns an integer, so getting the .Count for an integer doesn't make sense and causes an the error you see.
Alan