private Geometry toGeometry(byte[] bytes) { if ( bytes == null ) { return null; } final ByteBuffer buffer = ByteBuffer.from( bytes ); final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.MYSQL_WKB ); return decoder.decode( buffer ); }
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() ); } }
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.startsWith( "00" ) || pgValue.startsWith( "01" ) ) { //we have a WKB because this pgValue starts with the bit-order byte buffer = ByteBuffer.from( pgValue ); final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); return decoder.decode( buffer ); } else { return parseWkt( pgValue ); } } throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() ); }
private static ByteBuffer toByteBuffer(Object obj) { byte[] raw = null; if ( obj == null ) { return null; } if ( ( obj instanceof byte[] ) ) { raw = (byte[]) obj; } else if ( obj instanceof Blob ) { raw = toByteArray( (Blob) obj ); } else { throw new IllegalArgumentException( "Expected byte array or BLOB" ); } ByteBuffer buffer = ByteBuffer.from( raw ); buffer.setByteOrder( ByteOrder.valueOf( raw[0] ) ); return buffer; }
@Override protected Geometry decode(Object o) { return JTS.to( Wkb.fromWkb( ByteBuffer.from( (byte[]) o ) ) ); } }
return decoder.decode( ByteBuffer.from( (byte[]) object ) );
@Override protected Geometry decode(Object bytes) { if ( bytes == null ) { return null; } ByteBuffer buffer = ByteBuffer.from( (byte[]) bytes ); WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.MYSQL_WKB ); return JTS.to( decoder.decode( buffer ) ); } }
private Object readResolve() throws ObjectStreamException { return Wkb.fromWkb(ByteBuffer.from(this.buffer)); }
@Override @Nullable public Geometry getValue(ResultSet rs, int startIndex) throws SQLException { byte[] bytes = rs.getBytes(startIndex); if (bytes != null) { WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(bytes)); } else { return null; } }
@Override @Nullable public Geometry getValue(ResultSet rs, int startIndex) throws SQLException { byte[] bytes = rs.getBytes(startIndex); if (bytes != null) { WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(bytes)); } else { return null; } }
@Override public Object getObject(ResultSet rs, int columnIndex) throws SQLException { byte[] bytes=rs.getBytes(columnIndex); WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(bytes)); }
@Override public Object getObject(ResultSet rs, String columnLabel) throws SQLException { byte[] bytes=rs.getBytes(columnLabel); WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(bytes)); }
@Override @Nullable public Geometry getValue(ResultSet rs, int startIndex) throws SQLException { byte[] bytes = rs.getBytes(startIndex); if (bytes != null) { byte[] wkb = new byte[bytes.length - 4]; System.arraycopy(bytes, 4, wkb, 0, wkb.length); int srid = bytes[3] << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); // TODO make sure srid is set WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(wkb)); } else { return null; } }
@Override @Nullable public Geometry getValue(ResultSet rs, int startIndex) throws SQLException { byte[] bytes = rs.getBytes(startIndex); if (bytes != null) { byte[] wkb = new byte[bytes.length - 4]; System.arraycopy(bytes, 4, wkb, 0, wkb.length); int srid = bytes[3] << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); // TODO make sure srid is set WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(wkb)); } else { return null; } }
@Override @Nullable public Geometry 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 decoder.decode(ByteBuffer.from(wkb)); } else { return null; } }
@Override @Nullable public Geometry 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 decoder.decode(ByteBuffer.from(wkb)); } else { return null; } }
private Object toGeometry(byte[] bytes){ ByteBuffer buffer = ByteBuffer.from(bytes); WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.MYSQL_WKB); return decoder.decode(buffer); }
private Object convertInternal(Object obj){ if (obj instanceof PGobject){ String pgValue = ((PGobject)obj).getValue(); if (pgValue.charAt(0) == 'S') { WktDecoder decoder = Wkt.newDecoder(Wkt.Dialect.POSTGIS_EWKT_1); return decoder.decode(pgValue); } ByteBuffer buffer = ByteBuffer.from(pgValue); WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(buffer); } byte[] bytes=Converters.getDefault().convertObject(obj, byte[].class); WkbDecoder decoder = Wkb.newDecoder(Wkb.Dialect.POSTGIS_EWKB_1); return decoder.decode(ByteBuffer.from(bytes)); }