Search in sources :

Example 1 with ExtractGeometryFilterVisitorResult

use of org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult in project geowave by locationtech.

the class KMeansRunner method run.

public void run() throws IOException {
    initContext();
    // Validate inputs
    if (inputDataStore == null) {
        LOGGER.error("You must supply an input datastore!");
        throw new IOException("You must supply an input datastore!");
    }
    if (isUseTime()) {
        scaledRange = KMeansUtils.setRunnerTimeParams(this, inputDataStore, typeName);
        if (scaledRange == null) {
            LOGGER.error("Failed to set time params for kmeans. Please specify a valid feature type.");
            throw new ParameterException("--useTime option: Failed to set time params");
        }
    }
    // Retrieve the feature adapters
    final VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
    List<String> featureTypeNames;
    // If provided, just use the one
    if (typeName != null) {
        featureTypeNames = new ArrayList<>();
        featureTypeNames.add(typeName);
    } else {
        // otherwise, grab all the feature adapters
        featureTypeNames = FeatureDataUtils.getFeatureTypeNames(inputDataStore);
    }
    bldr.setTypeNames(featureTypeNames.toArray(new String[0]));
    // This is required due to some funkiness in GeoWaveInputFormat
    final PersistentAdapterStore adapterStore = inputDataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = inputDataStore.createInternalAdapterStore();
    // Add a spatial filter if requested
    try {
        if (cqlFilter != null) {
            Geometry bbox = null;
            String cqlTypeName;
            if (typeName == null) {
                cqlTypeName = featureTypeNames.get(0);
            } else {
                cqlTypeName = typeName;
            }
            final short adapterId = internalAdapterStore.getAdapterId(cqlTypeName);
            final DataTypeAdapter<?> adapter = adapterStore.getAdapter(adapterId).getAdapter();
            if (adapter instanceof GeotoolsFeatureDataAdapter) {
                final String geometryAttribute = ((GeotoolsFeatureDataAdapter) adapter).getFeatureType().getGeometryDescriptor().getLocalName();
                Filter filter;
                filter = ECQL.toFilter(cqlFilter);
                final ExtractGeometryFilterVisitorResult geoAndCompareOpData = (ExtractGeometryFilterVisitorResult) filter.accept(new ExtractGeometryFilterVisitor(GeometryUtils.getDefaultCRS(), geometryAttribute), null);
                bbox = geoAndCompareOpData.getGeometry();
            }
            if ((bbox != null) && !bbox.equals(GeometryUtils.infinity())) {
                bldr.constraints(bldr.constraintsFactory().spatialTemporalConstraints().spatialConstraints(bbox).build());
            }
        }
    } catch (final CQLException e) {
        LOGGER.error("Unable to parse CQL: " + cqlFilter);
    }
    // Load RDD from datastore
    final RDDOptions kmeansOpts = new RDDOptions();
    kmeansOpts.setMinSplits(minSplits);
    kmeansOpts.setMaxSplits(maxSplits);
    kmeansOpts.setQuery(bldr.build());
    final GeoWaveRDD kmeansRDD = GeoWaveRDDLoader.loadRDD(session.sparkContext(), inputDataStore, kmeansOpts);
    // Retrieve the input centroids
    LOGGER.debug("Retrieving input centroids from RDD...");
    centroidVectors = RDDUtils.rddFeatureVectors(kmeansRDD, timeField, scaledTimeRange);
    centroidVectors.cache();
    // Init the algorithm
    final KMeans kmeans = new KMeans();
    kmeans.setInitializationMode("kmeans||");
    kmeans.setK(numClusters);
    kmeans.setMaxIterations(numIterations);
    if (epsilon > -1.0) {
        kmeans.setEpsilon(epsilon);
    }
    // Run KMeans
    LOGGER.debug("Running KMeans algorithm...");
    outputModel = kmeans.run(centroidVectors.rdd());
    LOGGER.debug("Writing results to output store...");
    writeToOutputStore();
    LOGGER.debug("Results successfully written!");
}
Also used : VectorQueryBuilder(org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) KMeans(org.apache.spark.mllib.clustering.KMeans) IOException(java.io.IOException) RDDOptions(org.locationtech.geowave.analytic.spark.RDDOptions) Geometry(org.locationtech.jts.geom.Geometry) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter) Filter(org.opengis.filter.Filter) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) ParameterException(com.beust.jcommander.ParameterException) GeoWaveRDD(org.locationtech.geowave.analytic.spark.GeoWaveRDD) CQLException(org.geotools.filter.text.cql2.CQLException) ExtractGeometryFilterVisitor(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitor)

Example 2 with ExtractGeometryFilterVisitorResult

use of org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult in project geowave by locationtech.

the class ExtractGeometryFilterVisitorTest method testCrosses.

@Test
public void testCrosses() throws CQLException, TransformException, ParseException {
    final Filter filter = CQL.toFilter(String.format("CROSSES(%s, POLYGON((0 0, 0 25, 10 25, 10 0, 0 0)))", geomAttributeName));
    final Query query = new Query("type", filter);
    final ExtractGeometryFilterVisitorResult result = (ExtractGeometryFilterVisitorResult) query.getFilter().accept(visitorWithDescriptor, null);
    final Envelope bounds = new Envelope(0, 10, 0, 25);
    final Geometry bbox = new GeometryFactory().toGeometry(bounds);
    assertTrue(bbox.equalsTopo(result.getGeometry()));
    assertTrue(result.getCompareOp() == CompareOperation.CROSSES);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) Envelope(org.locationtech.jts.geom.Envelope) Test(org.junit.Test)

Example 3 with ExtractGeometryFilterVisitorResult

use of org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult in project geowave by locationtech.

the class ExtractGeometryFilterVisitorTest method testEquals.

@Test
public void testEquals() throws CQLException, TransformException, ParseException {
    final Filter filter = CQL.toFilter(String.format("EQUALS(geom, POLYGON((0 0, 0 25, 10 25, 10 0, 0 0)))", geomAttributeName));
    final Query query = new Query("type", filter);
    final ExtractGeometryFilterVisitorResult result = (ExtractGeometryFilterVisitorResult) query.getFilter().accept(visitorWithDescriptor, null);
    final Envelope bounds = new Envelope(0, 10, 0, 25);
    final Geometry bbox = new GeometryFactory().toGeometry(bounds);
    assertTrue(bbox.equalsTopo(result.getGeometry()));
    assertTrue(result.getCompareOp() == CompareOperation.EQUALS);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) Envelope(org.locationtech.jts.geom.Envelope) Test(org.junit.Test)

Example 4 with ExtractGeometryFilterVisitorResult

use of org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult in project geowave by locationtech.

the class ExtractGeometryFilterVisitorTest method testOverlapsOrCrosses.

@Test
public void testOverlapsOrCrosses() throws CQLException, TransformException, ParseException {
    // TOUCHES geometry is completely contained within OVERLAPS geometry
    // we are testing to see if we are able to combine dissimilar geometric
    // relations correctly
    // to extract query geometry. Note, we can't combine two different
    // predicates into one but
    // we can combine geometries for the purpose of deriving linear
    // constraints
    final Filter filter = CQL.toFilter(String.format("OVERLAPS(%s, POLYGON((0 0, 0 50, 20 50, 20 0, 0 0))) OR TOUCHES(%s, POLYGON((0 0, 0 25, 10 25, 10 0, 0 0)))", geomAttributeName, geomAttributeName));
    final Query query = new Query("type", filter);
    final ExtractGeometryFilterVisitorResult result = (ExtractGeometryFilterVisitorResult) query.getFilter().accept(visitorWithDescriptor, null);
    final Envelope bounds = new Envelope(0, 20, 0, 50);
    final Geometry bbox = new GeometryFactory().toGeometry(bounds);
    assertTrue(bbox.equalsTopo(result.getGeometry()));
    assertTrue(result.getCompareOp() == null);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) Envelope(org.locationtech.jts.geom.Envelope) Test(org.junit.Test)

Example 5 with ExtractGeometryFilterVisitorResult

use of org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult in project geowave by locationtech.

the class ExtractGeometryFilterVisitorTest method testDisjoint.

@Test
public void testDisjoint() throws CQLException, TransformException, ParseException {
    final Filter filter = CQL.toFilter(String.format("DISJOINT(%s, POLYGON((0 0, 0 25, 10 25, 10 0, 0 0)))", geomAttributeName));
    final Query query = new Query("type", filter);
    final ExtractGeometryFilterVisitorResult result = (ExtractGeometryFilterVisitorResult) query.getFilter().accept(visitorWithDescriptor, null);
    // for non-inclusive filters we can't extract query geometry and
    // predicate
    // assertTrue(Double.isNaN(result.getGeometry().getArea()));
    assertTrue(result.getCompareOp() == null);
}
Also used : Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) Test(org.junit.Test)

Aggregations

ExtractGeometryFilterVisitorResult (org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult)22 Geometry (org.locationtech.jts.geom.Geometry)20 Filter (org.opengis.filter.Filter)20 Query (org.geotools.data.Query)16 Test (org.junit.Test)16 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)14 Envelope (org.locationtech.jts.geom.Envelope)13 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)2 AbstractOperation (org.geotools.coverage.processing.AbstractOperation)2 CoverageProcessor (org.geotools.coverage.processing.CoverageProcessor)2 GeotoolsFeatureDataAdapter (org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter)2 VectorQueryBuilder (org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder)2 ExtractGeometryFilterVisitor (org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitor)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)2 InvalidParameterValueException (org.opengis.parameter.InvalidParameterValueException)2