If needed, you can create a GIS network programmatically.
In this article we look at the code example of the following model:
Demo model: Create GIS Network by Code Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).We create a GIS network that consists of two GIS points, which are connected by a GIS route. The GIS route consists of two segments. In the demo model, the train enters the GIS network at one GIS point and moves along the GIS route to another GIS point.
To create a GIS network programmatically, we enter the following Java code in the createRoute() function, which is called from the On startup action of the Main agent (Agent actions properties section):
//Create and initialize GIS points
GISPoint gp1 = new GISPoint(map, 60.19, 24.94);
GISPoint gp2 = new GISPoint(map, 59.92, 25);
GISPoint gp3 = new GISPoint(map, 59.43, 24.95);
gp1.initialize();
gp2.initialize();
gp3.initialize();
//Create GIS route segments
GISMarkupSegmentLine segment1 = new GISMarkupSegmentLine(gp1.getLatitude(), gp1.getLongitude(), gp2.getLatitude(), gp2.getLongitude());
GISMarkupSegmentLine segment2 = new GISMarkupSegmentLine(gp2.getLatitude(), gp2.getLongitude(), gp3.getLatitude(), gp3.getLongitude());
//Create and initialize a curve
Curve curve = new Curve();
curve.addSegment(segment1);
curve.addSegment(segment2);
curve.initialize();
//Create a GIS route
GISRoute gr = new GISRoute(map, curve, gp1, gp3, true);
gr.initialize();
//Create a GIS network
GISNetwork n = new GISNetwork(map, "n", true, gp1, gp2, gp3, gr);
First, we create three GIS points. The GISPoint constructor has three arguments: the name of the GIS map, where the GIS points will be added (in our model it is map), followed by the GIS point's latitude and longitude coordinates.
GISPoint gp1 = new GISPoint(map, 60.19, 24.94);
GISPoint gp2 = new GISPoint(map, 59.92, 25);
GISPoint gp3 = new GISPoint(map, 59.43, 24.95);
Then, the created elements should be initialized via their initialize() function.
gp2.initialize();
gp1.initialize();
gp3.initialize();
Next, we create a GIS route.
We start with creating GIS route segments, then we create a curve, and add these segments to the curve. Finally, we create a GIS route, passing the curve as one of the arguments into the route's constructor.
Our GIS route consists of two segments. Each route segment is created via the GISMarkupSegmentLine constructor. This constructor accepts four arguments: latitude and longitude of the start point of the segment, followed by the segment's end point's latitude and longitude.
GISMarkupSegmentLine segment1 = new GISMarkupSegmentLine(gp1.getLatitude(), gp1.getLongitude(), gp2.getLatitude(), gp2.getLongitude());
GISMarkupSegmentLine segment2 = new GISMarkupSegmentLine(gp2.getLatitude(), gp2.getLongitude(), gp3.getLatitude(), gp3.getLongitude());
The constructors that don’t use this argument construct segments as geodesic lines (that is, isGeodesic == true.)
Then, we create a curve, add the required GIS route segments to the curve in the order of their placement from the starting point of the GIS route. Finally, we initialize the curve.
Curve curve = new Curve();
curve.addSegment(segment1);
curve.addSegment(segment2);
curve.initialize();
Now we create the GIS route itself and initialize it. The GISRoute class constructor has five arguments: the GIS map, the created curve, the start GIS point and the end GIS point of the route, and the boolean argument, which defines whether the route is bidirectional (we allow movement in both directions by passing true value).
//Create a GIS route
GISRoute gr = new GISRoute(map, curve, gp1, gp3, true);
gr.initialize();
We now have all the elements that are needed to create our GIS network. In the GISNetwork constructor, we provide the reference to the GIS map, pass the network name, make the network visible by passing true value as its next argument (visible), and then list all the space markup elements that construct this GIS network:
GISNetwork n = new GISNetwork(map, "n", true, gp1, gp2, gp3, gr);
-
How can we improve this article?
-