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;
}
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();
}
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);
}
}
}
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");
}
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);
}
Aggregations