使用Java的流行Repast zombie教程采用了NetworkBuilder,可以让您可视化感染网络。我想知道是否有类似的东西可用于地理/ GIS背景。或者,任何关于我如何构建自己的想法?例如,如果我的代理从A点移动到B点,我是否可以编写一个函数来绘制连接两者的向量?
答案 0 :(得分:2)
我写了一个投影监听器类,它将管理地理投影中的Repast网络对象,这些对象可以在GIS显示中显示。附加的侦听器类可以放在您的项目中,并在ContextBuilder中用于链接地理和网络投影,如下所示:
GeographyParameters geoParams = new GeographyParameters();
Geography geography = GeographyFactoryFinder.createGeographyFactory(null).createGeography("Geography", context, geoParams);
NetworkBuilder<Object> netBuilder = new NetworkBuilder<Object>("Network", context, true);
Network net = netBuilder.buildNetwork();
GISNetworkListener netListener = new GISNetworkListener(context, geography, net);
GIS NetworkListener在指定的上下文中绑定地理和网络,以便一个中的更改将反映在另一个中。这可以处理代理移动,添加和从地理位置移除以及网络添加/删除边缘事件。如果您有多个网络,则可以为每个网络创建单独的侦听器实例。只要在您的代码中正常使用Repast网络和地理预测,就不需要额外的代码来更新预测之间的事件。
要显示GIS显示中的网络边缘,您需要创建一个扩展RepastEdge的简单边缘类,例如附加的MyNetworkEdge类。这只是因为显示向导需要用户类来进行样式设置。在显示中,您可以在代理类型对话框中选择MyNetworkEdge类,并指定行类型或样式类,如附加的MyNetworkStyle类。要在Repast网络中创建链接,您需要使用MyNetworkEdge的添加和删除边缘方法,例如
net.addEdge(new MyNetworkEdge(source, target));
您当然可以根据需要为MyNetworkEdge类添加更多功能。
ProjectionListener类:
package geography;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import repast.simphony.context.Context;
import repast.simphony.space.gis.Geography;
import repast.simphony.space.graph.Network;
import repast.simphony.space.graph.RepastEdge;
import repast.simphony.space.projection.ProjectionEvent;
import repast.simphony.space.projection.ProjectionListener;
/**
* A ProjectionListener implementation for managing Repast network edges in a
* Repast geography projection. This listener responds to both geography event
* and network events.
*
* @author Eric Tatara
*
*/
public class GISNetworkListener implements ProjectionListener {
Context context;
Network network;
Geography geography;
GeometryFactory fac = new GeometryFactory();
public GISNetworkListener(Context c, Geography g, Network n) {
context = c;
network = n;
geography = g;
network.addProjectionListener(this);
geography.addProjectionListener(this);
}
@Override
public void projectionEventOccurred(ProjectionEvent evt) {
// When an object is moved in the geography, its network edges positions
// should be updated if the object has edges.
if (evt.getType() == ProjectionEvent.OBJECT_MOVED){
Iterable<RepastEdge> edges = network.getEdges(evt.getSubject());
if (edges != null){
for (RepastEdge e : edges){
// Get the existing geometry for this edge
MultiLineString lineFeature = (MultiLineString)geography.getGeometry(e);
Coordinate sourceCoord = geography.getGeometry(e.getSource()).getCoordinate();
Coordinate targetCoord = geography.getGeometry(e.getTarget()).getCoordinate();
Coordinate coords[] = lineFeature.getCoordinates();
// Update the edge coordinates based on the source and target object
// (agent) coordinates.
coords[0].setCoordinate(sourceCoord);
coords[1].setCoordinate(targetCoord);
}
}
}
// When a Repast network edge is added, create a new MultiLineString geometry
// to represent the edge in the geography.
else if (evt.getType() == ProjectionEvent.EDGE_ADDED){
RepastEdge e = (RepastEdge)evt.getSubject();
Coordinate sourceCoord = geography.getGeometry(e.getSource()).getCoordinate();
Coordinate targetCoord = geography.getGeometry(e.getTarget()).getCoordinate();
LineString lineString = fac.createLineString(new Coordinate[]{sourceCoord,
targetCoord});
MultiLineString mls = fac.createMultiLineString(new LineString[]{lineString});
context.add(e);
geography.move(e, mls);
}
// When a Repast edge remove event occurs, remove the edge geometry from the
// geography and the context. This should also occur automatically when agents
// are removed from a context or network.
else if (evt.getType() == ProjectionEvent.EDGE_REMOVED){
RepastEdge e = (RepastEdge)evt.getSubject();
geography.move(e, null);
context.remove(e);
}
}
}
RepastEdge类:
package geography;
import repast.simphony.space.graph.RepastEdge;
public class MyNetworkEdge extends RepastEdge {
public MyNetworkEdge(Object source, Object target){
super(source, target, false);
}
}
网络样式类:
package geography;
import gov.nasa.worldwind.render.SurfacePolyline;
import gov.nasa.worldwind.render.SurfaceShape;
import java.awt.Color;
import repast.simphony.visualization.gis3D.style.SurfaceShapeStyle;
/**
* Style for MyNetworkEdges.
*
* @author Eric Tatara
*
*/
public class MyNetworkStyle implements SurfaceShapeStyle<MyNetworkEdge>{
@Override
public SurfaceShape getSurfaceShape(MyNetworkEdge object, SurfaceShape shape) {
return new SurfacePolyline();
}
@Override
public Color getFillColor(MyNetworkEdge obj) {
return null;
}
@Override
public double getFillOpacity(MyNetworkEdge obj) {
return 0;
}
@Override
public Color getLineColor(MyNetworkEdge obj) {
return Color.BLUE;
}
@Override
public double getLineOpacity(MyNetworkEdge obj) {
return 1.0;
}
@Override
public double getLineWidth(MyNetworkEdge obj) {
return 2;
}
}