/** Transforms a single coordinate point. */ @Override public DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst) throws MismatchedDimensionException, TransformException { final int srcDim = source.getDimension(); final int tgtDim = target.getDimension(); if (ptSrc.getDimension() != srcDim) { throw new MismatchedDimensionException(); } double[] ordinates = new double[Math.max(srcDim, tgtDim)]; for (int i = 0; i < srcDim; i++) { ordinates[i] = ptSrc.getOrdinate(i); } source.pj.transform(target.pj, ordinates.length, ordinates, 0, 1); if (ptDst != null) { if (ptDst.getDimension() != tgtDim) { throw new MismatchedDimensionException(); } for (int i = 0; i < tgtDim; i++) { ptDst.setOrdinate(i, ordinates[i]); } } else { if (ordinates.length != tgtDim) { ordinates = Arrays.copyOf(ordinates, tgtDim); } ptDst = new SimpleDirectPosition(ordinates); } return ptDst; }
public static HashMap<String, Double> getRegionParamsFromGridCoverage( GridCoverage2D gridCoverage) { HashMap<String, Double> envelopeParams = new HashMap<String, Double>(); Envelope envelope = gridCoverage.getEnvelope(); DirectPosition lowerCorner = envelope.getLowerCorner(); double[] westSouth = lowerCorner.getCoordinate(); DirectPosition upperCorner = envelope.getUpperCorner(); double[] eastNorth = upperCorner.getCoordinate(); GridGeometry2D gridGeometry = gridCoverage.getGridGeometry(); GridEnvelope2D gridRange = gridGeometry.getGridRange2D(); int height = gridRange.height; int width = gridRange.width; AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS(); double xRes = XAffineTransform.getScaleX0(gridToCRS); double yRes = XAffineTransform.getScaleY0(gridToCRS); envelopeParams.put(NORTH, eastNorth[1]); envelopeParams.put(SOUTH, westSouth[1]); envelopeParams.put(WEST, westSouth[0]); envelopeParams.put(EAST, eastNorth[0]); envelopeParams.put(XRES, xRes); envelopeParams.put(YRES, yRes); envelopeParams.put(ROWS, (double) height); envelopeParams.put(COLS, (double) width); return envelopeParams; }
public TemplateTile(TileRequest req, SampleSource sampleSource) { super(req); this.samples = new Sample[width * height]; CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D(); int i = 0; try { MathTransform tr = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84); // grid coordinate object to be reused for examining each cell GridCoordinates2D coord = new GridCoordinates2D(); for (int gy = 0; gy < height; gy++) { if (gy % 100 == 0) LOG.trace("raster line {} / {}", gy, height); for (int gx = 0; gx < width; gx++) { coord.x = gx; coord.y = gy; // find coordinates for current raster cell in tile CRS DirectPosition sourcePos = gg.gridToWorld(coord); // convert coordinates in tile CRS to WGS84 // LOG.debug("world : {}", sourcePos); tr.transform(sourcePos, sourcePos); // LOG.debug("wgs84 : {}", sourcePos); // axis order can vary double lon = sourcePos.getOrdinate(0); double lat = sourcePos.getOrdinate(1); // TODO: axes are reversed in the default mathtransform Sample s = sampleSource.getSample(lon, lat); samples[i++] = s; } } } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); } }
/** Returns {@code true} if this direct position is equals to the given object. */ @Override public boolean equals(final Object object) { if (object instanceof DirectPosition) { final DirectPosition other = (DirectPosition) object; if (other.getCoordinateReferenceSystem() == null) { return Arrays.equals(ordinates, other.getCoordinate()); } } return false; }
/** * Executes a linear Interpolation between two positions and returns the DirectPosition at the * distance 'par' on this straight line * * <p>This method is NON-ROBUST, e.g. it might suffer by errors caused by the Floating-point * arithmetic * * @param p0 - start position of linear interpolation * @param p1 - end position of linear interpolation * @param par - distance on the straight line, for which to search DirectPosition (from p0 to p1) * @return Position on the straight line at parameter 'par' */ public static DirectPositionImpl linearInterpolate( DirectPosition p0, DirectPosition p1, double par) { // Test ok // 0.0 <= factor <= 1.0 // par = 0 => result = dp0 // par = 1 => result = dp1 double[] coord = AlgoPointND.evaluate(p0.getCoordinates(), p1.getCoordinates(), par); return new DirectPositionImpl(p0.getCoordinateReferenceSystem(), coord); }
public boolean contains(DirectPosition location) { ensureCompatibleReferenceSystem(location); if (isEmpty()) { return false; } return location.getOrdinate(0) >= getMinX() && location.getOrdinate(0) <= getMaxX() && location.getOrdinate(1) >= getMinY() && location.getOrdinate(1) <= getMaxY(); }
/** Formats the specified position. */ static String toString(final DirectPosition position) { final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(position)).append('['); final int dimension = position.getDimension(); for (int i = 0; i < dimension; i++) { if (i != 0) { buffer.append(", "); } buffer.append(position.getOrdinate(i)); } return buffer.append(']').toString(); }
/** Returns a hash value for the given coordinate. */ static int hashCode(final DirectPosition position) { final int dimension = position.getDimension(); int code = 1; for (int i = 0; i < dimension; i++) { final long bits = Double.doubleToLongBits(position.getOrdinate(i)); code = 31 * code + ((int) (bits) ^ (int) (bits >>> 32)); } final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem(); if (crs != null) { code += crs.hashCode(); } return code; }
/** * Sets this direct position to the given position. If the given position is {@code null}, then * all ordinate values are set to {@linkplain Double#NaN NaN}. * * @param position The new position. * @since 2.5 */ public void setPosition(final DirectPosition position) { final int dimension = getDimension(); if (position != null) { ensureDimensionMatch("position", position.getDimension(), dimension); for (int i = 0; i < dimension; i++) { setOrdinate(i, position.getOrdinate(i)); } } else { for (int i = 0; i < dimension; i++) { setOrdinate(i, Double.NaN); } } }
/** * Make sure that the specified location uses the same CRS as this one. * * @param location * @throws MismatchedReferenceSystemException if the CRS are incompatible. */ protected void ensureCompatibleReferenceSystem(DirectPosition location) { if (crs != null) { final CoordinateReferenceSystem other = location.getCoordinateReferenceSystem(); if (other != null) { if (!CRS.equalsIgnoreMetadata(crs, other)) { throw new MismatchedReferenceSystemException( Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM)); } } } }
/** * Returns {@code true} if the specified object is also a {@linkplain DirectPosition direct * position} with equals {@linkplain #getCoordinate coordinate} and {@linkplain * #getCoordinateReferenceSystem CRS}. * * @param object The object to compare with this position. * @return {@code true} if the given object is equals to this position. */ @Override public boolean equals(final Object object) { if (object instanceof DirectPosition) { final DirectPosition that = (DirectPosition) object; final int dimension = getDimension(); if (dimension == that.getDimension()) { for (int i = 0; i < dimension; i++) { if (!Utilities.equals(this.getOrdinate(i), that.getOrdinate(i))) { return false; } } if (Utilities.equals( this.getCoordinateReferenceSystem(), that.getCoordinateReferenceSystem())) { assert hashCode() == that.hashCode() : this; return true; } } } return false; }
private void storeResult( double[] interpolatedValues, HashMap<Integer, Coordinate> interpolatedCoordinatesMap) throws MismatchedDimensionException, Exception { WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null); Set<Integer> pointsToInterpolateIdSett = interpolatedCoordinatesMap.keySet(); Iterator<Integer> idIterator = pointsToInterpolateIdSett.iterator(); int c = 0; MathTransform transf = inInterpolationGrid.getCRSToGrid2D(); final DirectPosition gridPoint = new DirectPosition2D(); while (idIterator.hasNext()) { int id = idIterator.next(); Coordinate coordinate = (Coordinate) interpolatedCoordinatesMap.get(id); DirectPosition point = new DirectPosition2D( inInterpolationGrid.getCoordinateReferenceSystem(), coordinate.x, coordinate.y); transf.transform(point, gridPoint); double[] gridCoord = gridPoint.getCoordinate(); int x = (int) gridCoord[0]; int y = (int) gridCoord[1]; outIter.setSample(x, y, 0, checkResultValue(interpolatedValues[c])); c++; } RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inInterpolationGrid); outGrid = CoverageUtilities.buildCoverage( "gridded", outWR, regionMap, inInterpolationGrid.getCoordinateReferenceSystem()); }
/** * Expand to include the provided DirectPosition * * @param pt */ public void expandToInclude(DirectPosition pt) { Coordinate coordinate = new Coordinate(pt.getOrdinate(0), pt.getOrdinate(1)); expandToInclude(coordinate); }
/** * Returns {@code true} if the provided location is contained by this bounding box. * * @since 2.4 */ public boolean contains(DirectPosition pos) { ensureCompatibleReferenceSystem(pos); return super.contains(pos.getOrdinate(0), pos.getOrdinate(1)); }
public boolean extractTiffMetadata(File tifFile, TiffMeta surface) throws UnknownCRSException, FactoryException, TransformException, IOException { Preconditions.checkArgument(tifFile != null, "File is null."); GeoTiffReader gtr = new GeoTiffReader(tifFile); try { CoordinateReferenceSystem crs = gtr.getCoordinateReferenceSystem(); surface.setCRS(crs); Integer epsgCode = CRS.lookupEpsgCode(crs, true); if (epsgCode == null) { ReferenceIdentifier name = crs.getName(); String crsName = "Unknown"; if (name != null) { crsName = name.toString(); } throw new UnknownCRSException(crsName); } String srid = "EPSG:" + epsgCode; surface.setSrid(srid); // // extremaOp(surface, gtr.read(null)); /* * Build the envelope and set to WGS84 */ GeneralEnvelope origEnv = gtr.getOriginalEnvelope(); DirectPosition ll = origEnv.getLowerCorner(); DirectPosition ur = origEnv.getUpperCorner(); Envelope e = new Envelope(); e.expandToInclude(ll.getOrdinate(0), ll.getOrdinate(1)); e.expandToInclude(ur.getOrdinate(0), ur.getOrdinate(1)); Geometry poly = envelopeToWgs84(epsgCode, e); if (poly instanceof Polygon) { surface.setEnvelope((Polygon) poly); } /* * Figure out the pixel size */ ImageLayout imageLayout = gtr.getImageLayout(); int imageWidth = imageLayout.getWidth(null); int imageHeight = imageLayout.getHeight(null); double pixelSizeX = e.getWidth() / imageWidth; double pixelSizeY = e.getHeight() / imageHeight; surface.setPixelSizeX(pixelSizeX); surface.setPixelSizeY(pixelSizeY); surface.setMinVal(0d); surface.setMaxVal(100d); GridCoverage2D gridCoverage2D = gtr.read(null); try { int nDims = gridCoverage2D.getNumSampleDimensions(); surface.setNumSampleDimensions(nDims); extremaOp(surface, gridCoverage2D); } finally { gridCoverage2D.dispose(false); } } finally { gtr.dispose(); } return true; }
/** * * <!-- begin-user-doc --> * <!-- end-user-doc --> * * @generated modifiable */ public Object parse(ElementInstance instance, Node node, Object value) throws Exception { CoordinateReferenceSystem crs = GML3ParsingUtils.crs(node); if (node.getChild("lowerCorner") != null) { DirectPosition l = (DirectPosition) node.getChildValue("lowerCorner"); DirectPosition u = (DirectPosition) node.getChildValue("upperCorner"); return new ReferencedEnvelope( l.getOrdinate(0), u.getOrdinate(0), l.getOrdinate(1), u.getOrdinate(1), crs); } if (node.hasChild(Coordinate.class)) { List c = node.getChildValues(Coordinate.class); Coordinate c1 = (Coordinate) c.get(0); Coordinate c2 = (Coordinate) c.get(1); return new ReferencedEnvelope(c1.x, c2.x, c1.y, c2.y, crs); } if (node.hasChild(DirectPosition.class)) { List dp = node.getChildValues(DirectPosition.class); DirectPosition dp1 = (DirectPosition) dp.get(0); DirectPosition dp2 = (DirectPosition) dp.get(1); return new ReferencedEnvelope( dp1.getOrdinate(0), dp2.getOrdinate(0), dp1.getOrdinate(1), dp2.getOrdinate(1), crs); } if (node.hasChild(CoordinateSequence.class)) { CoordinateSequence seq = (CoordinateSequence) node.getChildValue(CoordinateSequence.class); return new ReferencedEnvelope(seq.getX(0), seq.getX(1), seq.getY(0), seq.getY(1), crs); } return null; }
/** * Get ordinate offset from given size. * * @param point * @param ordinate * @param size * @return */ private double offset(DirectPosition point, int ordinate, double size) { return point.getOrdinate(ordinate) % size; }