Dear all,
I have weights associated with all edges of the graph. While in the attached case, all the weights are non-zero. In another case, some of the weights might be zero. For example, when trying to denote traffic flow on a road. I want to export a shapefile that contains the weights of the edges, so I can draw the map in a GIS application in line widths proportional to weights or something to denote traffic flow, or a colorbar.
I tried using the "save" statement as below:
save shpfile to:"../results/road_network_weighted.shp" type: "shp" with:[weight:: shortest_path.weight];
But I only get one column in the shapefile, called geometry. I discovered this, when I read the file in R. When I imported this in QGIS, I see nothing in the attribute table.
If a shapefile cannot be saved with weights, is there any other way, I can export the weights, and then overlay it on the map.
Complete code:
/***
* Name: pathlength
* Author: chintan
* Description:
* Tags: Tag1, Tag2, TagN
***/
model pathlength
/* Insert your model definition here */
global {
graph road_network;
graph road_network_weighted;
file roads_shapefile <- file("../includes/SpatialJoin/WA_roads.shp");
file od_csvfile <- csv_file("../includes/zipcodes_OD_WA.csv", ",");
float step <- 1 #mn; // Time step of 1 minute for the simulation
path shortest_path;
geometry shape <- envelope(roads_shapefile);
matrix od_pairs <- matrix(od_csvfile);
int od_count <- od_pairs.rows;
point source;
point target;
point source_temp1;
point source_temp2;
list<road> roadsList;
init {
// Create road from roads_shapefile - it contains ID, and maxspped for each segment
create road from: roads_shapefile
with: [maxspeed :: float(read('Spd')) #miles / #hour,
road_ID :: int(read('ID'))] ;
road_network <- as_edge_graph(road);
// create a list of roads - this is needed for finding the current road etc.
roadsList <- (road as list);
// Add time to travel as weights to the map
map<road, float> map_weights <- road as_map (each::each.shape.perimeter / each.maxspeed);
road_network_weighted <- copy(road_network) with_weights map_weights;
loop i from: 500 to: 500 {
source <- point(road_network.vertices closest_to (point(to_GAMA_CRS({float(od_pairs[3, i]), float(od_pairs[2, i])}, "EPSG:4326"))));
target <- point(road_network.vertices closest_to (point(to_GAMA_CRS({float(od_pairs[6, i]), float(od_pairs[5, i])}, "EPSG:4326"))));
}
shortest_path <- path_between(road_network_weighted, source, target);
write "Time between source and target:" + shortest_path.weight + " seconds";
write(shortest_path);
create shpfile from: [geometry(road_network_weighted)];
save shpfile to:"../results/road_network_weighted.shp" type: "shp" with:[weight:: shortest_path.weight];
create vehicle with: [
shape:: source,
the_target:: target
];
// save vehicle to:"../results/vehicle.shp" type:"shp"; //with: [location: the_target];
}
}
species vehicle skills: [moving] {
point the_target;
float veh_speed <- 1.0;
road currentRoad;
reflex move {
currentRoad <- (roadsList select (each != currentRoad)) with_min_of (each distance_to self);
// Find the speed of traffic on this road
ask currentRoad {
myself.veh_speed <- self.maxspeed; // Use maxpseed until the speed of traffic is found
write("Vehicle speed: " + myself.veh_speed + " m/s");
}
// Goto the target
path path_travelled <- goto(target: the_target, on: road_network, speed: veh_speed, return_path: true);
write(location);
}
aspect default {
draw circle(1000) color: #magenta;
}
}
species shpfile{
float weight;
aspect k{
draw shape+50 color:#orange;
}
}
species road {
geometry display_shape <- line(shape.points, 2.0);
float maxspeed;
int road_ID;
aspect default {
draw shape color: #black;
}
}
experiment goto_network type: gui {
output {
display objects_display type: opengl {
species road aspect: default refresh: false;
species vehicle aspect: default;
graphics points_display {
draw circle(1000) color: #red at: source;
draw circle(1000) color: #blue at: target;
draw geometry(shortest_path.segments) color: #green width: 4;
}
}
}
}
Also, shared here:
https://drive.google.com/file/d/11I_9qPEQn4upQ0se9QX75zAMtsl7YGGS/view?usp=sharing
Thanks