/** * Convert lat/lon coordinates to projection coordinates. * * @param from array of lat/lon coordinates: from[2][n], where from[0][i], from[1][i] is the * (lat,lon) coordinate of the ith point * @param to resulting array of projection coordinates, where to[0][i], to[1][i] is the (x,y) * coordinate of the ith point * @param latIndex index of latitude in "from" * @param lonIndex index of longitude in "from" * @return the "to" array. */ public double[][] latLonToProj(double[][] from, double[][] to, int latIndex, int lonIndex) { int cnt = from[0].length; double[] fromLatA = from[latIndex]; double[] fromLonA = from[lonIndex]; double[] resultXA = to[INDEX_X]; double[] resultYA = to[INDEX_Y]; double toX, toY; for (int i = 0; i < cnt; i++) { double fromLat = fromLatA[i]; double fromLon = fromLonA[i]; fromLat = Math.toRadians(fromLat); double lonDiff = Math.toRadians(LatLonPointImpl.lonNormal(fromLon - lon0Degrees)); double cosc = sinLat0 * Math.sin(fromLat) + cosLat0 * Math.cos(fromLat) * Math.cos(lonDiff); if (cosc >= 0) { toX = R * Math.cos(fromLat) * Math.sin(lonDiff); toY = R * (cosLat0 * Math.sin(fromLat) - sinLat0 * Math.cos(fromLat) * Math.cos(lonDiff)); } else { toX = Double.POSITIVE_INFINITY; toY = Double.POSITIVE_INFINITY; } resultXA[i] = toX; resultYA[i] = toY; } return to; }
/** * Convert lat/lon coordinates to projection coordinates. * * @param from array of lat/lon coordinates: from[2][n], where (from[0][i], from[1][i]) is the * (lat,lon) coordinate of the ith point * @param to resulting array of projection coordinates: to[2][n] where (to[0][i], to[1][i]) is the * (x,y) coordinate of the ith point * @return the "to" array */ public double[][] projToLatLon(double[][] from, double[][] to) { int cnt = from[0].length; double[] fromXA = from[INDEX_X]; double[] fromYA = from[INDEX_Y]; double[] toLatA = to[INDEX_LAT]; double[] toLonA = to[INDEX_LON]; double toLat, toLon; for (int i = 0; i < cnt; i++) { double fromX = fromXA[i]; double fromY = fromYA[i]; double rho = Math.sqrt(fromX * fromX + fromY * fromY); double c = Math.asin(rho / R); toLon = lon0; double temp = 0; if (Math.abs(rho) > TOLERANCE) { toLat = Math.asin(Math.cos(c) * sinLat0 + (fromY * Math.sin(c) * cosLat0 / rho)); if (Math.abs(lat0 - PI_OVER_4) > TOLERANCE) { // not 90 or -90 temp = rho * cosLat0 * Math.cos(c) - fromY * sinLat0 * Math.sin(c); toLon = lon0 + Math.atan(fromX * Math.sin(c) / temp); } else if (lat0 == PI_OVER_4) { toLon = lon0 + Math.atan(fromX / -fromY); temp = -fromY; } else { toLon = lon0 + Math.atan(fromX / fromY); temp = fromY; } } else { toLat = lat0; } toLat = Math.toDegrees(toLat); toLon = Math.toDegrees(toLon); if (temp < 0) { toLon += 180; } toLon = LatLonPointImpl.lonNormal(toLon); toLatA[i] = toLat; toLonA[i] = toLon; } return to; }
/** * Convert a LatLonPoint to projection coordinates * * @param latLon convert from these lat, lon coordinates * @param result the object to write to * @return the given result */ public ProjectionPoint latLonToProj(LatLonPoint latLon, ProjectionPointImpl result) { double toX, toY; double fromLat = latLon.getLatitude(); double fromLon = latLon.getLongitude(); fromLat = Math.toRadians(fromLat); double lonDiff = Math.toRadians(LatLonPointImpl.lonNormal(fromLon - lon0Degrees)); double cosc = sinLat0 * Math.sin(fromLat) + cosLat0 * Math.cos(fromLat) * Math.cos(lonDiff); if (cosc >= 0) { toX = R * Math.cos(fromLat) * Math.sin(lonDiff); toY = R * (cosLat0 * Math.sin(fromLat) - sinLat0 * Math.cos(fromLat) * Math.cos(lonDiff)); } else { toX = Double.POSITIVE_INFINITY; toY = Double.POSITIVE_INFINITY; } result.setLocation(toX, toY); return result; }
/** * Convert projection coordinates to a LatLonPoint Note: a new object is not created on each call * for the return value. * * @param world convert from these projection coordinates * @param result the object to write to * @return LatLonPoint convert to these lat/lon coordinates */ public LatLonPoint projToLatLon(ProjectionPoint world, LatLonPointImpl result) { double toLat, toLon; double fromX = world.getX(); double fromY = world.getY(); double rho = Math.sqrt(fromX * fromX + fromY * fromY); double c = Math.asin(rho / R); toLon = lon0; double temp = 0; if (Math.abs(rho) > TOLERANCE) { toLat = Math.asin(Math.cos(c) * sinLat0 + (fromY * Math.sin(c) * cosLat0 / rho)); if (Math.abs(lat0 - PI_OVER_4) > TOLERANCE) { // not 90 or -90 temp = rho * cosLat0 * Math.cos(c) - fromY * sinLat0 * Math.sin(c); toLon = lon0 + Math.atan(fromX * Math.sin(c) / temp); } else if (lat0 == PI_OVER_4) { toLon = lon0 + Math.atan(fromX / -fromY); temp = -fromY; } else { toLon = lon0 + Math.atan(fromX / fromY); temp = fromY; } } else { toLat = lat0; } toLat = Math.toDegrees(toLat); toLon = Math.toDegrees(toLon); if (temp < 0) { toLon += 180; } toLon = LatLonPointImpl.lonNormal(toLon); result.setLatitude(toLat); result.setLongitude(toLon); return result; }