Skip to content

Commit

Permalink
drop Filter.by helpers
Browse files Browse the repository at this point in the history
Reason: They did not really fulfil a specific purpose because one can already "and" combine OSH+OSM filters by calling `.filter` twice, and the geometry callback filter basically just replicates the `.filter(Predicate)` anyway.
Also the given documentation and examples were not in line with the actual behaviour when one would have negated the filter, so it in the end just confusing to use.
  • Loading branch information
tyrasd committed Jun 2, 2021
1 parent b51d75f commit 9f8fecf
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 110 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Changelog
### breaking changes

* reorganize java packages, moving them from `org/heigit/bigspatialdata` to `org/heigit/ohsome`
* integrate [ohsome-filter](https://gitlab.gistools.geog.uni-heidelberg.de/giscience/big-data/ohsome/libs/ohsome-filter) module fully into this repository, renaming it to `oshdb-filter` ([#306]), and enhance filtering capabilities ([#380])
* integrate [ohsome-filter](https://gitlab.gistools.geog.uni-heidelberg.de/giscience/big-data/ohsome/libs/ohsome-filter) module fully into this repository, renaming it to `oshdb-filter` ([#306])
* rename and move submodules of `oshdb-tool` ([#384])
* rename some classes, methods and enum constants; move some classes/interfaces ([#369], [#374])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.heigit.ohsome.oshdb.filter;

import java.util.function.Supplier;
import org.heigit.ohsome.oshdb.filter.NegatableFilter.FilterInternal;
import org.heigit.ohsome.oshdb.osh.OSHEntity;
import org.heigit.ohsome.oshdb.osm.OSMEntity;
import org.heigit.ohsome.oshdb.util.function.OSHEntityFilter;
import org.heigit.ohsome.oshdb.util.function.OSMEntityFilter;
import org.heigit.ohsome.oshdb.util.function.SerializableBiPredicate;
import org.locationtech.jts.geom.Geometry;

/**
* A filter condition which can be applied to an OSM entity.
Expand Down Expand Up @@ -69,80 +66,4 @@ boolean applyOSMNegated(OSMEntity entity) {
});
}

/**
* Constructs a custom filter based on OSH & OSM predicates.
*
* <p>The callbacks are called for each OSH/OSM entity and decide whether the OSH/OSM object
* should be kept (by returning true) or discarded (by returning false).</p>
*
* <p>Example: `Filter.by(osh -&gt; osh.getId() == 42, osm -&gt; osm.getVersion() == 1);`</p>
*
* @param oshCallback predicate which tests osh entities
* @param osmCallback predicate which tests osm entities
* @return a filter object which filters using the given predicates
*/
static Filter by(
OSHEntityFilter oshCallback,
OSMEntityFilter osmCallback) {
return new NegatableFilter(new FilterInternal() {
@Override
public boolean applyOSH(OSHEntity entity) {
return oshCallback.test(entity);
}

@Override
public boolean applyOSM(OSMEntity entity) {
return osmCallback.test(entity);
}

@Override
boolean applyOSMNegated(OSMEntity entity) {
return !osmCallback.test(entity);
}
});
}

/**
* Constructs a custom filter based on OSH &amp; OSM predicates and a geometry test.
*
* <p>The callbacks are called for each OSM feature and decide whether the feature
* should be kept (by returning true) or discarded (by returning false).</p>
*
* <p>Example: `Filter.by(osh -&gt; osh.getId() == 42, osm -&gt; osm.getVersion() == 1,
* (osm, geometrySupplier) -&gt; geometrySupplier.get() instanceOf Polygon);`</p>
*
* @param oshCallback predicate which tests osh entities
* @param osmCallback predicate which tests osm entities
* @param geomCallback predicate which tests osm geometries, alongside the geometry (given as a
* supplier method), the entity itself is given also to be able to perform
* filtering on the whole "OSM Feature" (metadata + tags + geometry).
* @return a filter object which filters using the given predicates
*/
static Filter by(
OSHEntityFilter oshCallback,
OSMEntityFilter osmCallback,
SerializableBiPredicate<OSMEntity, Supplier<Geometry>> geomCallback
) {
return new NegatableFilter(new FilterInternal() {
@Override
public boolean applyOSH(OSHEntity entity) {
return oshCallback.test(entity);
}

@Override
public boolean applyOSM(OSMEntity entity) {
return osmCallback.test(entity);
}

@Override
public boolean applyOSMGeometry(OSMEntity entity, Supplier<Geometry> geometrySupplier) {
return geomCallback.test(entity, geometrySupplier);
}

@Override
boolean applyOSMGeometryNegated(OSMEntity entity, Supplier<Geometry> geometrySupplier) {
return !geomCallback.test(entity, geometrySupplier);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import org.junit.Test;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import org.heigit.ohsome.oshdb.osh.OSHNode;
import org.heigit.ohsome.oshdb.osm.OSMNode;
import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

/**
* Tests helper methods for creating filters from lambda functions.
Expand All @@ -29,7 +25,7 @@ public void testFilterByOSMEntity() {
FilterExpression expression;
expression = Filter.byOSMEntity(e -> e.getVersion() == 1);
assertTrue(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
expression = Filter.byOSMEntity(e -> e.getVersion() != 1);
expression = expression.negate();
assertFalse(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
}

Expand All @@ -38,30 +34,7 @@ public void testFilterByOSHEntity() {
FilterExpression expression;
expression = Filter.byOSHEntity(e -> e.getId() == 1);
assertTrue(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
expression = Filter.byOSHEntity(e -> e.getId() != 1);
expression = expression.negate();
assertFalse(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
}

@Test
public void testFilterByCombined() {
FilterExpression expression;
expression = Filter.by(e -> e.getId() == 1, e -> e.getVersion() == 1);
assertTrue(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
expression = Filter.by(e -> e.getId() != 1, e -> e.getVersion() == 1);
assertFalse(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
expression = Filter.by(e -> e.getId() == 1, e -> e.getVersion() != 1);
assertFalse(expression.applyOSH(testOSHEntity) && expression.applyOSM(testOSMEntity));
}

@Test
public void testFilterByCombinedWithGeom() {
final Geometry emptyPoint = gf.createPoint(new Coordinate(0, 0));
FilterExpression expression;
expression = Filter.by(e -> true, e -> true, (e, g) -> g.get() instanceof Point);
assertTrue(expression.applyOSMGeometry(testOSMEntity, () -> emptyPoint));
assertTrue(expression.applyOSMGeometry(testOSMEntity, emptyPoint));
expression = Filter.by(e -> true, e -> true, (e, g) -> g.get() instanceof Polygon);
assertFalse(expression.applyOSMGeometry(testOSMEntity, () -> emptyPoint));
assertFalse(expression.applyOSMGeometry(testOSMEntity, emptyPoint));
}
}

0 comments on commit 9f8fecf

Please sign in to comment.