-
Notifications
You must be signed in to change notification settings - Fork 19
Extend Filters, extract Ids #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
16928ca
514a5cc
b642123
4a9580a
6d83b24
4529e56
bb87c50
9284e1a
e335a4b
bdb936b
d2df862
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.heigit.ohsome.oshdb.filter; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static java.util.Collections.emptySet; | ||
|
|
||
| public class FilterUtil { | ||
|
|
||
| private FilterUtil() { | ||
| // utility class | ||
| } | ||
|
|
||
| public static Set<Long> ids(FilterExpression expression) { | ||
| return ids(expression.normalize()); | ||
| } | ||
|
|
||
| public static Set<Long> ids(List<List<Filter>> normalized) { | ||
| var ids = new HashSet<Long>(); | ||
| for (var orGroups : normalized) { | ||
| var groupIds = new HashSet<Long>(); | ||
| for (var filter: orGroups) { | ||
| if (filter instanceof IdFilterEquals equals){ | ||
| groupIds.add(equals.getId()); | ||
| } else if (filter instanceof IdFilterEqualsAnyOf equalsAnyOf) { | ||
| groupIds.addAll(equalsAnyOf.getIds()); | ||
| } else if (filter instanceof IdFilterRange range) { | ||
| range.getRange().getIds().forEach(groupIds::add); | ||
| } | ||
| } | ||
| if (groupIds.isEmpty()) { | ||
| return emptySet(); | ||
| } | ||
| ids.addAll(groupIds); | ||
| } | ||
| return ids; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could move the code from public static Set<OSMType> extractTypes(FilterExpression expression) {
return extractTypes(expression.normalize());
}
public static Set<OSMType> extractTypes(List<List<Filter>> normalized) {
var allTypes = EnumSet.noneOf(OSMType.class);
extract(normalized, ignored -> Stream.of(true)).keySet()
.forEach(allTypes::addAll);
return allTypes;
}and in mapRed = mapRed.osmTypeInternal(FilterUtil.extractTypes(filter)); |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,19 @@ | |
| * A tag filter which executes a "id [not] in (id1, id2, …)" check. | ||
| */ | ||
| public class IdFilterEqualsAnyOf extends NegatableFilter { | ||
|
|
||
| private final Set<Long> ids; | ||
|
|
||
| IdFilterEqualsAnyOf(@Nonnull Collection<Long> idList) { | ||
| this(new HashSet<>(idList)); | ||
| } | ||
|
|
||
| IdFilterEqualsAnyOf(@Nonnull Set<Long> ids) { | ||
| super(new FilterInternal() { | ||
| private final Set<Long> ids = new HashSet<>(idList); | ||
|
|
||
| @Override | ||
| public boolean applyOSH(OSHEntity entity) { | ||
| return this.ids.contains(entity.getId()); | ||
| return ids.contains(entity.getId()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -28,7 +34,7 @@ boolean applyOSHNegated(OSHEntity entity) { | |
|
|
||
| @Override | ||
| public boolean applyOSM(OSMEntity entity) { | ||
| return this.ids.contains(entity.getId()); | ||
| return ids.contains(entity.getId()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -38,12 +44,18 @@ boolean applyOSMNegated(OSMEntity entity) { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| return "id:in" + this.ids.stream().map(String::valueOf).collect(Collectors.joining(",")); | ||
| return "id:in" + ids.stream().map(String::valueOf).collect(Collectors.joining(",")); | ||
| } | ||
| }); | ||
|
|
||
| if (idList.isEmpty()) { | ||
| if (ids.isEmpty()) { | ||
| throw new IllegalStateException("list of ids must not be empty in a id in (list) filter"); | ||
| } | ||
|
|
||
| this.ids = ids; | ||
| } | ||
|
|
||
| public Set<Long> getIds() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be included in |
||
| return ids; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.heigit.ohsome.oshdb.filter; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class FilterUtilTest extends FilterTest{ | ||
|
|
||
| @Test | ||
| void extractIds() { | ||
| extractIds("id:123", Set.of(123L)); | ||
| extractIds("id:123 or id:234", Set.of(123L, 234L)); | ||
| extractIds("id:123 and not id:234", Set.of(123L)); | ||
| extractIds("id:123 or name=heigit", Set.of()); | ||
| extractIds("id:(123, 234)", Set.of(123L, 234L)); | ||
| extractIds("id:(123 .. 125)", Set.of(123L, 124L, 125L)); | ||
| } | ||
|
|
||
| private void extractIds(String filter, Set<Long> expected) { | ||
| var expression = parser.parse(filter); | ||
| var actual = FilterUtil.ids(expression); | ||
|
|
||
| assertEquals(expected.size(), actual.size()); | ||
| assertTrue(expected.containsAll(actual)); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.