com.vividsolutions.jts.io.WKBReader Java Examples

The following examples show how to use com.vividsolutions.jts.io.WKBReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoWKB.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void doEvaluate(TExecutionContext context, LazyList<? extends ValueSource> inputs, ValueTarget output) {
    byte[] data = new byte[0];

    if (inputs.get(0).hasAnyValue()) {
        Object o = inputs.get(0).getObject();
        if (o instanceof BlobRef) {
            BlobRef blob;
            blob = (BlobRef) o;
            String mode = context.getQueryContext().getStore().getConfig().getProperty(AkBlob.RETURN_UNWRAPPED);
            if (mode.equalsIgnoreCase(AkBlob.UNWRAPPED)){
                data = blob.getBytes();
            }
            else {
                if (blob.isShortLob()) {
                    data = blob.getBytes();
                } else {
                    LobService ls = context.getQueryContext().getServiceManager().getServiceByClass(LobService.class);
                    data = ls.readBlob(context.getQueryContext().getSession(), blob.getId());
                }
            }
        } else if (o instanceof byte[]) {
            data = (byte[])o;
        }
    }
    
    WKBReader reader = (WKBReader)context.preptimeObjectAt(READER_CONTEXT_POS);
    try {
        Geometry geometry = reader.read(data);
        output.putObject(geometry);
    } catch(ParseException e) {
        throw new InvalidSpatialObjectException(e.getMessage());
    }
}
 
Example #2
Source File: GeometrySerializer.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void setFactory(GeometryFactory factory) {
	if(factory == null){
		throw new IllegalArgumentException();
	}
	
	sFactory = factory;
	sReader = new WKBReader(sFactory);
}
 
Example #3
Source File: GeometrySerializer.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry instantiate(SerializationStreamReader streamReader)
		throws SerializationException {
	WKBReader reader = sReader;
	
	String hex = streamReader.readString();
	byte[] wkb = WKBReader.hexToBytes(hex);
	
	
	try {
		Geometry g = reader.read(wkb);
		return g;
	} catch (ParseException e) {
		throw new SerializationException(e);
	}
}
 
Example #4
Source File: IOUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry readWKBHexString(String wkb, GeometryFactory geomFact)
throws ParseException, IOException 
{
  WKBReader reader = new WKBReader(geomFact);
  WKBHexFileReader fileReader = new WKBHexFileReader(new StringReader(wkb), reader);
  List geomList = fileReader.read();
  
  if (geomList.size() == 1)
    return (Geometry) geomList.get(0);
  
  return geomFact.createGeometryCollection(GeometryFactory.toGeometryArray(geomList));
}
 
Example #5
Source File: SimpleDemo.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void demonstrateWkb() {
	GeometryFactory gf = new GeometryFactory();

	WKBReader wkbReader = new WKBReader(gf);
	WKBWriter wkbWriter = new WKBWriter();

	// geometry collection from above
	String hexEncodedWkb = "00000000070000000600000000014132D53A3BC2DADC414"
			+ "10BBD1DFB613500000000040000000200000000014132D53A3BC2DADC414"
			+ "10BBD1DFB61350000000001415026FE8EF0B6B74153F78BCEFDB09A00000"
			+ "00002000000024132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B"
			+ "74153F78BCEFDB09A0000000005000000020000000002000000024132D53"
			+ "A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B74153F78BCEFDB09A000"
			+ "0000002000000024132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B"
			+ "6B74153F78BCEFDB09A000000000300000001000000044132D53A3BC2DAD"
			+ "C41410BBD1DFB6135415026FE8EF0B6B74153F78BCEFDB09A4157C81E8EF"
			+ "0B6B7415F693E8EFDB09A4132D53A3BC2DADC41410BBD1DFB61350000000"
			+ "00600000002000000000300000001000000044132D53A3BC2DADC41410BB"
			+ "D1DFB6135415026FE8EF0B6B74153F78BCEFDB09A4157C81E8EF0B6B7415"
			+ "F693E8EFDB09A4132D53A3BC2DADC41410BBD1DFB6135000000000300000"
			+ "001000000044132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B74"
			+ "153F78BCEFDB09A4157C81E8EF0B6B7415F693E8EFDB09A4132D53A3BC2D"
			+ "ADC41410BBD1DFB6135";

	try {
		Geometry g = wkbReader.read(WKBReader.hexToBytes(hexEncodedWkb));

		sLogger.info("Geom from WKB: " + g);

		byte[] freshWkb = wkbWriter.write(g);
		String freshWkbHex = WKBWriter.toHex(freshWkb);

		sLogger.warning("Hexes are equal?   "
				+ hexEncodedWkb.equals(freshWkbHex));
	} catch (ParseException e) {
		sLogger.log(Level.WARNING, "Unable to parse hex wkb", e);
	}
}
 
Example #6
Source File: GeoWKB.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object inside = stack.pop();
  Object pcterror = stack.pop();
  Object wkb = stack.pop();

  if (!(wkb instanceof byte[]) || !(inside instanceof Boolean) || (!(pcterror instanceof Double) && !(pcterror instanceof Long))) {
    throw new WarpScriptException(getName() + " expects a WKB byte array, an error percentage or resolution (even number between 2 and 30) and a boolean as the top 3 elements of the stack.");
  }

  // Check the resolution is even and in 2..30, if relevant
  if (pcterror instanceof Long) {
    long res = ((Number) pcterror).longValue();
    if (1 == (res % 2) || res > 30 || res < 2) {
      throw new WarpScriptException(getName() + " expects the resolution to be an even number between 2 and 30");
    }
  }

  //
  // Read WKB
  //

  WKBReader reader = new WKBReader();

  Geometry geometry = null;

  try {
    byte[] bytes = (byte[]) wkb;
    geometry = reader.read(bytes);
  } catch (ParseException pe) {
    throw new WarpScriptException(pe);
  }

  //
  // Apply buffer if defined
  //
  Map<Object,Object> buffer = (Map<Object,Object>) stack.getAttribute(GEOBUFFER.ATTR_GEOBUFFER);
  
  if (null != buffer) {
    // Clear the buffer
    stack.setAttribute(GEOBUFFER.ATTR_GEOBUFFER, null);
    // Apply the buffer operation
    BufferOp bop = new BufferOp(geometry, (BufferParameters) buffer.get(GEOBUFFER.KEY_PARAMS));
    geometry = bop.getResultGeometry(((Double) buffer.get(GEOBUFFER.KEY_DIST)).doubleValue());
  }
  
  //
  // Convert Geometry to a GeoXPShape
  //

  int maxcells = ((Number) stack.getAttribute(WarpScriptStack.ATTRIBUTE_MAX_GEOCELLS)).intValue();
  Object shape = null;

  if (!this.uniform) {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  } else {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  }

  if (null == shape) {
    throw new WarpScriptException("Maximum number of cells exceeded in a geographic shape (warpscript.maxgeocells=" + maxcells + ")");
  }

  stack.push(shape);
  return stack;
}
 
Example #7
Source File: GeoWKB.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void finishPreptimePhase(TPreptimeContext context) {
    context.set(READER_CONTEXT_POS, new WKBReader());
}
 
Example #8
Source File: SqlUtils.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
static Geometry read(final byte[] b) throws IOException, ParseException {
	final WKBReader wkb = new WKBReader();
	final Geometry geom = wkb.read(b);
	return geom;
}
 
Example #9
Source File: SqlUtils.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
static Geometry InputStream2Geometry(final InputStream inputStream) throws Exception {
	Geometry dbGeometry = null;
	if (inputStream != null) {
		// convert the stream to a byte[] array
		// so it can be passed to the WKBReader
		final byte[] buffer = new byte[255];
		int bytesRead = 0;
		final ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((bytesRead = inputStream.read(buffer)) != -1) {
			baos.write(buffer, 0, bytesRead);
		}

		final byte[] geometryAsBytes = baos.toByteArray();

		if (geometryAsBytes.length < 5) {
			throw new Exception("Invalid geometry inputStream - less than five bytes");
		}

		// first four bytes of the geometry are the SRID,
		// followed by the actual WKB. Determine the SRID
		// here
		final byte[] sridBytes = new byte[4];
		System.arraycopy(geometryAsBytes, 0, sridBytes, 0, 4);
		final boolean bigEndian = geometryAsBytes[4] == 0x00;

		int srid = 0;
		if (bigEndian) {
			for (final byte sridByte : sridBytes) {
				srid = (srid << 8) + (sridByte & 0xff);
			}
		} else {
			for (int i = 0; i < sridBytes.length; i++) {
				srid += (sridBytes[i] & 0xff) << 8 * i;
			}
		}

		// use the JTS WKBReader for WKB parsing
		final WKBReader wkbReader = new WKBReader();

		// copy the byte array, removing the first four
		// SRID bytes
		final byte[] wkb = new byte[geometryAsBytes.length - 4];
		System.arraycopy(geometryAsBytes, 4, wkb, 0, wkb.length);
		dbGeometry = wkbReader.read(wkb);
		dbGeometry.setSRID(srid);
	}

	return dbGeometry;
}
 
Example #10
Source File: GeometrySerializer.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static WKBReader getReader() {
	return sReader;
}