/**
   * 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;
  }
  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;
  }
  /** return the LonLatEnvelope of the entire image in "EPSG:4326" */
  private Envelope getLLEAsEnvelope() {
    String code = getNativeSRSCode();
    LonLatEnvelope lle = description.getLonLatEnvelope();
    Envelope tmp =
        GeometryFactory.createEnvelope(
            lle.getMin().getX(),
            lle.getMin().getY(),
            lle.getMax().getX(),
            lle.getMax().getY(),
            null);
    try {
      if (!code.equals("EPSG:4326")) {
        GeoTransformer trans = new GeoTransformer(code);
        tmp = trans.transform(tmp, "EPSG:4326");
      }
    } catch (Exception e) {
      LOGGER.logError(StringTools.stackTraceToString(e));
    }

    return tmp;
  }
Example #4
0
    public void run() {
      Container container = getContentPane();
      container.removeAll();
      container.setLayout(new GridLayout(3, 1));

      int features = shapeFile.getRecordNum();
      container.add(new JLabel("Indexing..."));
      JProgressBar progressBar = new JProgressBar(1, features);
      progressBar.setStringPainted(true);
      container.add(progressBar);
      cancel = new JButton("Cancel");
      cancel.addActionListener(frame);
      container.add(cancel);
      pack();

      boolean geometry = false;
      DBaseIndex[] index = new DBaseIndex[properties.length];
      RTree rtree = null;

      try {
        String[] dataTypes = shapeFile.getDataTypes();
        int[] lengths = shapeFile.getDataLengths();

        if (geometryCheckBox.isSelected() && !hasGeometry) {
          geometry = true;
          rtree = new RTree(2, 11, fileName + ".rti");
        }

        boolean indexes = false;
        for (int i = 0; i < index.length; i++) {
          if (checkboxes[i].isSelected() && !hasIndex[i]) {
            index[i] =
                DBaseIndex.createIndex(
                    fileName + "$" + properties[i],
                    properties[i],
                    lengths[i],
                    uniqueBoxes[i].isSelected(),
                    (dataTypes[i].equalsIgnoreCase("N")
                        || dataTypes[i].equalsIgnoreCase("I")
                        || dataTypes[i].equalsIgnoreCase("F")));
            indexes = true;
          } else index[i] = null;
        }

        if (geometry || indexes) {
          for (int i = 1; i < features + 1; i++) {
            Feature feature = shapeFile.getFeatureByRecNo(i);

            if (geometry) {
              Geometry[] geometries = feature.getGeometryPropertyValues();
              if (geometries.length == 0) {
                LOG.logInfo("no geometries at recno" + i);
                continue;
              }
              Envelope envelope = null;
              // TODO: deal with more than one geometry; handle geometry=null (allowed
              // in shapefile)
              envelope = (feature.getDefaultGeometryPropertyValue()).getEnvelope();
              if (envelope == null) { // assume a Point-geometry
                Point pnt = (Point) geometries[0];
                envelope =
                    GeometryFactory.createEnvelope(
                        pnt.getX(), pnt.getY(), pnt.getX(), pnt.getY(), null);
              }
              HyperBoundingBox box =
                  new HyperBoundingBox(
                      new HyperPoint(envelope.getMin().getAsArray()),
                      new HyperPoint(envelope.getMax().getAsArray()));
              rtree.insert(new Integer(i), box);
            }

            for (int j = 0; j < index.length; j++) {
              if (index[j] != null) {
                QualifiedName qn = new QualifiedName(properties[j]);
                index[j].addKey((Comparable) feature.getDefaultProperty(qn), i);
              }
            }

            progressBar.setValue(i);

            synchronized (this) {
              if (stop) {
                shapeFile.close();
                if (geometry) {
                  rtree.close();
                  new File(fileName + ".rti").delete();
                }
                for (int j = 0; j < index.length; j++) {
                  if (index[j] != null) {
                    index[j].close();
                    new File(fileName + "$" + properties[j] + ".ndx").delete();
                  }
                }
                System.exit(3);
              }
            }
          }
        }

        try {
          if (geometry) {
            rtree.close();
          }
          shapeFile.close();

          for (int i = 0; i < index.length; i++) if (index[i] != null) index[i].close();
        } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(frame, e);
          System.exit(1);
        }

        if (!geometryCheckBox.isSelected() && hasGeometry) {
          new File(fileName + ".rti").delete();
        }

        for (int i = 0; i < index.length; i++) {
          if (!checkboxes[i].isSelected() && hasIndex[i]) {
            new File(fileName + "$" + properties[i] + ".ndx").delete();
          }
        }

        System.exit(0);
      } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(frame, ex);
        System.exit(1);
      }
    }
  /**
   * 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;
  }