| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package uk.me.parabola.util; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.io.FileNotFoundException; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.InputStream; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.LinkedHashSet; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import java.util.Set; |
| 29 |
import java.util.stream.Collectors; |
| 30 |
|
| 31 |
import org.junit.Test; |
| 32 |
|
| 33 |
import func.lib.Args; |
| 34 |
import uk.me.parabola.imgfmt.Utils; |
| 35 |
import uk.me.parabola.imgfmt.app.Area; |
| 36 |
import uk.me.parabola.imgfmt.app.Coord; |
| 37 |
import uk.me.parabola.mkgmap.osmstyle.function.IsInFunction; |
| 38 |
import uk.me.parabola.mkgmap.reader.osm.Element; |
| 39 |
import uk.me.parabola.mkgmap.reader.osm.FeatureKind; |
| 40 |
import uk.me.parabola.mkgmap.reader.osm.Node; |
| 41 |
import uk.me.parabola.mkgmap.reader.osm.OsmMapDataSource; |
| 42 |
import uk.me.parabola.mkgmap.reader.osm.Way; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class IsInUtilTest { |
| 50 |
|
| 51 |
Area testSourceBbox = null; |
| 52 |
|
| 53 |
private static final String allPointMethods = "in,in_or_on,on"; |
| 54 |
private static final String allLineMethods = "all,all_in_or_on,on,any,none"; |
| 55 |
private static final String allPolygonMethods = "all,any"; |
| 56 |
|
| 57 |
private static final Map<Integer, String> pointMethods = new HashMap<>(); |
| 58 |
private static final Map<Integer, String> lineMethods = new HashMap<>(); |
| 59 |
private static final Map<Integer, String> polygonMethods = new HashMap<>(); |
| 60 |
|
| 61 |
public IsInUtilTest() { |
| 62 |
|
| 63 |
pointMethods.put(1, "in,in_or_on"); |
| 64 |
pointMethods.put(2, "in_or_on,on"); |
| 65 |
pointMethods.put(4, ""); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
lineMethods.put(1, "all,all_in_or_on,any"); |
| 77 |
lineMethods.put(2, "all_in_or_on,on"); |
| 78 |
lineMethods.put(3, "all,all_in_or_on,any"); |
| 79 |
lineMethods.put(4, "none"); |
| 80 |
|
| 81 |
lineMethods.put(6, "none"); |
| 82 |
lineMethods.put(7, "any"); |
| 83 |
|
| 84 |
polygonMethods.put(1, "all,any"); |
| 85 |
polygonMethods.put(2, "all,any"); |
| 86 |
polygonMethods.put(3, "all,any"); |
| 87 |
polygonMethods.put(4, ""); |
| 88 |
|
| 89 |
polygonMethods.put(6, ""); |
| 90 |
polygonMethods.put(7, "any"); |
| 91 |
} |
| 92 |
|
| 93 |
private static boolean invokeMethod(IsInFunction anInst, String method, FeatureKind kind, Element el) { |
| 94 |
anInst.setParams(Arrays.asList("landuse", "residential", method), kind); |
| 95 |
String rslt = anInst.calcImpl(el); |
| 96 |
return "true".equals(rslt); |
| 97 |
} |
| 98 |
|
| 99 |
public List<String> testWithVariants(FeatureKind kind, Element el, String name, Set<Way> polygons) { |
| 100 |
List<String> errors = new ArrayList<>(); |
| 101 |
|
| 102 |
IsInFunction anInst = new IsInFunction(); |
| 103 |
List<Element> matchingPolygons = new ArrayList<>(); |
| 104 |
for (Way polygon : polygons) |
| 105 |
matchingPolygons.add(polygon); |
| 106 |
anInst.unitTestAugment(new ElementQuadTree(testSourceBbox, matchingPolygons)); |
| 107 |
|
| 108 |
String expectedVal = el.getTag("expected"); |
| 109 |
if (expectedVal != null && !"?".equals(expectedVal)) { |
| 110 |
int expected = Integer.parseInt(expectedVal); |
| 111 |
String allMethods = ""; |
| 112 |
Map<Integer, String> methods = null; |
| 113 |
switch (kind) { |
| 114 |
case POINT: |
| 115 |
allMethods = allPointMethods; |
| 116 |
methods = pointMethods; |
| 117 |
break; |
| 118 |
case POLYLINE: |
| 119 |
allMethods = allLineMethods; |
| 120 |
methods = lineMethods; |
| 121 |
break; |
| 122 |
case POLYGON: |
| 123 |
allMethods = allPolygonMethods; |
| 124 |
methods = polygonMethods; |
| 125 |
break; |
| 126 |
} |
| 127 |
if (!methods.containsKey(expected)) { |
| 128 |
errors.add(name + " failed, no methods for expected: " + expectedVal); |
| 129 |
return errors; |
| 130 |
} |
| 131 |
String[] trueMethods = methods.get(expected).split(","); |
| 132 |
if (trueMethods[0].isEmpty()) |
| 133 |
trueMethods = new String[0]; |
| 134 |
List<String> falseMethods = new ArrayList<>(); |
| 135 |
for (String tstMethod : allMethods.split(",")) { |
| 136 |
boolean inList = false; |
| 137 |
for (String trueMethod : trueMethods) |
| 138 |
if (tstMethod.equals(trueMethod)) { |
| 139 |
inList = true; |
| 140 |
break; |
| 141 |
} |
| 142 |
if (!inList) |
| 143 |
falseMethods.add(tstMethod); |
| 144 |
} |
| 145 |
|
| 146 |
for (String tstMethod : trueMethods) |
| 147 |
if (!invokeMethod(anInst, tstMethod, kind, el)) |
| 148 |
errors.add(name + " failed, expected: " + expectedVal + ". " + tstMethod + " should be true"); |
| 149 |
for (String tstMethod : falseMethods) |
| 150 |
if (invokeMethod(anInst, tstMethod, kind, el)) |
| 151 |
errors.add(name + " failed, expected: " + expectedVal + ". " + tstMethod + " should be false"); |
| 152 |
|
| 153 |
if (!errors.isEmpty() || !(el instanceof Way)) |
| 154 |
return errors; |
| 155 |
Way w2 = (Way) el.copy(); |
| 156 |
Collections.reverse(w2.getPoints()); |
| 157 |
for (String tstMethod : trueMethods) |
| 158 |
if (!invokeMethod(anInst, tstMethod, kind, w2)) |
| 159 |
errors.add(name + " failed reversed, expected: " + expectedVal + ". " + tstMethod + " should be true"); |
| 160 |
for (String tstMethod : falseMethods) |
| 161 |
if (invokeMethod(anInst, tstMethod, kind, w2)) |
| 162 |
errors.add(name + " failed reversed, expected: " + expectedVal + ". " + tstMethod + " should be false"); |
| 163 |
|
| 164 |
if (!errors.isEmpty() || !w2.hasIdenticalEndPoints()) |
| 165 |
return errors; |
| 166 |
List<Coord> points = w2.getPoints(); |
| 167 |
for (int i = 1; i < w2.getPoints().size(); i++) { |
| 168 |
points.remove(points.size() - 1); |
| 169 |
Collections.rotate(points, 1); |
| 170 |
points.add(points.get(0)); |
| 171 |
for (String tstMethod : trueMethods) |
| 172 |
if (!invokeMethod(anInst, tstMethod, kind, w2)) |
| 173 |
errors.add(name + " failed rotated, expected: " + expectedVal + ". " + tstMethod + " should be true"); |
| 174 |
for (String tstMethod : falseMethods) |
| 175 |
if (invokeMethod(anInst, tstMethod, kind, w2)) |
| 176 |
errors.add(name + " failed rotated, expected: " + expectedVal + ". " + tstMethod + " should be false"); |
| 177 |
} |
| 178 |
} |
| 179 |
return errors; |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void testBasic() throws FileNotFoundException { |
| 184 |
|
| 185 |
|
| 186 |
class TestSource extends OsmMapDataSource { |
| 187 |
@Override |
| 188 |
public Set<String> getUsedTags() { |
| 189 |
|
| 190 |
return null; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public void load(String name, boolean addBackground) throws FileNotFoundException { |
| 195 |
try (InputStream is = Utils.openFile(name)) { |
| 196 |
parse(is, name); |
| 197 |
} catch (IOException e) { |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
elementSaver.finishLoading(); |
| 202 |
} |
| 203 |
} |
| 204 |
TestSource src = new TestSource(); |
| 205 |
src.config(new EnhancedProperties()); |
| 206 |
src.load(Args.TEST_RESOURCE_OSM + "is-in-samples.osm", false); |
| 207 |
testSourceBbox = src.getElementSaver().getBoundingBox(); |
| 208 |
|
| 209 |
ElementQuadTree qt = IsInFunction.buildTree(src.getElementSaver(), "landuse", "residential"); |
| 210 |
ArrayList<String> allErrors = new ArrayList<>(); |
| 211 |
for (Node n: src.getElementSaver().getNodes().values()) { |
| 212 |
String name = n.getTag("name"); |
| 213 |
if (name != null) { |
| 214 |
Area elementBbox = Area.getBBox(Collections.singletonList((n).getLocation())); |
| 215 |
Set<Way> polygons = qt.get(elementBbox).stream().map(e -> (Way) e) |
| 216 |
.collect(Collectors.toCollection(LinkedHashSet::new)); |
| 217 |
allErrors.addAll(testWithVariants(FeatureKind.POINT, n, name, polygons)); |
| 218 |
} |
| 219 |
} |
| 220 |
for (Way w: src.getElementSaver().getWays().values()) { |
| 221 |
String name = w.getTag("name"); |
| 222 |
if (name != null) { |
| 223 |
Area elementBbox = Area.getBBox(w.getPoints()); |
| 224 |
Set<Way> polygons = qt.get(elementBbox).stream().map(e -> (Way) e) |
| 225 |
.collect(Collectors.toCollection(LinkedHashSet::new)); |
| 226 |
if (name.startsWith("w")) |
| 227 |
allErrors.addAll(testWithVariants(FeatureKind.POLYLINE, w, name, polygons)); |
| 228 |
else |
| 229 |
allErrors.addAll(testWithVariants(FeatureKind.POLYGON, w, name, polygons)); |
| 230 |
} |
| 231 |
} |
| 232 |
for (String msg : allErrors) { |
| 233 |
System.err.println(msg); |
| 234 |
} |
| 235 |
assertTrue("Found errors. Check System.err content", allErrors.isEmpty()); |
| 236 |
} |
| 237 |
} |