org.locationtech.jts.io.WKBReader Java Examples

The following examples show how to use org.locationtech.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: GeoUtils.java    From elasticsearch-plugin-geoshape with MIT License 6 votes vote down vote up
public static String exportWkbTo(BytesRef wkb, InternalGeoShape.OutputFormat output_format, GeoJsonWriter geoJsonWriter)
        throws ParseException {
    switch (output_format) {
        case WKT:
            Geometry geom = new WKBReader().read(wkb.bytes);
            return new WKTWriter().write(geom);
        case WKB:
            return WKBWriter.toHex(wkb.bytes);
        default:
            Geometry geo = new WKBReader().read(wkb.bytes);
            return geoJsonWriter.write(geo);
    }
}
 
Example #2
Source File: GeoShapeAggregator.java    From elasticsearch-plugin-geoshape with MIT License 6 votes vote down vote up
public GeoShapeAggregator(
        String name,
        AggregatorFactories factories,
        SearchContext context,
        ValuesSource valuesSource,
        InternalGeoShape.OutputFormat output_format,
        boolean must_simplify,
        int zoom,
        GeoShape.Algorithm algorithm,
        BucketCountThresholds bucketCountThresholds,
        Aggregator parent,
        List<PipelineAggregator> pipelineAggregators,
        Map<String, Object> metaData
) throws IOException {
    super(name, factories, context, parent, pipelineAggregators, metaData);
    this.valuesSource = valuesSource;
    this.output_format = output_format;
    this.must_simplify = must_simplify;
    this.zoom = zoom;
    this.algorithm = algorithm;
    bucketOrds = new BytesRefHash(1, context.bigArrays());
    this.bucketCountThresholds = bucketCountThresholds;

    this.wkbReader = new WKBReader();
    this.geometryFactory = new GeometryFactory();
}
 
Example #3
Source File: GeometryUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a byte array as well-known binary to a JTS geometry
 *
 * @param binary The well known binary
 * @return The JTS geometry
 */
public static Geometry geometryFromBinary(
    final byte[] binary,
    final @Nullable Integer precision,
    final byte serializationVersion) {
  if (serializationVersion < FieldUtils.SERIALIZATION_VERSION) {
    try {
      return new WKBReader().read(binary);
    } catch (final ParseException e) {
      LOGGER.warn("Unable to deserialize geometry data", e);
      throw new GeoWaveSerializationException(e);
    }
  }

  return geometryFromBinary(binary, precision);
}
 
Example #4
Source File: Geometry.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
    readMetadata(in);
    
    byte[] wellKnownBinary = WritableUtils.readCompressedByteArray(in);
    try {
        geometry = new WKBReader().read(wellKnownBinary);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Cannot parse the geometry", e);
    }
    validate();
}
 
Example #5
Source File: Geometry.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void read(Kryo kryo, Input input) {
    readMetadata(kryo, input);
    int wkbLength = input.read();
    byte[] wellKnownBinary = new byte[wkbLength];
    input.read(wellKnownBinary);
    try {
        geometry = new WKBReader().read(wellKnownBinary);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Cannot parse the geometry", e);
    }
}
 
Example #6
Source File: GeoPkgGeomReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
protected Geometry read() throws IOException { // header must be read!
    // read the geometry
    try {
        WKBReader wkbReader = new WKBReader(factory);
        Geometry g = wkbReader.read(input);
        g.setSRID(header.getSrid());
        return g;
    } catch (ParseException e) {
        throw new IOException(e);
    }
}
 
Example #7
Source File: AccumuloRangeQueryTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies equality for interning is still working as expected (topologically), as the the
 * largeQuery() test has a dependency on this;
 *
 * @throws ParseException
 */
@Test
public void testInterning() throws ParseException {
  final Geometry g =
      GeometryUtils.GEOMETRY_FACTORY.createPolygon(
          new Coordinate[] {
              new Coordinate(0, 0),
              new Coordinate(1, 0),
              new Coordinate(1, 1),
              new Coordinate(0, 1),
              new Coordinate(0, 0)});
  final Geometry gNewInstance =
      GeometryUtils.GEOMETRY_FACTORY.createPolygon(
          new Coordinate[] {
              new Coordinate(0, 0),
              new Coordinate(1, 0),
              new Coordinate(1, 1),
              new Coordinate(0, 1),
              new Coordinate(0, 0)});
  final WKBWriter wkbWriter = new WKBWriter();
  final byte[] b = wkbWriter.write(g);
  final byte[] b2 = new byte[b.length];
  System.arraycopy(b, 0, b2, 0, b.length);
  final WKBReader wkbReader = new WKBReader();
  final Geometry gSerialized = wkbReader.read(b);
  final Geometry gSerializedArrayCopy = wkbReader.read(b2);

  Assert.assertEquals(g, gNewInstance);
  Assert.assertEquals(g, gSerializedArrayCopy);
  Assert.assertEquals(gSerialized, gSerializedArrayCopy);
  Assert.assertEquals(gSerialized, gSerializedArrayCopy);
}
 
Example #8
Source File: GeometryUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a byte array as well-known binary to a JTS geometry
 *
 * @param binary The well known binary
 * @return The JTS geometry
 */
public static Geometry geometryFromBinary(
    final byte[] binary,
    final @Nullable Integer precision) {
  try {
    if (precision == null) {
      return new WKBReader().read(binary);
    }
    return new TWKBReader().read(binary);
  } catch (final ParseException e) {
    throw new GeoWaveSerializationException("Unable to deserialize geometry data", e);
  }
}
 
Example #9
Source File: GeoWaveGrpcVectorService.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void spatialQuery(
    final SpatialQueryParametersProtos request,
    final StreamObserver<FeatureProtos> responseObserver) {

  final String storeName = request.getBaseParams().getStoreName();
  final StoreLoader storeLoader = new StoreLoader(storeName);

  String typeName = request.getBaseParams().getTypeName();
  String indexName = request.getBaseParams().getIndexName();
  VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
  if (typeName.equalsIgnoreCase("")) {
    typeName = null;
  } else {
    bldr = bldr.addTypeName(typeName);
  }
  if (indexName.equalsIgnoreCase("")) {
    indexName = null;
  } else {
    bldr = bldr.indexName(indexName);
  }

  // first check to make sure the data store exists
  if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
    throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
  }

  final DataStore dataStore = storeLoader.createDataStore();

  Geometry queryGeom = null;

  try {
    queryGeom =
        new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(
            request.getGeometry().toByteArray());
  } catch (final FactoryRegistryException | org.locationtech.jts.io.ParseException e) {
    LOGGER.error("Exception encountered creating query geometry", e);
  }

  try (final CloseableIterator<SimpleFeature> iterator =
      dataStore.query(
          bldr.constraints(
              bldr.constraintsFactory().spatialTemporalConstraints().spatialConstraints(
                  queryGeom).build()).build())) {
    while (iterator.hasNext()) {
      final SimpleFeature simpleFeature = iterator.next();
      final SimpleFeatureType type = simpleFeature.getType();
      final FeatureProtos.Builder b = FeatureProtos.newBuilder();
      final FeatureAttributeProtos.Builder attBuilder = FeatureAttributeProtos.newBuilder();

      for (int i = 0; i < type.getAttributeDescriptors().size(); i++) {
        setAttributeBuilderValue(simpleFeature.getAttribute(i), attBuilder);
        b.putAttributes(type.getAttributeDescriptors().get(i).getLocalName(), attBuilder.build());
      }
      final FeatureProtos f = b.build();
      responseObserver.onNext(f);
    }
    responseObserver.onCompleted();
  }
}
 
Example #10
Source File: GeoWaveGrpcVectorService.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void spatialTemporalQuery(
    final SpatialTemporalQueryParametersProtos request,
    final StreamObserver<FeatureProtos> responseObserver) {

  final String storeName = request.getSpatialParams().getBaseParams().getStoreName();
  final StoreLoader storeLoader = new StoreLoader(storeName);

  // first check to make sure the data store exists
  if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
    throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
  }

  final DataStore dataStore = storeLoader.createDataStore();
  VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();

  String typeName = request.getSpatialParams().getBaseParams().getTypeName();
  String indexName = request.getSpatialParams().getBaseParams().getIndexName();

  if (typeName.equalsIgnoreCase("")) {
    typeName = null;
  } else {
    bldr = bldr.addTypeName(typeName);
  }
  if (indexName.equalsIgnoreCase("")) {
    indexName = null;
  } else {
    bldr = bldr.indexName(indexName);
  }

  final int constraintCount = request.getTemporalConstraintsCount();
  SpatialTemporalConstraintsBuilder stBldr =
      bldr.constraintsFactory().spatialTemporalConstraints();
  for (int i = 0; i < constraintCount; i++) {
    final TemporalConstraintsProtos t = request.getTemporalConstraints(i);
    stBldr.addTimeRange(
        Interval.of(
            Instant.ofEpochMilli(Timestamps.toMillis(t.getStartTime())),
            Instant.ofEpochMilli(Timestamps.toMillis(t.getEndTime()))));
  }

  Geometry queryGeom = null;

  try {
    queryGeom =
        new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(
            request.getSpatialParams().getGeometry().toByteArray());
    stBldr = stBldr.spatialConstraints(queryGeom);

    stBldr =
        stBldr.spatialConstraintsCompareOperation(
            CompareOperation.valueOf(request.getCompareOperation()));
  } catch (final FactoryRegistryException | org.locationtech.jts.io.ParseException e) {
    LOGGER.error("Exception encountered creating query geometry", e);
  }

  try (final CloseableIterator<SimpleFeature> iterator =
      dataStore.query(bldr.constraints(stBldr.build()).build())) {
    while (iterator.hasNext()) {
      final SimpleFeature simpleFeature = iterator.next();
      final SimpleFeatureType type = simpleFeature.getType();
      final FeatureProtos.Builder b = FeatureProtos.newBuilder();
      final FeatureAttributeProtos.Builder attBuilder = FeatureAttributeProtos.newBuilder();

      for (int i = 0; i < type.getAttributeDescriptors().size(); i++) {
        setAttributeBuilderValue(simpleFeature.getAttribute(i), attBuilder);
        b.putAttributes(type.getAttributeDescriptors().get(i).getLocalName(), attBuilder.build());
      }
      final FeatureProtos f = b.build();
      responseObserver.onNext(f);
    }
    responseObserver.onCompleted();
  }
}