예제 #1
0
  /**
   * Creates a {@link Geometry} with the same extent as the given envelope. The Geometry returned is
   * guaranteed to be valid. To provide this behaviour, the following cases occur:
   *
   * <p>If the <code>Envelope</code> is:
   *
   * <ul>
   *   <li>null : returns an empty {@link Point}
   *   <li>a point : returns a non-empty {@link Point}
   *   <li>a line : returns a two-point {@link LineString}
   *   <li>a rectangle : returns a {@link Polygon} whose points are (minx, miny), (minx, maxy),
   *       (maxx, maxy), (maxx, miny), (minx, miny).
   * </ul>
   *
   * @param envelope the <code>Envelope</code> to convert
   * @return an empty <code>Point</code> (for null <code>Envelope</code>s), a <code>Point</code>
   *     (when min x = max x and min y = max y) or a <code>Polygon</code> (in all other cases)
   */
  public Geometry toGeometry(Envelope envelope) {
    // null envelope - return empty point geometry
    if (envelope.isNull()) {
      return createPoint((CoordinateSequence) null);
    }

    // point?
    if (envelope.getMinX() == envelope.getMaxX() && envelope.getMinY() == envelope.getMaxY()) {
      return createPoint(new Coordinate(envelope.getMinX(), envelope.getMinY()));
    }

    // vertical or horizontal line?
    if (envelope.getMinX() == envelope.getMaxX() || envelope.getMinY() == envelope.getMaxY()) {
      return createLineString(
          new Coordinate[] {
            new Coordinate(envelope.getMinX(), envelope.getMinY()),
            new Coordinate(envelope.getMaxX(), envelope.getMaxY())
          });
    }

    // create a CW ring for the polygon
    return createPolygon(
        createLinearRing(
            new Coordinate[] {
              new Coordinate(envelope.getMinX(), envelope.getMinY()),
              new Coordinate(envelope.getMinX(), envelope.getMaxY()),
              new Coordinate(envelope.getMaxX(), envelope.getMaxY()),
              new Coordinate(envelope.getMaxX(), envelope.getMinY()),
              new Coordinate(envelope.getMinX(), envelope.getMinY())
            }),
        null);
  }
예제 #2
0
  /**
   * Fits screen to specified geometry bounds.
   *
   * @param aArea A geometry in geo coordinates space.
   * @throws Exception
   */
  public void fit(Geometry aArea) throws Exception {

    Geometry bounds = aArea.getBoundary();
    Envelope envBounds = bounds.getEnvelopeInternal();
    Point2D.Double leftUpCorner = new Point2D.Double(envBounds.getMinX(), envBounds.getMinY());
    Point2D.Double rightBottomCorner = new Point2D.Double(envBounds.getMaxX(), envBounds.getMaxY());
    Point2D.Double cartlu = geo2Cartesian(leftUpCorner);
    Point2D.Double cartrb = geo2Cartesian(rightBottomCorner);
    double destWidth = Math.abs(cartrb.getX() - cartlu.getX());
    double destHeight = Math.abs(cartrb.getY() - cartlu.getY());
    Coordinate centre =
        new Coordinate((cartrb.getX() + cartlu.getX()) / 2, (cartrb.getY() + cartlu.getY()) / 2);

    Dimension size = getSize();
    Point2D.Double screenLT = awtScreen2Cartesian(new Point(0, 0));
    Point2D.Double screenBR = awtScreen2Cartesian(new Point(size.width, size.height));

    double srcWidth = screenBR.x - screenLT.x;
    double srcHeight = screenBR.y - screenLT.y;
    double sx = srcWidth / destWidth;
    double sy = srcHeight / destHeight;
    double coef = Math.min(sx, sy);
    coef = snapScale(coef);
    scaleView(coef, coef, false);

    Point2D.Double projectedScreenCenter = screen2Cartesian(new Point(0, 0));
    translateView(projectedScreenCenter.x - centre.x, projectedScreenCenter.y - centre.y, true);
    repaint();
  }
 public MCIndexedPointInAreaLocator(Geometry g) {
   areaGeom = g;
   if (!(g instanceof Polygonal)) throw new IllegalArgumentException("Argument must be Polygonal");
   buildIndex(g);
   Envelope env = g.getEnvelopeInternal();
   maxXExtent = env.getMaxX() + 1.0;
 }
예제 #4
0
 private boolean addChild(Node parent, RelationshipType type, Node newChild) {
   Envelope childEnvelope = getChildNodeEnvelope(newChild, type);
   double[] childBBox =
       new double[] {
         childEnvelope.getMinX(), childEnvelope.getMinY(),
         childEnvelope.getMaxX(), childEnvelope.getMaxY()
       };
   parent.createRelationshipTo(newChild, type);
   return expandParentBoundingBoxAfterNewChild(parent, childBBox);
 }
예제 #5
0
  /**
   * Computes the distance between this and another <code>Envelope</code>. The distance between
   * overlapping Envelopes is 0. Otherwise, the distance is the Euclidean distance between the
   * closest points.
   */
  @Override
  public double distance(final Envelope env) {
    if (intersects(env)) {
      return 0;
    }

    double dx = 0.0;
    if (getMaxX() < env.getMinX()) {
      dx = env.getMinX() - getMaxX();
    } else if (getMinX() > env.getMaxX()) {
      dx = getMinX() - env.getMaxX();
    }

    double dy = 0.0;
    if (getMaxY() < env.getMinY()) {
      dy = env.getMinY() - getMaxY();
    } else if (getMinY() > env.getMaxY()) {
      dy = getMinY() - env.getMaxY();
    }

    double dz = 0.0;
    double otherMinZ = getMinZOf(env);
    double otherMaxZ = getMaxZOf(env);
    if (maxz < otherMinZ) {
      dz = otherMinZ - maxz;
    } else if (minz > otherMaxZ) {
      dz = minz - otherMaxZ;
    }

    // if either is zero, the envelopes overlap either vertically or
    // horizontally
    if (dx == 0.0 && dz == 0.0) {
      return dy;
    }
    if (dy == 0.0 && dz == 0.0) {
      return dx;
    }
    if (dx == 0.0 && dy == 0.0) {
      return dz;
    }
    return Math.sqrt(dx * dx + dy * dy + dz * dz);
  }
  /**
   * Creates a rectangular {@link Polygon}.
   *
   * @return a rectangular Polygon
   */
  public Polygon createRectangle() {
    int i;
    int ipt = 0;
    int nSide = nPts / 4;
    if (nSide < 1) nSide = 1;
    double XsegLen = dim.getEnvelope().getWidth() / nSide;
    double YsegLen = dim.getEnvelope().getHeight() / nSide;

    Coordinate[] pts = new Coordinate[4 * nSide + 1];
    Envelope env = dim.getEnvelope();

    double maxx = env.getMinX() + nSide * XsegLen;
    double maxy = env.getMinY() + nSide * XsegLen;

    for (i = 0; i < nSide; i++) {
      double x = env.getMinX() + i * XsegLen;
      double y = env.getMinY();
      pts[ipt++] = new Coordinate(x, y);
    }
    for (i = 0; i < nSide; i++) {
      double x = env.getMaxX();
      double y = env.getMinY() + i * YsegLen;
      pts[ipt++] = new Coordinate(x, y);
    }
    for (i = 0; i < nSide; i++) {
      double x = env.getMaxX() - i * XsegLen;
      double y = env.getMaxY();
      pts[ipt++] = new Coordinate(x, y);
    }
    for (i = 0; i < nSide; i++) {
      double x = env.getMinX();
      double y = env.getMaxY() - i * YsegLen;
      pts[ipt++] = new Coordinate(x, y);
    }
    pts[ipt++] = new Coordinate(pts[0]);

    LinearRing ring = geomFact.createLinearRing(pts);
    Polygon poly = geomFact.createPolygon(ring, null);
    return poly;
  }
예제 #7
0
 /**
  * Computes the intersection of two {@link Envelope}s.
  *
  * @param env the envelope to intersect with
  * @return a new Envelope representing the intersection of the envelopes (this will be the null
  *     envelope if either argument is null, or they do not intersect
  */
 @Override
 public Envelope3D intersection(final Envelope env) {
   if (isNull() || env.isNull() || !intersects(env)) {
     return new Envelope3D();
   }
   Envelope xyInt = super.intersection(env);
   double otherMinZ = getMinZOf(env);
   double intMinZ = minz > otherMinZ ? minz : otherMinZ;
   double otherMaxZ = getMaxZOf(env);
   double intMaxZ = maxz < otherMaxZ ? maxz : otherMaxZ;
   return new Envelope3D(
       xyInt.getMinX(), xyInt.getMaxX(), xyInt.getMinY(), xyInt.getMaxY(), intMinZ, intMaxZ);
 }
예제 #8
0
  /**
   * Fix an IndexNode bounding box after a child has been removed
   *
   * @param indexNode
   * @return true if something has changed
   */
  private boolean adjustParentBoundingBox(Node indexNode, RelationshipType relationshipType) {
    double[] old = null;
    if (indexNode.hasProperty(INDEX_PROP_BBOX)) {
      old = (double[]) indexNode.getProperty(INDEX_PROP_BBOX);
    }

    Envelope bbox = null;

    Iterator<Relationship> iterator =
        indexNode.getRelationships(relationshipType, Direction.OUTGOING).iterator();
    while (iterator.hasNext()) {
      Node childNode = iterator.next().getEndNode();

      if (bbox == null) {
        bbox = new Envelope(getChildNodeEnvelope(childNode, relationshipType));
      } else {
        bbox.expandToInclude(getChildNodeEnvelope(childNode, relationshipType));
      }
    }

    if (bbox == null) {
      // this could happen in an empty tree
      bbox = new Envelope(0, 0, 0, 0);
    }

    if (old.length != 4
        || bbox.getMinX() != old[0]
        || bbox.getMinY() != old[1]
        || bbox.getMaxX() != old[2]
        || bbox.getMaxY() != old[3]) {
      indexNode.setProperty(
          INDEX_PROP_BBOX,
          new double[] {bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY()});
      return true;
    } else {
      return false;
    }
  }
예제 #9
0
 /**
  * Computes the list of envelopes (from 2 to 4, possibily 0 if env covers this) resulting from the
  * extrusion of env from this. Only in 2D for the moment. Does not return null envelopes.
  */
 public List<Envelope> extrusion(final Envelope env) {
   List<Envelope> list = new ArrayList();
   double x1 = getMinX();
   double x2 = getMaxX();
   double y1 = getMinY();
   double y2 = getMaxY();
   double xx1 = env.getMinX();
   double xx2 = env.getMaxX();
   double yy1 = env.getMinY();
   double yy2 = env.getMaxY();
   if (x2 >= x1 && yy1 >= y1) {
     list.add(new Envelope(x1, x2, y1, yy1));
   }
   if (xx1 >= x1 && y2 >= yy1) {
     list.add(new Envelope(x1, xx1, yy1, y2));
   }
   if (x2 >= xx1 && y2 >= yy2) {
     list.add(new Envelope(xx1, x2, yy2, y2));
   }
   if (x2 >= xx2 && yy2 >= yy1) {
     list.add(new Envelope(xx2, x2, yy1, yy2));
   }
   return list;
 }