use of org.locationtech.geowave.adapter.vector.util.PolygonAreaCalculator 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;
}
Aggregations