Search in sources :

Example 1 with GeotoolsFeatureDataAdapter

use of org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter 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 GeotoolsFeatureDataAdapter

use of org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter in project geowave by locationtech.

the class GeoWaveGeometryPrecisionIT method ingestData.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void ingestData(final Geometry[] geometries, @Nullable final Integer geometryPrecision) {
    dataStore = dataStorePluginOptions.createDataStore();
    final SpatialOptions spatialOptions = new SpatialOptions();
    spatialOptions.setGeometryPrecision(geometryPrecision);
    spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(spatialOptions);
    final SpatialTemporalOptions spatialTemporalOptions = new SpatialTemporalOptions();
    spatialTemporalOptions.setGeometryPrecision(geometryPrecision);
    spatialTemporalIndex = SpatialTemporalDimensionalityTypeProvider.createIndexFromOptions(spatialTemporalOptions);
    final GeotoolsFeatureDataAdapter fda = SimpleIngest.createDataAdapter(featureType);
    final SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
    final List<SimpleFeature> features = new ArrayList<>();
    for (int i = 0; i < geometries.length; i++) {
        builder.set(GEOMETRY_ATTRIBUTE_NAME, geometries[i]);
        builder.set(TIME_ATTRIBUTE_NAME, new Date());
        features.add(builder.buildFeature(String.valueOf(i)));
    }
    dataStore.addType(fda, spatialIndex, spatialTemporalIndex);
    try (Writer writer = dataStore.createWriter(fda.getTypeName())) {
        for (final SimpleFeature feat : features) {
            writer.write(feat);
        }
    }
}
Also used : GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter) ArrayList(java.util.ArrayList) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Date(java.util.Date) Writer(org.locationtech.geowave.core.store.api.Writer) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 3 with GeotoolsFeatureDataAdapter

use of org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter in project geowave by locationtech.

the class FeatureDataUtils method getGeomField.

public static String getGeomField(final DataStorePluginOptions dataStore, final String typeName) {
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final DataTypeAdapter<?> adapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(typeName)).getAdapter();
    if ((adapter != null) && (adapter instanceof GeotoolsFeatureDataAdapter)) {
        final GeotoolsFeatureDataAdapter gtAdapter = (GeotoolsFeatureDataAdapter) adapter;
        final SimpleFeatureType featureType = gtAdapter.getFeatureType();
        if (featureType.getGeometryDescriptor() != null) {
            return featureType.getGeometryDescriptor().getLocalName();
        }
    }
    return null;
}
Also used : PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter)

Example 4 with GeotoolsFeatureDataAdapter

use of org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter in project geowave by locationtech.

the class FeatureDataUtils method getFeatureType.

public static SimpleFeatureType getFeatureType(final DataStorePluginOptions dataStore, String typeName) {
    // if no id provided, locate a single featureadapter
    if (typeName == null) {
        final List<String> typeNameList = FeatureDataUtils.getFeatureTypeNames(dataStore);
        if (typeNameList.size() >= 1) {
            typeName = typeNameList.get(0);
        } else if (typeNameList.isEmpty()) {
            LOGGER.error("No feature adapters found for use with time param");
            return null;
        } else {
            LOGGER.error("Multiple feature adapters found. Please specify one.");
            return null;
        }
    }
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final DataTypeAdapter<?> adapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(typeName)).getAdapter();
    if ((adapter != null) && (adapter instanceof GeotoolsFeatureDataAdapter)) {
        final GeotoolsFeatureDataAdapter gtAdapter = (GeotoolsFeatureDataAdapter) adapter;
        return gtAdapter.getFeatureType();
    }
    return null;
}
Also used : PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter)

Example 5 with GeotoolsFeatureDataAdapter

use of org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter in project geowave by locationtech.

the class GeoServerIngestIT method testExamplesIngest.

@Test
public void testExamplesIngest() throws Exception {
    final DataStore ds = dataStorePluginOptions.createDataStore();
    final SimpleFeatureType sft = SimpleIngest.createPointFeatureType();
    final Index spatialIdx = TestUtils.createWebMercatorSpatialIndex();
    final Index spatialTemporalIdx = TestUtils.createWebMercatorSpatialTemporalIndex();
    final GeotoolsFeatureDataAdapter fda = SimpleIngest.createDataAdapter(sft);
    final List<SimpleFeature> features = getGriddedTemporalFeatures(new SimpleFeatureBuilder(sft), 8675309);
    LOGGER.info(String.format("Beginning to ingest a uniform grid of %d features", features.size()));
    int ingestedFeatures = 0;
    final int featuresPer5Percent = features.size() / 20;
    ds.addType(fda, spatialIdx, spatialTemporalIdx);
    final BoundingBoxStatistic mercatorBounds = new BoundingBoxStatistic(fda.getTypeName(), sft.getGeometryDescriptor().getLocalName());
    mercatorBounds.setSourceCrs(fda.getFeatureType().getGeometryDescriptor().getCoordinateReferenceSystem());
    mercatorBounds.setDestinationCrs(TestUtils.CUSTOM_CRS);
    mercatorBounds.setTag("MERCATOR_BOUNDS");
    ds.addStatistic(mercatorBounds);
    try (Writer writer = ds.createWriter(fda.getTypeName())) {
        for (final SimpleFeature feat : features) {
            writer.write(feat);
            ingestedFeatures++;
            if ((ingestedFeatures % featuresPer5Percent) == 0) {
                LOGGER.info(String.format("Ingested %d percent of features", (ingestedFeatures / featuresPer5Percent) * 5));
            }
        }
    }
    final BoundingBoxValue env = ds.aggregateStatistics(StatisticQueryBuilder.newBuilder(BoundingBoxStatistic.STATS_TYPE).typeName(fda.getTypeName()).fieldName(sft.getGeometryDescriptor().getLocalName()).tag("MERCATOR_BOUNDS").build());
    TestUtils.assertStatusCode("Should Create 'testomatic' Workspace", 201, geoServerServiceClient.addWorkspace("testomatic"));
    storeServiceClient.addStoreReRoute(dataStorePluginOptions.getGeoWaveNamespace(), dataStorePluginOptions.getType(), dataStorePluginOptions.getGeoWaveNamespace(), dataStorePluginOptions.getOptionsAsMap());
    TestUtils.assertStatusCode("Should Add " + dataStorePluginOptions.getGeoWaveNamespace() + " Datastore", 201, geoServerServiceClient.addDataStore(dataStorePluginOptions.getGeoWaveNamespace(), "testomatic", dataStorePluginOptions.getGeoWaveNamespace()));
    TestUtils.assertStatusCode("Should Publish '" + ServicesTestEnvironment.TEST_STYLE_NAME_NO_DIFFERENCE + "' Style", 201, geoServerServiceClient.addStyle(ServicesTestEnvironment.TEST_SLD_NO_DIFFERENCE_FILE, ServicesTestEnvironment.TEST_STYLE_NAME_NO_DIFFERENCE));
    muteLogging();
    TestUtils.assertStatusCode("Should return 400, that layer was already added", 400, geoServerServiceClient.addStyle(ServicesTestEnvironment.TEST_SLD_NO_DIFFERENCE_FILE, ServicesTestEnvironment.TEST_STYLE_NAME_NO_DIFFERENCE));
    unmuteLogging();
    TestUtils.assertStatusCode("Should Publish '" + ServicesTestEnvironment.TEST_STYLE_NAME_MINOR_SUBSAMPLE + "' Style", 201, geoServerServiceClient.addStyle(ServicesTestEnvironment.TEST_SLD_MINOR_SUBSAMPLE_FILE, ServicesTestEnvironment.TEST_STYLE_NAME_MINOR_SUBSAMPLE));
    TestUtils.assertStatusCode("Should Publish '" + ServicesTestEnvironment.TEST_STYLE_NAME_MAJOR_SUBSAMPLE + "' Style", 201, geoServerServiceClient.addStyle(ServicesTestEnvironment.TEST_SLD_MAJOR_SUBSAMPLE_FILE, ServicesTestEnvironment.TEST_STYLE_NAME_MAJOR_SUBSAMPLE));
    TestUtils.assertStatusCode("Should Publish '" + ServicesTestEnvironment.TEST_STYLE_NAME_DISTRIBUTED_RENDER + "' Style", 201, geoServerServiceClient.addStyle(ServicesTestEnvironment.TEST_SLD_DISTRIBUTED_RENDER_FILE, ServicesTestEnvironment.TEST_STYLE_NAME_DISTRIBUTED_RENDER));
    TestUtils.assertStatusCode("Should Publish '" + SimpleIngest.FEATURE_NAME + "' Layer", 201, geoServerServiceClient.addLayer(dataStorePluginOptions.getGeoWaveNamespace(), WORKSPACE, null, null, "point"));
    if (!(ds instanceof Closeable)) {
        // this is kinda hacky, but its only for the integration test - the
        // problem is that GeoServer and this thread have different class
        // loaders so the RocksDB "singleton" instances are not shared in
        // this JVM and GeoServer currently has a lock on the datastore
        // after the previous addlayer - add layer tries to lookup adapters
        // while it does not have the lock and therefore fails
        muteLogging();
        TestUtils.assertStatusCode("Should return 400, that layer was already added", 400, geoServerServiceClient.addLayer(dataStorePluginOptions.getGeoWaveNamespace(), WORKSPACE, null, null, "point"));
        unmuteLogging();
    }
    final BufferedImage biDirectRender = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, "point", 920, 360, null, true);
    final BufferedImage ref = ImageIO.read(new File(REFERENCE_WMS_IMAGE_PATH));
    // being a little lenient because of differences in O/S rendering
    TestUtils.testTileAgainstReference(biDirectRender, ref, 0, 0.07);
    BufferedImage biSubsamplingWithoutError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_NO_DIFFERENCE, 920, 360, null, false);
    Assert.assertNotNull(ref);
    // being a little lenient because of differences in O/S rendering
    TestUtils.testTileAgainstReference(biSubsamplingWithoutError, ref, 0, 0.07);
    BufferedImage biSubsamplingWithExpectedError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_MINOR_SUBSAMPLE, 920, 360, null, false);
    TestUtils.testTileAgainstReference(biSubsamplingWithExpectedError, ref, 0.01, 0.15);
    BufferedImage biSubsamplingWithLotsOfError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_MAJOR_SUBSAMPLE, 920, 360, null, false);
    TestUtils.testTileAgainstReference(biSubsamplingWithLotsOfError, ref, 0.3, 0.4);
    final BufferedImage biDistributedRendering = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_DISTRIBUTED_RENDER, 920, 360, null, true);
    TestUtils.testTileAgainstReference(biDistributedRendering, ref, 0, 0.07);
    // Test subsampling with only the spatial-temporal index
    ds.removeIndex(spatialIdx.getName());
    ServicesTestEnvironment.getInstance().restartServices();
    biSubsamplingWithoutError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_NO_DIFFERENCE, 920, 360, null, true);
    Assert.assertNotNull(ref);
    // being a little lenient because of differences in O/S rendering
    TestUtils.testTileAgainstReference(biSubsamplingWithoutError, ref, 0, 0.071);
    biSubsamplingWithExpectedError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_MINOR_SUBSAMPLE, 920, 360, null, true);
    TestUtils.testTileAgainstReference(biSubsamplingWithExpectedError, ref, 0.01, 0.151);
    biSubsamplingWithLotsOfError = getWMSSingleTile(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), SimpleIngest.FEATURE_NAME, ServicesTestEnvironment.TEST_STYLE_NAME_MAJOR_SUBSAMPLE, 920, 360, null, true);
    TestUtils.testTileAgainstReference(biSubsamplingWithLotsOfError, ref, 0.3, 0.41);
}
Also used : BoundingBoxStatistic(org.locationtech.geowave.core.geotime.store.statistics.BoundingBoxStatistic) Closeable(java.io.Closeable) Index(org.locationtech.geowave.core.store.api.Index) BoundingBoxValue(org.locationtech.geowave.core.geotime.store.statistics.BoundingBoxStatistic.BoundingBoxValue) SimpleFeature(org.opengis.feature.simple.SimpleFeature) BufferedImage(java.awt.image.BufferedImage) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter) DataStore(org.locationtech.geowave.core.store.api.DataStore) File(java.io.File) Writer(org.locationtech.geowave.core.store.api.Writer) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Aggregations

GeotoolsFeatureDataAdapter (org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter)18 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)11 DataStore (org.locationtech.geowave.core.store.api.DataStore)9 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)9 InternalAdapterStore (org.locationtech.geowave.core.store.adapter.InternalAdapterStore)8 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)7 Index (org.locationtech.geowave.core.store.api.Index)7 InternalDataAdapter (org.locationtech.geowave.core.store.adapter.InternalDataAdapter)5 ParameterException (com.beust.jcommander.ParameterException)4 VectorQueryBuilder (org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder)4 Writer (org.locationtech.geowave.core.store.api.Writer)4 File (java.io.File)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)3 Geometry (org.locationtech.jts.geom.Geometry)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2