Example #1
0
        public Object parseXMLObject(XMLObject xo) throws XMLParseException {

          LinkedList<Point2D> Point2Ds = new LinkedList<Point2D>();
          boolean closed;
          Polygon2D polygon;

          if (xo.getChild(Polygon2D.class) != null) { // This is a regular polygon

            polygon = (Polygon2D) xo.getChild(Polygon2D.class);

          } else { // This is an arbitrary polygon

            KMLCoordinates coordinates = (KMLCoordinates) xo.getChild(KMLCoordinates.class);
            closed = xo.getAttribute(CLOSED, false);

            if ((!closed && coordinates.length < 3) || (closed && coordinates.length < 4))
              throw new XMLParseException(
                  "Insufficient point2Ds in polygon '"
                      + xo.getId()
                      + "' to define a polygon in 2D");

            for (int i = 0; i < coordinates.length; i++)
              Point2Ds.add(new Point2D.Double(coordinates.x[i], coordinates.y[i]));

            polygon = new Polygon2D(Point2Ds, closed);
          }

          polygon.setFillValue(xo.getAttribute(FILL_VALUE, 0.0));

          return polygon;
        }
Example #2
0
  private static LinkedList<Point2D> getCirclePoints(
      double centerLat, double centerLong, int numberOfPoints, double radius) {

    LinkedList<Point2D> Point2Ds = new LinkedList<Point2D>();

    double lat1, long1;
    double d_rad;
    double delta_pts;
    double radial, lat_rad, dlon_rad, lon_rad;

    // convert coordinates to radians
    lat1 = Math.toRadians(centerLat);
    long1 = Math.toRadians(centerLong);

    // radius is in meters
    d_rad = radius / 6378137;

    // loop through the array and write points
    for (int i = 0; i <= numberOfPoints; i++) {
      delta_pts = 360 / (double) numberOfPoints;
      radial = Math.toRadians((double) i * delta_pts);

      // This algorithm is limited to distances such that dlon < pi/2
      lat_rad =
          Math.asin(
              Math.sin(lat1) * Math.cos(d_rad)
                  + Math.cos(lat1) * Math.sin(d_rad) * Math.cos(radial));
      dlon_rad =
          Math.atan2(
              Math.sin(radial) * Math.sin(d_rad) * Math.cos(lat1),
              Math.cos(d_rad) - Math.sin(lat1) * Math.sin(lat_rad));
      lon_rad = ((long1 + dlon_rad + Math.PI) % (2 * Math.PI)) - Math.PI;

      Point2Ds.add(new Point2D.Double(Math.toDegrees(lat_rad), Math.toDegrees(lon_rad)));
    }
    return Point2Ds;
  }
Example #3
0
  public Polygon2D clip(Rectangle2D boundingBox) {

    LinkedList<Point2D> clippedPolygon = new LinkedList<Point2D>();

    Point2D p; // current Point2D
    Point2D p2; // next Point2D

    // make copy of original polygon to work with
    LinkedList<Point2D> workPoly = new LinkedList<Point2D>(point2Ds);

    // loop through all for clipping edges
    for (Side side : Side.values()) {
      clippedPolygon.clear();
      for (int i = 0; i < workPoly.size() - 1; i++) {
        p = workPoly.get(i);
        p2 = workPoly.get(i + 1);
        if (isInsideClip(p, side, boundingBox)) {
          if (isInsideClip(p2, side, boundingBox))
            // here both point2Ds are inside the clipping window so add the second one
            clippedPolygon.add(p2);
          else
            // the seond Point2D is outside so add the intersection Point2D
            clippedPolygon.add(intersectionPoint2D(side, p, p2, boundingBox));
        } else {
          // so first Point2D is outside the window here
          if (isInsideClip(p2, side, boundingBox)) {
            // the following Point2D is inside so add the insection Point2D and also p2
            clippedPolygon.add(intersectionPoint2D(side, p, p2, boundingBox));
            clippedPolygon.add(p2);
          }
        }
      }
      // make sure that first and last element are the same, we want a closed polygon
      if (!clippedPolygon.getFirst().equals(clippedPolygon.getLast()))
        clippedPolygon.add(clippedPolygon.getFirst());
      // we have to keep on working with our new clipped polygon
      workPoly = new LinkedList<Point2D>(clippedPolygon);
    }
    return new Polygon2D(clippedPolygon, true);
  }