1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.command.Command;
|
---|
13 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
18 | import org.openstreetmap.josm.data.validation.Test;
|
---|
19 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Checks for untagged ways
|
---|
24 | *
|
---|
25 | * @author frsantos
|
---|
26 | */
|
---|
27 | public class UntaggedWay extends Test
|
---|
28 | {
|
---|
29 | /** Empty way error */
|
---|
30 | protected static final int EMPTY_WAY = 301;
|
---|
31 | /** Untagged way error */
|
---|
32 | protected static final int UNTAGGED_WAY = 302;
|
---|
33 | /** Unnamed way error */
|
---|
34 | protected static final int UNNAMED_WAY = 303;
|
---|
35 | /** One node way error */
|
---|
36 | protected static final int ONE_NODE_WAY = 304;
|
---|
37 | /** Unnamed junction error */
|
---|
38 | protected static final int UNNAMED_JUNCTION = 305;
|
---|
39 | /** Untagged, but commented way error */
|
---|
40 | protected static final int COMMENTED_WAY = 306;
|
---|
41 |
|
---|
42 | private LinkedList<Way> multipolygonways;
|
---|
43 |
|
---|
44 | /** Ways that must have a name */
|
---|
45 | public static final Set<String> NAMED_WAYS = new HashSet<String>();
|
---|
46 | static
|
---|
47 | {
|
---|
48 | NAMED_WAYS.add( "motorway" );
|
---|
49 | NAMED_WAYS.add( "trunk" );
|
---|
50 | NAMED_WAYS.add( "primary" );
|
---|
51 | NAMED_WAYS.add( "secondary" );
|
---|
52 | NAMED_WAYS.add( "tertiary" );
|
---|
53 | NAMED_WAYS.add( "residential" );
|
---|
54 | NAMED_WAYS.add( "pedestrian" ); ;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Constructor
|
---|
59 | */
|
---|
60 | public UntaggedWay()
|
---|
61 | {
|
---|
62 | super(tr("Untagged, empty and one node ways."),
|
---|
63 | tr("This test checks for untagged, empty and one node ways."));
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void visit(Way w)
|
---|
68 | {
|
---|
69 | if (!w.isUsable()) return;
|
---|
70 |
|
---|
71 | Map<String, String> tags = w.getKeys();
|
---|
72 | if( tags.size() != 0 )
|
---|
73 | {
|
---|
74 | String highway = tags.get("highway");
|
---|
75 | if(highway != null && NAMED_WAYS.contains(highway))
|
---|
76 | {
|
---|
77 | if( !tags.containsKey("name") && !tags.containsKey("ref") )
|
---|
78 | {
|
---|
79 | boolean isRoundabout = false;
|
---|
80 | boolean hasName = false;
|
---|
81 | for( String key : w.keySet())
|
---|
82 | {
|
---|
83 | hasName = key.startsWith("name:") || key.endsWith("_name") || key.endsWith("_ref");
|
---|
84 | if( hasName )
|
---|
85 | break;
|
---|
86 | if(key.equals("junction"))
|
---|
87 | {
|
---|
88 | isRoundabout = w.get("junction").equals("roundabout");
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | if( !hasName && !isRoundabout)
|
---|
94 | errors.add( new TestError(this, Severity.WARNING, tr("Unnamed ways"), UNNAMED_WAY, w) );
|
---|
95 | else if(isRoundabout)
|
---|
96 | errors.add( new TestError(this, Severity.WARNING, tr("Unnamed junction"), UNNAMED_JUNCTION, w) );
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | if(!w.isTagged() && !multipolygonways.contains(w))
|
---|
102 | {
|
---|
103 | if(w.hasKeys())
|
---|
104 | errors.add( new TestError(this, Severity.WARNING, tr("Untagged ways (commented)"), COMMENTED_WAY, w) );
|
---|
105 | else
|
---|
106 | errors.add( new TestError(this, Severity.WARNING, tr("Untagged ways"), UNTAGGED_WAY, w) );
|
---|
107 | }
|
---|
108 |
|
---|
109 | if( w.getNodesCount() == 0 )
|
---|
110 | {
|
---|
111 | errors.add( new TestError(this, Severity.ERROR, tr("Empty ways"), EMPTY_WAY, w) );
|
---|
112 | }
|
---|
113 | else if( w.getNodesCount() == 1 )
|
---|
114 | {
|
---|
115 | errors.add( new TestError(this, Severity.ERROR, tr("One node ways"), ONE_NODE_WAY, w) );
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void startTest(ProgressMonitor monitor)
|
---|
122 | {
|
---|
123 | super.startTest(monitor);
|
---|
124 | multipolygonways = new LinkedList<Way>();
|
---|
125 | for (Relation r : Main.main.getCurrentDataSet().getRelations())
|
---|
126 | {
|
---|
127 | if(r.isUsable() && "multipolygon".equals(r.get("type")))
|
---|
128 | {
|
---|
129 | for (RelationMember m : r.getMembers())
|
---|
130 | {
|
---|
131 | if(m.getMember() != null && m.getMember() instanceof Way &&
|
---|
132 | m.getMember().isUsable() && !m.getMember().isTagged())
|
---|
133 | multipolygonways.add((Way)m.getMember());
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void endTest()
|
---|
141 | {
|
---|
142 | multipolygonways = null;
|
---|
143 | super.endTest();
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public boolean isFixable(TestError testError)
|
---|
148 | {
|
---|
149 | if( testError.getTester() instanceof UntaggedWay )
|
---|
150 | {
|
---|
151 | return testError.getCode() == EMPTY_WAY
|
---|
152 | || testError.getCode() == ONE_NODE_WAY;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public Command fixError(TestError testError)
|
---|
160 | {
|
---|
161 | return DeleteCommand.delete(Main.map.mapView.getEditLayer(), testError.getPrimitives());
|
---|
162 | }
|
---|
163 | }
|
---|