org.geotools.referencing.GeodeticCalculator Java Examples

The following examples show how to use org.geotools.referencing.GeodeticCalculator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestENU.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testWithGeotools() throws MatrixException {
    Coordinate c1 = new Coordinate(11, 46, 0);
    Coordinate c2 = new Coordinate(11.001, 46.001, 0);

    GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    gc.setStartingGeographicPoint(c1.x, c1.y);
    gc.setDestinationGeographicPoint(c2.x, c2.y);
    double orthodromicDistance = gc.getOrthodromicDistance();

    ENU enu = new ENU(c1);
    Coordinate ce1 = enu.wgs84ToEnu(c1);
    Coordinate ce2 = enu.wgs84ToEnu(c2);

    double distance = ce1.distance(ce2);
    assertTrue(isDeltaOk(orthodromicDistance, distance));
    
    Coordinate c1Back = enu.enuToWgs84(ce1);
    Coordinate c2Back = enu.enuToWgs84(ce2);
    
    assertEquals(0, c1.distance(c1Back), 0.000001);
    assertEquals(0, c2.distance(c2Back), 0.000001);
    
}
 
Example #2
Source File: CoordinateCircleDistanceFn.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Override
public double measure(final Coordinate c1, final Coordinate c2) {
  try {
    return JTS.orthodromicDistance(c1, c2, getCRS());
  } catch (final TransformException e) {
    throw new RuntimeException("Failed to transform coordinates to provided CRS", e);
  } catch (final java.lang.AssertionError ae) {
    // weird error with orthodromic distance..when distance is too close
    // (0.05 meter), it fails the tolerance test
    LOGGER.info("when distance is too close(0.05 meter), it fails the tolerance test", ae);

    final GeodeticCalculator calc = new GeodeticCalculator(getCRS());
    calc.setStartingGeographicPoint(c1.x, c1.y);
    calc.setDestinationGeographicPoint(c2.x, c2.y);
    return ((DefaultEllipsoid) calc.getEllipsoid()).orthodromicDistance(
        calc.getStartingGeographicPoint().getX(),
        calc.getStartingGeographicPoint().getY(),
        calc.getDestinationGeographicPoint().getX(),
        calc.getDestinationGeographicPoint().getY());
  }
}
 
Example #3
Source File: GeometryUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
/** farther point in longitudinal axis given a latitude */
private static Point farthestPoint(
    final CoordinateReferenceSystem crs,
    final Point point,
    final double meters) {
  final GeodeticCalculator calc = new GeodeticCalculator(crs);
  calc.setStartingGeographicPoint(point.getX(), point.getY());
  calc.setDirection(90, meters);
  Point2D dest2D = calc.getDestinationGeographicPoint();
  // if this flips over the date line then try the other direction
  if (dest2D.getX() < point.getX()) {
    calc.setDirection(-90, meters);
    dest2D = calc.getDestinationGeographicPoint();
  }
  return point.getFactory().createPoint(new Coordinate(dest2D.getX(), dest2D.getY()));
}
 
Example #4
Source File: ConfigurationDtoPostProcessor.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
private double getUnitLength(String mapCrsKey, Bbox mapBounds) throws LayerException {
	try {
		if (null == mapBounds) {
			throw new LayerException(ExceptionCode.MAP_MAX_EXTENT_MISSING);
		}
		Crs crs = geoService.getCrs2(mapCrsKey);
		GeodeticCalculator calculator = new GeodeticCalculator(crs);
		Coordinate center = new Coordinate(0.5 * (mapBounds.getX() + mapBounds.getMaxX()),
				0.5 * (mapBounds.getY() + mapBounds.getMaxY()));
		calculator.setStartingPosition(new DirectPosition2D(crs, center.getX(), center.getY()));
		calculator.setDestinationPosition(new DirectPosition2D(crs, center.getX() + 1, center.getY()));
		return calculator.getOrthodromicDistance();
	} catch (TransformException e) {
		throw new LayerException(e, ExceptionCode.TRANSFORMER_CREATE_LAYER_TO_MAP_FAILED);
	}
}
 
Example #5
Source File: GeoUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns boundaries of a box shape which centre is the point defined by the
 * given longitude and latitude. The distance between the center point and the
 * edges of the box is defined in meters by the given distance. Based on standard
 * EPSG:4326 long/lat projection. The result is an array of length 4 where
 * the values at each index are:
 *
 * <ul>
 * <li>Index 0: Maximum latitude (north edge of box shape).</li>
 * <li>Index 1: Maximum longitude (east edge of box shape).</li>
 * <li>Index 2: Minimum latitude (south edge of box shape).</li>
 * <li>Index 3: Minimum longitude (west edge of box shape).</li>
 * </ul>
 *
 * @param longitude the longitude.
 * @param latitude the latitude.
 * @param distance the distance in meters to each box edge.
 * @return an array of length 4.
 */
public static double[] getBoxShape( double longitude, double latitude, double distance )
{
    double[] box = new double[4];

    GeodeticCalculator calc = new GeodeticCalculator();
    calc.setStartingGeographicPoint( longitude, latitude );

    calc.setDirection( 0, distance );
    Point2D north = calc.getDestinationGeographicPoint();

    calc.setDirection( 90, distance );
    Point2D east = calc.getDestinationGeographicPoint();

    calc.setDirection( 180, distance );
    Point2D south = calc.getDestinationGeographicPoint();

    calc.setDirection( -90, distance );
    Point2D west = calc.getDestinationGeographicPoint();

    box[0] = north.getY();
    box[1] = east.getX();
    box[2] = south.getY();
    box[3] = west.getX();

    return box;
}
 
Example #6
Source File: GeoUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Computes the distance between two points.
 *
 * @param from the origin point.
 * @param to the end point.
 * @return the orthodromic distance between the given points.
 */
public static double getDistanceBetweenTwoPoints( Point2D from, Point2D to)
{
    GeodeticCalculator calc = new GeodeticCalculator();
    calc.setStartingGeographicPoint( from );
    calc.setDestinationGeographicPoint( to);

    return calc.getOrthodromicDistance();
}
 
Example #7
Source File: CrsUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts meters to degrees, based on a given coordinate in 90 degrees direction.
 * 
 * @param meters the meters to convert.
 * @param c the position to consider.
 * @return the converted degrees.
 */
public static double getMetersAsWGS84( double meters, Coordinate c ) {
    GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    gc.setStartingGeographicPoint(c.x, c.y);
    gc.setDirection(90, meters);
    Point2D destinationGeographicPoint = gc.getDestinationGeographicPoint();
    double degrees = Math.abs(destinationGeographicPoint.getX() - c.x);
    return degrees;
}
 
Example #8
Source File: OmsGeopaparazzi4Converter.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Extracts profile information from logs.
 * 
 * @param log the log to analyze.
 * @param size the number of points in the log (as off: int size = log.points.size(); )
 * @param xProfile the array of to put the progressive distance in.
 * @param yProfile the array of to put the elevation in.
 * @param xPlanim the array of to put the x coord in.
 * @param yPlanim the array of to put the y coord in.
 * @param timestampArray  the array of to put the times in.
 */
public static void populateProfilesForSingleLog( GpsLog log, int size, double[] xProfile, double[] yProfile, double[] xPlanim,
        double[] yPlanim, long[] timestampArray ) {
    GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    double runningDistance = 0;
    for( int i = 0; i < size - 1; i++ ) {
        GpsPoint p1 = log.points.get(i);
        GpsPoint p2 = log.points.get(i + 1);
        double lon1 = p1.lon;
        double lat1 = p1.lat;
        double altim1 = p1.altim;
        long utc1 = p1.utctime;
        double lon2 = p2.lon;
        double lat2 = p2.lat;
        double altim2 = p2.altim;
        long utc2 = p2.utctime;

        gc.setStartingGeographicPoint(lon1, lat1);
        gc.setDestinationGeographicPoint(lon2, lat2);
        double distance = gc.getOrthodromicDistance();
        runningDistance += distance;

        if (i == 0) {
            xProfile[i] = 0.0;
            yProfile[i] = altim1;

            xPlanim[i] = lon1;
            yPlanim[i] = lat1;

            timestampArray[i] = utc1;
        }
        xProfile[i + 1] = runningDistance;
        yProfile[i + 1] = altim2;

        xPlanim[i + 1] = lon2;
        yPlanim[i + 1] = lat2;
        timestampArray[i + 1] = utc2;
    }
}
 
Example #9
Source File: HM.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static double distanceLL( double lon1, double lat1, double lon2, double lat2 ) {
    GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    gc.setStartingGeographicPoint(lon1, lat1);
    gc.setDestinationGeographicPoint(lon2, lat2);
    double distance = gc.getOrthodromicDistance();
    return distance;
}
 
Example #10
Source File: GeometryCalculations.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Build geometries with the provided coordinate at the center. The width of the geometry is twice
 * the distance provided. More than one geometry is return when passing the date line.
 *
 * @param distances [x,y] = [longitude, latitude]
 * @param unit
 * @param coordinate
 * @return the geometries that were built
 */
public List<Geometry> buildSurroundingGeometries(
    final double[] distances,
    final Unit<Length> unit,
    final Coordinate coordinate) {
  final List<Geometry> geos = new LinkedList<>();
  final GeodeticCalculator geoCalc = new GeodeticCalculator();
  geoCalc.setStartingGeographicPoint(coordinate.x, coordinate.y);
  try {
    geoCalc.setDirection(0, unit.getConverterTo(Units.METRE).convert(distances[1]));
    final DirectPosition north = geoCalc.getDestinationPosition();
    geoCalc.setDirection(90, unit.getConverterTo(Units.METRE).convert(distances[0]));
    final DirectPosition east = geoCalc.getDestinationPosition();
    geoCalc.setStartingGeographicPoint(coordinate.x, coordinate.y);
    geoCalc.setDirection(-90, unit.getConverterTo(Units.METRE).convert(distances[0]));
    final DirectPosition west = geoCalc.getDestinationPosition();
    geoCalc.setDirection(180, unit.getConverterTo(Units.METRE).convert(distances[1]));
    final DirectPosition south = geoCalc.getDestinationPosition();

    final double x1 = west.getOrdinate(0);
    final double x2 = east.getOrdinate(0);
    final double y1 = north.getOrdinate(1);
    final double y2 = south.getOrdinate(1);

    handleBoundaries(geos, coordinate, x1, x2, y1, y2);
    return geos;
  } catch (final TransformException ex) {
    LOGGER.error("Unable to build geometry", ex);
  }

  return null;
}
 
Example #11
Source File: GeoDistanceUtil.java    From searoute with European Union Public License 1.2 4 votes vote down vote up
public static double getDistanceKM(double slon, double slat, double dlon, double dlat){
	GeodeticCalculator gc = new GeodeticCalculator();
	gc.setStartingGeographicPoint(slon, slat);
	gc.setDestinationGeographicPoint(dlon, dlat);
	return gc.getOrthodromicDistance() * 0.001;
}
 
Example #12
Source File: ElasticParserUtil.java    From elasticgeo with GNU General Public License v3.0 4 votes vote down vote up
public ElasticParserUtil() {
    this.geometryFactory = new GeometryFactory();
    this.geodeticCalculator = new GeodeticCalculator(DefaultEllipsoid.WGS84);
    this.wktReader = new WKTReader();
}