예제 #1
0
  /**
   * Decode the object into a {@code Geometry}
   *
   * @param object The object to decode
   * @return The {@code Geometry}
   */
  public static Geometry from(Object object) {
    if (object == null) {
      return null;
    }
    try {

      if (object instanceof com.vividsolutions.jts.geom.Geometry) {
        return JTS.from((com.vividsolutions.jts.geom.Geometry) object);
      }
      final WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1);
      if (object instanceof Blob) {
        return decoder.decode(toByteBuffer((Blob) object));
      } else if (object instanceof byte[]) {
        return decoder.decode(ByteBuffer.from((byte[]) object));
      } else if (object instanceof com.vividsolutions.jts.geom.Envelope) {
        return toPolygon(JTS.from((com.vividsolutions.jts.geom.Envelope) object));
      } else {
        throw new IllegalArgumentException(
            "Can't convert database object of type " + object.getClass().getCanonicalName());
      }
    } catch (Exception e) {
      LOGGER.warn("Could not convert database object to a Geometry.");
      throw new HibernateException(e);
    }
  }
예제 #2
0
  private static ByteBuffer toByteBuffer(Blob blob) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final byte[] buf = new byte[1024];

    InputStream in = null;
    try {
      in = blob.getBinaryStream();
      int n = 0;
      while ((n = in.read(buf)) >= 0) {
        baos.write(buf, 0, n);
      }
    } catch (Exception e) {
      LOGGER.warn("Could not convert database BLOB object to binary stream.", e);
    } finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (IOException e) {
        LOGGER.warn("Could not close binary stream.");
      }
    }
    return ByteBuffer.from(baos.toByteArray());
  }