Ejemplo n.º 1
0
  @Test
  public void test() throws IOException {
    GeoJSONWriter writer = new GeoJSONWriter();

    Point point = new GeometryFactory().createPoint(new Coordinate(1, 1));
    GeoJSON json = writer.write(point);
    System.out.println(json);

    GeoJSONReader reader = new GeoJSONReader();
    Geometry geometry = reader.read(json);
    System.out.println(geometry);

    LineString lineString =
        new GeometryFactory()
            .createLineString(
                new Coordinate[] {
                  new Coordinate(1, 1),
                  new Coordinate(1, 2),
                  new Coordinate(2, 2),
                  new Coordinate(1, 1)
                });
    json = writer.write(lineString);
    System.out.println(json);

    Polygon polygon = new GeometryFactory().createPolygon(lineString.getCoordinates());
    json = writer.write(polygon);
    System.out.println(json);

    MultiPoint multiPoint = new GeometryFactory().createMultiPoint(lineString.getCoordinates());
    json = writer.write(multiPoint);
    System.out.println(json);

    MultiLineString multiLineString =
        new GeometryFactory().createMultiLineString(new LineString[] {lineString, lineString});
    json = writer.write(multiLineString);
    System.out.println(json);

    MultiPolygon multiPolygon =
        new GeometryFactory().createMultiPolygon(new Polygon[] {polygon, polygon});
    json = writer.write(multiPolygon);
    System.out.println(json);

    geometry = reader.read(json);
    System.out.println(geometry);
  }
Ejemplo n.º 2
0
  public static void write(
      Network network, LinkOccupancyCalculator linkVols, double factor, String file) {
    Map<Link, Double> volumes = new HashMap<>(network.getLinks().size());
    Map<Link, Double> loads = new HashMap<>(network.getLinks().size());

    double maxVolume = 0;
    double minVolume = Double.MAX_VALUE;

    for (Link link : network.getLinks().values()) {
      String linkId = link.getId().toString();
      if (!(linkId.startsWith(ODCalibrator.VIRTUAL_ID_PREFIX) || linkId.contains(".l"))) {
        double vol = linkVols.getOccupancy(link.getId()) * factor;
        double load = vol / (link.getCapacity() * 24);
        volumes.put(link, vol);
        loads.put(link, load);

        maxVolume = Math.max(maxVolume, vol);
        minVolume = Math.min(minVolume, vol);
      }
    }

    MathTransform transform = null;
    try {
      transform = CRS.findMathTransform(CRSUtils.getCRS(31467), CRSUtils.getCRS(4326));
    } catch (FactoryException e) {
      e.printStackTrace();
    }

    GeoJSONWriter jsonWriter = new GeoJSONWriter();
    List<Feature> features = new ArrayList<>(network.getLinks().size());

    for (Link link : volumes.keySet()) {
      double coord1[] =
          new double[] {link.getFromNode().getCoord().getX(), link.getFromNode().getCoord().getY()};
      double coord2[] =
          new double[] {link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY()};

      try {
        transform.transform(coord1, 0, coord1, 0, 1);
        transform.transform(coord2, 0, coord2, 0, 1);
      } catch (TransformException e) {
        e.printStackTrace();
      }

      double coords[][] = new double[2][2];
      coords[0][0] = coord1[0];
      coords[0][1] = coord1[1];
      coords[1][0] = coord2[0];
      coords[1][1] = coord2[1];

      LineString lineString = new LineString(coords);

      Map<String, Object> properties = new HashMap<>();
      double volume = volumes.get(link);
      double load = loads.get(link);
      properties.put("volume", volume);
      properties.put("load", load);
      properties.put("capacity", link.getCapacity());
      double width = (volume - minVolume) / (maxVolume - minVolume);
      properties.put("width", width);
      Color color = ColorUtils.getRedGreenColor(load);
      properties.put("color", "#" + String.format("%06x", color.getRGB() & 0x00FFFFFF));

      Feature feature = new Feature(lineString, properties);
      features.add(feature);
    }

    try {
      FeatureCollection fCollection = jsonWriter.write(features);
      BufferedWriter writer = new BufferedWriter(new FileWriter(file));
      writer.write(fCollection.toString());
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }