Search in sources :

Example 1 with FeatureDataAdapter

use of org.locationtech.geowave.adapter.vector.FeatureDataAdapter in project geowave by locationtech.

the class KMeansUtils method writeClusterHulls.

public static DataTypeAdapter writeClusterHulls(final JavaRDD<Vector> inputCentroids, final KMeansModel clusterModel, final DataStorePluginOptions outputDataStore, final String hullAdapterName, final boolean computeMetadata) {
    final JavaPairRDD<Integer, Iterable<Vector>> groupByRdd = KMeansHullGenerator.groupByIndex(inputCentroids, clusterModel);
    final JavaPairRDD<Integer, Geometry> hullRdd = KMeansHullGenerator.generateHullsRDD(groupByRdd);
    final SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
    typeBuilder.setName(hullAdapterName);
    typeBuilder.setNamespaceURI(BasicFeatureTypes.DEFAULT_NAMESPACE);
    try {
        typeBuilder.setCRS(CRS.decode("EPSG:4326", true));
    } catch (final FactoryException e) {
        LOGGER.error(e.getMessage(), e);
    }
    final AttributeTypeBuilder attrBuilder = new AttributeTypeBuilder();
    typeBuilder.add(attrBuilder.binding(Geometry.class).nillable(false).buildDescriptor(Geometry.class.getName().toString()));
    typeBuilder.add(attrBuilder.binding(Integer.class).nillable(false).buildDescriptor("ClusterIndex"));
    typeBuilder.add(attrBuilder.binding(Integer.class).nillable(false).buildDescriptor("Count"));
    typeBuilder.add(attrBuilder.binding(Double.class).nillable(false).buildDescriptor("Area"));
    typeBuilder.add(attrBuilder.binding(Double.class).nillable(false).buildDescriptor("Density"));
    final SimpleFeatureType sfType = typeBuilder.buildFeatureType();
    final SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sfType);
    final FeatureDataAdapter featureAdapter = new FeatureDataAdapter(sfType);
    final DataStore featureStore = outputDataStore.createDataStore();
    final Index featureIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
    final PolygonAreaCalculator polyCalc = (computeMetadata ? new PolygonAreaCalculator() : null);
    featureStore.addType(featureAdapter, featureIndex);
    try (Writer writer = featureStore.createWriter(featureAdapter.getTypeName())) {
        for (final Tuple2<Integer, Geometry> hull : hullRdd.collect()) {
            final Integer index = hull._1;
            final Geometry geom = hull._2;
            sfBuilder.set(Geometry.class.getName(), geom);
            sfBuilder.set("ClusterIndex", index);
            int count = 0;
            double area = 0.0;
            double density = 0.0;
            if (computeMetadata) {
                for (final Iterable<Vector> points : groupByRdd.lookup(index)) {
                    final Vector[] pointVec = Iterables.toArray(points, Vector.class);
                    count += pointVec.length;
                }
                try {
                    // HP Fortify "NULL Pointer Dereference" false positive
                    // Exception handling will catch if polyCalc is null
                    area = polyCalc.getAreaDensify(geom);
                    density = count / area;
                } catch (final Exception e) {
                    LOGGER.error("Problem computing polygon area: " + e.getMessage());
                }
            }
            sfBuilder.set("Count", count);
            sfBuilder.set("Area", area);
            sfBuilder.set("Density", density);
            final SimpleFeature sf = sfBuilder.buildFeature("Hull-" + index);
            writer.write(sf);
        }
    }
    return featureAdapter;
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) FactoryException(org.opengis.referencing.FactoryException) Index(org.locationtech.geowave.core.store.api.Index) AttributeTypeBuilder(org.geotools.feature.AttributeTypeBuilder) DataStore(org.locationtech.geowave.core.store.api.DataStore) Vector(org.apache.spark.mllib.linalg.Vector) PolygonAreaCalculator(org.locationtech.geowave.adapter.vector.util.PolygonAreaCalculator) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) FactoryException(org.opengis.referencing.FactoryException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(org.locationtech.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) Writer(org.locationtech.geowave.core.store.api.Writer) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with FeatureDataAdapter

use of org.locationtech.geowave.adapter.vector.FeatureDataAdapter in project geowave by locationtech.

the class DBScanMapReduceTest method setUp.

@Before
public void setUp() throws IOException {
    mapDriver = MapDriver.newMapDriver(nnMapper);
    reduceDriver = ReduceDriver.newReduceDriver(nnReducer);
    mapDriver.getConfiguration().set(GeoWaveConfiguratorBase.enumToConfKey(NNMapReduce.class, PartitionParameters.Partition.DISTANCE_THRESHOLDS), "10,10");
    reduceDriver.getConfiguration().setDouble(GeoWaveConfiguratorBase.enumToConfKey(NNMapReduce.class, PartitionParameters.Partition.MAX_DISTANCE), 10);
    ftype = AnalyticFeature.createGeometryFeatureAdapter("centroid", new String[] { "extra1" }, BasicFeatureTypes.DEFAULT_NAMESPACE, ClusteringUtils.CLUSTERING_CRS).getFeatureType();
    reduceDriver.getConfiguration().setClass(GeoWaveConfiguratorBase.enumToConfKey(DBScanMapReduce.class, HullParameters.Hull.PROJECTION_CLASS), SimpleFeatureProjection.class, Projection.class);
    final FeatureDataAdapter adapter = new FeatureDataAdapter(ftype);
    JobContextAdapterStore.addDataAdapter(mapDriver.getConfiguration(), adapter);
    JobContextAdapterStore.addDataAdapter(reduceDriver.getConfiguration(), adapter);
    JobContextInternalAdapterStore.addTypeName(mapDriver.getConfiguration(), adapter.getTypeName(), adapterId);
    JobContextInternalAdapterStore.addTypeName(reduceDriver.getConfiguration(), adapter.getTypeName(), adapterId);
    serializations();
}
Also used : FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) NNMapReduce(org.locationtech.geowave.analytic.mapreduce.nn.NNMapReduce) Before(org.junit.Before)

Example 3 with FeatureDataAdapter

use of org.locationtech.geowave.adapter.vector.FeatureDataAdapter in project geowave by locationtech.

the class SqlResultsWriter method writeResults.

public void writeResults(String typeName) {
    if (typeName == null) {
        typeName = DEFAULT_TYPE_NAME;
        LOGGER.warn("Using default type name (adapter id): '" + DEFAULT_TYPE_NAME + "' for SQL output");
    }
    final StructType schema = results.schema();
    final SimpleFeatureType featureType = SchemaConverter.schemaToFeatureType(schema, typeName);
    final SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(featureType);
    final FeatureDataAdapter featureAdapter = new FeatureDataAdapter(featureType);
    final DataStore featureStore = outputDataStore.createDataStore();
    final Index featureIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
    featureStore.addType(featureAdapter, featureIndex);
    try (Writer writer = featureStore.createWriter(featureAdapter.getTypeName())) {
        final List<Row> rows = results.collectAsList();
        for (int r = 0; r < rows.size(); r++) {
            final Row row = rows.get(r);
            for (int i = 0; i < schema.fields().length; i++) {
                final StructField field = schema.apply(i);
                final Object rowObj = row.apply(i);
                if (rowObj != null) {
                    if (field.name().equals("geom")) {
                        final Geometry geom = (Geometry) rowObj;
                        sfBuilder.set("geom", geom);
                    } else if (field.dataType() == DataTypes.TimestampType) {
                        final long millis = ((Timestamp) rowObj).getTime();
                        final Date date = new Date(millis);
                        sfBuilder.set(field.name(), date);
                    } else {
                        sfBuilder.set(field.name(), rowObj);
                    }
                }
            }
            final SimpleFeature sf = sfBuilder.buildFeature("result-" + nf.format(r));
            writer.write(sf);
        }
    }
}
Also used : StructType(org.apache.spark.sql.types.StructType) Index(org.locationtech.geowave.core.store.api.Index) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) Date(java.util.Date) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(org.locationtech.jts.geom.Geometry) StructField(org.apache.spark.sql.types.StructField) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.locationtech.geowave.core.store.api.DataStore) Row(org.apache.spark.sql.Row) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) Writer(org.locationtech.geowave.core.store.api.Writer) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 4 with FeatureDataAdapter

use of org.locationtech.geowave.adapter.vector.FeatureDataAdapter in project geowave by locationtech.

the class SpatialQueryExample method ingestPolygonFeature.

private void ingestPolygonFeature() throws ParseException {
    log.info("Ingesting polygon data");
    // First, we'll build our third kind of SimpleFeature, which we'll call
    // "polygon-feature"
    // We need the type builder to build the feature type
    final SimpleFeatureTypeBuilder sftBuilder = new SimpleFeatureTypeBuilder();
    // AttributeTypeBuilder for the attributes of the SimpleFeature
    final AttributeTypeBuilder attrBuilder = new AttributeTypeBuilder();
    // Here we're setting the SimpleFeature name. Later on, we'll be able to
    // query GW just by this particular feature.
    sftBuilder.setName("polygon-feature");
    // Add the attributes to the feature
    // Add the geometry attribute, which is mandatory for GeoWave to be able
    // to construct an index out of the SimpleFeature
    // Will be any arbitrary geometry; in this case, a polygon.
    sftBuilder.add(attrBuilder.binding(Geometry.class).nillable(false).buildDescriptor("geometry"));
    // Add another attribute just to be able to filter by it in CQL
    sftBuilder.add(attrBuilder.binding(String.class).nillable(false).buildDescriptor("filter"));
    // Create the SimpleFeatureType
    final SimpleFeatureType sfType = sftBuilder.buildFeatureType();
    // We need the adapter for all our operations with GeoWave
    final FeatureDataAdapter sfAdapter = new FeatureDataAdapter(sfType);
    // Now we build the actual features. We'll create one polygon.
    // First point
    final SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sfType);
    // For ease of use, we'll create the polygon geometry with WKT format.
    final String polygonDefinition = "POLYGON (( " + "-80.3045654296875 25.852426562716428, " + "-80.123291015625 25.808545671771615, " + "-80.19195556640625 25.7244467526159, " + "-80.34233093261719 25.772068899816585, " + "-80.3045654296875 25.852426562716428" + "))";
    final Geometry geom = new WKTReader(JTSFactoryFinder.getGeometryFactory()).read(polygonDefinition);
    sfBuilder.set("geometry", geom);
    sfBuilder.set("filter", "Polygon");
    // When calling buildFeature, we need to pass an unique id for that
    // feature, or it will be overwritten.
    final SimpleFeature polygon = sfBuilder.buildFeature("1");
    final ArrayList<SimpleFeature> features = new ArrayList<>();
    features.add(polygon);
    // Ingest the data. For that purpose, we need the feature adapter,
    // the index type (the default spatial index is used here),
    // and an iterator of SimpleFeature
    ingest(sfAdapter, new SpatialIndexBuilder().createIndex(), features);
    log.info("Polygon data ingested");
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AttributeTypeBuilder(org.geotools.feature.AttributeTypeBuilder) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SpatialIndexBuilder(org.locationtech.geowave.core.geotime.index.api.SpatialIndexBuilder) ArrayList(java.util.ArrayList) WKTReader(org.locationtech.jts.io.WKTReader) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 5 with FeatureDataAdapter

use of org.locationtech.geowave.adapter.vector.FeatureDataAdapter in project geowave by locationtech.

the class SpatialQueryExample method ingestPointBasicFeature.

private void ingestPointBasicFeature() {
    // First, we'll build our first kind of SimpleFeature, which we'll call
    // "basic-feature"
    // We need the type builder to build the feature type
    final SimpleFeatureTypeBuilder sftBuilder = new SimpleFeatureTypeBuilder();
    // AttributeTypeBuilder for the attributes of the SimpleFeature
    final AttributeTypeBuilder attrBuilder = new AttributeTypeBuilder();
    // Here we're setting the SimpleFeature name. Later on, we'll be able to
    // query GW just by this particular feature.
    sftBuilder.setName("basic-feature");
    // Add the attributes to the feature
    // Add the geometry attribute, which is mandatory for GeoWave to be able
    // to construct an index out of the SimpleFeature
    sftBuilder.add(attrBuilder.binding(Point.class).nillable(false).buildDescriptor("geometry"));
    // Add another attribute just to be able to filter by it in CQL
    sftBuilder.add(attrBuilder.binding(String.class).nillable(false).buildDescriptor("filter"));
    // Create the SimpleFeatureType
    final SimpleFeatureType sfType = sftBuilder.buildFeatureType();
    // We need the adapter for all our operations with GeoWave
    final FeatureDataAdapter sfAdapter = new FeatureDataAdapter(sfType);
    // Now we build the actual features. We'll create two points.
    // First point
    final SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sfType);
    sfBuilder.set("geometry", GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(-80.211181640625, 25.848101000701597)));
    sfBuilder.set("filter", "Basic-Stadium");
    // When calling buildFeature, we need to pass an unique id for that
    // feature, or it will be overwritten.
    final SimpleFeature basicPoint1 = sfBuilder.buildFeature("1");
    // Construct the second feature.
    sfBuilder.set("geometry", GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(-80.191360, 25.777804)));
    sfBuilder.set("filter", "Basic-College");
    final SimpleFeature basicPoint2 = sfBuilder.buildFeature("2");
    final ArrayList<SimpleFeature> features = new ArrayList<>();
    features.add(basicPoint1);
    features.add(basicPoint2);
    // Ingest the data. For that purpose, we need the feature adapter,
    // the index type (the default spatial index is used here),
    // and an iterator of SimpleFeature
    ingest(sfAdapter, new SpatialIndexBuilder().createIndex(), features);
}
Also used : AttributeTypeBuilder(org.geotools.feature.AttributeTypeBuilder) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(org.locationtech.jts.geom.Coordinate) SpatialIndexBuilder(org.locationtech.geowave.core.geotime.index.api.SpatialIndexBuilder) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

FeatureDataAdapter (org.locationtech.geowave.adapter.vector.FeatureDataAdapter)71 SimpleFeature (org.opengis.feature.simple.SimpleFeature)36 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)33 Test (org.junit.Test)25 DataStore (org.locationtech.geowave.core.store.api.DataStore)25 Index (org.locationtech.geowave.core.store.api.Index)24 Coordinate (org.locationtech.jts.geom.Coordinate)21 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)17 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)16 Date (java.util.Date)12 Geometry (org.locationtech.jts.geom.Geometry)12 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)12 SpatialOptions (org.locationtech.geowave.core.geotime.index.SpatialOptions)11 Point (org.locationtech.jts.geom.Point)11 AttributeTypeBuilder (org.geotools.feature.AttributeTypeBuilder)10 Before (org.junit.Before)10 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)10 DataStorePluginOptions (org.locationtech.geowave.core.store.cli.store.DataStorePluginOptions)10 ArrayList (java.util.ArrayList)9 Envelope (org.locationtech.jts.geom.Envelope)9