Changeset 15764 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2020-01-26T00:18:32+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java
r15440 r15764 19 19 import java.util.Optional; 20 20 import java.util.function.Predicate; 21 import java.util.function.Supplier; 21 22 import java.util.regex.Matcher; 22 23 import java.util.regex.Pattern; … … 264 265 265 266 /** 267 * Classes implementing this interface can provide Match instances themselves and do not rely on {@link #compile(String)}. 268 * 269 * @since 15764 270 */ 271 @FunctionalInterface 272 public interface MatchSupplier extends Supplier<Match> { 273 @Override 274 Match get(); 275 } 276 277 /** 266 278 * Base class for all search criteria. If the criterion only depends on an object's tags, 267 279 * inherit from {@link org.openstreetmap.josm.data.osm.search.SearchCompiler.TaggedMatch}. … … 1944 1956 */ 1945 1957 public static Match compile(SearchSetting setting) throws SearchParseError { 1958 if (setting instanceof MatchSupplier) { 1959 return ((MatchSupplier) setting).get(); 1960 } 1946 1961 if (setting.mapCSSSearch) { 1947 1962 return compileMapCSS(setting.text); -
trunk/src/org/openstreetmap/josm/gui/autofilter/AutoFilterManager.java
r14206 r15764 20 20 import java.util.regex.Matcher; 21 21 import java.util.regex.Pattern; 22 import java.util.stream.IntStream; 23 import java.util.stream.Stream; 22 24 23 25 import org.openstreetmap.josm.actions.mapmode.MapMode; … … 27 29 import org.openstreetmap.josm.data.osm.FilterModel; 28 30 import org.openstreetmap.josm.data.osm.OsmPrimitive; 31 import org.openstreetmap.josm.data.osm.OsmUtils; 29 32 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; 30 33 import org.openstreetmap.josm.data.osm.event.DataChangedEvent; … … 38 41 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 39 42 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 43 import org.openstreetmap.josm.data.osm.search.SearchCompiler; 44 import org.openstreetmap.josm.data.osm.search.SearchCompiler.MatchSupplier; 40 45 import org.openstreetmap.josm.data.preferences.BooleanProperty; 41 46 import org.openstreetmap.josm.data.preferences.StringProperty; … … 77 82 78 83 /** 84 * Property to determine if the auto filter should assume sensible defaults for values (such as layer=1 for bridge=yes). 85 */ 86 private static final BooleanProperty PROP_AUTO_FILTER_DEFAULTS = new BooleanProperty("auto.filter.defaults", true); 87 88 /** 79 89 * The unique instance. 80 90 */ … … 148 158 } 149 159 160 static class CompiledFilter extends Filter implements MatchSupplier { 161 final String key; 162 final String value; 163 164 CompiledFilter(String key, String value) { 165 this.key = key; 166 this.value = value; 167 this.enable = true; 168 this.inverted = true; 169 this.text = key + "=" + value; 170 } 171 172 @Override 173 public SearchCompiler.Match get() { 174 return new SearchCompiler.Match() { 175 @Override 176 public boolean match(OsmPrimitive osm) { 177 return getTagValuesForPrimitive(key, osm).anyMatch(value::equals); 178 } 179 }; 180 } 181 } 182 150 183 private synchronized void addNewButtons(NavigableSet<String> values) { 151 184 int i = 0; … … 153 186 MapView mapView = MainApplication.getMap().mapView; 154 187 for (final String value : values.descendingSet()) { 155 Filter filter = new Filter(); 156 filter.enable = true; 157 filter.inverted = true; 158 filter.text = enabledRule.getKey() + "=" + value; 188 Filter filter = new CompiledFilter(enabledRule.getKey(), value); 159 189 String label = enabledRule.getValueFormatter().apply(value); 160 190 AutoFilter autoFilter = new AutoFilter(label, filter.text, filter); … … 198 228 if (ds != null) { 199 229 BBox bbox = MainApplication.getMap().mapView.getState().getViewArea().getLatLonBoundsBox().toBBox(); 200 Consumer<OsmPrimitive> consumer = getTagValuesConsumer(key, values);230 Consumer<OsmPrimitive> consumer = o -> getTagValuesForPrimitive(key, o).forEach(values::add); 201 231 ds.searchNodes(bbox).forEach(consumer); 202 232 ds.searchWays(bbox).forEach(consumer); … … 206 236 } 207 237 208 static Consumer<OsmPrimitive> getTagValuesConsumer(String key, Set<String> values) { 209 return o -> { 210 String value = o.get(key); 211 if (value != null) { 212 Pattern p = Pattern.compile("(-?[0-9]+)-(-?[0-9]+)"); 213 for (String v : value.split(";")) { 214 Matcher m = p.matcher(v); 215 if (m.matches()) { 216 int a = Integer.parseInt(m.group(1)); 217 int b = Integer.parseInt(m.group(2)); 218 for (int i = Math.min(a, b); i <= Math.max(a, b); i++) { 219 values.add(Integer.toString(i)); 220 } 221 } else { 222 values.add(v); 223 } 238 static Stream<String> getTagValuesForPrimitive(String key, OsmPrimitive osm) { 239 String value = osm.get(key); 240 if (value != null) { 241 Pattern p = Pattern.compile("(-?[0-9]+)-(-?[0-9]+)"); 242 return OsmUtils.splitMultipleValues(value).flatMap(v -> { 243 Matcher m = p.matcher(v); 244 if (m.matches()) { 245 int a = Integer.parseInt(m.group(1)); 246 int b = Integer.parseInt(m.group(2)); 247 return IntStream.rangeClosed(Math.min(a, b), Math.max(a, b)) 248 .mapToObj(Integer::toString); 249 } else { 250 return Stream.of(v); 224 251 } 225 } 226 }; 252 }); 253 } else if (PROP_AUTO_FILTER_DEFAULTS.get() && "layer".equals(key)) { 254 // assume sensible defaults, see #17496 255 if (osm.hasTag("bridge") || osm.hasTag("power", "line") || osm.hasTag("location", "overhead")) { 256 return Stream.of("1"); 257 } else if (osm.isKeyTrue("tunnel") || osm.hasTag("tunnel", "culvert") || osm.hasTag("location", "underground")) { 258 return Stream.of("-1"); 259 } else if (osm.hasTag("tunnel", "building_passage") || osm.hasKey("highway", "railway", "waterway")) { 260 return Stream.of("0"); 261 } 262 } 263 return Stream.empty(); 227 264 } 228 265
Note:
See TracChangeset
for help on using the changeset viewer.