/** * 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(); double fromLat_r = Math.toRadians(fromLat); // infinite projection if ((Math.abs(90.0 - Math.abs(fromLat))) < TOLERANCE) { toX = Double.POSITIVE_INFINITY; toY = Double.POSITIVE_INFINITY; } else { toX = A * Math.toRadians(LatLonPointImpl.range180(fromLon - this.lon0)); toY = A * SpecialMathFunction.atanh(Math.sin(fromLat_r)); // p 41 Snyder } result.setLocation(toX + falseEasting, toY + falseNorthing); return result; }
/** * 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; }
/** Converts a 3D variable to a {@link Metadata} representation */ private static void convert3DVariable(GridDatatype g, Date date, Map<String, Metadata> metaMap) throws IOException { Variable v = g.getVariable(); System.out.println("Reading: " + v.getFullName()); Array values = v.read(); int h = v.getShape(1); int w = v.getShape(2); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { LatLonPoint pt = g.getCoordinateSystem().getLatLon(j, i); String hash = GeoHash.encode((float) pt.getLatitude(), (float) pt.getLongitude(), 10).toLowerCase(); Metadata meta = metaMap.get(hash); if (meta == null) { /* We need to create Metadata for this location */ meta = new Metadata(); UUID metaUUID = UUID.nameUUIDFromBytes(hash.getBytes()); meta.setName(metaUUID.toString()); SpatialProperties location = new SpatialProperties((float) pt.getLatitude(), (float) pt.getLongitude()); meta.setSpatialProperties(location); TemporalProperties time = new TemporalProperties(date.getTime()); meta.setTemporalProperties(time); metaMap.put(hash, meta); } String featureName = v.getFullName().toLowerCase(); float featureValue = values.getFloat(i * w + j); Feature feature = new Feature(featureName, featureValue); meta.putAttribute(feature); } } }
private static void makeSelectBB(StringBuffer sbuff, LatLonRect bb) { // LAT min max;LON min max // For ADDE URL's, lon is positive east, on the server, lon is positive west. // AddeURLConnection handles the conversion. // Format for lat/lon is either decimal degrees or DD:MM:SS LatLonPoint ll = bb.getLowerLeftPoint(); LatLonPoint ur = bb.getUpperRightPoint(); sbuff.append("LAT "); sbuff.append(ll.getLatitude()); sbuff.append(" "); sbuff.append(ur.getLatitude()); sbuff.append(";LON "); sbuff.append(ll.getLongitude()); sbuff.append(" "); sbuff.append(ur.getLongitude()); }
protected Element genLonLatEnvelope(GridCoordSystem gcs) { // <CoverageOfferingBrief>/lonLatEnvelope Element lonLatEnvelopeElem = new Element("lonLatEnvelope", wcsNS); lonLatEnvelopeElem.setAttribute("srsName", "urn:ogc:def:crs:OGC:1.3:CRS84"); LatLonRect llbb = gcs.getLatLonBoundingBox(); LatLonPoint llpt = llbb.getLowerLeftPoint(); LatLonPoint urpt = llbb.getUpperRightPoint(); // <CoverageOfferingBrief>/lonLatEnvelope/gml:pos String firstPosition = llpt.getLongitude() + " " + llpt.getLatitude(); double lon = llpt.getLongitude() + llbb.getWidth(); String secondPosition = lon + " " + urpt.getLatitude(); // ToDo WCS 1.0Plus - Deal with conversion to meters. (Yikes!!) CoordinateAxis1D vertAxis = gcs.getVerticalAxis(); if (vertAxis != null) { // See verAxis.getUnitsString() double zeroIndexValue = vertAxis.getCoordValue(0); double sizeIndexValue = vertAxis.getCoordValue(((int) vertAxis.getSize()) - 1); if (vertAxis.getPositive().equals(ucar.nc2.constants.CF.POSITIVE_UP)) { firstPosition += " " + zeroIndexValue; secondPosition += " " + sizeIndexValue; } else { firstPosition += " " + sizeIndexValue; secondPosition += " " + zeroIndexValue; } } lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(firstPosition)); lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(secondPosition)); // <CoverageOfferingBrief>/lonLatEnvelope/gml:timePostion [2] if (gcs.hasTimeAxis()) { lonLatEnvelopeElem.addContent( new Element("timePosition", gmlNS) .addContent(gcs.getCalendarDateRange().getStart().toString())); lonLatEnvelopeElem.addContent( new Element("timePosition", gmlNS) .addContent(gcs.getCalendarDateRange().getEnd().toString())); } return lonLatEnvelopeElem; }
protected Element genLonLatEnvelope(GridCoordSystem gcs) { // <CoverageOfferingBrief>/lonLatEnvelope Element lonLatEnvelopeElem = new Element("lonLatEnvelope", wcsNS); lonLatEnvelopeElem.setAttribute("srsName", "urn:ogc:def:crs:OGC:1.3:CRS84"); LatLonRect llbb = gcs.getLatLonBoundingBox(); LatLonPoint llpt = llbb.getLowerLeftPoint(); LatLonPoint urpt = llbb.getUpperRightPoint(); // <CoverageOfferingBrief>/lonLatEnvelope/gml:pos String firstPosition = llpt.getLongitude() + " " + llpt.getLatitude(); double lon = llpt.getLongitude() + llbb.getWidth(); String secondPosition = lon + " " + urpt.getLatitude(); // ToDo WCS 1.0Plus - Add vertical (Deal with conversion to meters. Yikes!!) // CoordinateAxis1D vertAxis = gcs.getVerticalAxis(); // if ( vertAxis != null ) // { // // See verAxis.getUnitsString() // firstPosition += " " + vertAxis.getCoordValue( 0); // secondPostion += " " + vertAxis.getCoordValue( ((int)vertAxis.getSize()) - 1); // } lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(firstPosition)); lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(secondPosition)); // <CoverageOfferingBrief>/lonLatEnvelope/gml:timePostion [2] if (gcs.hasTimeAxis()) { DateRange dr = gcs.getDateRange(); if (dr != null) { lonLatEnvelopeElem.addContent( new Element("timePosition", gmlNS).addContent(dr.getStart().toDateTimeStringISO())); lonLatEnvelopeElem.addContent( new Element("timePosition", gmlNS).addContent(dr.getEnd().toDateTimeStringISO())); } } return lonLatEnvelopeElem; }
void timeProjection(ProjectionImpl proj) { java.util.Random r = new java.util.Random((long) this.hashCode()); LatLonPointImpl startL = new LatLonPointImpl(); double[][] from = new double[2][NPTS]; for (int i = 0; i < NPTS; i++) { from[0][i] = (180.0 * (r.nextDouble() - .5)); // random latlon point from[1][i] = (360.0 * (r.nextDouble() - .5)); // random latlon point } int n = REPEAT * NPTS; // double[][] result = new double[2][NTRIALS]; // normal long t1 = System.currentTimeMillis(); for (int k = 0; k < REPEAT; k++) { for (int i = 0; i < NPTS; i++) { ProjectionPoint p = proj.latLonToProj(from[0][i], from[1][i]); LatLonPoint endL = proj.projToLatLon(p); if (checkit) { assert close(from[0][i], endL.getLatitude()) : "lat: " + from[0][i] + "!=" + endL.getLatitude(); assert close(from[1][i], endL.getLongitude()) : "lon: " + from[1][i] + "!=" + endL.getLongitude(); } } } long took = System.currentTimeMillis() - t1; sumNormal += took; System.out.println( n + " normal " + proj.getClassName() + " took " + took + " msecs "); // == "+ .001*took/NTRIALS+" secs/call "); // array long t2 = System.currentTimeMillis(); for (int k = 0; k < REPEAT; k++) { double[][] to = proj.latLonToProj(from); double[][] result2 = proj.projToLatLon(to); if (checkit) { for (int i = 0; i < NPTS; i++) { assert close(from[0][i], result2[0][i]) : "lat: " + from[0][i] + "!=" + result2[0][i]; assert close(from[1][i], result2[1][i]) : "lon: " + from[1][i] + "!=" + result2[1][i]; } } } took = System.currentTimeMillis() - t2; sumArray += took; System.out.println( n + " array " + proj.getClassName() + " took " + took + " msecs "); // == "+ .001*took/NTRIALS+" secs/call "); // System.out.println(NTRIALS + " array "+ proj.getClassName()+" took "+took+ " msecs == "+ // .001*took/NTRIALS/REPEAT+" secs/call "); }
@Test public void bugReport() throws IOException { try (GridDataset dataset = GridDataset.open( "http://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc")) { GridDatatype firstGridInfo = dataset.getGrids().get(0); System.out.println("Grid name =" + firstGridInfo.getName()); GeoGrid firstGrid = (GeoGrid) dataset.getGrids().get(0); System.out.println("WHOLE GRID"); GridCoordSystem gcs = firstGrid.getCoordinateSystem(); System.out.println("is lat/lon system ? " + gcs.isLatLon()); assert gcs.isLatLon(); LatLonRect rect = gcs.getLatLonBoundingBox(); System.out.println( "gcs bounding box : latmin=" + rect.getLatMin() + " latmax=" + rect.getLatMax() + " lonmin=" + rect.getLonMin() + " lonmax=" + rect.getLonMax()); System.out.println("projection : " + gcs.getProjection()); System.out.println( "width =" + gcs.getXHorizAxis().getSize() + ", height=" + gcs.getYHorizAxis().getSize()); System.out.println( "X is regular ? " + ((CoordinateAxis1D) gcs.getXHorizAxis()).isRegular()); System.out.println("X is contiguous ? " + gcs.getXHorizAxis().isContiguous()); System.out.println( "X start : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getStart()); System.out.println( "X increment : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getIncrement()); System.out.println( "Y is regular ? " + ((CoordinateAxis1D) gcs.getYHorizAxis()).isRegular()); System.out.println("Y is contiguous ? " + gcs.getYHorizAxis().isContiguous()); System.out.println( "Y start : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getStart()); System.out.println( "Y increment : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getIncrement()); LatLonPoint p = gcs.getLatLon(0, 0); System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); p = gcs.getLatLon(1, 1); System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); System.out.println("looking up lat=" + p.getLatitude() + " lon=" + p.getLongitude()); int[] xy = gcs.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null); System.out.println("index= (" + xy[0] + ", " + xy[1] + ")"); Assert.assertEquals(xy[0], 1); Assert.assertEquals(xy[1], 1); // -------------------------------------------------------------------------- double latMin = -20.D, latMax = -10.D, lonMin = 35.D, lonMax = 45.D; System.out.println( "\nSUBGRID (latmin=" + latMin + " latmax=" + latMax + " lonmin=" + lonMin + " lonmax=" + lonMax + ")"); LatLonRect latLonRect = new LatLonRect(new LatLonPointImpl(latMin, lonMin), new LatLonPointImpl(latMax, lonMax)); GeoGrid gridSubset = firstGrid.subset(null, null, latLonRect, 0, 1, 1); GridCoordSystem gcs2 = gridSubset.getCoordinateSystem(); rect = gcs2.getLatLonBoundingBox(); System.out.println("is lat/lon system ? " + gcs2.isLatLon()); System.out.println( "gcs bounding box : latmin=" + rect.getLatMin() + " latmax=" + rect.getLatMax() + " lonmin=" + rect.getLonMin() + " lonmax=" + rect.getLonMax()); System.out.println("projection : " + gcs.getProjection()); System.out.println( "width =" + gcs2.getXHorizAxis().getSize() + ", height=" + gcs2.getYHorizAxis().getSize()); System.out.println( "X is regular ? " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).isRegular()); System.out.println("X is contiguous ? " + gcs2.getXHorizAxis().isContiguous()); System.out.println( "X start : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getStart()); System.out.println( "X increment : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getIncrement()); System.out.println( "Y is regular ? " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).isRegular()); System.out.println("Y is contiguous ? " + gcs2.getYHorizAxis().isContiguous()); System.out.println( "Y start : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getStart()); System.out.println( "Y increment : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getIncrement()); p = gcs2.getLatLon(0, 0); System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); p = gcs2.getLatLon(1, 1); System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); System.out.println("looking up lat=" + p.getLatitude() + " lon=" + p.getLongitude()); xy = gcs2.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null); System.out.println("index= (" + xy[0] + ", " + xy[1] + ")"); Assert.assertEquals(xy[0], 1); Assert.assertEquals(xy[1], 1); } catch (IOException | InvalidRangeException e) { e.printStackTrace(); } }