| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.osm; |
|---|
| 3 | |
|---|
| 4 | import java.util.Collection; |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * |
|---|
| 8 | * @author Petr_DlouhÜ |
|---|
| 9 | */ |
|---|
| 10 | public class FilterWorker { |
|---|
| 11 | /** |
|---|
| 12 | * Apply the filters to the primitives of the data set. |
|---|
| 13 | * |
|---|
| 14 | * There are certain rules to ensure that a way is not displayed "naked" |
|---|
| 15 | * without its nodes (1) and on the other hand to avoid hiding a way but |
|---|
| 16 | * leaving its nodes visible as a cloud of points (2). |
|---|
| 17 | * |
|---|
| 18 | * In normal (non-inverted) mode only problem (2) is relevant. |
|---|
| 19 | * Untagged child nodes of filtered ways that are not used by other |
|---|
| 20 | * unfiltered ways are filtered as well. |
|---|
| 21 | * |
|---|
| 22 | * If a filter applies explicitly to a node, (2) is ignored and it |
|---|
| 23 | * is filtered in any case. |
|---|
| 24 | * |
|---|
| 25 | * In inverted mode usually only problem (1) is relevant. |
|---|
| 26 | * If the inverted filter applies explicitly to a node, this no longer |
|---|
| 27 | * means it is filtered in any case: |
|---|
| 28 | * E.g. the filter [searchtext="highway=footway", inverted=true] displays |
|---|
| 29 | * the footways only. But that does not mean, the nodes of the footway |
|---|
| 30 | * (which do not have the highway tag) should be filtered as well. |
|---|
| 31 | * |
|---|
| 32 | * So first the Filter is applied for ways and relations. Then to nodes |
|---|
| 33 | * (but hides them only if they are not used by any unfiltered way). |
|---|
| 34 | */ |
|---|
| 35 | public static boolean executeFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) { |
|---|
| 36 | |
|---|
| 37 | boolean changed = false; |
|---|
| 38 | |
|---|
| 39 | // First relation and ways |
|---|
| 40 | for (OsmPrimitive primitive: all) { |
|---|
| 41 | if (!(primitive instanceof Node)) { |
|---|
| 42 | if (filterMatcher.isHidden(primitive)) { |
|---|
| 43 | changed = changed | primitive.setDisabledState(true); |
|---|
| 44 | } else if (filterMatcher.isDisabled(primitive)) { |
|---|
| 45 | changed = changed | primitive.setDisabledState(false); |
|---|
| 46 | } else { |
|---|
| 47 | changed = changed | primitive.unsetDisabledState(); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | // Then nodes (because they state may depend on parent ways) |
|---|
| 53 | for (OsmPrimitive primitive: all) { |
|---|
| 54 | if (primitive instanceof Node) { |
|---|
| 55 | if (filterMatcher.isHidden(primitive)) { |
|---|
| 56 | changed = changed | primitive.setDisabledState(true); |
|---|
| 57 | } else if (filterMatcher.isDisabled(primitive)) { |
|---|
| 58 | changed = changed | primitive.setDisabledState(false); |
|---|
| 59 | } else { |
|---|
| 60 | changed = changed | primitive.unsetDisabledState(); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | return changed; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public static boolean executeFilters(OsmPrimitive primitive, FilterMatcher filterMatcher) { |
|---|
| 69 | boolean changed = false; |
|---|
| 70 | if (filterMatcher.isHidden(primitive)) { |
|---|
| 71 | changed = changed | primitive.setDisabledState(true); |
|---|
| 72 | } else if (filterMatcher.isDisabled(primitive)) { |
|---|
| 73 | changed = changed | primitive.setDisabledState(false); |
|---|
| 74 | } else { |
|---|
| 75 | changed = changed | primitive.unsetDisabledState(); |
|---|
| 76 | } |
|---|
| 77 | return changed; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public static void clearFilterFlags(Collection<OsmPrimitive> prims) { |
|---|
| 81 | for (OsmPrimitive osm : prims) { |
|---|
| 82 | osm.unsetDisabledState(); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|