Skip to content

Instantly share code, notes, and snippets.

@wlievens
Last active December 1, 2017 10:30
Show Gist options
  • Save wlievens/dd566876da4d308ccbf3ba2861610704 to your computer and use it in GitHub Desktop.
Save wlievens/dd566876da4d308ccbf3ba2861610704 to your computer and use it in GitHub Desktop.
@Getter
@Setter
@AllArgsConstructor
private static class GeometryHandle
{
Geometry geometry;
}
public static Collector<Geometry, GeometryHandle, Geometry> collectorUnion(@NonNull GeometryFactory factory)
{
return new Collector<Geometry, GeometryHandle, Geometry>()
{
@Override
public Supplier<GeometryHandle> supplier()
{
return () -> new GeometryHandle(factory.createGeometryCollection(new Geometry[0]));
}
@Override
public BiConsumer<GeometryHandle, Geometry> accumulator()
{
return (a, b) -> a.geometry = a.geometry.union(b);
}
@Override
public BinaryOperator<GeometryHandle> combiner()
{
return (a, b) -> new GeometryHandle(a.geometry.union(b.geometry));
}
@Override
public Function<GeometryHandle, Geometry> finisher()
{
return GeometryHandle::getGeometry;
}
@Override
public Set<Characteristics> characteristics()
{
return Sets.immutableEnumSet(Characteristics.UNORDERED);
}
};
}
@wlievens
Copy link
Author

wlievens commented Dec 1, 2017

Java Stream API Collector implementation for the union operation on JTS geometries.

Depends on JTS, Lombok and Guava, but the latter two can be factored out trivially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment