WayTest.java (16641B)
1 package osm.protobuf; 2 3 import java.io.File; 4 import java.io.RandomAccessFile; 5 import java.net.URI; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 11 import org.junit.Assert; 12 import org.junit.Test; 13 14 import osm.message.Entity; 15 import osm.message.Way; 16 17 /** 18 * Demonstrates how to read a file. Reads sample.pbf from the resources folder 19 * and prints details about it to the standard output. 20 * 21 * @author Michael Tandy 22 */ 23 public class WayTest { 24 public static record ExpectedWay(long id, List<Long> children, List<String> tags) { 25 26 public Map<String, String> tagMap() { 27 Map<String, String> map = new HashMap<>(); 28 for (int i = 0; i < tags.size(); i += 2) { 29 map.put(tags.get(i), tags.get(i + 1)); 30 } 31 return map; 32 } 33 } 34 35 @Test 36 public void test() throws Exception { 37 ExpectedWay[] expected = new ExpectedWay[] { 38 new ExpectedWay(158788812, 39 List.of(1709246789l, 1709246746l, 1709246741l, 1709246791l), 40 List.of("highway", "footway")), 41 new ExpectedWay(53588781, 42 List.of(676945323l, 676945327l, 676945325l, 676945326l, 676945331l, 43 676945323l), 44 List.of("landuse", "garages", "source", "survey")), 45 new ExpectedWay(158788810, 46 List.of(1709246675l, 1709246737l, 1709246734l, 1709246676l), 47 List.of("highway", "footway")), 48 new ExpectedWay(156255508, 49 List.of(45169425l, 1685167371l, 1685167341l, 1685167287l, 1685167296l, 50 1685167315l, 1685167387l, 51 1685167328l, 1685167373l, 1685167290l, 1685167282l, 52 1685167313l, 1685167381l, 53 1685167391l, 1685167394l, 1685167304l, 1685167376l, 54 1606957353l), 55 List.of("carriageway_ref", "A", "highway", "motorway", "lanes", "3", 56 "layer", "-1", "lit", 57 "yes", "maxspeed", "national", "name", 58 "Hatfield Tunnel", "oneway", "yes", "ref", 59 "A1(M)", "source:maxspeed", "local_knowledge", "tunnel", 60 "yes")), 61 new ExpectedWay(54932035, 62 List.of(691202854l, 691202855l, 691202857l), 63 List.of("highway", "residential", "name", "Jasmine Gardens", "source", 64 "OS_OpenData_StreetView")), 65 new ExpectedWay(16946553, 66 List.of(175697671l, 175697862l, 175697821l, 175697824l), 67 List.of("highway", "residential", "name", "Oak Tree Close")), 68 new ExpectedWay(52083876, 69 List.of(663806653l, 663806656l, 663806658l), 70 List.of("highway", "service")), 71 new ExpectedWay(55081202, 72 List.of(692944951l, 692944957l, 692944963l, 692944966l, 692944951l), 73 List.of("leisure", "common", "source", "yahoo")), 74 new ExpectedWay(16946600, 75 List.of(175698430l, 175698550l, 691203053l, 691203054l, 175698551l, 76 1709246675l, 175698553l, 77 1709246789l, 691203055l), 78 List.of("highway", "residential", "name", "Harmony Close", "source", 79 "OS_OpenData_StreetView")), 80 new ExpectedWay(16946559, 81 List.of(175697862l, 175697881l), 82 List.of("created_by", "Potlatch 0.5d", "highway", "residential", "name", 83 "Oak Tree Close")), 84 new ExpectedWay(16945846, 85 List.of(175684459l, 175685100l, 175685102l, 175685104l, 676945189l, 86 175685106l, 676945315l, 87 175685109l, 175685111l, 175684462l), 88 List.of("highway", "residential", "name", "Stockbreach Close")), 89 new ExpectedWay(54932037, 90 List.of(175698553l, 691202860l), 91 List.of("highway", "residential", "name", "Harmony Close", "source", 92 "OS_OpenData_StreetView")), 93 new ExpectedWay(16946584, 94 List.of(175698155l, 175698323l, 175698430l, 1709246676l, 175698324l, 95 691203051l), 96 List.of("highway", "residential", "name", "The Minims", "source", 97 "OS_OpenData_StreetView")), 98 new ExpectedWay(8361329, 99 List.of(25365926l, 25365927l, 25365928l), 100 List.of("highway", "residential", "name", "The Paddock", "source", 101 "OS_OpenData_StreetView")), 102 new ExpectedWay(16945926, 103 List.of(175686498l, 175686201l, 663806661l, 175686499l), 104 List.of("highway", "residential", "name", "Wellfield Close")), 105 new ExpectedWay(52083878, 106 List.of(663806664l, 663806666l, 663806668l, 663806670l, 663806672l, 107 663806673l, 663806664l), 108 List.of("leisure", "common", "source", "yahoo")), 109 new ExpectedWay(3084923, 110 List.of(675146l, 1145410964l, 1539682123l, 32950368l, 241806356l, 111 675148l, 675149l, 820969139l, 112 175698155l, 675150l, 390911769l, 647224486l, 675151l, 113 175685910l, 14713407l, 175684463l, 114 647057820l, 647224485l, 1739780291l), 115 List.of("abutters", "residential", "highway", "secondary", "name", 116 "Wellfield Road", "ref", 117 "B197")), 118 new ExpectedWay(8361331, 119 List.of(25365925l, 691203049l, 25365930l, 25365931l), 120 List.of("highway", "residential", "name", "Walsingham Close", "source", 121 "OS_OpenData_StreetView")), 122 new ExpectedWay(54932038, 123 List.of(175699187l, 691202861l), 124 List.of("highway", "residential", "name", "Middlefield", "source", 125 "OS_OpenData_StreetView")), 126 new ExpectedWay(16946620, 127 List.of(175698975l, 691203109l, 175699187l, 691203110l, 691203111l), 128 List.of("highway", "residential", "name", "Middlefield", "source", 129 "OS_OpenData_StreetView")), 130 new ExpectedWay(157868709, 131 List.of(1701110757l, 1701110775l), 132 List.of("bridge", "yes", "cycleway", "track", "highway", "cycleway", 133 "layer", "1", "name", 134 "Alban Way", "ncn_ref", "61", "railway", "abandoned")), 135 new ExpectedWay(52083877, 136 List.of(663806656l, 663806661l), 137 List.of("highway", "service")), 138 new ExpectedWay(53588764, 139 List.of(676945267l, 677438877l, 677439947l, 676945292l, 676945293l, 140 676945332l, 647224613l, 141 175686498l), 142 List.of("highway", "footway")), 143 new ExpectedWay(49161822, 144 List.of(30983851l, 623624257l, 623624154l, 623624259l, 623624261l), 145 List.of("highway", "residential", "name", "Worcester Road")), 146 new ExpectedWay(49161823, 147 List.of(623624259l, 691203098l, 691203099l, 623624267l), 148 List.of("highway", "residential", "name", "Ely Close")), 149 new ExpectedWay(53588782, 150 List.of(676945327l, 676945329l), 151 List.of("bicycle", "no", "highway", "footway", "source", "survey")), 152 new ExpectedWay(54932044, 153 List.of(691202869l, 691202855l), 154 List.of("highway", "residential", "name", "Jasmine Gardens", "source", 155 "OS_OpenData_StreetView")), 156 new ExpectedWay(49161817, 157 List.of(623624154l, 623624155l, 623624156l), 158 List.of("highway", "residential", "name", "Malvern Close")), 159 new ExpectedWay(53588780, 160 List.of(676945350l, 676945352l, 676945334l, 676945335l, 676945346l, 161 676945347l, 676945350l), 162 List.of("building", "yes", "name", "Friendship House", "source", 163 "survey")), 164 new ExpectedWay(53588749, 165 List.of(676945199l, 676945316l, 676945317l, 676945195l, 676945319l, 166 676945197l, 676945199l), 167 List.of("landuse", "garages", "source", "survey")), 168 new ExpectedWay(4673618, 169 List.of(675148l, 25365921l, 1539682089l, 25365922l, 1539682039l, 170 691202838l, 25365923l, 171 25365924l, 25365925l, 25365926l, 175698975l, 30983851l, 172 175697671l, 30983852l, 173 691203106l, 30983853l, 240134267l, 267826070l, 174 365548881l, 32953193l, 175683944l, 175 927070648l, 647225601l, 677440300l, 32953194l, 176 673784380l, 32953195l), 177 List.of("highway", "tertiary", "name", "Lemsford Road")), 178 new ExpectedWay(53638158, 179 List.of(677439941l, 677439943l, 677439944l, 677439946l, 676945241l, 180 175683342l, 653970876l, 181 653970877l, 1697422651l, 651594517l, 676945267l, 182 677438877l, 677439947l, 647224485l, 183 1739780291l, 1739780285l, 1739780280l, 672628083l, 184 651652534l, 651652536l, 1739780294l, 185 623540483l, 623540479l, 623540472l, 623540467l, 186 647224465l, 647057820l, 175684463l, 187 14713407l, 175685910l, 675151l, 692887101l, 647224486l, 188 647105115l, 647105117l, 189 647105119l, 647105121l, 647105123l, 647105125l, 190 647105127l, 647105128l, 647105129l, 191 647105130l, 647105131l, 647105132l, 1111758071l, 192 647105133l, 1111758069l, 647105134l, 193 1111758072l, 647105135l, 1111758067l, 647105136l, 194 647105137l, 647105138l, 647105139l, 195 647105140l, 647105141l, 647105047l, 1692947499l, 196 647105142l, 647105143l, 647105144l, 197 647105145l, 647105146l, 647105147l, 647105148l, 198 647105149l, 647105150l, 647105151l, 199 647105152l, 647105153l, 647105154l, 647105155l, 200 647105156l, 647105157l, 647105158l, 201 647105159l, 647105160l, 647105161l, 647105162l, 202 647105163l, 647105164l, 647105165l, 203 647105166l, 534874147l, 534873285l, 534873274l, 204 534873262l, 534873251l, 534873208l, 205 534873185l, 534873171l, 647105167l, 647105168l, 206 647105169l, 647105170l, 647105171l, 207 647105172l, 647105102l, 647225601l, 677439941l), 208 List.of("landuse", "residential")), 209 new ExpectedWay(53588748, 210 List.of(676945322l, 676945325l, 676945327l, 676945320l, 676945192l, 211 676945315l), 212 List.of("bicycle", "no", "highway", "footway", "source", "survey")), 213 new ExpectedWay(50772651, 214 List.of(175685111l, 676945322l, 175685506l, 647224613l), 215 List.of("highway", "residential", "name", "Town Fields")), 216 new ExpectedWay(158788824, 217 List.of(175685507l, 1709246749l), 218 List.of("highway", "footway")), 219 new ExpectedWay(54932042, 220 List.of(691202866l, 691202871l, 691202873l), 221 List.of("highway", "residential", "name", "Jasmine Gardens", "source", 222 "OS_OpenData_StreetView")), 223 new ExpectedWay(53152061, 224 List.of(672663467l, 672663468l, 672663469l, 672663470l, 672663471l, 225 672663473l, 672663474l, 226 672663476l, 672663477l, 672663478l, 672663467l), 227 List.of("amenity", "retirement _home", "building", "yes", "name", 228 "Greenacres", "source:area", 229 "yahoo", "source:name", "survey")), 230 new ExpectedWay(53638215, 231 List.of(175685506l, 676945329l, 175685507l), 232 List.of("highway", "service", "source", "survey")), 233 new ExpectedWay(157868710, 234 List.of(1701110775l, 287659881l, 692887118l, 1709246791l, 1709246749l, 235 240134269l, 240134268l), 236 List.of("cycleway", "track", "highway", "cycleway", "name", "Alban Way", 237 "ncn_ref", "61", 238 "railway", "abandoned")), 239 new ExpectedWay(54932036, 240 List.of(25365927l, 691202858l), 241 List.of("highway", "residential", "name", "The Paddock", "source", 242 "OS_OpenData_StreetView")), 243 new ExpectedWay(54932039, 244 List.of(175697821l, 691202863l), 245 List.of("highway", "residential", "name", "Oak Tree Close", "source", 246 "OS_OpenData_StreetView")), 247 new ExpectedWay(55081204, 248 List.of(692945016l, 692945017l, 692945018l, 692945019l, 692945020l, 249 692945021l, 692945022l, 250 692945016l), 251 List.of("leisure", "common")), 252 new ExpectedWay(55071941, 253 List.of(692887118l, 692887101l), 254 List.of("foot", "yes", "highway", "footway")), 255 new ExpectedWay(157868707, 256 List.of(240134268l, 240134269l, 1709246749l, 1709246791l, 692887118l, 257 287659881l), 258 List.of("cycleway", "track", "highway", "cycleway", "name", "Alban Way", 259 "ncn_ref", "61")), 260 261 }; 262 263 URI testFileURL = WayTest.class.getClassLoader().getResource("protobuf-test.osm.pbf").toURI(); 264 265 Iterator<Entity> entities = new BlobSpliterator( 266 new RandomAccessFile(new File(testFileURL), "r"), 267 System.out::println) 268 .stream() 269 .flatMap(List::stream) 270 .filter(Way.class::isInstance) 271 .iterator(); 272 273 int expectedIndex = 0; 274 while (entities.hasNext() && expectedIndex < expected.length) { 275 Way way = (Way) entities.next(); 276 ExpectedWay ex = expected[expectedIndex]; 277 278 Assert.assertEquals(ex.id, way.getID()); 279 Assert.assertEquals(ex.children, way.children); 280 System.out.printf("expecting:\t%s\ngot:\t\t%s\n", ex.tagMap(), way.getTags()); 281 Assert.assertEquals(ex.tagMap(), way.getTags()); 282 283 expectedIndex++; 284 } 285 } 286 }