예제 #1
0
  /**
   * Transforms the referenced envelope to the specified coordinate reference system using the
   * specified amount of points.
   *
   * <p>This method can handle the case where the envelope contains the North or South pole, or when
   * it cross the &plusmn;180� longitude.
   *
   * @param targetCRS The target coordinate reference system.
   * @param lenient {@code true} if datum shift should be applied even if there is insuffisient
   *     information. Otherwise (if {@code false}), an exception is thrown in such case.
   * @param numPointsForTransformation The number of points to use for sampling the envelope.
   * @return The transformed envelope.
   * @throws FactoryException if the math transform can't be determined.
   * @throws TransformException if at least one coordinate can't be transformed.
   * @see CRS#transform(CoordinateOperation, org.opengis.geometry.Envelope)
   * @since 2.3
   */
  public ReferencedEnvelope transform(
      final CoordinateReferenceSystem targetCRS,
      final boolean lenient,
      final int numPointsForTransformation)
      throws TransformException, FactoryException {
    if (crs == null) {
      if (isEmpty()) {
        // We don't have a CRS yet because we are still empty, being empty is
        // something we can represent in the targetCRS
        return new ReferencedEnvelope(targetCRS);
      } else {
        // really this is a the code that created this ReferencedEnvelope
        throw new NullPointerException(
            "Unable to transform referenced envelope, crs has not yet been provided.");
      }
    }
    if (getDimension() != targetCRS.getCoordinateSystem().getDimension()) {
      if (lenient) {
        return JTS.transformTo3D(this, targetCRS, lenient, numPointsForTransformation);
      } else {
        throw new MismatchedDimensionException(
            Errors.format(
                ErrorKeys.MISMATCHED_DIMENSION_$3,
                crs.getName().getCode(),
                new Integer(getDimension()),
                new Integer(targetCRS.getCoordinateSystem().getDimension())));
      }
    }
    /*
     * Gets a first estimation using an algorithm capable to take singularity in account
     * (North pole, South pole, 180� longitude). We will expand this initial box later.
     */
    CoordinateOperationFactory coordinateOperationFactory =
        CRS.getCoordinateOperationFactory(lenient);

    final CoordinateOperation operation =
        coordinateOperationFactory.createOperation(crs, targetCRS);
    final GeneralEnvelope transformed = CRS.transform(operation, this);
    transformed.setCoordinateReferenceSystem(targetCRS);

    /*
     * Now expands the box using the usual utility methods.
     */
    final ReferencedEnvelope target = new ReferencedEnvelope(transformed);
    final MathTransform transform = operation.getMathTransform();
    JTS.transform(this, target, transform, numPointsForTransformation);

    return target;
  }
예제 #2
0
  /**
   * Added this test after a bug was reported in JTS.transform for converting between WGS84 (2D) and
   * DefaultGeocentric.CARTESIAN (3D).
   */
  @Test
  public void transformCoordinate2DCRSTo3D() throws Exception {
    CoordinateReferenceSystem srcCRS = DefaultGeographicCRS.WGS84;
    CoordinateReferenceSystem targetCRS = DefaultGeocentricCRS.CARTESIAN;
    MathTransform transform = CRS.findMathTransform(srcCRS, targetCRS);

    Coordinate srcCoord = new Coordinate(0, 0);
    Coordinate dest0 = JTS.transform(srcCoord, null, transform);

    srcCoord.x = 180;
    Coordinate dest180 = JTS.transform(srcCoord, null, transform);

    // Only a perfunctory check on the return values - mostly we
    // just wanted to make sure there was no exception
    assertEquals(dest0.x, -dest180.x, TOL);
    assertEquals(dest0.y, dest180.y, TOL);
    assertEquals(dest0.z, dest180.z, TOL);
  }
예제 #3
0
  @Test
  public void toEnvelope() {
    Coordinate[] coords = getPolyCoords();
    GeometryFactory gf = new GeometryFactory();
    Geometry geom = gf.createPolygon(gf.createLinearRing(coords), null);

    ReferencedEnvelope refEnv = JTS.toEnvelope(geom);
    assertTrue(geom.getEnvelopeInternal().equals(refEnv));
  }
예제 #4
0
  @Test
  public void getEnvelope2D() {
    ReferencedEnvelope refEnv = new ReferencedEnvelope(-10, 10, -5, 5, DefaultGeographicCRS.WGS84);

    Envelope2D env2D = JTS.getEnvelope2D(refEnv, refEnv.getCoordinateReferenceSystem());

    CRS.equalsIgnoreMetadata(
        refEnv.getCoordinateReferenceSystem(), env2D.getCoordinateReferenceSystem());

    assertTrue(env2D.boundsEquals(refEnv, 0, 1, TOL));
  }
예제 #5
0
  @Test
  public void toDirectPosition() {
    Coordinate c = new Coordinate(40, 40);
    DirectPosition wrapper = JTS.toDirectPosition(c, DefaultGeographicCRS.WGS84);

    GeneralDirectPosition expected = new GeneralDirectPosition(DefaultGeographicCRS.WGS84);
    expected.setOrdinate(0, 40);
    expected.setOrdinate(1, 40);

    assertEquals(expected, wrapper);
  }
예제 #6
0
  @Test
  public void toGeometry_BoundingBox() {
    BoundingBox bbox = new ReferencedEnvelope(-10, 10, -5, 5, null);
    Geometry geom = JTS.toGeometry(bbox);
    assertTrue(geom instanceof com.vividsolutions.jts.geom.Polygon);

    Envelope geomEnv = geom.getEnvelopeInternal();
    assertEquals(-10.0, geomEnv.getMinX(), TOL);
    assertEquals(10.0, geomEnv.getMaxX(), TOL);
    assertEquals(-5.0, geomEnv.getMinY(), TOL);
    assertEquals(5.0, geomEnv.getMaxY(), TOL);
  }
예제 #7
0
  @Test
  public void toGeometry_ReferencedEnvelope() {
    ReferencedEnvelope refEnv = new ReferencedEnvelope(-10, 10, -5, 5, DefaultGeographicCRS.WGS84);
    Geometry geom = JTS.toGeometry(refEnv);
    assertTrue(geom instanceof com.vividsolutions.jts.geom.Polygon);

    Envelope geomEnv = geom.getEnvelopeInternal();
    assertEquals(-10.0, geomEnv.getMinX(), TOL);
    assertEquals(10.0, geomEnv.getMaxX(), TOL);
    assertEquals(-5.0, geomEnv.getMinY(), TOL);
    assertEquals(5.0, geomEnv.getMaxY(), TOL);
  }
예제 #8
0
  @Test
  public void toGeometry_Envelope() {
    Envelope refEnv = new Envelope(-10, 10, -5, 5);
    Geometry geom = JTS.toGeometry(refEnv);
    assertTrue(geom instanceof com.vividsolutions.jts.geom.Polygon);

    Envelope geomEnv = geom.getEnvelopeInternal();
    assertEquals(-10.0, geomEnv.getMinX(), TOL);
    assertEquals(10.0, geomEnv.getMaxX(), TOL);
    assertEquals(-5.0, geomEnv.getMinY(), TOL);
    assertEquals(5.0, geomEnv.getMaxY(), TOL);
  }
예제 #9
0
  @Test
  public void toGeometry_Shape_Poly() {
    Shape shape = new Polygon(XPOINTS, YPOINTS, NPOINTS);
    Geometry geom = JTS.toGeometry(shape);
    assertTrue(geom instanceof LinearRing);

    Coordinate[] coords = geom.getCoordinates();
    assertEquals(NPOINTS + 1, coords.length);

    CoordList list = new CoordList(coords);
    Coordinate c = new Coordinate();
    for (int i = 0; i < NPOINTS; i++) {
      c.x = XPOINTS[i];
      c.y = YPOINTS[i];
      assertTrue(list.contains(c));
    }
  }
예제 #10
0
  @Test
  public void toGeometry_Shape_Line() {
    GeneralPath path = new GeneralPath();

    path.moveTo(XPOINTS[0], YPOINTS[0]);
    for (int i = 1; i < NPOINTS; i++) {
      path.lineTo(XPOINTS[i], YPOINTS[i]);
    }

    Geometry geom = JTS.toGeometry(path);
    assertTrue(geom instanceof LineString);

    Coordinate[] coords = geom.getCoordinates();
    assertEquals(NPOINTS, coords.length);

    CoordList list = new CoordList(coords);
    Coordinate c = new Coordinate();
    for (int i = 0; i < NPOINTS; i++) {
      c.x = XPOINTS[i];
      c.y = YPOINTS[i];
      assertTrue(list.contains(c));
    }
  }