org.geotools.referencing.CRS.transform()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(313)

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

CRS.transform介绍

[英]Transforms the given envelope to the specified CRS. If the given envelope is null, or the Envelope#getCoordinateReferenceSystem is null, or the given target CRS is null, or the transform MathTransform#isIdentity, then the envelope is returned unchanged. Otherwise a new transformed envelope is returned.

Don't use this method if there is many envelopes to transform. This method is provided as a convenience when there is only one envelope to transform between CRS that can't be known in advance. If there is many of them or if the CRS are restricted to known values, get the CoordinateOperation or MathTransform once for ever and invoke one of the methods below instead (unless if performance is not a concern).
[中]将给定信封转换为指定的CRS。如果给定的信封为空,或者信封#getCoordinateReferenceSystem为空,或者给定的目标CRS为空,或者transform MathTransform#isIdentity,则返回的信封将保持不变。否则将返回一个新的转换信封。
如果有许多封套要转换,请不要使用此方法。当在CRS之间只有一个不能预先知道的包络进行变换时,提供这种方法是为了方便。如果有许多方法,或者如果CRS限制为已知值,则永远获取CoordinationOperation或MathTransform,并调用以下方法之一(除非性能不是问题)。

代码示例

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

  1. envelope = CRS.transform(envelope, destCRS);
  2. } catch (TransformException e) {
  3. throw (IOException)

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

  1. GridGeometry2D gg = coverage.getGridGeometry();
  2. GeneralEnvelope padRange =
  3. CRS.transform(gg.getCRSToGrid2D(PixelOrientation.UPPER_LEFT), bounds);
  4. GridEnvelope2D targetRange =
  5. new GridEnvelope2D(

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

  1. final GeneralEnvelope targetEnvelope;
  2. if (!CRS.equalsIgnoreMetadata(sourceCRS, targetCRS)) {
  3. targetEnvelope = CRS.transform(envelope, targetCRS);
  4. } else {
  5. targetEnvelope = new GeneralEnvelope(envelope);

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

  1. CRS.transform(gridToWorldCorner, new GeneralEnvelope(testRange.getBounds()));
  2. testEnvelope.setCoordinateReferenceSystem(reader.getCoordinateReferenceSystem());

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

  1. /**
  2. * Transforms an envelope using the given {@linkplain MathTransform math transform}. The
  3. * transformation is only approximative. Note that the returned envelope may not have the same
  4. * number of dimensions than the original envelope.
  5. *
  6. * <p>Note that this method can not handle the case where the envelope contains the North or
  7. * South pole, or when it cross the &plusmn;180° longitude, because {@linkplain MathTransform
  8. * math transforms} do not carry suffisient informations. For a more robust envelope
  9. * transformation, use {@link #transform(CoordinateOperation, Envelope)} instead.
  10. *
  11. * @param transform The transform to use.
  12. * @param envelope Envelope to transform, or {@code null}. This envelope will not be modified.
  13. * @return The transformed envelope, or {@code null} if {@code envelope} was null.
  14. * @throws TransformException if a transform failed.
  15. * @since 2.4
  16. * @see #transform(CoordinateOperation, Envelope)
  17. */
  18. public static GeneralEnvelope transform(final MathTransform transform, final Envelope envelope)
  19. throws TransformException {
  20. return transform(transform, envelope, null);
  21. }

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

  1. final CoordinateReferenceSystem nativeCRS = reader.getCoordinateReferenceSystem();
  2. if (!CRS.equalsIgnoreMetadata(requestCRS, nativeCRS)) {
  3. requestedEnvelope = CRS.transform(requestedEnvelope, nativeCRS);
  4. GeneralEnvelope requestedGrid = CRS.transform(crsToGrid, requestedEnvelope);
  5. double[] spans = new double[requestedGrid.getDimension()];
  6. double[] resolutions = new double[requestedGrid.getDimension()];

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

  1. /**
  2. * Returns the pixel coordinate of a rectangle containing the specified geographic area. If the
  3. * rectangle can't be computed, then this method returns {@code null}.
  4. */
  5. final Rectangle inverseTransform(Rectangle2D bounds) {
  6. if (bounds != null && gridFromCRS2D != null) {
  7. try {
  8. bounds = org.geotools.referencing.CRS.transform(gridFromCRS2D, bounds, null);
  9. final int xmin = (int) Math.floor(bounds.getMinX() - 0.5);
  10. final int ymin = (int) Math.floor(bounds.getMinY() - 0.5);
  11. final int xmax = (int) Math.ceil(bounds.getMaxX() - 0.5);
  12. final int ymax = (int) Math.ceil(bounds.getMaxY() - 0.5);
  13. return new Rectangle(xmin, ymin, xmax - xmin, ymax - ymin);
  14. } catch (TransformException exception) {
  15. // Ignore, since this method is invoked from 'GridCoverage.prefetch' only.
  16. // It doesn't matter if the transformation failed; 'prefetch' is just a hint.
  17. }
  18. }
  19. return null;
  20. }

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

  1. /**
  2. * @see org.geotools.coverage.io.CoverageReadRequest#setDomainSubset(java.awt.Rectangle,
  3. * org.opengis.referencing.operation.MathTransform2D,
  4. * org.opengis.referencing.crs.CoordinateReferenceSystem)
  5. */
  6. public void setDomainSubset(
  7. final Rectangle rasterArea,
  8. final MathTransform2D gridToWorldTrasform,
  9. final CoordinateReferenceSystem crs)
  10. throws MismatchedDimensionException, TransformException {
  11. // get input elements
  12. this.rasterArea = (Rectangle) rasterArea.clone();
  13. this.gridToWorldTransform = gridToWorldTrasform;
  14. // create a bbox
  15. GeneralEnvelope env =
  16. CRS.transform(
  17. gridToWorldTrasform, new ReferencedEnvelope(rasterArea.getBounds2D(), crs));
  18. this.geographicArea = new ReferencedEnvelope(new ReferencedEnvelope(env), crs);
  19. }

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

  1. public BoundingBox toBounds(CoordinateReferenceSystem targetCRS) throws TransformException {
  2. Envelope transformed = new GeneralEnvelope((BoundingBox) this);
  3. transformed = CRS.transform(transformed, targetCRS);
  4. return new Envelope2D(transformed);
  5. }

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

  1. /** Sets BBOX and SRS using the provided Envelope. */
  2. public void setBBox(Envelope envelope) {
  3. String version = properties.getProperty(VERSION);
  4. boolean forceXY = version == null || !version.startsWith("1.3");
  5. String srsName = CRS.toSRS(envelope.getCoordinateReferenceSystem());
  6. CoordinateReferenceSystem crs = toServerCRS(srsName, forceXY);
  7. Envelope bbox;
  8. try {
  9. bbox = CRS.transform(envelope, crs);
  10. } catch (TransformException e) {
  11. bbox = envelope;
  12. }
  13. StringBuffer sb = new StringBuffer();
  14. sb.append(bbox.getMinimum(0));
  15. sb.append(",");
  16. sb.append(bbox.getMinimum(1) + ",");
  17. sb.append(bbox.getMaximum(0) + ",");
  18. sb.append(bbox.getMaximum(1));
  19. setBBox(sb.toString());
  20. }
  21. /**

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

  1. private void setEnvelopeFromTransform(AffineTransform tempTransform) throws TransformException {
  2. final GeneralEnvelope envelope =
  3. CRS.transform(
  4. ProjectiveTransform.create(tempTransform),
  5. new GeneralEnvelope(nativeGridRange));
  6. envelope.setCoordinateReferenceSystem(crs);
  7. setCoverageEnvelope(envelope);
  8. }

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

  1. /**
  2. * Return a crop region from a specified envelope, leveraging on the grid to world
  3. * transformation.
  4. *
  5. * @param refinedRequestedBBox the crop envelope
  6. * @return a {@code Rectangle} representing the crop region.
  7. * @throws TransformException in case a problem occurs when going back to raster space.
  8. */
  9. private Rectangle getCropRegion() throws TransformException {
  10. final MathTransform gridToWorldTransform = getOriginalGridToWorld(PixelInCell.CELL_CORNER);
  11. final MathTransform worldToGridTransform = gridToWorldTransform.inverse();
  12. final GeneralEnvelope rasterArea = CRS.transform(worldToGridTransform, requestedBBox);
  13. final Rectangle2D ordinates = rasterArea.toRectangle2D();
  14. // THIS IS FUNDAMENTAL IN ORDER TO AVOID PROBLEMS WHEN DOING TILING
  15. return ordinates.getBounds();
  16. }

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

  1. private Rectangle computeRasterArea(
  2. ReferencedEnvelope computedBBox, MathTransform2D requestedWorldToGrid)
  3. throws TransformException, FactoryException {
  4. final ReferencedEnvelope cropBBOXInRequestCRS =
  5. Utils.reprojectEnvelope(computedBBox, requestCRS, requestedBBox);
  6. // make sure it falls within the requested envelope
  7. cropBBOXInRequestCRS.intersection((org.locationtech.jts.geom.Envelope) requestedBBox);
  8. // now go back to raster space
  9. Rectangle computedRasterArea =
  10. new GeneralGridEnvelope(
  11. CRS.transform(requestedWorldToGrid, cropBBOXInRequestCRS),
  12. PixelInCell.CELL_CORNER,
  13. false)
  14. .toRectangle();
  15. // intersect with the original requested raster space to be sure that we stay within
  16. // the requested raster area
  17. XRectangle2D.intersect(computedRasterArea, requestedRasterArea, computedRasterArea);
  18. return computedRasterArea;
  19. }

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

  1. public void testTransformLambertAzimuthalEqualAreaWgs84NonPolar() throws Exception {
  2. CoordinateReferenceSystem crs = CRS.decode("EPSG:3035", true);
  3. // a bbox that does _not_ include the pole
  4. Envelope2D envelope = new Envelope2D(crs);
  5. envelope.setFrameFromDiagonal(4029000, 2676000, 4696500, 3567700);
  6. Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
  7. // check we did _not_ get the whole range of longitudes
  8. assertEquals(5.42, transformed.getMinimum(0), 1e-2);
  9. assertEquals(15.88, transformed.getMaximum(0), 1e-2);
  10. }

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

  1. public void testTransformPolarStereographicToOther() throws Exception {
  2. CoordinateReferenceSystem antarcticPs = CRS.decode("EPSG:3031", true);
  3. CoordinateReferenceSystem australianPs = CRS.decode("EPSG:3032", true);
  4. Envelope2D envelope = new Envelope2D(antarcticPs);
  5. envelope.add(-4223632.8125, -559082.03125);
  6. envelope.add(5053710.9375, 3347167.96875);
  7. Envelope transformed = CRS.transform(envelope, australianPs);
  8. // has a false easting and northing, we can only check the spans are equal
  9. assertEquals(transformed.getSpan(0), transformed.getSpan(1), 1d);
  10. assertEquals(transformed.getMaximum(0), 1.2309982175378662E7, 1d);
  11. }

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

  1. public void testTransformLambertAzimuthalEqualAreaWgs84() throws Exception {
  2. CoordinateReferenceSystem crs = CRS.decode("EPSG:3574", true);
  3. Envelope2D envelope = new Envelope2D(crs);
  4. // random bbox that does include the pole
  5. envelope.add(-3142000, -3142000);
  6. envelope.add(3142000, 3142000);
  7. Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
  8. // check we got the whole range of longitudes, since the original bbox contains the pole
  9. assertEquals(-180d, transformed.getMinimum(0), 0d);
  10. assertEquals(180d, transformed.getMaximum(0), 0d);
  11. }

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

  1. public void testTransformPolarStereographicWgs84FalseOrigin() throws Exception {
  2. // this one has false origins at 6000000/6000000
  3. CoordinateReferenceSystem crs = CRS.decode("EPSG:3032", true);
  4. Envelope2D envelope = new Envelope2D(crs);
  5. envelope.add(5900000, 5900000);
  6. envelope.add(6100000, 6100000);
  7. Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
  8. // check we got the whole range of longitudes, since the original bbox contains the pole
  9. assertEquals(-180d, transformed.getMinimum(0), 0d);
  10. assertEquals(180d, transformed.getMaximum(0), 0d);
  11. }

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

  1. public void testTransformPolarStereographicWgs84() throws Exception {
  2. CoordinateReferenceSystem crs = CRS.decode("EPSG:3031", true);
  3. Envelope2D envelope = new Envelope2D(crs);
  4. // random bbox that does include the pole
  5. envelope.add(-4223632.8125, -559082.03125);
  6. envelope.add(5053710.9375, 3347167.96875);
  7. Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
  8. // check we got the whole range of longitudes, since the original bbox contains the pole
  9. assertEquals(-180d, transformed.getMinimum(0), 0d);
  10. assertEquals(180d, transformed.getMaximum(0), 0d);
  11. // another bbox
  12. envelope = new Envelope2D(crs);
  13. // random bbox that does not include the pole, but it's really just slightly off it
  14. envelope.add(-10718812.640513, -10006238.053703);
  15. envelope.add(12228504.561708, -344209.75803081);
  16. transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
  17. assertEquals(-90, transformed.getMinimum(1), 0.1d);
  18. }

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

  1. public void testTransformWgs84PolarStereographic() throws Exception {
  2. CoordinateReferenceSystem crs = CRS.decode("EPSG:3031", true);
  3. Envelope2D envelope = new Envelope2D(DefaultGeographicCRS.WGS84);
  4. envelope.add(-180, -90);
  5. envelope.add(180, 0);
  6. Envelope transformed = CRS.transform(envelope, crs);
  7. // the result is a square
  8. assertEquals(transformed.getMaximum(0), transformed.getMaximum(1), 1d);
  9. assertEquals(transformed.getMinimum(0), transformed.getMinimum(1), 1d);
  10. assertEquals(Math.abs(transformed.getMinimum(0)), transformed.getMaximum(0), 1d);
  11. assertEquals(transformed.getMaximum(0), 1.236739621845986E7, 1d);
  12. }

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

  1. /** Tests the transformations of an envelope. */
  2. @Test
  3. public void testEnvelopeTransformation() throws FactoryException, TransformException {
  4. final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.UTM_10N);
  5. final CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84;
  6. final MathTransform crsTransform = CRS.findMathTransform(WGS84, mapCRS, true);
  7. assertFalse(crsTransform.isIdentity());
  8. final GeneralEnvelope firstEnvelope, transformedEnvelope, oldEnvelope;
  9. firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43});
  10. firstEnvelope.setCoordinateReferenceSystem(WGS84);
  11. transformedEnvelope = CRS.transform(crsTransform, firstEnvelope);
  12. transformedEnvelope.setCoordinateReferenceSystem(mapCRS);
  13. oldEnvelope = CRS.transform(crsTransform.inverse(), transformedEnvelope);
  14. oldEnvelope.setCoordinateReferenceSystem(WGS84);
  15. assertTrue(oldEnvelope.contains(firstEnvelope, true));
  16. assertTrue(oldEnvelope.equals(firstEnvelope, 0.02, true));
  17. }

相关文章