public static void transformCoordinate(Coordinate coord, MathTransform transform) { double[] points = new double[] { coord.x, coord.y }; try { transform.transform(points, 0, points, 0, 1); } catch (TransformException e) { e.printStackTrace(); } coord.x = points[0]; coord.y = points[1]; } }
@Deprecated public static Coordinate convertToLonLat( MathTransform transform, Coordinate xy) { final Coordinate to = new Coordinate(); final Coordinate yx = new Coordinate(xy.y, xy.x); try { JTS.transform(yx, to, transform.inverse()); } catch (final TransformException e) { e.printStackTrace(); } return new Coordinate(to.y, to.x); }
private String makeCoordinateString(Coordinate coordinate) { if(transform != null) { double[] points = new double[]{coordinate.x, coordinate.y}; try { transform.transform(points, 0, points, 0, 1); } catch (TransformException e) { e.printStackTrace(); return null; } StringBuffer buffer = new StringBuffer(50); buffer.append(Double.toString(points[0])); buffer.append(","); buffer.append(Double.toString(points[1])); return buffer.toString(); } else { StringBuffer buffer = new StringBuffer(50); buffer.append(Double.toString(coordinate.x)); buffer.append(","); buffer.append(Double.toString(coordinate.y)); return coordinate.toString(); } }
@Deprecated public static ProjectedCoordinate convertLonLatToEuclidean( Coordinate lonlat) { final MathTransform transform = getTransform(lonlat); final Coordinate to = new Coordinate(); // the transform seems to swap the lat lon pairs Coordinate latlon = new Coordinate(lonlat.y, lonlat.x); try { JTS.transform(latlon, to, transform); } catch (final TransformException e) { e.printStackTrace(); } return new ProjectedCoordinate(transform, new Coordinate(to.y, to.x), lonlat); }
/** * Creates a new point that is a transformed copy of the original point. * * @param point * a the original point. * @param transform * the transformation to be applied. * @return a new transformed point. */ public static Point transformPoint(Point point, MathTransform transform) { if (geoFactory == null) geoFactory = new GeometryFactory(); double[] points = new double[] { point.getCoordinate().x, point.getCoordinate().y }; try { transform.transform(points, 0, points, 0, 1); } catch (TransformException e) { e.printStackTrace(); } Point p = geoFactory.createPoint(new Coordinate(points[0], points[1])); return p; }
/** * Creates a new point that is a transformed copy of the original point. * * @param point * a the original point. * @param transform * the transformation to be applied. * @return a new transformed point. */ public static Point transformPoint(Point point, MathTransform transform) { if (geoFactory == null) geoFactory = new GeometryFactory(); double[] points = new double[] { point.getCoordinate().x, point.getCoordinate().y }; try { transform.transform(points, 0, points, 0, 1); } catch (TransformException e) { e.printStackTrace(); } Point p = geoFactory.createPoint(new Coordinate(points[0], points[1])); return p; } }
private ReferencedEnvelope computeReprojectedBounds() { final Rectangle2D bounds = new Rectangle2D.Double(); bounds.setFrameFromDiagonal(westBound, northBound, eastBound, southBound); final ReferencedEnvelope boundsEnvelope = new ReferencedEnvelope(bounds, DefaultGeographicCRS.WGS84); ReferencedEnvelope targetEnvelope = null; try { targetEnvelope = boundsEnvelope.transform(targetCRS, true); } catch (TransformException e) { e.printStackTrace(); } catch (FactoryException e) { e.printStackTrace(); } if (targetEnvelope == null) { throw new OperatorException("Cannot compute reprojected bounds"); } return targetEnvelope; //new Rectangle2D.Double(envelope.getMinX(), envelope.getMinY(), envelope.getWidth(), envelope.getHeight()); }
public static <T extends Geometry> T Transform(String sourceEpsgCRSCode, String targetEpsgCRSCode, T sourceObject) { try { CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEpsgCRSCode); CoordinateReferenceSystem targetCRS = CRS.decode(targetEpsgCRSCode); final MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false); return (T) JTS.transform(sourceObject, transform); } catch (FactoryException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (MismatchedDimensionException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (TransformException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
public void apply() { try { context.setCoordinateReferenceSystem(liste.getCRS()); } catch (NoSuchAuthorityCodeException ex) { ex.printStackTrace(); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } }
private void initGeoCoding(Product product) { try { String projectionCode = metadata.getProjectionCode(); if (projectionCode != null && projectionCode.startsWith("epsg")) { CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", null); CoordinateReferenceSystem crs = factory.createProjectedCRS(projectionCode); if (crs != null) { AffineTransform transformation = new AffineTransform(); transformation.translate(metadata.getRasterGeoRefY(), metadata.getRasterGeoRefX()); transformation.scale(metadata.getRasterGeoRefSizeX(), -metadata.getRasterGeoRefSizeY()); transformation.rotate(-Math.toRadians(imageMetadata.getOrientation())); // Why do we need to do this??? Because it seems that the coordinates given were somehow // offset during processing transformation.translate(-product.getSceneRasterHeight(), -product.getSceneRasterWidth()); Rectangle rectangle = new Rectangle(metadata.getRasterWidth(), metadata.getRasterHeight()); CrsGeoCoding geoCoding = new CrsGeoCoding(crs, rectangle, transformation); product.setSceneGeoCoding(geoCoding); } } } catch (FactoryException e) { e.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } }
public ReferencedEnvelope getLayerBounds() throws IOException { ReferencedEnvelope env; CoordinateReferenceSystem sourceCrs; env = layer[0].getBounds(); sourceCrs = env.getCoordinateReferenceSystem(); if (env != null) { try { if ((sourceCrs != null) && (crs != null) && !CRS.equalsIgnoreMetadata(sourceCrs, crs)) { env = env.transform(crs, true); } } catch (FactoryException e) { e.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } } return env; }
@Override public void propertyChange(PropertyChangeEvent arg0) { super.propertyChange(arg0); MapContext context = renderingStrategy.getContext(); try { selectionMapContext.setCoordinateReferenceSystem(context.getCoordinateReferenceSystem()); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } selectionStrategy.refresh(); }
@Override protected void mapAreaChanged(StrategyEvent event) { super.mapAreaChanged(event); MapContext context = renderingStrategy.getContext(); try { selectionMapContext.setCoordinateReferenceSystem(context.getCoordinateReferenceSystem()); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } selectionStrategy.setMapArea(event.getMapArea()); }
public CrsGeoCoding getCrsGeoCoding () { Coordinate2D coordUpperLeft = getUpperLeft(); CrsGeoCoding crsGeoCoding = null; String EPSG = getEPSG(); try { crsGeoCoding = new CrsGeoCoding(CRS.decode("EPSG:" + EPSG), getRasterWidth(), getRasterHeight(), coordUpperLeft.x, coordUpperLeft.y, getBestResolution(), getBestResolution(), 0.0, 0.0); } catch (FactoryException e) { e.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } return crsGeoCoding; }
private void adjusteContexts() { MapContext context = renderingStrategy.getContext(); if (!context.getCoordinateReferenceSystem().equals(memoryMapContext.getCoordinateReferenceSystem())) { try { memoryMapContext.setCoordinateReferenceSystem(context.getCoordinateReferenceSystem()); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } } memoryStrategy.setMapArea(renderingStrategy.getMapArea()); memoryStrategy.refresh(); }
@Override protected void mapContextChanged(StrategyEvent event) { super.mapContextChanged(event); if (event.getContext() != oldMapcontext) { oldMapcontext = event.getContext(); selectionMapContext.clearLayerList(); copies.clear(); MapContext context = event.getContext(); try { selectionMapContext.setCoordinateReferenceSystem(context.getCoordinateReferenceSystem()); selectionStrategy.setMapArea(renderingStrategy.getMapArea()); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } } }
@Override protected void setRendering(boolean render) { super.setRendering(render); MapContext context = renderingStrategy.getContext(); try { selectionMapContext.setCoordinateReferenceSystem(context.getCoordinateReferenceSystem()); } catch (TransformException ex) { ex.printStackTrace(); } catch (FactoryException ex) { ex.printStackTrace(); } selectionStrategy.setMapArea(renderingStrategy.getMapArea()); selectionStrategy.refresh(); }
public double getElevation(Coord coord) { GridGeometry2D gg = grid.getGridGeometry(); Coord transformedCoord = ct.transform(coord); GridCoordinates2D posGrid = null; try { posGrid = gg.worldToGrid(new DirectPosition2D(transformedCoord.getX(), transformedCoord.getY())); } catch (InvalidGridGeometryException e) { e.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } double[] pixel = new double[1]; double[] data = gridData.getPixel(posGrid.x, posGrid.y, pixel); return data[0]; } }
/** * Calculates the distance between two points. If the SRID is 0, a cartesian coordinate system is assumed and {@link * CartesianDistanceCalculator} is used, otherwise {@link JTS#orthodromicDistance(Coordinate, Coordinate, * CoordinateReferenceSystem)} is used. * * @param p1 the source point * @param p2 the target point * @return the distance between <tt>p1</tt> and <tt>p2</tt> or {@code Double.NaN} if the distance cannot be * calculated. * @throws RuntimeException if points have different SRIDs */ @Override public double distance(Point p1, Point p2) { if (p1.getSRID() == p2.getSRID()) { if (p1.getSRID() == 0) { return CartesianDistanceCalculator.getInstance().distance(p1, p2); } else { try { return JTS.orthodromicDistance(p1.getCoordinate(), p2.getCoordinate(), CRSUtils.getCRS(p1.getSRID())); } catch (TransformException e) { e.printStackTrace(); return Double.NaN; } } } else { throw new RuntimeException("Incompatible coordinate reference systems."); } }
private boolean apply(SimpleFeature feature, final String att) { Geometry g = (Geometry) feature.getAttribute(att); if (g == null) { return false;// filter it out } Envelope e = g.getEnvelopeInternal(); // only do work if its a small geometry final boolean canSimplify = screenMap.canSimplify(e); if (!canSimplify) { return true; } boolean includeFeature = true; try { includeFeature = screenMap.checkAndSet(e); } catch (TransformException e1) { e1.printStackTrace(); } if (includeFeature && g.getNumPoints() > 4) { Geometry newGeom = screenMap.getSimplifiedShape(e.getMinX(), e.getMinY(), e.getMaxX(), e.getMaxY(), g.getFactory(), g.getClass()); feature.setAttribute(att, newGeom); } return includeFeature; } }