Example #1
0
 public Object getSimplifiedShape() {
   CoordinateSequenceFactory csf = geometryFactory.getCoordinateSequenceFactory();
   if (type.isPointType()) {
     CoordinateSequence cs = csf.create(1, 2);
     cs.setOrdinate(0, 0, (minX + maxX) / 2);
     cs.setOrdinate(0, 1, (minY + maxY) / 2);
     return geometryFactory.createMultiPoint(new Point[] {geometryFactory.createPoint(cs)});
   } else if (type.isLineType()) {
     CoordinateSequence cs = csf.create(2, 2);
     cs.setOrdinate(0, 0, minX);
     cs.setOrdinate(0, 1, minY);
     cs.setOrdinate(1, 0, maxX);
     cs.setOrdinate(1, 1, maxY);
     return geometryFactory.createMultiLineString(
         new LineString[] {geometryFactory.createLineString(cs)});
   } else if (type.isPolygonType()) {
     CoordinateSequence cs = csf.create(5, 2);
     cs.setOrdinate(0, 0, minX);
     cs.setOrdinate(0, 1, minY);
     cs.setOrdinate(1, 0, minX);
     cs.setOrdinate(1, 1, maxY);
     cs.setOrdinate(2, 0, maxX);
     cs.setOrdinate(2, 1, maxY);
     cs.setOrdinate(3, 0, maxX);
     cs.setOrdinate(3, 1, minY);
     cs.setOrdinate(4, 0, minX);
     cs.setOrdinate(4, 1, minY);
     LinearRing ring = geometryFactory.createLinearRing(cs);
     return geometryFactory.createMultiPolygon(
         new Polygon[] {geometryFactory.createPolygon(ring, null)});
   } else {
     return shape();
   }
 }
Example #2
0
    public Object getSimplifiedShape(ScreenMap sm) {
      if (type.isPointType()) {
        return shape();
      }

      Class geomType = Geometry.class;
      if (type.isLineType()) {
        geomType = MultiLineString.class;
      } else if (type.isMultiPointType()) {
        geomType = MultiPoint.class;
      } else if (type.isPolygonType()) {
        geomType = MultiPolygon.class;
      }
      return sm.getSimplifiedShape(minX, minY, maxX, maxY, geometryFactory, geomType);
    }
Example #3
0
  private void init(boolean strict, GeometryFactory gf) throws IOException, ShapefileException {
    geometryFactory = gf;

    if (channel instanceof FileChannel && useMemoryMappedBuffer) {
      FileChannel fc = (FileChannel) channel;
      buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
      buffer.position(0);
      this.currentOffset = 0;
    } else {
      // force useMemoryMappedBuffer to false
      this.useMemoryMappedBuffer = false;
      // start small
      buffer = NIOUtilities.allocate(1024);
      fill(buffer, channel);
      buffer.flip();
      this.currentOffset = 0;
    }
    header = new ShapefileHeader();
    header.read(buffer, strict);
    fileShapeType = header.getShapeType();
    handler = fileShapeType.getShapeHandler(gf);
    if (handler == null) {
      throw new IOException("Unsuported shape type:" + fileShapeType);
    }

    headerTransfer = ByteBuffer.allocate(8);
    headerTransfer.order(ByteOrder.BIG_ENDIAN);

    // make sure the record end is set now...
    record.end = this.toFileOffset(buffer.position());
  }
Example #4
0
 private Double customShapeArea() {
   ScriptEngineManager factory = new ScriptEngineManager();
   ScriptEngine engine = factory.getEngineByName("JavaScript");
   coordinates.forEach((k, v) -> engine.put(k, v));
   try {
     return (Double) engine.eval(shapeType.getFormula());
   } catch (ScriptException ex) {
     return null;
   }
 }
Example #5
0
  /**
   * Transfer (by bytes) the data at the current record to the ShapefileWriter.
   *
   * @param bounds double array of length four for transfering the bounds into
   * @return The length of the record transfered in bytes
   */
  public int transferTo(ShapefileWriter writer, int recordNum, double[] bounds) throws IOException {

    buffer.position(this.toBufferOffset(record.end));
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.getInt(); // record number
    int rl = buffer.getInt();
    int mark = buffer.position();
    int len = rl * 2;

    buffer.order(ByteOrder.LITTLE_ENDIAN);
    ShapeType recordType = ShapeType.forID(buffer.getInt());

    if (recordType.isMultiPoint()) {
      for (int i = 0; i < 4; i++) {
        bounds[i] = buffer.getDouble();
      }
    } else if (recordType != ShapeType.NULL) {
      bounds[0] = bounds[1] = buffer.getDouble();
      bounds[2] = bounds[3] = buffer.getDouble();
    }

    // write header to shp and shx
    headerTransfer.position(0);
    headerTransfer.putInt(recordNum).putInt(rl).position(0);
    writer.shpChannel.write(headerTransfer);
    headerTransfer.putInt(0, writer.offset).position(0);
    writer.offset += rl + 4;
    writer.shxChannel.write(headerTransfer);

    // reset to mark and limit at end of record, then write
    int oldLimit = buffer.limit();
    buffer.position(mark).limit(mark + len);
    writer.shpChannel.write(buffer);
    buffer.limit(oldLimit);

    record.end = this.toFileOffset(buffer.position());
    record.number++;

    return len;
  }
Example #6
0
 private Double calculateShapeArea() {
   switch (shapeType.getName()) {
     case "Circle":
       return circleArea();
     case "Rectangle":
       return rectangleArea();
     case "Triangle":
       return triangleArea();
     default:
       return customShapeArea();
   }
 }
Example #7
0
  private void init(WarningListener warningListener) throws IOException, ShapefileException {
    header = readHeader(channel, warningListener);
    fileShapeType = header.getShapeType();
    handler = fileShapeType.getShapeHandler();

    // recordHeader = ByteBuffer.allocateDirect(8);
    // recordHeader.order(ByteOrder.BIG_ENDIAN);

    if (handler == null) {
      throw new IOException("Unsuported shape type:" + fileShapeType);
    }
    buffer = new ReadBufferManager(channel);
  }
Example #8
0
  /**
   * Bulk write method for writing a collection of (hopefully) like geometries of the given
   * ShapeType.
   */
  public void write(final GeometryCollection geometries, final ShapeType type)
      throws IOException, DataStoreException {
    handler = type.getShapeHandler(true);

    writeHeaders(geometries, type);

    lp = shapeBuffer.position();
    for (int i = 0, ii = geometries.getNumGeometries(); i < ii; i++) {
      Geometry g = geometries.getGeometryN(i);

      writeGeometry(g);
    }

    close();
  }
Example #9
0
  ContextMenu getContextMenu() {
    if (null != ctxMenu) {
      return ctxMenu;
    }

    ctxMenu = new ContextMenu();
    Msg.getList(this, "item.shape")
        .forEach(
            item -> {
              MenuItem menuItem = new MenuItem(item.trim());
              menuItem.setOnAction(
                  e -> {
                    GraphicsContext gc = canvas.getGraphicsContext2D();
                    gc.clearRect(0, 0, CIRCLE_RADIUS, CIRCLE_RADIUS);
                    gc.setFill(Color.TAN);
                    ShapeType.valueOf(menuItem.getText().toUpperCase()).drawWith(gc);
                  });
              ctxMenu.getItems().add(menuItem);
            });
    return ctxMenu;
  }
Example #10
0
  /**
   * Write the headers for this shapefile including the bounds, shape type, the number of geometries
   * and the total fileLength (in actual bytes, NOT 16 bit words).
   */
  public void writeHeaders(
      final Envelope bounds,
      final ShapeType type,
      final int numberOfGeometries,
      final int fileLength)
      throws IOException {

    try {
      handler = type.getShapeHandler(true);
    } catch (DataStoreException se) {
      throw new RuntimeException("unexpected Exception", se);
    }
    if (shapeBuffer == null) allocateBuffers();

    ShapefileHeader.write(
        shapeBuffer,
        type,
        fileLength / 2,
        bounds.getMinX(),
        bounds.getMinY(),
        bounds.getMaxX(),
        bounds.getMaxY());

    shx.moveToHeaderStart();
    shx.writeHeader(
        type,
        50 + 4 * numberOfGeometries,
        bounds.getMinX(),
        bounds.getMinY(),
        bounds.getMaxX(),
        bounds.getMaxY());

    offset = 50;
    this.type = type;
    cnt = 0;

    shpChannel.position(0);
    drain();
  }
Example #11
0
  /**
   * Fetch the next record information.
   *
   * @param offset
   * @throws IOException
   * @return The record instance associated with this reader.
   */
  public Geometry geomAt(int offset) throws IOException {

    // need to update position
    buffer.position(offset);

    // record header
    buffer.skip(8);

    // shape record is all little endian
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    // read the type, handlers don't need it
    ShapeType recordType = ShapeType.forID(buffer.getInt());

    // this usually happens if the handler logic is bunk,
    // but bad files could exist as well...
    if (recordType != ShapeType.NULL && recordType != fileShapeType) {
      throw new IllegalStateException(
          "ShapeType changed illegally from " + fileShapeType + " to " + recordType);
    }

    return handler.read(buffer, recordType);
  }
Example #12
0
 @Override
 public void xMLIO(XMLObject xMLObject) {
   super.xMLIO(xMLObject);
   xMLObject.manageStringAttribute("name", name);
   xMLObject.manageChildList(sprites);
 }
Example #13
0
  /**
   * Fetch the next record information.
   *
   * @throws IOException
   * @return The record instance associated with this reader.
   */
  public Record nextRecord() throws IOException {

    // need to update position
    buffer.position(getNextOffset());
    if (currentShape != UNKNOWN) currentShape++;

    // record header is big endian
    buffer.order(ByteOrder.BIG_ENDIAN);

    // read shape record header
    int recordNumber = buffer.getInt();
    // silly ESRI say contentLength is in 2-byte words
    // and ByteByffer uses bytes.
    // track the record location
    int recordLength = buffer.getInt() * 2;

    if (!buffer.isReadOnly() && !useMemoryMappedBuffer) {
      // capacity is less than required for the record
      // copy the old into the newly allocated
      if (buffer.capacity() < recordLength + 8) {
        this.currentOffset += buffer.position();
        ByteBuffer old = buffer;
        // ensure enough capacity for one more record header
        buffer = ensureCapacity(buffer, recordLength + 8, useMemoryMappedBuffer);
        buffer.put(old);
        NIOUtilities.clean(old, useMemoryMappedBuffer);
        fill(buffer, channel);
        buffer.position(0);
      } else
      // remaining is less than record length
      // compact the remaining data and read again,
      // allowing enough room for one more record header
      if (buffer.remaining() < recordLength + 8) {
        this.currentOffset += buffer.position();
        buffer.compact();
        fill(buffer, channel);
        buffer.position(0);
      }
    }

    // shape record is all little endian
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    // read the type, handlers don't need it
    ShapeType recordType = ShapeType.forID(buffer.getInt());

    // this usually happens if the handler logic is bunk,
    // but bad files could exist as well...
    if (recordType != ShapeType.NULL && recordType != fileShapeType) {
      throw new IllegalStateException(
          "ShapeType changed illegally from " + fileShapeType + " to " + recordType);
    }

    // peek at bounds, then reset for handler
    // many handler's may ignore bounds reading, but we don't want to
    // second guess them...
    buffer.mark();
    if (recordType.isMultiPoint()) {
      record.minX = buffer.getDouble();
      record.minY = buffer.getDouble();
      record.maxX = buffer.getDouble();
      record.maxY = buffer.getDouble();
    } else if (recordType != ShapeType.NULL) {
      record.minX = record.maxX = buffer.getDouble();
      record.minY = record.maxY = buffer.getDouble();
    }
    buffer.reset();

    record.offset = record.end;
    // update all the record info.
    record.length = recordLength;
    record.type = recordType;
    record.number = recordNumber;
    // remember, we read one int already...
    record.end = this.toFileOffset(buffer.position()) + recordLength - 4;
    // mark this position for the reader
    record.start = buffer.position();
    // clear any cached shape
    record.shape = null;

    return record;
  }