示例#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);
    }
  }
 @Override
 @Nullable
 public T getValue(ResultSet rs, int startIndex) throws SQLException {
   byte[] bytes = rs.getBytes(startIndex);
   if (bytes != null) {
     byte[] wkb;
     if (bytes[0] != 0 && bytes[0] != 1) { // decodes EWKB
       wkb = new byte[bytes.length - 32];
       System.arraycopy(bytes, 32, wkb, 0, wkb.length);
     } else {
       wkb = bytes;
     }
     WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1);
     return (T) decoder.decode(ByteBuffer.from(wkb));
   } else {
     return null;
   }
 }
 public static Geometry toGeometry(Object object) {
   if (object == null) {
     return null;
   }
   ByteBuffer buffer = null;
   if (object instanceof PGobject) {
     String pgValue = ((PGobject) object).getValue();
     if (pgValue.charAt(0) == 'S') { // /we have a Wkt value
       final WktDecoder decoder = Wkt.newDecoder(Wkt.Dialect.POSTGIS_EWKT_1);
       return decoder.decode(pgValue);
     } else {
       buffer = ByteBuffer.from(pgValue);
       final WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1);
       return decoder.decode(buffer);
     }
   }
   throw new IllegalStateException(
       "Received object of type " + object.getClass().getCanonicalName());
 }