private static boolean intersects(
      Envelope requestEnv, String requestCrs, Envelope regionEnv, String regionCrs)
      throws CRSException, GeometryException, CRSTransformationException, UnknownCRSException {
    Surface request = GeometryFactory.createSurface(requestEnv, CRSFactory.create(requestCrs));
    Surface region = GeometryFactory.createSurface(regionEnv, CRSFactory.create(regionCrs));

    if (!requestCrs.equalsIgnoreCase(regionCrs)) {
      GeoTransformer gt = new GeoTransformer(requestCrs);
      region = (Surface) gt.transform(region);
    }

    return request.intersects(region);
  }
  /**
   * Parse Lat Lon Bounding Box
   *
   * @param llBox
   * @return Envelope
   * @throws XMLParsingException
   * @throws UnknownCRSException
   */
  protected Envelope parseLatLonBoundingBox(Element llBox)
      throws XMLParsingException, UnknownCRSException {
    LOG.entering();

    double minx = XMLTools.getRequiredNodeAsDouble(llBox, "./@minx", nsContext);
    double maxx = XMLTools.getRequiredNodeAsDouble(llBox, "./@maxx", nsContext);
    double miny = XMLTools.getRequiredNodeAsDouble(llBox, "./@miny", nsContext);
    double maxy = XMLTools.getRequiredNodeAsDouble(llBox, "./@maxy", nsContext);
    /** default crs = EPSG:4326 */
    CoordinateSystem crs = CRSFactory.create("EPSG:4326");
    Envelope env = GeometryFactory.createEnvelope(minx, miny, maxx, maxy, crs);

    LOG.exiting();
    return env;
  }
Example #3
0
 /**
  * @param name
  * @param select
  * @throws UnknownCRSException
  */
 Table(String name, String select, List<Pair<String, String>> geometryColumns)
     throws UnknownCRSException {
   this.name = name;
   this.select = select.trim();
   String[] tmp = StringTools.toArray(select, " ", false);
   for (String value : tmp) {
     if (value.startsWith("$")) {
       variables.add(value);
     }
   }
   for (Pair<String, String> pair : geometryColumns) {
     CoordinateSystem crs = CRSFactory.create(pair.second);
     Pair<String, CoordinateSystem> p = new Pair<String, CoordinateSystem>(pair.first, crs);
     this.geometryColumns.add(p);
   }
 }
  private static boolean intersects(
      Envelope envelope, Code reqCRS, Envelope[] envs, LonLatEnvelope llEnv)
      throws UnknownCRSException {

    boolean res = false;
    String reqCRSCode = reqCRS.getCode();

    try {
      if (envs == null || envs.length == 0) {
        Envelope latlonEnv =
            GeometryFactory.createEnvelope(
                llEnv.getMin().getX(),
                llEnv.getMin().getY(),
                llEnv.getMax().getX(),
                llEnv.getMax().getY(),
                CRSFactory.create("EPSG:4326"));

        if (!"EPSG:4326".equals(reqCRSCode)) {
          res = intersects(envelope, reqCRSCode, latlonEnv, "EPSG:4326");
        } else {
          res = envelope.intersects(latlonEnv);
        }
      } else {
        for (int i = 0; i < envs.length && !res; i++) {
          if (intersects(
              envelope, reqCRSCode, envs[i], envs[i].getCoordinateSystem().getPrefixedName())) {
            res = true;
            break;
          }
        }
      }
    } catch (GeometryException ex) {
      LOG.logWarning("intersection test; translation into surface failed", ex);
    } catch (CRSException ex) {
      LOG.logWarning(
          "intersection test; transformation of reqeust envelope/valid area impossible", ex);
    } catch (CRSTransformationException ex) {
      LOG.logWarning("intersection test; transformation of reqeust envelope/valid area failed", ex);
    }
    return res;
  }
  private static void transformRasterFile(
      String inFile,
      String inCRS,
      String outFile,
      String outCRS,
      Float imageQuality,
      int ppgridsize,
      int polynomOrder)
      throws Exception {

    System.out.println("Loading raster " + inFile);
    BufferedImage image = ImageUtils.loadImage(inFile);

    CoordinateSystem sourceCRS = CRSFactory.create(inCRS);
    CoordinateSystem targetCRS = CRSFactory.create(outCRS);

    WorldFile worldFile = WorldFile.readWorldFile(inFile, WorldFile.TYPE.CENTER, image);
    Envelope inEnvelope = worldFile.getEnvelope();

    // create minimal CoverageOffering for ImageGridCoverage
    // most parts are not used
    DomainSet ds = new DomainSet(new SpatialDomain(new Envelope[] {inEnvelope}));
    RangeSet rs = new RangeSet("", "");
    CodeList[] dummyCodeList = new CodeList[] {new CodeList("", new String[] {})};
    CodeList[] nativeSRSCodeList = new CodeList[] {new CodeList("", new String[] {inCRS})};

    SupportedSRSs supSRSs =
        new SupportedSRSs(dummyCodeList, dummyCodeList, dummyCodeList, nativeSRSCodeList);

    SupportedFormats supFormats = new SupportedFormats(dummyCodeList);

    CoverageOffering coverageOffering =
        new CoverageOffering("", "", "", null, null, null, ds, rs, supSRSs, supFormats, null, null);

    ImageGridCoverage igc = new ImageGridCoverage(coverageOffering, inEnvelope, image);

    GeoTransformer gt = new GeoTransformer(targetCRS);

    Envelope outEnvelope = gt.transform(inEnvelope, sourceCRS, true);

    // calculate new output size
    // use square pixels for output, ie. the aspect ratio changes
    double deltaX = outEnvelope.getWidth();
    double deltaY = outEnvelope.getHeight();
    double diagSize = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    // pixelSize for calculation of the new image size
    double pixelSize =
        diagSize / Math.sqrt(Math.pow(image.getWidth(), 2) + Math.pow(image.getHeight(), 2));
    int height = (int) (deltaY / pixelSize + 0.5);
    int width = (int) (deltaX / pixelSize + 0.5);
    // realPixelSize for center type world files, etc.
    double realPixelSize =
        diagSize
            / Math.sqrt(Math.pow(image.getWidth() - 1, 2) + Math.pow(image.getHeight() - 1, 2));

    System.out.println("Transforming raster from " + inCRS + " to " + outCRS);
    igc =
        (ImageGridCoverage)
            gt.transform(
                igc,
                outEnvelope,
                width,
                height,
                ppgridsize,
                polynomOrder,
                new InterpolationNearest());

    image = igc.getAsImage(-1, -1);

    System.out.println("Saving raster " + outFile);
    ImageUtils.saveImage(image, outFile, imageQuality);

    // save new WorldFile
    WorldFile outWorldFile = new WorldFile(realPixelSize, realPixelSize, 0.0f, 0.0f, outEnvelope);
    String basename = outFile.substring(0, outFile.lastIndexOf("."));
    WorldFile.writeWorldFile(outWorldFile, basename);
  }
  /**
   * returns the layers offered by the WMPS
   *
   * @param layerElem
   * @param parent
   * @return Layer
   * @throws XMLParsingException
   * @throws UnknownCRSException
   */
  protected Layer parseLayers(Element layerElem, Layer parent)
      throws XMLParsingException, UnknownCRSException {
    LOG.entering();

    boolean queryable = XMLTools.getNodeAsBoolean(layerElem, "./@queryable", nsContext, false);

    int cascaded = XMLTools.getNodeAsInt(layerElem, "./@cascaded", nsContext, 0);
    boolean opaque = XMLTools.getNodeAsBoolean(layerElem, "./@opaque", nsContext, false);
    boolean noSubsets = XMLTools.getNodeAsBoolean(layerElem, "./@noSubsets", nsContext, false);
    int fixedWidth = XMLTools.getNodeAsInt(layerElem, "./@fixedWidth", nsContext, 0);
    int fixedHeight = XMLTools.getNodeAsInt(layerElem, "./@fixedHeight", nsContext, 0);
    String name = XMLTools.getNodeAsString(layerElem, "./Name", nsContext, null);
    String title = XMLTools.getRequiredNodeAsString(layerElem, "./Title", nsContext);
    String layerAbstract = XMLTools.getNodeAsString(layerElem, "./Abstract", nsContext, null);
    String[] keywords = XMLTools.getNodesAsStrings(layerElem, "./KeywordList/Keyword", nsContext);
    String[] srs = XMLTools.getNodesAsStrings(layerElem, "./SRS", nsContext);

    List nl = XMLTools.getNodes(layerElem, "./BoundingBox", nsContext);
    // TODO substitue with Envelope
    LayerBoundingBox[] bboxes = null;
    if (nl.size() == 0 && parent != null) {
      // inherit BoundingBoxes from parent layer
      bboxes = parent.getBoundingBoxes();
    } else {
      bboxes = parseLayerBoundingBoxes(nl);
    }

    Element llBox = (Element) XMLTools.getNode(layerElem, "./LatLonBoundingBox", nsContext);
    Envelope llBoundingBox = null;

    if (llBox == null && parent != null) {
      // inherit LatLonBoundingBox parent layer
      llBoundingBox = parent.getLatLonBoundingBox();
    } else if (llBox != null) {
      llBoundingBox = parseLatLonBoundingBox(llBox);
    } else {
      /** Default crs = EPSG:4326 */
      CoordinateSystem crs = CRSFactory.create("EPSG:4326");
      llBoundingBox = GeometryFactory.createEnvelope(-180, -90, 180, 90, crs);
    }

    Dimension[] dimensions = parseDimensions(layerElem);

    Extent[] extents = parseExtents(layerElem);

    Attribution attribution = parseAttribution(layerElem);

    AuthorityURL[] authorityURLs = parseAuthorityURLs(layerElem);

    MetadataURL[] metadataURLs = parseMetadataURLs(layerElem);

    DataURL[] dataURLs = parseDataURL(layerElem);

    Identifier[] identifiers = parseIdentifiers(layerElem);

    FeatureListURL[] featureListURLs = parseFeatureListURL(layerElem);

    Style[] styles = parseStyles(layerElem);

    ScaleHint scaleHint = parseScaleHint(layerElem);

    Layer layer =
        new Layer(
            queryable,
            cascaded,
            opaque,
            noSubsets,
            fixedWidth,
            fixedHeight,
            name,
            title,
            layerAbstract,
            llBoundingBox,
            attribution,
            scaleHint,
            keywords,
            srs,
            bboxes,
            dimensions,
            extents,
            authorityURLs,
            identifiers,
            metadataURLs,
            dataURLs,
            featureListURLs,
            styles,
            null,
            null,
            parent);

    // get Child layers
    nl = XMLTools.getNodes(layerElem, "./Layer", nsContext);
    Layer[] layers = new Layer[nl.size()];
    for (int i = 0; i < layers.length; i++) {
      layers[i] = parseLayers((Element) nl.get(i), layer);
    }

    // set child layers
    layer.setLayer(layers);

    LOG.exiting();
    return layer;
  }