Ejemplo n.º 1
0
 /**
  * Appends the XML representation of the given <code>Envelope</code> (as WGS84BoundingBoxType[])
  * to the passed <code>Element</code>.
  *
  * @param root
  * @param envelope
  */
 public static void appendWgs84BoundingBox(Element root, Envelope envelope) {
   LOG.entering();
   Element wgs84BoundingBoxElement = XMLTools.appendElement(root, OWS, "ows:WGS84BoundingBox");
   XMLTools.appendElement(
       wgs84BoundingBoxElement,
       OWS,
       "ows:LowerCorner",
       envelope.getMin().getX() + " " + envelope.getMin().getY());
   XMLTools.appendElement(
       wgs84BoundingBoxElement,
       OWS,
       "ows:UpperCorner",
       envelope.getMax().getX() + " " + envelope.getMax().getY());
   LOG.exiting();
 }
 /**
  * @param node
  * @return minimum x coordinate
  * @throws GeometryException
  */
 public static String getXMin(Node node) throws GeometryException {
   if (node == null) {
     return "";
   }
   Geometry geometry = GMLGeometryAdapter.wrap((Element) node, null);
   Envelope envelope = geometry.getEnvelope();
   return Double.toString(envelope.getMin().getX());
 }
  /**
   * Calculate the rectangle that belongs to the given destination envelope in the given source
   * image.
   */
  private Object[] calcRegionRectangle(
      Envelope dstenv, Envelope srcenv, double srcwidth, double srcheight) {
    GeoTransform gt =
        new WorldToScreenTransform(
            srcenv.getMin().getX(),
            srcenv.getMin().getY(),
            srcenv.getMax().getX(),
            srcenv.getMax().getY(),
            0,
            0,
            srcwidth - 1,
            srcheight - 1);

    int minx = (int) Math.round(gt.getDestX(dstenv.getMin().getX()));
    int miny = (int) Math.round(gt.getDestY(dstenv.getMax().getY()));
    int maxx = (int) Math.round(gt.getDestX(dstenv.getMax().getX()));
    int maxy = (int) Math.round(gt.getDestY(dstenv.getMin().getY()));
    Rectangle rect = new Rectangle(minx, miny, maxx - minx + 1, maxy - miny + 1);
    LonLatEnvelope lonLatEnvelope = calcLonLatEnvelope(dstenv, getNativeSRSCode());

    return new Object[] {rect, dstenv, lonLatEnvelope};
  }
  /**
   * returns the minimum coordinate of the envelope of the geometry encoded by the passed node as a
   * double array
   *
   * @param node
   * @return the minimum coordinate of the envelope of the geometry encoded by the passed node as a
   *     double array
   * @throws GeometryException
   */
  public static String getMinAsArray(Node node) throws GeometryException {
    if (node == null) {
      return "";
    }
    Geometry geometry = GMLGeometryAdapter.wrap((Element) node, null);
    if (geometry instanceof Point) {
      return "";
    }
    Envelope env = geometry.getEnvelope();
    StringBuffer sb = new StringBuffer(100);

    Position pos = env.getMin();
    int dim = pos.getCoordinateDimension();
    double[] d = pos.getAsArray();
    for (int i = 0; i < dim - 1; i++) {
      sb.append(Double.toString(d[i])).append(' ');
    }
    sb.append(Double.toString(d[dim - 1]));

    return sb.toString();
  }
Ejemplo n.º 5
0
  /**
   * read part from ECW-file which falls within env and return this part as BufferedImage with
   * dimenions width and height
   *
   * @param env bounding box in world coordinates of requested part
   * @param width width of the returned image
   * @param height height of the returned image
   */
  public BufferedImage getBufferedImage(Envelope env, int width, int height) throws JNCSException {

    int bandlist[];
    int line, pRGBArray[] = null;

    // Setup the view parameters for the ecw file.
    bandlist = new int[ecwFile.numBands];
    for (int i = 0; i < ecwFile.numBands; i++) {
      bandlist[i] = i;
    }

    // Check if the envelope is within the area of the ecw-image
    double dWorldTLX = env.getMin().getX();
    double dWorldTLY = env.getMax().getY();

    LOG.logDebug("tlx: " + dWorldTLX + " tly: " + dWorldTLY);

    if (dWorldTLX < ecwFile.originX) dWorldTLX = ecwFile.originX;
    if (dWorldTLY > ecwFile.originY) dWorldTLY = ecwFile.originY;
    double dWorldBRX = env.getMax().getX();
    double dWorldBRY = env.getMin().getY();

    LOG.logDebug("brx: " + dWorldBRX + " bry: " + dWorldBRY);

    if (dWorldBRX > (ecwFile.originX + ((ecwFile.width - 1) * ecwFile.cellIncrementX))) // Huh?
      // ECW
      // does
      // not
      // except
      // the
      // full
      // width
      dWorldBRX = ecwFile.originX + ((ecwFile.width - 1) * ecwFile.cellIncrementX);
    if (dWorldBRY
        < (ecwFile.originY
            + (ecwFile.height * ecwFile.cellIncrementY)
            - (ecwFile.cellIncrementY / 2)))
      dWorldBRY =
          ecwFile.originY
              + (ecwFile.height * ecwFile.cellIncrementY)
              - (ecwFile.cellIncrementY / 2);

    // Work out the correct aspect for the setView call.
    // double dEnvAspect = (dWorldBRX - dWorldTLX) / (dWorldTLY - dWorldBRY);
    // double dImgAspect = (double) width / (double) height;

    LOG.logDebug("tlx: " + dWorldTLX + " tly: " + dWorldTLY);
    LOG.logDebug("brx: " + dWorldBRX + " bry: " + dWorldBRY);
    LOG.logDebug("width: " + width + " height: " + height);

    int nDatasetTLX = (int) Math.round((dWorldTLX - ecwFile.originX) / ecwFile.cellIncrementX);
    int nDatasetTLY = (int) Math.round((dWorldTLY - ecwFile.originY) / ecwFile.cellIncrementY);

    LOG.logDebug("ptlx: " + nDatasetTLX + " ptly: " + nDatasetTLY);

    int nDatasetBRX = (int) Math.round((dWorldBRX - ecwFile.originX) / ecwFile.cellIncrementX);
    int nDatasetBRY = (int) Math.round((dWorldBRY - ecwFile.originY) / ecwFile.cellIncrementY);

    LOG.logDebug("pbrx: " + nDatasetBRX + " pbry: " + nDatasetBRY);

    if (nDatasetBRX > (ecwFile.width - 1)) nDatasetBRX = ecwFile.width - 1;
    if (nDatasetBRY > (ecwFile.height - 1)) nDatasetBRY = ecwFile.height - 1;

    LOG.logDebug("pbrx: " + nDatasetBRX + " pbry: " + nDatasetBRY);

    // Check for supersampling
    int viewWidth = width;
    int viewHeight = height;
    if ((nDatasetBRX - nDatasetTLX) < viewWidth || (nDatasetBRY - nDatasetTLY) < viewHeight) {
      viewWidth = nDatasetBRX - nDatasetTLX;
      viewHeight = nDatasetBRY - nDatasetTLY;
    }
    if (viewWidth == 0) viewWidth = 1;
    if (viewHeight == 0) viewHeight = 1;

    LOG.logDebug("Width: " + width + " Height: " + height);
    LOG.logDebug("viewWidth: " + viewWidth + " viewHeight: " + viewHeight);

    // Create an image of the ecw file.
    BufferedImage ecwImage = new BufferedImage(viewWidth, viewHeight, BufferedImage.TYPE_INT_RGB);
    pRGBArray = new int[width];

    // Set the view
    ecwFile.setView(
        ecwFile.numBands,
        bandlist,
        nDatasetTLX,
        nDatasetTLY,
        nDatasetBRX,
        nDatasetBRY,
        viewWidth,
        viewHeight);

    // Read the scan lines
    for (line = 0; line < viewHeight; line++) {
      ecwFile.readLineRGBA(pRGBArray);
      ecwImage.setRGB(0, line, viewWidth, 1, pRGBArray, 0, viewWidth);
    }

    if (width != viewWidth || height != viewHeight) {
      LOG.logDebug("enlarge image");
      BufferedImage enlargedImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics g = enlargedImg.getGraphics();
      g.drawImage(ecwImage, 0, 0, width, height, 0, 0, viewWidth, viewHeight, null);
      ecwImage = enlargedImg;
      g.dispose();
    }

    return ecwImage;
  }
Ejemplo n.º 6
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);
      }
    }