1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.data.projection.Ellipsoid.WGS84;
|
---|
5 |
|
---|
6 | import java.awt.geom.Area;
|
---|
7 | import java.text.MessageFormat;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.LinkedHashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Objects;
|
---|
15 | import java.util.function.IntFunction;
|
---|
16 | import java.util.function.IntSupplier;
|
---|
17 | import java.util.regex.PatternSyntaxException;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.data.osm.INode;
|
---|
21 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
23 | import org.openstreetmap.josm.data.osm.IRelationMember;
|
---|
24 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
|
---|
29 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
30 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
31 | import org.openstreetmap.josm.gui.mappaint.Range;
|
---|
32 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.OpenEndPseudoClassCondition;
|
---|
33 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
34 | import org.openstreetmap.josm.tools.Geometry;
|
---|
35 | import org.openstreetmap.josm.tools.Geometry.PolygonIntersection;
|
---|
36 | import org.openstreetmap.josm.tools.Logging;
|
---|
37 | import org.openstreetmap.josm.tools.Pair;
|
---|
38 | import org.openstreetmap.josm.tools.Utils;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * MapCSS selector.
|
---|
42 | *
|
---|
43 | * A rule has two parts, a selector and a declaration block
|
---|
44 | * e.g.
|
---|
45 | * <pre>
|
---|
46 | * way[highway=residential]
|
---|
47 | * { width: 10; color: blue; }
|
---|
48 | * </pre>
|
---|
49 | *
|
---|
50 | * The selector decides, if the declaration block gets applied or not.
|
---|
51 | *
|
---|
52 | * All implementing classes of Selector are immutable.
|
---|
53 | */
|
---|
54 | public interface Selector {
|
---|
55 |
|
---|
56 | /** selector base that matches anything. */
|
---|
57 | String BASE_ANY = "*";
|
---|
58 |
|
---|
59 | /** selector base that matches on OSM object node. */
|
---|
60 | String BASE_NODE = "node";
|
---|
61 |
|
---|
62 | /** selector base that matches on OSM object way. */
|
---|
63 | String BASE_WAY = "way";
|
---|
64 |
|
---|
65 | /** selector base that matches on OSM object relation. */
|
---|
66 | String BASE_RELATION = "relation";
|
---|
67 |
|
---|
68 | /** selector base that matches with any area regardless of whether the area border is only modelled with a single way or with
|
---|
69 | * a set of ways glued together with a relation.*/
|
---|
70 | String BASE_AREA = "area";
|
---|
71 |
|
---|
72 | /** selector base for special rules containing meta information. */
|
---|
73 | String BASE_META = "meta";
|
---|
74 |
|
---|
75 | /** selector base for style information not specific to nodes, ways or relations. */
|
---|
76 | String BASE_CANVAS = "canvas";
|
---|
77 |
|
---|
78 | /** selector base for artificial bases created to use preferences. */
|
---|
79 | String BASE_SETTING = "setting";
|
---|
80 |
|
---|
81 | /** selector base for grouping settings. */
|
---|
82 | String BASE_SETTINGS = "settings";
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Apply the selector to the primitive and check if it matches.
|
---|
86 | *
|
---|
87 | * @param env the Environment. env.mc and env.layer are read-only when matching a selector.
|
---|
88 | * env.source is not needed. This method will set the matchingReferrers field of env as
|
---|
89 | * a side effect! Make sure to clear it before invoking this method.
|
---|
90 | * @return true, if the selector applies
|
---|
91 | */
|
---|
92 | boolean matches(Environment env);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Returns the subpart, if supported. A subpart identifies different rendering layers (<code>::subpart</code> syntax).
|
---|
96 | * @return the subpart, if supported
|
---|
97 | * @throws UnsupportedOperationException if not supported
|
---|
98 | */
|
---|
99 | Subpart getSubpart();
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns the scale range, an interval of the form "lower < x <= upper" where 0 <= lower < upper.
|
---|
103 | * @return the scale range, if supported
|
---|
104 | * @throws UnsupportedOperationException if not supported
|
---|
105 | */
|
---|
106 | Range getRange();
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Create an "optimized" copy of this selector that omits the base check.
|
---|
110 | *
|
---|
111 | * For the style source, the list of rules is preprocessed, such that
|
---|
112 | * there is a separate list of rules for nodes, ways, ...
|
---|
113 | *
|
---|
114 | * This means that the base check does not have to be performed
|
---|
115 | * for each rule, but only once for each primitive.
|
---|
116 | *
|
---|
117 | * @return a selector that is identical to this object, except the base of the
|
---|
118 | * "rightmost" selector is not checked
|
---|
119 | */
|
---|
120 | Selector optimizedBaseCheck();
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * The type of child of parent selector.
|
---|
124 | * @see ChildOrParentSelector
|
---|
125 | */
|
---|
126 | enum ChildOrParentSelectorType {
|
---|
127 | CHILD, PARENT, SUBSET_OR_EQUAL, NOT_SUBSET_OR_EQUAL, SUPERSET_OR_EQUAL, NOT_SUPERSET_OR_EQUAL, CROSSING, SIBLING,
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * <p>Represents a child selector or a parent selector.</p>
|
---|
132 | *
|
---|
133 | * <p>In addition to the standard CSS notation for child selectors, JOSM also supports
|
---|
134 | * an "inverse" notation:</p>
|
---|
135 | * <pre>
|
---|
136 | * selector_a > selector_b { ... } // the standard notation (child selector)
|
---|
137 | * relation[type=route] > way { ... } // example (all ways of a route)
|
---|
138 | *
|
---|
139 | * selector_a < selector_b { ... } // the inverse notation (parent selector)
|
---|
140 | * node[traffic_calming] < way { ... } // example (way that has a traffic calming node)
|
---|
141 | * </pre>
|
---|
142 | * <p>Child: see <a href="https://josm.openstreetmap.de/wiki/Help/Styles/MapCSSImplementation#Childselector">wiki</a>
|
---|
143 | * <br>Parent: see <a href="https://josm.openstreetmap.de/wiki/Help/Styles/MapCSSImplementation#Parentselector">wiki</a></p>
|
---|
144 | */
|
---|
145 | class ChildOrParentSelector implements Selector {
|
---|
146 | public final Selector left;
|
---|
147 | public final LinkSelector link;
|
---|
148 | public final Selector right;
|
---|
149 | public final ChildOrParentSelectorType type;
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Constructs a new {@code ChildOrParentSelector}.
|
---|
153 | * @param a the first selector
|
---|
154 | * @param link link
|
---|
155 | * @param b the second selector
|
---|
156 | * @param type the selector type
|
---|
157 | */
|
---|
158 | public ChildOrParentSelector(Selector a, LinkSelector link, Selector b, ChildOrParentSelectorType type) {
|
---|
159 | CheckParameterUtil.ensureParameterNotNull(a, "a");
|
---|
160 | CheckParameterUtil.ensureParameterNotNull(b, "b");
|
---|
161 | CheckParameterUtil.ensureParameterNotNull(link, "link");
|
---|
162 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
163 | this.left = a;
|
---|
164 | this.link = link;
|
---|
165 | this.right = b;
|
---|
166 | this.type = type;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * <p>Finds the first referrer matching {@link #left}</p>
|
---|
171 | *
|
---|
172 | * <p>The visitor works on an environment and it saves the matching
|
---|
173 | * referrer in {@code e.parent} and its relative position in the
|
---|
174 | * list referrers "child list" in {@code e.index}.</p>
|
---|
175 | *
|
---|
176 | * <p>If after execution {@code e.parent} is null, no matching
|
---|
177 | * referrer was found.</p>
|
---|
178 | *
|
---|
179 | */
|
---|
180 | private class MatchingReferrerFinder implements PrimitiveVisitor {
|
---|
181 | private final Environment e;
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Constructor
|
---|
185 | * @param e the environment against which we match
|
---|
186 | */
|
---|
187 | MatchingReferrerFinder(Environment e) {
|
---|
188 | this.e = e;
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public void visit(INode n) {
|
---|
193 | // node should never be a referrer
|
---|
194 | throw new AssertionError();
|
---|
195 | }
|
---|
196 |
|
---|
197 | private <T extends IPrimitive> void doVisit(T parent, IntSupplier counter, IntFunction<IPrimitive> getter) {
|
---|
198 | // If e.parent is already set to the first matching referrer.
|
---|
199 | // We skip any following referrer injected into the visitor.
|
---|
200 | if (e.parent != null) return;
|
---|
201 |
|
---|
202 | if (!left.matches(e.withPrimitive(parent)))
|
---|
203 | return;
|
---|
204 | int count = counter.getAsInt();
|
---|
205 | if (link.conds == null) {
|
---|
206 | // index is not needed, we can avoid the sequential search below
|
---|
207 | e.parent = parent;
|
---|
208 | e.count = count;
|
---|
209 | return;
|
---|
210 | }
|
---|
211 | for (int i = 0; i < count; i++) {
|
---|
212 | if (getter.apply(i).equals(e.osm) && link.matches(e.withParentAndIndexAndLinkContext(parent, i, count))) {
|
---|
213 | e.parent = parent;
|
---|
214 | e.index = i;
|
---|
215 | e.count = count;
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Override
|
---|
222 | public void visit(IWay<?> w) {
|
---|
223 | doVisit(w, w::getNodesCount, w::getNode);
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public void visit(IRelation<?> r) {
|
---|
228 | doVisit(r, r::getMembersCount, i -> r.getMember(i).getMember());
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | private abstract static class AbstractFinder implements PrimitiveVisitor {
|
---|
233 | protected final Environment e;
|
---|
234 |
|
---|
235 | protected AbstractFinder(Environment e) {
|
---|
236 | this.e = e;
|
---|
237 | }
|
---|
238 |
|
---|
239 | @Override
|
---|
240 | public void visit(INode n) {
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | public void visit(IWay<?> w) {
|
---|
245 | }
|
---|
246 |
|
---|
247 | @Override
|
---|
248 | public void visit(IRelation<?> r) {
|
---|
249 | }
|
---|
250 |
|
---|
251 | public void visit(Collection<? extends IPrimitive> primitives) {
|
---|
252 | for (IPrimitive p : primitives) {
|
---|
253 | if (e.child != null) {
|
---|
254 | // abort if first match has been found
|
---|
255 | break;
|
---|
256 | } else if (isPrimitiveUsable(p)) {
|
---|
257 | p.accept(this);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public boolean isPrimitiveUsable(IPrimitive p) {
|
---|
263 | return !e.osm.equals(p) && p.isUsable();
|
---|
264 | }
|
---|
265 |
|
---|
266 | protected void addToChildren(Environment e, IPrimitive p) {
|
---|
267 | if (e.children == null) {
|
---|
268 | e.children = new LinkedHashSet<>();
|
---|
269 | }
|
---|
270 | e.children.add(p);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | private class MultipolygonOpenEndFinder extends AbstractFinder {
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public void visit(IWay<?> w) {
|
---|
278 | w.visitReferrers(innerVisitor);
|
---|
279 | }
|
---|
280 |
|
---|
281 | MultipolygonOpenEndFinder(Environment e) {
|
---|
282 | super(e);
|
---|
283 | }
|
---|
284 |
|
---|
285 | private final PrimitiveVisitor innerVisitor = new AbstractFinder(e) {
|
---|
286 | @Override
|
---|
287 | public void visit(IRelation<?> r) {
|
---|
288 | if (r instanceof Relation && left.matches(e.withPrimitive(r))) {
|
---|
289 | final List<?> openEnds = MultipolygonCache.getInstance().get((Relation) r).getOpenEnds();
|
---|
290 | final int openEndIndex = openEnds.indexOf(e.osm);
|
---|
291 | if (openEndIndex >= 0) {
|
---|
292 | e.parent = r;
|
---|
293 | e.index = openEndIndex;
|
---|
294 | e.count = openEnds.size();
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | };
|
---|
299 | }
|
---|
300 |
|
---|
301 | private final class CrossingFinder extends AbstractFinder {
|
---|
302 |
|
---|
303 | private final String layer;
|
---|
304 | private Area area;
|
---|
305 |
|
---|
306 | private CrossingFinder(Environment e) {
|
---|
307 | super(e);
|
---|
308 | CheckParameterUtil.ensureThat(e.osm instanceof IWay, "Only ways are supported");
|
---|
309 | layer = OsmUtils.getLayer(e.osm);
|
---|
310 | }
|
---|
311 |
|
---|
312 | @Override
|
---|
313 | public void visit(IWay<?> w) {
|
---|
314 | if (Objects.equals(layer, OsmUtils.getLayer(w))
|
---|
315 | && left.matches(new Environment(w).withParent(e.osm))
|
---|
316 | && e.osm instanceof IWay) {
|
---|
317 | if (area == null) {
|
---|
318 | area = Geometry.getAreaEastNorth(e.osm);
|
---|
319 | }
|
---|
320 | Pair<PolygonIntersection, Area> is = Geometry.polygonIntersectionResult(
|
---|
321 | Geometry.getAreaEastNorth(w), area, Geometry.INTERSECTION_EPS_EAST_NORTH);
|
---|
322 | if (Geometry.PolygonIntersection.CROSSING == is.a) {
|
---|
323 | addToChildren(e, w);
|
---|
324 | // store intersection area to improve highlight and zoom to problem
|
---|
325 | if (e.intersections == null) {
|
---|
326 | e.intersections = new HashMap<>();
|
---|
327 | }
|
---|
328 | e.intersections.put(w, is.b);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Finds elements which are inside the right element, collects those in {@code children}
|
---|
336 | */
|
---|
337 | private class ContainsFinder extends AbstractFinder {
|
---|
338 | protected List<IPrimitive> toCheck;
|
---|
339 |
|
---|
340 | protected ContainsFinder(Environment e) {
|
---|
341 | super(e);
|
---|
342 | CheckParameterUtil.ensureThat(!(e.osm instanceof INode), "Nodes not supported");
|
---|
343 | }
|
---|
344 |
|
---|
345 | @Override
|
---|
346 | public void visit(Collection<? extends IPrimitive> primitives) {
|
---|
347 | for (IPrimitive p : primitives) {
|
---|
348 | if (p != e.osm && isPrimitiveUsable(p) && left.matches(new Environment(p).withParent(e.osm))) {
|
---|
349 | if (toCheck == null) {
|
---|
350 | toCheck = new ArrayList<>();
|
---|
351 | }
|
---|
352 | toCheck.add(p);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | void execGeometryTests() {
|
---|
358 | if (toCheck == null || toCheck.isEmpty())
|
---|
359 | return;
|
---|
360 | for (IPrimitive p : Geometry.filterInsideAnyPolygon(toCheck, e.osm)) {
|
---|
361 | addToChildren(e, p);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Finds elements which are inside the left element, or in other words, it finds elements enclosing e.osm.
|
---|
368 | * The found enclosing elements are collected in {@code e.children}.
|
---|
369 | */
|
---|
370 | private class InsideOrEqualFinder extends AbstractFinder {
|
---|
371 |
|
---|
372 | protected InsideOrEqualFinder(Environment e) {
|
---|
373 | super(e);
|
---|
374 | }
|
---|
375 |
|
---|
376 | @Override
|
---|
377 | public void visit(IWay<?> w) {
|
---|
378 | if (left.matches(new Environment(w).withParent(e.osm))
|
---|
379 | && w.getBBox().bounds(e.osm.getBBox())
|
---|
380 | && !Geometry.filterInsidePolygon(Collections.singletonList(e.osm), w).isEmpty()) {
|
---|
381 | addToChildren(e, w);
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | @Override
|
---|
386 | public void visit(IRelation<?> r) {
|
---|
387 | if (r instanceof Relation && r.isMultipolygon() && r.getBBox().bounds(e.osm.getBBox())
|
---|
388 | && left.matches(new Environment(r).withParent(e.osm))
|
---|
389 | && !Geometry.filterInsideMultipolygon(Collections.singletonList(e.osm), (Relation) r).isEmpty()) {
|
---|
390 | addToChildren(e, r);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void visitBBox(Environment e, AbstractFinder finder) {
|
---|
396 | boolean withNodes = finder instanceof ContainsFinder;
|
---|
397 | if (e.osm.getDataSet() == null) {
|
---|
398 | // do nothing
|
---|
399 | } else if (left instanceof OptimizedGeneralSelector) {
|
---|
400 | if (withNodes && ((OptimizedGeneralSelector) left).matchesBase(OsmPrimitiveType.NODE)) {
|
---|
401 | finder.visit(e.osm.getDataSet().searchNodes(e.osm.getBBox()));
|
---|
402 | }
|
---|
403 | if (((OptimizedGeneralSelector) left).matchesBase(OsmPrimitiveType.WAY)) {
|
---|
404 | finder.visit(e.osm.getDataSet().searchWays(e.osm.getBBox()));
|
---|
405 | }
|
---|
406 | if (((OptimizedGeneralSelector) left).matchesBase(OsmPrimitiveType.RELATION)) {
|
---|
407 | finder.visit(e.osm.getDataSet().searchRelations(e.osm.getBBox()));
|
---|
408 | }
|
---|
409 | } else {
|
---|
410 | if (withNodes) {
|
---|
411 | finder.visit(e.osm.getDataSet().searchNodes(e.osm.getBBox()));
|
---|
412 | }
|
---|
413 | finder.visit(e.osm.getDataSet().searchWays(e.osm.getBBox()));
|
---|
414 | finder.visit(e.osm.getDataSet().searchRelations(e.osm.getBBox()));
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | private static boolean isArea(IPrimitive p) {
|
---|
419 | return (p instanceof IWay && ((IWay<?>) p).isClosed() && ((IWay<?>) p).getNodesCount() >= 4)
|
---|
420 | || (p instanceof IRelation && p.isMultipolygon() && !p.isIncomplete());
|
---|
421 | }
|
---|
422 |
|
---|
423 | @Override
|
---|
424 | public boolean matches(Environment e) {
|
---|
425 |
|
---|
426 | if (!right.matches(e))
|
---|
427 | return false;
|
---|
428 |
|
---|
429 | if (ChildOrParentSelectorType.SUBSET_OR_EQUAL == type || ChildOrParentSelectorType.NOT_SUBSET_OR_EQUAL == type) {
|
---|
430 |
|
---|
431 | if (e.osm.getDataSet() == null || !isArea(e.osm)) {
|
---|
432 | // only areas can contain elements
|
---|
433 | return ChildOrParentSelectorType.NOT_SUBSET_OR_EQUAL == type;
|
---|
434 | }
|
---|
435 | ContainsFinder containsFinder = new ContainsFinder(e);
|
---|
436 | e.parent = e.osm;
|
---|
437 |
|
---|
438 | visitBBox(e, containsFinder);
|
---|
439 | containsFinder.execGeometryTests();
|
---|
440 | return ChildOrParentSelectorType.SUBSET_OR_EQUAL == type ? e.children != null : e.children == null;
|
---|
441 |
|
---|
442 | } else if (ChildOrParentSelectorType.SUPERSET_OR_EQUAL == type || ChildOrParentSelectorType.NOT_SUPERSET_OR_EQUAL == type) {
|
---|
443 |
|
---|
444 | if (e.osm.getDataSet() == null || (e.osm instanceof INode && ((INode) e.osm).getCoor() == null)
|
---|
445 | || (!(e.osm instanceof INode) && !isArea(e.osm))) {
|
---|
446 | return ChildOrParentSelectorType.NOT_SUPERSET_OR_EQUAL == type;
|
---|
447 | }
|
---|
448 |
|
---|
449 | InsideOrEqualFinder insideOrEqualFinder = new InsideOrEqualFinder(e);
|
---|
450 | e.parent = e.osm;
|
---|
451 |
|
---|
452 | visitBBox(e, insideOrEqualFinder);
|
---|
453 | return ChildOrParentSelectorType.SUPERSET_OR_EQUAL == type ? e.children != null : e.children == null;
|
---|
454 |
|
---|
455 | } else if (ChildOrParentSelectorType.CROSSING == type && e.osm instanceof IWay) {
|
---|
456 | e.parent = e.osm;
|
---|
457 | if (right instanceof OptimizedGeneralSelector
|
---|
458 | && e.osm.getDataSet() != null
|
---|
459 | && ((OptimizedGeneralSelector) right).matchesBase(OsmPrimitiveType.WAY)) {
|
---|
460 | final CrossingFinder crossingFinder = new CrossingFinder(e);
|
---|
461 | crossingFinder.visit(e.osm.getDataSet().searchWays(e.osm.getBBox()));
|
---|
462 | }
|
---|
463 | return e.children != null;
|
---|
464 | } else if (ChildOrParentSelectorType.SIBLING == type) {
|
---|
465 | if (e.osm instanceof INode) {
|
---|
466 | for (IPrimitive ref : e.osm.getReferrers(true)) {
|
---|
467 | if (ref instanceof IWay) {
|
---|
468 | IWay<?> w = (IWay<?>) ref;
|
---|
469 | final int i = w.getNodes().indexOf(e.osm);
|
---|
470 | if (i - 1 >= 0) {
|
---|
471 | final INode n = w.getNode(i - 1);
|
---|
472 | final Environment e2 = e.withPrimitive(n).withParent(w).withChild(e.osm);
|
---|
473 | if (left.matches(e2) && link.matches(e2.withLinkContext())) {
|
---|
474 | e.child = n;
|
---|
475 | e.index = i;
|
---|
476 | e.count = w.getNodesCount();
|
---|
477 | e.parent = w;
|
---|
478 | return true;
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 | } else if (ChildOrParentSelectorType.CHILD == type
|
---|
485 | && link.conds != null && !link.conds.isEmpty()
|
---|
486 | && link.conds.get(0) instanceof OpenEndPseudoClassCondition) {
|
---|
487 | if (e.osm instanceof INode) {
|
---|
488 | e.osm.visitReferrers(new MultipolygonOpenEndFinder(e));
|
---|
489 | return e.parent != null;
|
---|
490 | }
|
---|
491 | } else if (ChildOrParentSelectorType.CHILD == type) {
|
---|
492 | MatchingReferrerFinder collector = new MatchingReferrerFinder(e);
|
---|
493 | e.osm.visitReferrers(collector);
|
---|
494 | if (e.parent != null)
|
---|
495 | return true;
|
---|
496 | } else if (ChildOrParentSelectorType.PARENT == type) {
|
---|
497 | if (e.osm instanceof IWay) {
|
---|
498 | List<? extends INode> wayNodes = ((IWay<?>) e.osm).getNodes();
|
---|
499 | for (int i = 0; i < wayNodes.size(); i++) {
|
---|
500 | INode n = wayNodes.get(i);
|
---|
501 | if (left.matches(e.withPrimitive(n))
|
---|
502 | && link.matches(e.withChildAndIndexAndLinkContext(n, i, wayNodes.size()))) {
|
---|
503 | e.child = n;
|
---|
504 | e.index = i;
|
---|
505 | e.count = wayNodes.size();
|
---|
506 | return true;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | } else if (e.osm instanceof IRelation) {
|
---|
510 | List<? extends IRelationMember<?>> members = ((IRelation<?>) e.osm).getMembers();
|
---|
511 | for (int i = 0; i < members.size(); i++) {
|
---|
512 | IPrimitive member = members.get(i).getMember();
|
---|
513 | if (left.matches(e.withPrimitive(member))
|
---|
514 | && link.matches(e.withChildAndIndexAndLinkContext(member, i, members.size()))) {
|
---|
515 | e.child = member;
|
---|
516 | e.index = i;
|
---|
517 | e.count = members.size();
|
---|
518 | return true;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | }
|
---|
522 | }
|
---|
523 | return false;
|
---|
524 | }
|
---|
525 |
|
---|
526 | @Override
|
---|
527 | public Subpart getSubpart() {
|
---|
528 | return right.getSubpart();
|
---|
529 | }
|
---|
530 |
|
---|
531 | @Override
|
---|
532 | public Range getRange() {
|
---|
533 | return right.getRange();
|
---|
534 | }
|
---|
535 |
|
---|
536 | @Override
|
---|
537 | public Selector optimizedBaseCheck() {
|
---|
538 | return new ChildOrParentSelector(left, link, right.optimizedBaseCheck(), type);
|
---|
539 | }
|
---|
540 |
|
---|
541 | @Override
|
---|
542 | public String toString() {
|
---|
543 | return left.toString() + ' ' + (ChildOrParentSelectorType.PARENT == type ? '<' : '>') + link + ' ' + right;
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Super class of {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector} and
|
---|
549 | * {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector}.
|
---|
550 | * @since 5841
|
---|
551 | */
|
---|
552 | abstract class AbstractSelector implements Selector {
|
---|
553 |
|
---|
554 | protected final List<Condition> conds;
|
---|
555 |
|
---|
556 | protected AbstractSelector(List<Condition> conditions) {
|
---|
557 | this.conds = Utils.toUnmodifiableList(conditions);
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Determines if all conditions match the given environment.
|
---|
562 | * @param env The environment to check
|
---|
563 | * @return {@code true} if all conditions apply, false otherwise.
|
---|
564 | */
|
---|
565 | @Override
|
---|
566 | public boolean matches(Environment env) {
|
---|
567 | CheckParameterUtil.ensureParameterNotNull(env, "env");
|
---|
568 | for (Condition c : conds) {
|
---|
569 | try {
|
---|
570 | if (!c.applies(env)) return false;
|
---|
571 | } catch (PatternSyntaxException e) {
|
---|
572 | Logging.log(Logging.LEVEL_ERROR, "PatternSyntaxException while applying condition" + c + ':', e);
|
---|
573 | return false;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | return true;
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Returns the list of conditions.
|
---|
581 | * @return the list of conditions
|
---|
582 | */
|
---|
583 | public List<Condition> getConditions() {
|
---|
584 | return conds;
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * In a child selector, conditions on the link between a parent and a child object.
|
---|
590 | * See <a href="https://josm.openstreetmap.de/wiki/Help/Styles/MapCSSImplementation#Linkselector">wiki</a>
|
---|
591 | */
|
---|
592 | class LinkSelector extends AbstractSelector {
|
---|
593 |
|
---|
594 | public LinkSelector(List<Condition> conditions) {
|
---|
595 | super(conditions);
|
---|
596 | }
|
---|
597 |
|
---|
598 | @Override
|
---|
599 | public boolean matches(Environment env) {
|
---|
600 | Utils.ensure(env.isLinkContext(), "Requires LINK context in environment, got ''{0}''", env.getContext());
|
---|
601 | return super.matches(env);
|
---|
602 | }
|
---|
603 |
|
---|
604 | @Override
|
---|
605 | public Subpart getSubpart() {
|
---|
606 | throw new UnsupportedOperationException("Not supported yet.");
|
---|
607 | }
|
---|
608 |
|
---|
609 | @Override
|
---|
610 | public Range getRange() {
|
---|
611 | throw new UnsupportedOperationException("Not supported yet.");
|
---|
612 | }
|
---|
613 |
|
---|
614 | @Override
|
---|
615 | public Selector optimizedBaseCheck() {
|
---|
616 | throw new UnsupportedOperationException();
|
---|
617 | }
|
---|
618 |
|
---|
619 | @Override
|
---|
620 | public String toString() {
|
---|
621 | return "LinkSelector{conditions=" + conds + '}';
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * General selector. See <a href="https://josm.openstreetmap.de/wiki/Help/Styles/MapCSSImplementation#Selectors">wiki</a>
|
---|
627 | */
|
---|
628 | class GeneralSelector extends OptimizedGeneralSelector {
|
---|
629 |
|
---|
630 | public GeneralSelector(String base, Range zoom, List<Condition> conds, Subpart subpart) {
|
---|
631 | super(base, zoom, conds, subpart);
|
---|
632 | }
|
---|
633 |
|
---|
634 | public boolean matchesConditions(Environment e) {
|
---|
635 | return super.matches(e);
|
---|
636 | }
|
---|
637 |
|
---|
638 | @Override
|
---|
639 | public Selector optimizedBaseCheck() {
|
---|
640 | return new OptimizedGeneralSelector(this);
|
---|
641 | }
|
---|
642 |
|
---|
643 | @Override
|
---|
644 | public boolean matches(Environment e) {
|
---|
645 | return matchesBase(e) && super.matches(e);
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Superclass of {@link GeneralSelector}. Used to create an "optimized" copy of this selector that omits the base check.
|
---|
651 | * @see Selector#optimizedBaseCheck
|
---|
652 | */
|
---|
653 | class OptimizedGeneralSelector extends AbstractSelector {
|
---|
654 | public final String base;
|
---|
655 | public final Range range;
|
---|
656 | public final Subpart subpart;
|
---|
657 |
|
---|
658 | public OptimizedGeneralSelector(String base, Range range, List<Condition> conds, Subpart subpart) {
|
---|
659 | super(conds);
|
---|
660 | this.base = checkBase(base);
|
---|
661 | this.range = Objects.requireNonNull(range, "range");
|
---|
662 | this.subpart = subpart != null ? subpart : Subpart.DEFAULT_SUBPART;
|
---|
663 | }
|
---|
664 |
|
---|
665 | public OptimizedGeneralSelector(GeneralSelector s) {
|
---|
666 | this(s.base, s.range, s.conds, s.subpart);
|
---|
667 | }
|
---|
668 |
|
---|
669 | @Override
|
---|
670 | public Subpart getSubpart() {
|
---|
671 | return subpart;
|
---|
672 | }
|
---|
673 |
|
---|
674 | @Override
|
---|
675 | public Range getRange() {
|
---|
676 | return range;
|
---|
677 | }
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Set base and check if this is a known value.
|
---|
681 | * @param base value for base
|
---|
682 | * @return the matching String constant for a known value
|
---|
683 | * @throws IllegalArgumentException if value is not knwon
|
---|
684 | */
|
---|
685 | private static String checkBase(String base) {
|
---|
686 | switch(base) {
|
---|
687 | case "*": return BASE_ANY;
|
---|
688 | case "node": return BASE_NODE;
|
---|
689 | case "way": return BASE_WAY;
|
---|
690 | case "relation": return BASE_RELATION;
|
---|
691 | case "area": return BASE_AREA;
|
---|
692 | case "meta": return BASE_META;
|
---|
693 | case "canvas": return BASE_CANVAS;
|
---|
694 | case "setting": return BASE_SETTING;
|
---|
695 | case "settings": return BASE_SETTINGS;
|
---|
696 | default:
|
---|
697 | throw new IllegalArgumentException(MessageFormat.format("Unknown MapCSS base selector {0}", base));
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | public String getBase() {
|
---|
702 | return base;
|
---|
703 | }
|
---|
704 |
|
---|
705 | public boolean matchesBase(OsmPrimitiveType type) {
|
---|
706 | if (BASE_ANY.equals(base)) {
|
---|
707 | return true;
|
---|
708 | } else if (OsmPrimitiveType.NODE == type) {
|
---|
709 | return BASE_NODE.equals(base);
|
---|
710 | } else if (OsmPrimitiveType.WAY == type) {
|
---|
711 | return BASE_WAY.equals(base) || BASE_AREA.equals(base);
|
---|
712 | } else if (OsmPrimitiveType.RELATION == type) {
|
---|
713 | return BASE_AREA.equals(base) || BASE_RELATION.equals(base) || BASE_CANVAS.equals(base);
|
---|
714 | }
|
---|
715 | return false;
|
---|
716 | }
|
---|
717 |
|
---|
718 | public boolean matchesBase(IPrimitive p) {
|
---|
719 | if (!matchesBase(p.getType())) {
|
---|
720 | return false;
|
---|
721 | } else {
|
---|
722 | if (p instanceof IRelation) {
|
---|
723 | if (BASE_AREA.equals(base)) {
|
---|
724 | return ((IRelation<?>) p).isMultipolygon();
|
---|
725 | } else if (BASE_CANVAS.equals(base)) {
|
---|
726 | return p.get("#canvas") != null;
|
---|
727 | }
|
---|
728 | }
|
---|
729 | return true;
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | public boolean matchesBase(Environment e) {
|
---|
734 | return matchesBase(e.osm);
|
---|
735 | }
|
---|
736 |
|
---|
737 | @Override
|
---|
738 | public Selector optimizedBaseCheck() {
|
---|
739 | throw new UnsupportedOperationException();
|
---|
740 | }
|
---|
741 |
|
---|
742 | public static Range fromLevel(int a, int b) {
|
---|
743 | // for input validation in Range constructor below
|
---|
744 | double lower = 0;
|
---|
745 | double upper = Double.POSITIVE_INFINITY;
|
---|
746 | if (b != Integer.MAX_VALUE) {
|
---|
747 | lower = level2scale(b + 1);
|
---|
748 | }
|
---|
749 | if (a != 0) {
|
---|
750 | upper = level2scale(a);
|
---|
751 | }
|
---|
752 | return new Range(lower, upper);
|
---|
753 | }
|
---|
754 |
|
---|
755 | public static double level2scale(int lvl) {
|
---|
756 | if (lvl < 0)
|
---|
757 | throw new IllegalArgumentException("lvl must be >= 0 but is "+lvl);
|
---|
758 | // preliminary formula - map such that mapnik imagery tiles of the same
|
---|
759 | // or similar level are displayed at the given scale
|
---|
760 | return 2.0 * Math.PI * WGS84.a / Math.pow(2.0, lvl) / 2.56;
|
---|
761 | }
|
---|
762 |
|
---|
763 | public static int scale2level(double scale) {
|
---|
764 | if (scale < 0)
|
---|
765 | throw new IllegalArgumentException("scale must be >= 0 but is "+scale);
|
---|
766 | return (int) Math.floor(Math.log(2 * Math.PI * WGS84.a / 2.56 / scale) / Math.log(2));
|
---|
767 | }
|
---|
768 |
|
---|
769 | @Override
|
---|
770 | public String toString() {
|
---|
771 | return base
|
---|
772 | + (Range.ZERO_TO_INFINITY.equals(range) ? "" : range)
|
---|
773 | + (conds != null ? conds.stream().map(String::valueOf).collect(Collectors.joining("")) : "")
|
---|
774 | + (subpart != null && subpart != Subpart.DEFAULT_SUBPART ? ("::" + subpart) : "");
|
---|
775 | }
|
---|
776 | }
|
---|
777 | }
|
---|