/** Convert degrees to radians, with error of less than 0.5 ULP * @return instance converted into radians */ public DerivativeStructure toRadians() { final DerivativeStructure ds = new DerivativeStructure(compiler); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] = FastMath.toRadians(data[i]); } return ds; }
/** * @param distanceMeters Distance in meters. * @param latDeg Latitude of center point, in degree. * @return The number of longitude degree for the given distance. This is a slight overestimate * as the number of degree of longitude for a given distance depends on the exact * latitude. */ public static double metersToLonDegrees(double distanceMeters, double latDeg) { double dLatDeg = 360 * distanceMeters / (2 * Math.PI * RADIUS_OF_EARTH_IN_M); /* * The computation below ensure that minCosLat is the minimum value of cos(lat) for lat in * the range [lat-dLat, lat+dLat]. */ double minCosLat; if (latDeg > 0) { minCosLat = FastMath.cos(FastMath.toRadians(latDeg + dLatDeg)); } else { minCosLat = FastMath.cos(FastMath.toRadians(latDeg - dLatDeg)); } return dLatDeg / minCosLat; }
/** * Approximated, fast and under-estimated equirectangular distance between two points. * Works only for small delta lat/lon, fall-back on exact distance if not the case. * See: http://www.movable-type.co.uk/scripts/latlong.html */ public static final double fastDistance(double lat1, double lon1, double lat2, double lon2, double radius) { if (abs(lat1 - lat2) > MAX_LAT_DELTA_DEG || abs(lon1 - lon2) > MAX_LON_DELTA_DEG) { return distance(lat1, lon1, lat2, lon2, radius); } double dLat = toRadians(lat2 - lat1); double dLon = toRadians(lon2 - lon1) * cos(toRadians((lat1 + lat2) / 2)); return radius * sqrt(dLat * dLat + dLon * dLon) * MAX_ERR_INV; }
/** * Returns the approximate azimuth from coordinate A to B in decimal degrees clockwise from North, * in the range (-180° to +180°). The computation is exact for small delta between A and B. */ public static synchronized double getAzimuth(Coordinate a, Coordinate b) { double cosLat = FastMath.cos(FastMath.toRadians((a.y + b.y) / 2.0)); double dY = (b.y - a.y); // in degrees, we do not care about the units double dX = (b.x - a.x) * cosLat; // same if (Math.abs(dX) < 1e-10 && Math.abs(dY) < 1e-10) return 180; double az = FastMath.toDegrees(FastMath.atan2(dX, dY)); return az; }
public static final Envelope bounds(double lat, double lon, double latDistance, double lonDistance) { double radiusOfEarth = RADIUS_OF_EARTH_IN_M; double latRadians = toRadians(lat); double lonRadians = toRadians(lon); double latRadius = radiusOfEarth; double lonRadius = cos(latRadians) * radiusOfEarth; double latOffset = latDistance / latRadius; double lonOffset = lonDistance / lonRadius; double latFrom = toDegrees(latRadians - latOffset); double latTo = toDegrees(latRadians + latOffset); double lonFrom = toDegrees(lonRadians - lonOffset); double lonTo = toDegrees(lonRadians + lonOffset); return new Envelope(new Coordinate(lonFrom, latFrom), new Coordinate(lonTo, latTo)); }
private void makeSampleGrid (ShortestPathTree spt) { long t0 = System.currentTimeMillis(); final double gridSizeMeters = 300; // Todo: set dynamically and make sure this matches isoline builder params final double V0 = 1.00; // off-road walk speed in m/sec Coordinate coordinateOrigin = new Coordinate(); final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat; sampleGrid = new SparseMatrixZSampleGrid<WTWD>(16, spt.getVertexCount(), dX, dY, coordinateOrigin); SampleGridRenderer.sampleSPT(spt, sampleGrid, gridSizeMeters * 0.7, gridSizeMeters, V0, spt .getOptions().getMaxWalkDistance(), Integer.MAX_VALUE, cosLat); long t1 = System.currentTimeMillis(); LOG.info("Made SampleGrid from SPT in {} msec.", (int) (t1 - t0)); }
public static final double distance(double lat1, double lon1, double lat2, double lon2, double radius) { // http://en.wikipedia.org/wiki/Great-circle_distance lat1 = toRadians(lat1); // Theta-s lon1 = toRadians(lon1); // Lambda-s lat2 = toRadians(lat2); // Theta-f lon2 = toRadians(lon2); // Lambda-f double deltaLon = lon2 - lon1; double y = sqrt(p2(cos(lat2) * sin(deltaLon)) + p2(cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLon))); double x = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(deltaLon); return radius * atan2(y, x); }
final double offroadWalkSpeed = 1.00; // in m/sec final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat;
coordinateOrigin = sptRequest.from.getCoordinate(); final double gridSizeMeters = spgRequest.precisionMeters; final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat;
final double offroadWalkSpeed = 1.00; // in m/sec Coordinate coordinateOrigin = graph.getCenter().get(); final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat;
final double V0 = 1.00; // off-road walk speed in m/sec Coordinate coordinateOrigin = new Coordinate(); final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat;
final double V0 = walkSpeed; // off-road walk speed in m/sec Coordinate coordinateOrigin = pointSet.getCoordinate(0); // Use the first feature as the center of the projection final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y)); double dY = Math.toDegrees(GRID_SIZE_METERS / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M); double dX = dY / cosLat;
/** * Approximated, fast and under-estimated equirectangular distance between two points. * Correct only for small delta lat/lon * See: http://www.movable-type.co.uk/scripts/latlong.html */ public static double fastDistance(Coordinate from, Coordinate to) { double dLat = FastMath.toRadians(to.y - from.y); double dLon = FastMath.toRadians(to.x - from.x) * FastMath.cos(FastMath.toRadians((from.y + to.y) / 2)); return RADIUS_OF_EARTH_IN_M * FastMath.sqrt(dLat * dLat + dLon * dLon) * MAX_ERR_INV; } }
/** * @return Equirectangular approximation to distance. */ public static double fastDistance (double lat0, double lon0, double lat1, double lon1) { double midLat = (lat0 + lat1) / 2; double xscale = FastMath.cos(FastMath.toRadians(midLat)); double dx = xscale * (lon1 - lon0); double dy = (lat1 - lat0); return FastMath.sqrt(dx * dx + dy * dy) * METERS_PER_DEGREE_LATITUDE; }
/** Convert meters to degrees of longitude at the specified latitiude */ public static double metersToDegreesLongitude (double meters, double degreesLatitude) { double cosLat = FastMath.cos(FastMath.toRadians(degreesLatitude)); return metersToDegreesLatitude(meters) / cosLat; }
/** Return the pixel the given latitude falls within */ public static int latToPixel (double lat, int zoom) { double latRad = FastMath.toRadians(lat); return (int) ((1 - log(tan(latRad) + 1 / cos(latRad)) / Math.PI) * Math.pow(2, zoom - 1) * 256); }