private static Geometry<C2D> toPolygon(Envelope env) { final PositionSequence<C2D> ps = PositionSequenceBuilders.fixedSized( 4, C2D.class ) .add( env.lowerLeft().getCoordinate( 0 ), env.lowerLeft().getCoordinate( 1 ) ) .add( env.lowerLeft().getCoordinate( 0 ), env.upperRight().getCoordinate( 1 ) ) .add( env.upperRight().getCoordinate( 0 ), env.upperRight().getCoordinate( 1 ) ) .add( env.lowerLeft().getCoordinate( 0 ), env.lowerLeft().getCoordinate( 1 ) ) .toPositionSequence(); return new Polygon<C2D>( ps, CoordinateReferenceSystems.PROJECTED_2D_METER ); }
private static ByteBuffer addCrsId(byte[] wkb, byte orderByte, int typeCode, int crsId) { ByteBuffer buffer = ByteBuffer.allocate( wkb.length + 4 ); // original capacity + 4 bytes for the CRS ID buffer.setByteOrder( ByteOrder.valueOf( orderByte ) ); buffer.put( orderByte ); // write byte order buffer.putUInt( typeCode | POSTGIS_SRID_FLAG ); // set SRID flag buffer.putInt( crsId ); // write CRS ID // write remaining data for ( int i = 5; i < wkb.length; i++ ) { buffer.put( wkb[i] ); } buffer.rewind(); return buffer; }
@Override public String toString(Geometry value) { return value.toString(); }
public static byte[] toEWKB(Geometry<?> geometry) { WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.HANA_EWKB ); ByteBuffer bytes = encoder.encode( geometry, ByteOrder.NDR ); return bytes.toByteArray(); } }
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 ); }
@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 ) ); } }
return JTS.from( (org.locationtech.jts.geom.Geometry) object ); final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); if ( object instanceof Blob ) { return decoder.decode( toByteBuffer( (Blob) object ) ); return decoder.decode( ByteBuffer.from( (byte[]) object ) ); return toPolygon( JTS.from( (org.locationtech.jts.geom.Envelope) object ) );
private PGobject toPGobject(X value, WrapperOptions options) throws SQLException { final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 ); final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options ); final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString(); final PGobject obj = new PGobject(); obj.setType( "geometry" ); obj.setValue( hexString ); return obj; }
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; }
byte orderByte = buffer.get(); int typeCode = (int) buffer.getUInt(); if ( rsSrid.next() ) { int crsId = rsSrid.getInt( 1 ); buffer = addCrsId( buffer.toByteArray(), orderByte, typeCode, crsId );
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() ); } }
@Override public boolean test(Geometry<P> geom1, Geometry<P> geom2) { return delegate.equals( geom1, geom2 ); }
public GeolatteGeometryEquality() { this( new GeometryPointEquality() ); }
/** * Encode the specified {@code Geometry} into a WKB * * @param geometry The value to encode * * @return A byte-array representing the geometry in WKB. */ public static byte[] to(Geometry geometry) { final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 ); final ByteBuffer buffer = encoder.encode( geometry, ByteOrder.NDR ); return ( buffer == null ? null : buffer.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() ); }
@Override protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.MYSQL_WKB ); final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options ); final ByteBuffer buffer = encoder.encode( geometry, ByteOrder.NDR ); final byte[] bytes = ( buffer == null ? null : buffer.toByteArray() ); st.setBytes( index, bytes ); }
@Override protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException { final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.MYSQL_WKB ); final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options ); final ByteBuffer buffer = encoder.encode( geometry, ByteOrder.NDR ); final byte[] bytes = ( buffer == null ? null : buffer.toByteArray() ); st.setBytes( name, bytes ); } };