private static MathTransform getMathTransform(MapTransform mapTransform) throws FactoryException {
    if (mapTransform.getDescriptor() instanceof AffineTransformDescriptor) {
      return new AffineTransform2D(new AffineTransform(mapTransform.getParameterValues()));
    }
    if (mapTransform instanceof AlbersEqualAreaConicDescriptor.AEAC) {
      return createAlbersConicEqualAreaMathTransform(
          (AlbersEqualAreaConicDescriptor.AEAC) mapTransform);
    }
    if (mapTransform instanceof LambertConformalConicDescriptor.LCCT) {
      return createLambertConformalConicMathTransform(
          (LambertConformalConicDescriptor.LCCT) mapTransform);
    }
    if (mapTransform instanceof StereographicDescriptor.ST) {
      return createStereographicMathTransform((StereographicDescriptor.ST) mapTransform);
    }
    if (mapTransform instanceof TransverseMercatorDescriptor.TMT) {
      return createTransverseMercatorMathTransform((TransverseMercatorDescriptor.TMT) mapTransform);
    }

    return null;
  }
예제 #2
0
파일: EnviHeader.java 프로젝트: pmar/nest
  /**
   * Writes the systems byte order to the out stream. In java only high byte first.
   *
   * @param out the stream to write to
   * @param rasterDataNode
   */
  private static void writeMapProjectionInfo(PrintWriter out, RasterDataNode rasterDataNode) {
    Product product = rasterDataNode.getProduct();
    if (product == null) {
      return;
    }

    String mapProjectionName = "Arbitrary";
    String mapUnits = "Meters";
    double referencePixelX = 0, referencePixelY = 0;
    double easting = 0, northing = 0;
    double pixelSizeX = 0, pixelSizeY = 0;
    String datumName = "";
    int utmZone = -1;
    String utmHemisphere = "";
    MapProjection mapProjection = null;

    if (product.getGeoCoding() instanceof CrsGeoCoding) {
      final CrsGeoCoding crsGeoCoding = (CrsGeoCoding) product.getGeoCoding();
      final CoordinateReferenceSystem crs = crsGeoCoding.getMapCRS();

      final ImageGeometry imgGeom =
          ImageGeometry.createTargetGeometry(
              product, crs, null, null, null, null, null, null, null, null, null);

      final String crsName = crs.getName().toString().toUpperCase();
      if (crsName.equals("WGS84(DD)")) {
        mapProjectionName = "Geographic Lat/Lon";
        mapUnits = "Degrees";
      } else if (crsName.contains("UTM")) {
        mapProjectionName = "UTM";
        String zoneStr = crsName.substring(crsName.indexOf("ZONE") + 5, crsName.length()).trim();
        int i = 0;
        String zoneNumStr = "";
        while (Character.isDigit(zoneStr.charAt(i))) {
          zoneNumStr += zoneStr.charAt(i++);
        }
        utmZone = Integer.parseInt(zoneNumStr);

        GeoPos centrePos =
            crsGeoCoding.getGeoPos(
                new PixelPos(product.getSceneRasterWidth() / 2, product.getSceneRasterHeight() / 2),
                null);
        utmHemisphere = centrePos.getLat() > 0 ? "North" : "South";
      }
      referencePixelX = imgGeom.getReferencePixelX();
      referencePixelY = imgGeom.getReferencePixelY();
      easting = imgGeom.getEasting();
      northing = imgGeom.getNorthing();
      pixelSizeX = imgGeom.getPixelSizeX();
      pixelSizeY = imgGeom.getPixelSizeY();
      datumName = crsGeoCoding.getDatum().getName();

    } else if (product.getGeoCoding() instanceof MapGeoCoding) {
      final MapGeoCoding mapGeoCoding = (MapGeoCoding) product.getGeoCoding();

      final MapInfo info = mapGeoCoding.getMapInfo();
      if (info == null) {
        return;
      }
      mapProjection = info.getMapProjection();

      if (mapProjection instanceof UTMProjection) {
        mapProjectionName = "UTM";
        final UTMProjection utmProjection = (UTMProjection) mapProjection;
        utmZone = utmProjection.getZone();
        utmHemisphere = utmProjection.isNorth() ? "North" : "South";
      } else if (mapProjection.isPreDefined()) {
        mapProjectionName = mapProjection.getName();
      }

      if ("meter".equals(mapProjection.getMapUnit())) {
        mapUnits = "Meters";
      } else if ("degree".equals(mapProjection.getMapUnit())) {
        mapUnits = "Degrees";
      } else {
        mapUnits = mapProjection.getMapUnit();
      }

      datumName = mapGeoCoding.getDatum().getName();
    } else {
      return;
    }

    out.print(_enviMapInfo);
    out.print(" = {");
    out.print(mapProjectionName);
    out.print(",");
    out.print(referencePixelX + 1.0f);
    out.print(",");
    out.print(referencePixelY + 1.0f);
    out.print(",");
    out.print(easting);
    out.print(",");
    out.print(northing);
    out.print(",");
    out.print(pixelSizeX);
    out.print(",");
    out.print(pixelSizeY);
    out.print(",");
    if (utmZone != -1) {
      out.print(utmZone);
      out.print(",");
      out.print(utmHemisphere);
      out.print(",");
    }
    out.print(datumName);
    out.print(",");
    out.print("units=" + mapUnits);
    out.print("}");
    out.println();

    if (mapProjection != null && !mapProjection.isPreDefined()) {
      final MapTransform mapTransform = mapProjection.getMapTransform();
      final double[] parameterValues = mapTransform.getParameterValues();
      final String transformName = mapTransform.getDescriptor().getName();
      out.print(_enviProjectionInfo);
      out.print(" = {");
      if (transformName.equals(TransverseMercatorDescriptor.NAME)) {
        out.print(3);
        out.print(",");
        out.print(parameterValues[0]); // semi_major (meters)
        out.print(",");
        out.print(parameterValues[1]); // semi_minor (meters)
        out.print(",");
        out.print(parameterValues[2]); // latitude_of_origin (degree)
        out.print(",");
        out.print(parameterValues[3]); // central_meridian (degree)
        out.print(",");
        out.print(parameterValues[5]); // false_easting (meters)
        out.print(",");
        out.print(parameterValues[6]); // false_northing (meters)
        out.print(",");
        out.print(parameterValues[4]); //  scaling_factor (no unit)
        out.print(",");
      } else if (transformName.equals(LambertConformalConicDescriptor.NAME)) {
        out.print(4);
        out.print(",");
        out.print(parameterValues[0]); // semi_major (meters)
        out.print(",");
        out.print(parameterValues[1]); // semi_minor (meters)
        out.print(",");
        out.print(parameterValues[2]); // latitude_of_origin (degree)
        out.print(",");
        out.print(parameterValues[3]); // central_meridian (degree)
        out.print(",");
        out.print(0.0); // false_easting (meters)
        out.print(",");
        out.print(0.0); // false_northing (meters)
        out.print(",");
        out.print(parameterValues[4]); // latitude_of_intersection_1 (meters)
        out.print(",");
        out.print(parameterValues[5]); // latitude_of_intersection_2 (meters)
        out.print(",");
      }
      out.print(mapProjectionName);
      out.print(",");
      out.print("units=" + mapUnits);
      out.print("}");
      out.println();
    }
  }
  public static CoordinateReferenceSystem getCRS(MapProjection projection, Datum datum) {
    CoordinateReferenceSystem result = WGS84;

    try {
      final MapTransform mapTransform = projection.getMapTransform();
      if (mapTransform.getDescriptor() instanceof IdentityTransformDescriptor) {
        // 1. Identity map projection
        if (Datum.ITRF_97.equals(datum)) {
          result = ITRF97;
        } else if (Datum.WGS_72.equals(datum)) {
          result = WGS72;
        }
      } else if (projection instanceof UTMProjection && !Datum.ITRF_97.equals(datum)) {
        // 2. UTM map projections
        final UTMProjection utmProjection = (UTMProjection) projection;
        final int zone = utmProjection.getZone();

        if (zone >= 1 && zone <= 60) {
          final CRSAuthorityFactory factory =
              ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", null);
          if (utmProjection.isNorth()) {
            if (Datum.WGS_72.equals(datum)) {
              final int WGS72_UTM_zone_N_BASE = 32200;
              result = factory.createProjectedCRS("EPSG:" + (WGS72_UTM_zone_N_BASE + zone));
            } else if (Datum.WGS_84.equals(datum)) {
              final int WGS84_UTM_zone_N_BASE = 32600;
              result = factory.createProjectedCRS("EPSG:" + (WGS84_UTM_zone_N_BASE + zone));
            }
          } else {
            if (Datum.WGS_72.equals(datum)) {
              final int WGS72_UTM_zone_S_BASE = 32300;
              result = factory.createProjectedCRS("EPSG:" + (WGS72_UTM_zone_S_BASE + zone));
            } else if (Datum.WGS_84.equals(datum)) {
              final int WGS84_UTM_zone_S_BASE = 32700;
              result = factory.createProjectedCRS("EPSG:" + (WGS84_UTM_zone_S_BASE + zone));
            }
          }
        }
      } else if (Datum.ITRF_97.equals(datum)) {
        // 3. Other map projections
        final String crsName = "ITRF 97 / " + mapTransform.getDescriptor().getName();
        final MathTransform mathTransform = getMathTransform(mapTransform);
        if (mathTransform != null) {
          result =
              new DefaultProjectedCRS(crsName, ITRF97, mathTransform, DefaultCartesianCS.PROJECTED);
        }
      } else if (Datum.WGS_72.equals(datum)) {
        final String crsName = "WGS 72 / " + mapTransform.getDescriptor().getName();
        final MathTransform mathTransform = getMathTransform(mapTransform);
        if (mathTransform != null) {
          result =
              new DefaultProjectedCRS(crsName, WGS72, mathTransform, DefaultCartesianCS.PROJECTED);
        }
      } else if (Datum.WGS_84.equals(datum)) {
        final String crsName = "WGS 84 / " + mapTransform.getDescriptor().getName();
        final MathTransform mathTransform = getMathTransform(mapTransform);
        if (mathTransform != null) {
          result =
              new DefaultProjectedCRS(crsName, WGS84, mathTransform, DefaultCartesianCS.PROJECTED);
        }
      }
    } catch (FactoryException e) {
      // ignore
    }

    return result;
  }