The MapViewer Federate : Part 3

Continuing the previous post I’ll show you how to get the units discovered by RTI and display them in the web interface.

The web interface of our MapViewer is made using  OpenLayers   It connects to OpenStreetMap and shows the informations on screen by using “layers”.

The first layer is the background map comming from OpenStreetMap. We have one more layer: the units of the Federation that inherit from BasicUnit. They can be Tanks , Aircraft and any other that inherit from BasicUnit in a SOM.

There is a javascript code that polls the server to ask for units using Ajax, but is not in the scope of this post show it. All you need to know is we must respond using a GeoJSON string like this.

{"type":"FeatureCollection","features":[
  {"type":"Feature",
    "properties":{
        "name":"UNIT_A",
        "serial":"F4EC3",
        "position":"-54.34,23.10",
        "bearing":0,
        "color":"green",
        "size":15,
        "pin_image":"img/pins/aircraft.png"
    },
    "geometry":{
        "type":"Point",
        "coordinates":[-54.34,23.10]
    }
  },
  {"type":"Feature",
    "properties":{
        "name":"UNIT_B",
        "serial":"D54FE",
        "position":"-52.51,23.15",
        "bearing":0,
        "color":"green",
        "size":15,
        "pin_image":"img/pins/tank.png"
    },
    "geometry":{
        "type":"Point",
        "coordinates":[-52.51,23.15]
    }
  }
]}

There is a FeatureCollection and many Features, representing units. The only information that is flexible in this structure is “properties” . Here you can send any property you want to display on the screen, but how to do it you must discover by your own.

To make this work I created a Struts Action that consults the list of units and generates GeoJSON.

@Action(value="getUnits", results= {  
      @Result(name="ok", type="httpheader", params={"status", "200"}) } 
)   


@ParentPackage("default")
public class GetUnitsAction  {
  
  public String execute(){
    
    try { 
      String jsonResponse = UnitListProvider.getInstance().asJson();
      
      HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
      response.setCharacterEncoding("UTF-8"); 
      response.getWriter().write(jsonResponse);  
    } catch (IOException ex) {  
      System.out.println("[GetUnitsAction] Error."); 
    }
    
    return "ok";
  }
     

}

The FeatureCollection is generated by the Helper class UnitListProvider:

public class UnitListProvider {
  private static UnitListProvider instance;
  
  private String jsonBasic = "{\"type\":\"FeatureCollection\",\"features\":[#FEATURES#]}";
  
  public String asJson() {
    StringBuilder sb = new StringBuilder();
    String prefix = "";
    for( IUnit unit : MapViewFederate.getInstance().getUnitClass().getInstances() ) {
      String value = unit.getFeature();
      sb.append( prefix + value );
      prefix = ",";
    }
    return jsonBasic.replace("#FEATURES#", sb.toString() );
  }
  
  public static UnitListProvider getInstance() {
    if ( instance == null ) {
      instance = new UnitListProvider();
    }
    return instance;
  }

}

Each Unit should be responsible for generate its own GeoJSON Feature data:

public class BaseUnit implements IUnit {
  private String name;
  private String serial;
  private double longitude;
  private double latitude;
  private String imageName = "army";
  private int unitType = UNKNOWN;
  private String featureBasic = "{\"type\":\"Feature\",\"properties\":{\"name\":\"#NAME#\",\"serial\":\"#SERIAL#\","
      + "\"position\":\"#COORDINATES#\","
      + "\"bearing\":0,\"color\":\"green\",\"size\":15,\"pin_image\":\"img/pins/#IMG_NAME#.png\"},\"geometry\":{"
      + "\"type\":\"Point\",\"coordinates\":[#COORDINATES#]}}";

  
  @Override
  public String getFeature() {
    return featureBasic.replace("#IMG_NAME#", getImageName() ).replace("#NAME#", getName())
      .replace("#SERIAL#", getSerial() ).replace("#COORDINATES#", getCoordinates() );
  }

  // Getters and Setters ...
}

… and this BaseUnit  could be your already known BasicUnitObject to store Tanks and Aircraft discovered.

In the next post I’ll talk a little about the Tank and Aircraft Federates.

Add Comment