org.geotools.geometry.jts.JTS.transform()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(18.4k)|赞(0)|评价(0)|浏览(466)

本文整理了Java中org.geotools.geometry.jts.JTS.transform()方法的一些代码示例,展示了JTS.transform()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTS.transform()方法的具体详情如下:
包路径:org.geotools.geometry.jts.JTS
类名称:JTS
方法名:transform

JTS.transform介绍

[英]Transforms the coordinate using the provided math transform.
[中]使用提供的数学变换变换变换坐标。

代码示例

代码示例来源:origin: opentripplanner/OpenTripPlanner

double remainingMeters = remainingSeconds * request.walkSpeed;
    Geometry point = geomf.createPoint(vertexSeconds.getKey().getCoordinate());
    point = JTS.transform(point, toMeters);
    Geometry buffer = point.buffer(remainingMeters);
    bufferLists.put(thresholdSeconds, buffer);
if ( ! resultsProjected) geom = JTS.transform(geom, fromMeters);
contours.put(threshold, geom);

代码示例来源:origin: geotools/geotools

/**
 * Transforms the envelope using the specified math transform. Note that this method can not
 * handle the case where the envelope contains the North or South pole, or when it cross the
 * ±180� longitude, because {@linkplain MathTransform math transforms} do not carry
 * suffisient informations. For a more robust envelope transformation, use {@link
 * ReferencedEnvelope#transform(CoordinateReferenceSystem, boolean)} instead.
 *
 * @param envelope The envelope to transform.
 * @param transform The transform to use.
 * @return The transformed Envelope
 * @throws TransformException if at least one coordinate can't be transformed.
 */
public static Envelope transform(final Envelope envelope, final MathTransform transform)
    throws TransformException {
  return transform(envelope, null, transform, 5);
}

代码示例来源:origin: geotools/geotools

public static Envelope reprojectEnvelopeByEpsg(int srcEpsg, int destEpsg, Envelope srcEnvelope)
    throws FactoryException, TransformException {
  CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:" + srcEpsg); // $NON-NLS-1$
  CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:" + destEpsg); // $NON-NLS-1$
  MathTransform tr = CRS.findMathTransform(sourceCRS, targetCRS);
  // From that point, I'm not sure which kind of object is returned by
  // getLatLonBoundingBox(). But there is some convenience methods if CRS
  // like:
  return JTS.transform(srcEnvelope, tr);
}

代码示例来源:origin: geotools/geotools

static Geometry reprojectAndDensify(
    Geometry first,
    CoordinateReferenceSystem sourceCRS,
    CoordinateReferenceSystem targetCRS)
    throws FactoryException, TransformException {
  if (targetCRS == null) {
    targetCRS = CRS.parseWKT(ECKERT_IV_WKT);
  }
  MathTransform firstTransform = CRS.findMathTransform(sourceCRS, targetCRS);
  Geometry geometry = JTS.transform(densify(first, sourceCRS, 0.01d), firstTransform);
  return geometry;
}

代码示例来源:origin: geotools/geotools

/** Helper method to reproject a geometry. */
protected Geometry reproject(Object value, CoordinateReferenceSystem propertyCrs) {
  if (value == null) {
    return null;
  }
  if (!(value instanceof Geometry))
    throw new IllegalArgumentException(
        "Binary geometry filter, but second expression "
            + "is not a geometry literal? (it's a "
            + value.getClass()
            + ")");
  Geometry geom = (Geometry) value;
  // does it make sense to proceed?
  if (geom.getUserData() == null
      || !(geom.getUserData() instanceof CoordinateReferenceSystem)) return geom;
  try {
    // reproject
    CoordinateReferenceSystem geomCRS = (CoordinateReferenceSystem) geom.getUserData();
    Geometry transformed =
        JTS.transform(geom, CRS.findMathTransform(geomCRS, propertyCrs, true));
    transformed.setUserData(propertyCrs);
    return transformed;
  } catch (Exception e) {
    throw new RuntimeException("Could not reproject geometry " + value, e);
  }
}

代码示例来源:origin: geotools/geotools

/**
 * Compute the footprint.
 *
 * @param geometriesList the List of all the geometries found across the dataset
 * @param transform
 * @throws MismatchedDimensionException
 * @throws TransformException
 * @throws FactoryException
 */
private void computeFootprint(List<Polygon> geometriesList, MathTransform transform)
    throws MismatchedDimensionException, TransformException, FactoryException {
  // Creating the final multipolygon
  Polygon[] polArray = new Polygon[geometriesList.size()];
  Polygon[] polygons = geometriesList.toArray(polArray);
  final Geometry innerGeometry = new MultiPolygon(polygons, GF);
  if (footprintCoordinates == FootprintCoordinates.MODEL_SPACE) {
    this.footprint = JTS.transform(innerGeometry, transform);
  } else {
    this.footprint = innerGeometry;
    innerGeometry.setSRID(NO_SRID);
  }
  // Compute the ROIShape
  if (!innerGeometry.isEmpty()) {
    LiteShape2 shape = new LiteShape2(innerGeometry, TRANSLATED_TX, null, false);
    roiShape = (ROIShape) new ROIShape(shape);
  }
}

代码示例来源:origin: geotools/geotools

/**
 * Applies transform to all geometry attribute.
 *
 * @param feature Feature to be transformed
 * @param schema Schema for target transformation - transform( schema, crs )
 * @param transform MathTransform used to transform coordinates - reproject( crs, crs )
 * @return transformed Feature of type schema
 * @throws TransformException
 * @throws MismatchedDimensionException
 * @throws IllegalAttributeException
 */
public static SimpleFeature transform(
    SimpleFeature feature, SimpleFeatureType schema, MathTransform transform)
    throws MismatchedDimensionException, TransformException, IllegalAttributeException {
  feature = SimpleFeatureBuilder.copy(feature);
  GeometryDescriptor geomType = schema.getGeometryDescriptor();
  Geometry geom = (Geometry) feature.getAttribute(geomType.getLocalName());
  geom = JTS.transform(geom, transform);
  feature.setAttribute(geomType.getLocalName(), geom);
  return feature;
}

代码示例来源:origin: geotools/geotools

/**
 * 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);
}

代码示例来源:origin: geotools/geotools

/** Will reproject a geometry to another CRS. */
  @DescribeProcess(
    title = "Reproject Geometry",
    description = "Reprojects a given geometry into a supplied coordinate reference system."
  )
  @DescribeResult(name = "result", description = "Reprojected geometry")
  public static Geometry reproject(
      @DescribeParameter(name = "geometry", description = "Input geometry") Geometry geometry,
      @DescribeParameter(
            name = "sourceCRS",
            min = 0,
            description = "Coordinate reference system of input geometry"
          )
          CoordinateReferenceSystem sourceCRS,
      @DescribeParameter(
            name = "targetCRS",
            min = 0,
            description = "Target coordinate reference system to use for reprojection"
          )
          CoordinateReferenceSystem targetCRS) {

    try {
      return JTS.transform(geometry, CRS.findMathTransform(sourceCRS, targetCRS, true));
    } catch (Exception e) {
      throw new ProcessException("Reprojection faiiled", e);
    }
  }
}

代码示例来源:origin: geotools/geotools

/**
 * Reprojects a {@link JGrassRegion region}.
 *
 * @param sourceCRS the original {@link CoordinateReferenceSystem crs} of the region.
 * @param targetCRS the target {@link CoordinateReferenceSystem crs} of the region.
 * @param lenient defines whether to apply a lenient transformation or not.
 * @return a new {@link JGrassRegion region}.
 * @throws Exception exception that may be thrown when applying the transformation.
 */
public JGrassRegion reproject(
    CoordinateReferenceSystem sourceCRS,
    CoordinateReferenceSystem targetCRS,
    boolean lenient)
    throws Exception {
  MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, lenient);
  Envelope envelope = getEnvelope();
  Envelope targetEnvelope = JTS.transform(envelope, transform);
  return new JGrassRegion(
      targetEnvelope.getMinX(),
      targetEnvelope.getMaxX(),
      targetEnvelope.getMinY(),
      targetEnvelope.getMaxY(),
      getRows(),
      getCols());
}

代码示例来源:origin: geotools/geotools

/** Tests the transformation of a single coordinate. */
@Test
public void testTransformCoordinate() throws FactoryException, TransformException {
  Coordinate coord = new Coordinate(10, 10);
  AffineTransform at = AffineTransform.getScaleInstance(0.5, 1);
  MathTransform2D t =
      (MathTransform2D)
          ReferencingFactoryFinder.getMathTransformFactory(null)
              .createAffineTransform(new GeneralMatrix(at));
  coord = JTS.transform(coord, coord, t);
  assertEquals(new Coordinate(5, 10), coord);
  coord = JTS.transform(coord, coord, t.inverse());
  assertEquals(new Coordinate(10, 10), coord);
  CoordinateReferenceSystem crs =
      ReferencingFactoryFinder.getCRSFactory(null).createFromWKT(UTM_ZONE_10N);
  t =
      (MathTransform2D)
          ReferencingFactoryFinder.getCoordinateOperationFactory(null)
              .createOperation(DefaultGeographicCRS.WGS84, crs)
              .getMathTransform();
  coord = new Coordinate(-123, 55);
  coord = JTS.transform(coord, coord, t);
  coord = JTS.transform(coord, coord, t.inverse());
  assertEquals(-123, coord.x, EPS);
  assertEquals(55, coord.y, EPS);
}

代码示例来源:origin: geotools/geotools

@Test
public void testUTMDatelineWrapping() throws Exception {
  CoordinateReferenceSystem crs = CRS.decode("EPSG:32601", true);
  ReferencedEnvelope re = new ReferencedEnvelope(300000, 409800, 5890200, 6000000, crs);
  MathTransform mt = CRS.findMathTransform(crs, WGS84);
  Geometry geom = JTS.toGeometry(re);
  ReferencedEnvelope targetReferenceEnvelope =
      new ReferencedEnvelope(-180, 180, -90, 90, WGS84);
  ProjectionHandler ph =
      ProjectionHandlerFinder.getHandler(targetReferenceEnvelope, crs, true);
  Geometry preProcessed = ph.preProcess(geom);
  Geometry transformed = JTS.transform(preProcessed, mt);
  Geometry postProcessed = ph.postProcess(mt.inverse(), transformed);
  // sits across the dateline and it's "small" (used to cover the entire planet)
  Envelope ppEnvelope = postProcessed.getGeometryN(0).getEnvelopeInternal();
  assertTrue(ppEnvelope.contains(180, 54));
  // the original width is 109km, at this latitude one degree of longitude is only 65km
  assertEquals(1.7, ppEnvelope.getWidth(), 0.1);
}

代码示例来源:origin: geotools/geotools

@Test
public void testWrapGeometrySmall() throws Exception {
  // projected dateline CRS
  CoordinateReferenceSystem FIJI = CRS.decode("EPSG:3460", true);
  // a small geometry that will cross the dateline
  Geometry g =
      new WKTReader()
          .read(
              "POLYGON ((2139122 5880020, 2139122 5880030, 2139922 5880030, 2139122 5880020))");
  Geometry original = g.copy();
  // rendering bounds only slightly bigger than geometry
  ReferencedEnvelope world = new ReferencedEnvelope(178, 181, -1, 1, WGS84);
  // make sure the geometry is not wrapped, but it is preserved
  ProjectionHandler handler = ProjectionHandlerFinder.getHandler(world, FIJI, true);
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  // no cutting expected
  assertEquals(original, preProcessed);
  // post process
  MathTransform mt = CRS.findMathTransform(FIJI, WGS84);
  Geometry transformed = JTS.transform(g, mt);
  Geometry postProcessed = handler.postProcess(mt.inverse(), transformed);
  // check the geometry is in the same area as the rendering envelope
  assertTrue(world.contains(postProcessed.getEnvelopeInternal()));
}

代码示例来源:origin: geotools/geotools

@Test
public void testWrapGeometryLatLonMultipleTimes() throws Exception {
  ReferencedEnvelope renderingEnvelope =
      new ReferencedEnvelope(-90, 90, -580, 540, ED50_LATLON);
  // a geometry close to the dateline
  Geometry g = new WKTReader().read("POLYGON((-74 -33, -29 -33, -29 5, -74 5, -74 -33))");
  // make sure the geometry is not wrapped, but it is preserved
  ProjectionHandler handler =
      ProjectionHandlerFinder.getHandler(renderingEnvelope, WGS84, true);
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  MathTransform mt = handler.getRenderingTransform(CRS.findMathTransform(WGS84, ED50_LATLON));
  Geometry transformed = JTS.transform(preProcessed, mt);
  // post process (provide identity transform to force wrap heuristic)
  Geometry postProcessed = handler.postProcess(mt, transformed);
  assertTrue(postProcessed.isValid());
  // should have been replicated three times
  assertEquals(3, postProcessed.getNumGeometries());
}

代码示例来源:origin: geotools/geotools

@Test
public void testWrapGeometryReprojectToED50() throws Exception {
  ReferencedEnvelope world = new ReferencedEnvelope(-80, 80, -180, 180, ED50);
  ProjectionHandler handler = ProjectionHandlerFinder.getHandler(world, WGS84, true);
  // a geometry that will cross the dateline and sitting in the same area as the
  // rendering envelope (with wgs84 lon/latcoordinates)
  String wkt = "POLYGON((178 -80, 178 80, 182 80, 182 80, 178 -80))";
  Geometry g = new WKTReader().read(wkt);
  Geometry original = new WKTReader().read(wkt);
  MathTransform mt = CRS.findMathTransform(WGS84, ED50);
  mt = handler.getRenderingTransform(mt);
  Geometry reprojected = JTS.transform(original, mt);
  // make sure the geometry is not wrapped, but it is preserved
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  // no cutting expected
  assertEquals(original, preProcessed);
  // post process, this should wrap the geometry and clone it
  Geometry postProcessed = handler.postProcess(mt, reprojected);
  assertTrue(postProcessed instanceof MultiPolygon);
}

代码示例来源:origin: geotools/geotools

/** Checks that the measures of XYZM geometries are not transformed. */
@Test
public void testXyzmGeometriesMeasuresArePreserved() throws Exception {
  // build a XYM geometry and reproject it
  Geometry geometry = new WKTReader().read("LINESTRINGZM(170 -40 10 2, 190 40 15 7)");
  MathTransform transform = CRS.findMathTransform(WGS84, MERCATOR, true);
  Geometry transformed = JTS.transform(geometry, transform);
  // check that coordinates where transformed but measures preserved
  assertThat(transformed, instanceOf(LineString.class));
  LineString line = (LineString) transformed;
  assertThat(line.getCoordinateSequence().getDimension(), is(4));
  assertThat(line.getCoordinateSequence().getMeasures(), is(1));
  // check the first coordinate
  assertThat(line.getCoordinateSequence().getX(0), closeTo(1.8924313434856504E7, EPS));
  assertThat(line.getCoordinateSequence().getY(0), closeTo(-4838471.398061137, EPS));
  assertThat(line.getCoordinateSequence().getZ(0), is(10.0));
  assertThat(line.getCoordinateSequence().getM(0), is(2.0));
  // check the second coordinate
  assertThat(line.getCoordinateSequence().getX(1), closeTo(2.115070325072198E7, EPS));
  assertThat(line.getCoordinateSequence().getY(1), closeTo(4838471.398061137, EPS));
  assertThat(line.getCoordinateSequence().getZ(1), is(15.0));
  assertThat(line.getCoordinateSequence().getM(1), is(7.0));
}

代码示例来源:origin: geotools/geotools

/** Checks that the measures of XYM geometries are not transformed. */
@Test
public void testXymGeometriesMeasuresArePreserved() throws Exception {
  // build a XYM geometry and reproject it
  Geometry geometry = new WKTReader().read("LINESTRINGM(170 -40 2, 190 40 7)");
  MathTransform transform = CRS.findMathTransform(WGS84, MERCATOR, true);
  Geometry transformed = JTS.transform(geometry, transform);
  // check that coordinates where transformed but measures preserved
  assertThat(transformed, instanceOf(LineString.class));
  LineString line = (LineString) transformed;
  assertThat(line.getCoordinateSequence().getDimension(), is(3));
  assertThat(line.getCoordinateSequence().getMeasures(), is(1));
  // check the first coordinate
  assertThat(line.getCoordinateSequence().getX(0), closeTo(1.8924313434856504E7, EPS));
  assertThat(line.getCoordinateSequence().getY(0), closeTo(-4838471.398061137, EPS));
  assertThat(line.getCoordinateSequence().getZ(0), is(Double.NaN));
  assertThat(line.getCoordinateSequence().getM(0), is(2.0));
  // check the second coordinate
  assertThat(line.getCoordinateSequence().getX(1), closeTo(2.115070325072198E7, EPS));
  assertThat(line.getCoordinateSequence().getY(1), closeTo(4838471.398061137, EPS));
  assertThat(line.getCoordinateSequence().getZ(1), is(Double.NaN));
  assertThat(line.getCoordinateSequence().getM(1), is(7.0));
}

代码示例来源:origin: geotools/geotools

@Test
public void testWrapGeometryReprojectToLatLonED50() throws Exception {
  ReferencedEnvelope world = new ReferencedEnvelope(-80, 80, -180, 180, ED50_LATLON);
  // make sure the geometry is not wrapped, but it is preserved
  ProjectionHandler handler = ProjectionHandlerFinder.getHandler(world, WGS84, true);
  // a geometry that will cross the dateline and sitting in the same area as the
  // rendering envelope (with wgs84 lon/latcoordinates)
  String wkt = "POLYGON((178 -80, 178 80, 182 80, 182 80, 178 -80))";
  Geometry g = new WKTReader().read(wkt);
  Geometry original = new WKTReader().read(wkt);
  MathTransform mt = CRS.findMathTransform(WGS84, ED50_LATLON);
  MathTransform prepared =
      handler.getRenderingTransform(CRS.findMathTransform(WGS84, ED50_LATLON));
  Geometry reprojected = JTS.transform(original, prepared);
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  // no cutting expected
  assertEquals(original, preProcessed);
  // post process, this should wrap the geometry and clone it
  Geometry postProcessed = handler.postProcess(prepared, reprojected);
  assertTrue(postProcessed instanceof MultiPolygon);
}

代码示例来源:origin: geotools/geotools

@Test
public void testWrapGeometryMercator() throws Exception {
  ReferencedEnvelope world = new ReferencedEnvelope(160, 180, -40, 40, WGS84);
  ReferencedEnvelope mercatorEnvelope = world.transform(MERCATOR, true);
  // move it so that it crosses the dateline (measures are still accurate for something
  // crossing the dateline
  mercatorEnvelope.translate(mercatorEnvelope.getWidth() / 2, 0);
  // a geometry that will cross the dateline and sitting in the same area as the
  // rendering envelope
  Geometry g = new WKTReader().read("LINESTRING(170 -40, 190 40)");
  // make sure the geometry is not wrapped
  ProjectionHandler handler =
      ProjectionHandlerFinder.getHandler(mercatorEnvelope, WGS84, true);
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  // no cutting expected
  assertEquals(g, preProcessed);
  // transform and post process
  MathTransform mt = CRS.findMathTransform(WGS84, MERCATOR, true);
  Geometry transformed = JTS.transform(g, mt);
  Geometry postProcessed = handler.postProcess(mt.inverse(), transformed);
  Envelope env = postProcessed.getEnvelopeInternal();
  // check the geometry is in the same area as the rendering envelope
  assertEquals(mercatorEnvelope.getMinX(), env.getMinX(), EPS);
  assertEquals(mercatorEnvelope.getMaxX(), env.getMaxX(), EPS);
}

代码示例来源:origin: geotools/geotools

@Test
public void testDuplicateGeometryMercator() throws Exception {
  ReferencedEnvelope world = new ReferencedEnvelope(-180, 180, -50, 50, WGS84);
  ReferencedEnvelope mercatorEnvelope = world.transform(MERCATOR, true);
  // a geometry that will cross the dateline and sitting in the same area as the
  // rendering envelope
  Geometry g = new WKTReader().read("LINESTRING(170 -50, 190 50)");
  // make sure the geometry is not wrapped
  ProjectionHandler handler =
      ProjectionHandlerFinder.getHandler(mercatorEnvelope, WGS84, true);
  assertTrue(handler.requiresProcessing(g));
  Geometry preProcessed = handler.preProcess(g);
  // no cutting expected
  assertEquals(g, preProcessed);
  // transform and post process
  MathTransform mt = CRS.findMathTransform(WGS84, MERCATOR, true);
  Geometry transformed = JTS.transform(g, mt);
  Geometry postProcessed = handler.postProcess(mt, transformed);
  // should have been duplicated in two parts
  assertTrue(postProcessed instanceof MultiLineString);
  MultiLineString mls = (MultiLineString) postProcessed;
  assertEquals(2, mls.getNumGeometries());
  // the two geometries width should be the same as 20°
  double twentyDegWidth = mercatorEnvelope.getWidth() / 18;
  assertEquals(twentyDegWidth, mls.getGeometryN(0).getEnvelopeInternal().getWidth(), EPS);
  assertEquals(twentyDegWidth, mls.getGeometryN(1).getEnvelopeInternal().getWidth(), EPS);
}

相关文章