/** Returns a deep copy of this position. */ @Override public GeneralDirectPosition clone() { return new GeneralDirectPosition(ordinates); } }
/** * Transform the provided 3D direct position into 2D (Ellipsoidal height is ignored when * converting from {@link DefaultGeographicCRS#WGS84_3D} to {@link DefaultGeographicCRS#WGS84}). * * @param srcPosition Source 3D position * @param transformToWGS84_3D From source CRS to To WGS84_3D * @param transformFromWGS84 From WGS84 to target CRS * @return Position in target CRS as calculated by transform2 * @throws TransformException */ private static DirectPosition transformTo2D( GeneralDirectPosition srcPosition, MathTransform transformToWGS84_3D, MathTransform transformFromWGS84) throws TransformException { if (Double.isNaN(srcPosition.getOrdinate(2))) { srcPosition.setOrdinate( 2, 0.0); // lazy add 3rd ordinate if not provided to prevent failure } DirectPosition world3D = transformToWGS84_3D.transform(srcPosition, null); DirectPosition world2D = new GeneralDirectPosition(DefaultGeographicCRS.WGS84); world2D.setOrdinate(0, world3D.getOrdinate(0)); world2D.setOrdinate(1, world3D.getOrdinate(1)); DirectPosition targetPosition = transformFromWGS84.transform(world2D, null); return targetPosition; }
/** * A coordinate position consisting of all the {@linkplain #getMinimum minimal ordinates} for * each dimension for all points within the {@code Envelope}. * * @return The lower corner. */ @Override public DirectPosition getLowerCorner() { final int dim = ordinates.length / 2; final GeneralDirectPosition position = new GeneralDirectPosition(dim); System.arraycopy(ordinates, 0, position.ordinates, 0, dim); position.setCoordinateReferenceSystem(crs); return position; }
/** * Set the coordinate reference system in which the coordinate is given. * * @param crs The new coordinate reference system, or {@code null}. * @throws MismatchedDimensionException if the specified CRS doesn't have the expected number of * dimensions. */ public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) throws MismatchedDimensionException { checkCoordinateReferenceSystemDimension(crs, getDimension()); this.crs = crs; }
/** * Set this coordinate to the specified direct position. This method is identical to {@link * #setLocation(DirectPosition)}, but is slightly faster in the special case of an {@code * GeneralDirectPosition} implementation. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. */ public final void setLocation(final GeneralDirectPosition position) throws MismatchedDimensionException { ensureDimensionMatch("position", position.ordinates.length, ordinates.length); setCoordinateReferenceSystem(position.crs); System.arraycopy(position.ordinates, 0, ordinates, 0, ordinates.length); }
new GeneralDirectPosition(envelope.getCoordinateReferenceSystem()); GeneralDirectPosition upperCorner = new GeneralDirectPosition(envelope.getCoordinateReferenceSystem()); lowerCorner.setOrdinate(i, envelope.getLowerCorner().getOrdinate(i)); upperCorner.setOrdinate(i, envelope.getUpperCorner().getOrdinate(i));
private static void expandEnvelopeOnExtremePoints( GeneralDirectPosition centerPt, GeneralEnvelope transformed, MathTransform geoToTarget, Envelope geoEnvelope) throws TransformException { GeneralDirectPosition workPoint = new GeneralDirectPosition(centerPt.getDimension()); double centerLon = centerPt.getOrdinate(0); double minLon = geoEnvelope.getMinimum(0); double maxLon = geoEnvelope.getMaximum(0); double minLat = geoEnvelope.getMinimum(1); double maxLat = geoEnvelope.getMaximum(1); if (minLon <= centerLon && centerLon <= maxLon) { // intersection at boundaries, south includeTransformedPoint(transformed, geoToTarget, workPoint, centerLon, minLat); // intersection at boundaries, north includeTransformedPoint(transformed, geoToTarget, workPoint, centerLon, maxLat); } double centerLat = centerPt.getOrdinate(1); if (minLat <= centerLat && centerLat <= maxLat) { // intersection at boundaries, west includeTransformedPoint(transformed, geoToTarget, workPoint, minLon, centerLat); // intersection at boundaries, east includeTransformedPoint(transformed, geoToTarget, workPoint, maxLon, centerLat); } }
/** * Tests {@link GeneralDirectPosition#equals} method between different implementations. The * purpose of this test is also to run the assertion in the direct position implementations. */ @Test public void testEquals() { assertTrue(GeneralDirectPosition.class.desiredAssertionStatus()); assertTrue(DirectPosition2D.class.desiredAssertionStatus()); CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84; DirectPosition p1 = new DirectPosition2D(WGS84, 48.543261561072285, -123.47009555832284); GeneralDirectPosition p2 = new GeneralDirectPosition(48.543261561072285, -123.47009555832284); assertFalse(p1.equals(p2)); assertFalse(p2.equals(p1)); p2.setCoordinateReferenceSystem(WGS84); assertTrue(p1.equals(p2)); assertTrue(p2.equals(p1)); } }
/** * Returns the "real world" mouse's position. The coordinates are expressed * in Context's CoordinateSystem. * * @param dest A pre-allocated variable to store the mouse's location * in CoordinateSystems, can be set to <code>null</code>. * @return The mouse's location in CoordinateSystem coordinates. * @throws TransformException when transform is invalid. */ public GeneralDirectPosition getMapCoordinate(GeneralDirectPosition dest) throws TransformException { if (dest == null) { dest = new GeneralDirectPosition(getX(), getY()); } else { dest.setLocation(new Point2D.Double(getX(), getY())); } transform.transform(dest, dest); return dest; } }
/** * Sets the coordinate reference system in which the coordinate is given. The given CRS will be * used as: * * <p> * * <ul> * <li>the {@linkplain CoordinateOperation#getTargetCRS target CRS} for every call to {@link * #transform(DirectPosition)} * <li>the {@linkplain CoordinateOperation#getSourceCRS source CRS} for every call to {@link * #inverseTransform(CoordinateReferenceSystem)} * </ul> * * @param crs The new CRS for this direct position. * @throws MismatchedDimensionException if the specified CRS doesn't have the expected number of * dimensions. */ @Override public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) throws MismatchedDimensionException { ensureNonNull("crs", crs); super.setCoordinateReferenceSystem(crs); forward = null; inverse = null; }
private static void includeTransformedPoint( GeneralEnvelope envelope, MathTransform mt, GeneralDirectPosition workPoint, double x, double y) throws TransformException { workPoint.setOrdinate(0, x); workPoint.setOrdinate(1, y); mt.transform(workPoint, workPoint); envelope.add(workPoint); }
/** * * <!-- begin-user-doc --> * ATTENTION: I'm assuming a LatLon envelope here. * <!-- end-user-doc --> * * @generated modifiable */ public Object parse(ElementInstance instance, Node node, Object value) throws Exception { List positions = node.getChildren("pos"); if (!positions.isEmpty() && (positions.size() == 2)) { Node n1 = (Node) positions.get(0); Node n2 = (Node) positions.get(1); GeneralDirectPosition p1 = (GeneralDirectPosition) n1.getValue(); GeneralDirectPosition p2 = (GeneralDirectPosition) n2.getValue(); GeneralEnvelope envelope = new GeneralEnvelope(p1, p2); if (p1.getDimension() == 2 && p2.getDimension() == 2) { envelope.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); return envelope; } else if (p1.getDimension() > 2 && p2.getDimension() > 2) { envelope.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84_3D); return envelope; } } if (!positions.isEmpty()) { throw new RuntimeException("Envelope can have only two coordinates"); } throw new RuntimeException("Could not find coordinates for envelope"); }
@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); }
pos.setCoordinateReferenceSystem(crs); copy(p1, pos.ordinates); gc.setStartingPosition(pos);
private static GeneralDirectPosition getProjectionCenterLonLat( CoordinateReferenceSystem crs, GeneralDirectPosition centerPt) { // set defaults centerPt.setOrdinate(0, 0); centerPt.setOrdinate(1, 0); MapProjection projection = getMapProjection(crs); if (projection == null) { return centerPt; } for (GeneralParameterValue gpv : projection.getParameterValues().values()) { // for safety if (!(gpv instanceof ParameterValue)) { continue; } ParameterValue pv = (ParameterValue) gpv; ReferenceIdentifier pvName = pv.getDescriptor().getName(); if (MapProjection.AbstractProvider.LATITUDE_OF_ORIGIN.getName().equals(pvName)) { centerPt.setOrdinate(1, pv.doubleValue()); } else if (MapProjection.AbstractProvider.LATITUDE_OF_CENTRE.getName().equals(pvName)) { centerPt.setOrdinate(1, pv.doubleValue()); } else if (MapProjection.AbstractProvider.LONGITUDE_OF_CENTRE .getName() .equals(pvName)) { centerPt.setOrdinate(0, pv.doubleValue()); } else if (MapProjection.AbstractProvider.CENTRAL_MERIDIAN.getName().equals(pvName)) { centerPt.setOrdinate(0, pv.doubleValue()); } } return centerPt; }
/** * Set this coordinate to the specified direct position. If the specified position contains a * {@linkplain CoordinateReferenceSystem coordinate reference system}, then the CRS for this * position will be set to the CRS of the specified position. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. * @since 2.2 */ public final void setLocation(final DirectPosition position) throws MismatchedDimensionException { ensureDimensionMatch("position", position.getDimension(), ordinates.length); setCoordinateReferenceSystem(position.getCoordinateReferenceSystem()); for (int i = 0; i < ordinates.length; i++) { ordinates[i] = position.getOrdinate(i); } }
/** * Format the specified coordinates using the specified formatter, which should be an instance * of {@link CoordinateFormat}. */ private static String format(final Format cf, final double longitude, final double latitude) { return cf.format(new GeneralDirectPosition(longitude, latitude)); }
/** * Test case using example from EPSG Guidance Note number 7 part 2 (May 2005), section 2.4.3.1. */ @Test public void testTranslation() throws FactoryException, TransformException { final String classification = "Geocentric translations (geog2d domain)"; final MathTransformFactory factory = ReferencingFactoryFinder.getMathTransformFactory(null); final ParameterValueGroup param = factory.getDefaultParameters(classification); param.parameter("dx").setValue(84.87); param.parameter("dy").setValue(96.49); param.parameter("dz").setValue(116.95); final MathTransform test = factory.createParameterizedTransform(param); final GeneralDirectPosition position = new GeneralDirectPosition(3); position.setOrdinate(0, 3771793.97); position.setOrdinate(1, 140253.34); position.setOrdinate(2, 5124304.35); assertSame(position, test.transform(position, position)); assertEquals(3771878.84, position.getOrdinate(0), 1E-5); assertEquals(140349.83, position.getOrdinate(1), 1E-5); assertEquals(5124421.30, position.getOrdinate(2), 1E-5); }
new GeneralDirectPosition(envelope.getCoordinateReferenceSystem()); GeneralDirectPosition upperCorner = new GeneralDirectPosition(envelope.getCoordinateReferenceSystem()); lowerCorner.setOrdinate(i, envelope.getLowerCorner().getOrdinate(i)); upperCorner.setOrdinate(i, envelope.getUpperCorner().getOrdinate(i));
/** * A coordinate position consisting of all the {@linkplain #getMaximum maximal ordinates} for * each dimension for all points within the {@code Envelope}. * * @return The upper corner. */ @Override public DirectPosition getUpperCorner() { final int dim = ordinates.length / 2; final GeneralDirectPosition position = new GeneralDirectPosition(dim); System.arraycopy(ordinates, dim, position.ordinates, 0, dim); position.setCoordinateReferenceSystem(crs); return position; }