示例#1
0
 /**
  * Compare two grid to world transformations
  *
  * @param expectedTx
  * @param tx
  */
 private static void compareGrid2World(AffineTransform2D expectedTx, AffineTransform2D tx) {
   assertEquals("scalex", tx.getScaleX(), expectedTx.getScaleX(), 1E-6);
   assertEquals("scaley", tx.getScaleY(), expectedTx.getScaleY(), 1E-6);
   assertEquals("shearx", tx.getShearX(), expectedTx.getShearX(), 1E-6);
   assertEquals("sheary", tx.getShearY(), expectedTx.getShearY(), 1E-6);
   assertEquals("translatex", tx.getTranslateX(), expectedTx.getTranslateX(), 1E-6);
   assertEquals("translatey", tx.getTranslateY(), expectedTx.getTranslateY(), 1E-6);
 }
    /**
     * Encodes the DomainSet as per the GML spec of the provided {@link GridCoverage2D}
     *
     * <p>e.g.:
     *
     * <pre>{@code
     * <gml:domainSet>
     *    <gml:Grid gml:id="gr0001_C0001" dimension="2">
     *       <gml:limits>
     *          <gml:GridEnvelope>
     *             <gml:low>1 1</gml:low>
     *             <gml:high>5 3</gml:high>
     *          </gml:GridEnvelope>
     *       </gml:limits>
     *       <gml:axisLabels>Lat Long</gml:axisLabels>
     *    </gml:Grid>
     * </gml:domainSet>
     * }</pre>
     *
     * @param gc2d the {@link GridCoverage2D} for which to encode the DomainSet.
     * @param srsName
     * @param axesSwap
     */
    public void handleDomainSet(
        GridGeometry2D gg2D, int gridDimension, String gcName, String srsName, boolean axesSwap) {
      // setup vars
      final String gridId = "grid00__" + gcName;

      // Grid Envelope
      final GridEnvelope gridEnvelope = gg2D.getGridRange();

      final StringBuilder lowSb = new StringBuilder();
      for (int i : gridEnvelope.getLow().getCoordinateValues()) {
        lowSb.append(i).append(' ');
      }
      final StringBuilder highSb = new StringBuilder();
      for (int i : gridEnvelope.getHigh().getCoordinateValues()) {
        highSb.append(i).append(' ');
      }

      // build the fragment
      final AttributesImpl gridAttrs = new AttributesImpl();
      gridAttrs.addAttribute("", "gml:id", "gml:id", "", gridId);
      gridAttrs.addAttribute("", "dimension", "dimension", "", String.valueOf(gridDimension));

      start("gml:domainSet");
      start("gml:RectifiedGrid", gridAttrs);
      start("gml:limits");

      // GridEnvelope
      start("gml:GridEnvelope");
      element("gml:low", lowSb.toString().trim());
      element("gml:high", highSb.toString().trim());
      end("gml:GridEnvelope");

      end("gml:limits");

      // Axis Label
      element("gml:axisLabels", "i j");

      final MathTransform2D transform = gg2D.getGridToCRS2D(PixelOrientation.CENTER);
      if (!(transform instanceof AffineTransform2D)) {
        throw new IllegalStateException("Invalid grid to worl provided:" + transform.toString());
      }
      final AffineTransform2D g2W = (AffineTransform2D) transform;

      // Origin
      // we use ULC as per our G2W transformation
      final AttributesImpl pointAttr = new AttributesImpl();
      pointAttr.addAttribute("", "gml:id", "gml:id", "", "p00_" + gcName);
      pointAttr.addAttribute("", "srsName", "srsName", "", srsName);
      start("gml:origin");
      start("gml:Point", pointAttr);
      element(
          "gml:pos",
          axesSwap
              ? g2W.getTranslateY() + " " + g2W.getTranslateX()
              : g2W.getTranslateX() + " " + g2W.getTranslateY());
      end("gml:Point");
      end("gml:origin");

      // Offsets
      final AttributesImpl offsetAttr = new AttributesImpl();
      offsetAttr.addAttribute("", "srsName", "srsName", "", srsName);

      // notice the orientation of the transformation I create. The origin of the coordinates
      // in this grid is not at UPPER LEFT like in our grid to world but at LOWER LEFT !!!
      element(
          "gml:offsetVector",
          Double.valueOf(axesSwap ? g2W.getShearX() : g2W.getScaleX())
              + " "
              + Double.valueOf(axesSwap ? g2W.getScaleX() : g2W.getShearX()),
          offsetAttr);
      element(
          "gml:offsetVector",
          Double.valueOf(axesSwap ? g2W.getScaleY() : g2W.getShearY())
              + " "
              + Double.valueOf(axesSwap ? g2W.getShearY() : g2W.getScaleY()),
          offsetAttr);
      end("gml:RectifiedGrid");
      end("gml:domainSet");
    }