Refine search
@Override public Object asJSON(Object value, MaxwellOutputConfig config) { Geometry geometry = null; if ( value instanceof Geometry ) { geometry = (Geometry) value; } else if ( value instanceof byte[] ) { byte []bytes = (byte[]) value; // mysql sprinkles 4 mystery bytes on top of the GIS data. bytes = Arrays.copyOfRange(bytes, 4, bytes.length); final WKBReader reader = new WKBReader(); try { geometry = reader.read(bytes); } catch ( ParseException e ) { throw new RuntimeException("Could not parse geometry: " + e); } } else { throw new RuntimeException("Could not parse geometry column value: " + value); } return geometry.toText(); }
private List read(BufferedReader bufferedReader) throws IOException, ParseException { List geoms = new ArrayList(); while (! isAtEndOfFile(bufferedReader) && ! isAtLimit(geoms)) { String line = bufferedReader.readLine().trim(); if (line.length() == 0) continue; Geometry g = wkbReader.read(WKBReader.hexToBytes(line)); if (count >= offset) geoms.add(g); count++; } return geoms; }
/** * Converts a hexadecimal string to a byte array. * The hexadecimal digit symbols are case-insensitive. * * @param hex a string containing hex digits * @return an array of bytes with the value of the hex string */ public static byte[] hexToBytes(String hex) { int byteLen = hex.length() / 2; byte[] bytes = new byte[byteLen]; for (int i = 0; i < hex.length() / 2; i++) { int i2 = 2 * i; if (i2 + 1 > hex.length()) throw new IllegalArgumentException("Hex string has odd length"); int nib1 = hexToInt(hex.charAt(i2)); int nib0 = hexToInt(hex.charAt(i2 + 1)); byte b = (byte) ((nib1 << 4) + (byte) nib0); bytes[i] = b; } return bytes; }
public static Geometry getGeometry(InputStream is1, Integer srid, boolean allowEwkb) throws FunctionExecutionException { try { WKBReader reader = new WKBReader(); Geometry jtsGeom = reader.read(new InputStreamInStream(is1)); if (!allowEwkb && (jtsGeom.getSRID() != GeometryType.UNKNOWN_SRID || (jtsGeom.getCoordinate() != null && !Double.isNaN(jtsGeom.getCoordinate().z)))) { //don't allow ewkb - that needs an explicit function throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31160, "EWKB")); //$NON-NLS-1$ } if (srid != null) { jtsGeom.setSRID(srid); } return jtsGeom; } catch (ParseException e) { throw new FunctionExecutionException(e); } catch (IOException e) { throw new FunctionExecutionException(e); } finally { if (is1 != null) { try { is1.close(); } catch (IOException e) { } } } }
/** * Convert a WKB representation to a geometry * @param bytes the input WKB object * @param srid the input SRID * @return * @throws SQLException */ public static Geometry toGeometry(byte[] bytes, int srid) throws SQLException{ if(bytes==null) { return null; } WKBReader wkbReader = new WKBReader(); try { Geometry geometry = wkbReader.read(bytes); geometry.setSRID(srid); return geometry; } catch (ParseException ex) { throw new SQLException("Cannot parse the input bytes",ex); } }
public Geometry decodeGeometryValue(GeometryDescriptor descriptor, ResultSet rs, String column, GeometryFactory factory, Connection cx ) throws IOException, SQLException { byte[] bytes = rs.getBytes(column); if (bytes == null) { return null; } try { return new WKBReader(factory).read(bytes); } catch (ParseException e) { throw (IOException) new IOException().initCause(e); } //return JTS.geometryFromBytes( bytes ); }
@Override protected Bounds doRun(Connection cx) throws Exception { Schema schema = schema(); String sql = new SQL("SELECT st_asbinary(st_force_2d(st_extent(") .name(schema.geometry().name()).add(")))") .add(" FROM ").name(table.schema(), table.name()).toString(); LOG.debug(sql); ResultSet rs = open(open(cx.createStatement()).executeQuery(sql)); rs.next(); byte[] wkb = rs.getBytes(1); return new Bounds(new WKBReader().read(wkb).getEnvelopeInternal()); } });
import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.PrecisionModel; import com.vividsolutions.jts.io.WKBReader; import java.util.Arrays; import java.util.List; public class Foo { public static void main(String... args) throws Exception { final List<String> points = Arrays.asList( "0101000020E6100000AECB9307F9D812400F2ADCE003704940", "0101000020E6100000E40AAE6CD6DA1240941F95531C704940", "0101000020E6100000C0D7C68E7CD81240F550364044704940" ); final GeometryFactory gm = new GeometryFactory(new PrecisionModel(), 4326); final WKBReader wkbr = new WKBReader(gm); for (String point: points) { byte[] wkbBytes = wkbr.hexToBytes(point); final Geometry geom = wkbr.read(wkbBytes); System.out.printf("%s -> %s\n", point, geom.toText()); // you can access longitude and latitude via double longitude = geom.getCoordinate().x; double latitude = geom.getCoordinate().y; // then do what you need to do with it } } }
@Override public PolyLine backwardConvert(final byte[] wkb) { PolyLine polyLine = null; Geometry geometry = null; final WKBReader myReader = new WKBReader(); try { geometry = myReader.read(wkb); } catch (final ParseException e) { throw new CoreException("Cannot parse wkb : {}", WKBWriter.toHex(wkb)); } final Coordinate[] coordinates = geometry.getCoordinates(); final List<Location> locations = new ArrayList<>(); for (int i = 0; i < coordinates.length; i++) { // y = latitude, x = longitude from JTS Coordinate format locations.add(new Location(Latitude.degrees(coordinates[i].y), Longitude.degrees(coordinates[i].x))); } polyLine = new PolyLine(locations); return polyLine; }
public Geometry readWkb(String line) throws ParseException { final String[] columns = line.split(splitter.getDelimiter()); final byte[] aux = WKBReader.hexToBytes(columns[this.startOffset]); // For some unknown reasons, the wkb reader cannot be used in transient variable like the wkt reader. WKBReader wkbReader = new WKBReader(); final Geometry geometry = wkbReader.read(aux); if (carryInputData) { boolean firstColumnFlag = true; otherAttributes = ""; for (int i=0; i<columns.length;i++) { if (i!=this.startOffset ) { if (firstColumnFlag) { otherAttributes +=columns[i]; firstColumnFlag = false; } else { otherAttributes += "\t"+columns[i]; } } } geometry.setUserData(otherAttributes); } return geometry; }
public Geometry readWkb(String line) throws ParseException { final String[] columns = line.split(splitter.getDelimiter()); final byte[] aux = WKBReader.hexToBytes(columns[this.startOffset]); // For some unknown reasons, the wkb reader cannot be used in transient variable like the wkt reader. WKBReader wkbReader = new WKBReader(); final Geometry geometry = wkbReader.read(aux); handleNonSpatialDataToGeometry(geometry, Arrays.asList(columns)); return geometry; }
/** * Reads a single {@link Geometry} in WKB format from a byte array. * * @param bytes the byte array to read from * @return the geometry read * @throws ParseException if the WKB is ill-formed */ public Geometry read(byte[] bytes) throws ParseException { // possibly reuse the ByteArrayInStream? // don't throw IOExceptions, since we are not doing any I/O try { return read(new ByteArrayInStream(bytes)); } catch (IOException ex) { throw new RuntimeException("Unexpected IOException caught: " + ex.getMessage()); } }
import com.vividsolutions.jts.io.WKBReader; WKBReader wkbReader = new WKBReader(); byte[] geom = wkbReader.hexToBytes('0101000020E6100000C006335CD3043840504BBDB89EC14140'); System.out.println(wkbReader.read(geom)); // prints POINT (24.01885010000001 35.5126563)
case WKB: return string -> Optional.of(Collections.singletonList( new WkbPolygonConverter().backwardConvert(WKBReader.hexToBytes(string)))); case UNSUPPORTED: default:
public static Geometry getGeometry(InputStream is1, Integer srid, boolean allowEwkb) throws FunctionExecutionException { try { WKBReader reader = new WKBReader(); Geometry jtsGeom = reader.read(new InputStreamInStream(is1)); if (!allowEwkb && (jtsGeom.getSRID() != GeometryType.UNKNOWN_SRID || (jtsGeom.getCoordinate() != null && !Double.isNaN(jtsGeom.getCoordinate().z)))) { //don't allow ewkb - that needs an explicit function throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31160, "EWKB")); //$NON-NLS-1$ } if (srid != null) { jtsGeom.setSRID(srid); } return jtsGeom; } catch (ParseException e) { throw new FunctionExecutionException(e); } catch (IOException e) { throw new FunctionExecutionException(e); } finally { if (is1 != null) { try { is1.close(); } catch (IOException e) { } } } }
/** * Convert a WKB representation to a geometry * @param bytes the input WKB object * @param srid the input SRID * @return * @throws SQLException */ public static Geometry toGeometry(byte[] bytes, int srid) throws SQLException{ if(bytes==null) { return null; } WKBReader wkbReader = new WKBReader(); try { Geometry geometry = wkbReader.read(bytes); geometry.setSRID(srid); return geometry; } catch (ParseException ex) { throw new SQLException("Cannot parse the input bytes",ex); } }
@Override public Geometry readGeometry(DataInput in, GeometryFactory geomFac) throws IOException { int len = in.readInt(); byte[] bytes = new byte[len]; in.readFully(bytes); WKBReader wkbReader = new WKBReader(geomFac); try { return wkbReader.read(bytes); } catch (ParseException e) { throw new RuntimeException(e); } }
public void parse(String s) { /* * first see if we start with a token that matches "EPSG:[0-9]*". If so, * set the CRS from it; otherwise it is null (not the plugin default). */ if (s.startsWith("EPSG:") || s.startsWith("urn:")) { int n = s.indexOf(' '); this.crsId = s.substring(0, n); s = s.substring(n + 1); } try { if (s.contains("(")) { value = new WKTReader().read(s); } else { value = new WKBReader().read(WKBReader.hexToBytes(s)); } } catch (ParseException e) { throw new KlabRuntimeException(e); } }
/** * Creates a {@link com.vividsolutions.jts.geom.LineString} from a Well-Known-Binary (WKB) geometry. * * @param bytes the WKB geometry. * @return a {@link com.vividsolutions.jts.geom.LineString} representing the WKB geometry specified. */ public static LineString lineStringFromWKB(final byte[] bytes) { LineString lineString; try { lineString = (LineString) WKB_READER.get().read(bytes); } catch (ParseException e) { throw new IllegalStateException(e); } return lineString; } }
public PgWKBAttributeIO(boolean useByteArray) { this.useByteArray = useByteArray; wkbr = new WKBReader(); }