misc/persolijn

osm-protobuf/src/test/java/osm/protobuf/RelationTest.java in master
Repositories | Summary | Log | Files

RelationTest.java (7740B) download


  1package osm.protobuf;
  2
  3import org.junit.Test;
  4
  5import java.io.File;
  6import java.io.RandomAccessFile;
  7import java.net.URI;
  8import java.util.HashMap;
  9import java.util.Iterator;
 10import java.util.List;
 11import java.util.Map;
 12import java.util.stream.Stream;
 13
 14import org.junit.Assert;
 15
 16import osm.message.Entity;
 17import osm.message.Relation;
 18import osm.message.Relation.RelationMemberType;
 19
 20/**
 21 * Demonstrates how to read a file. Reads sample.pbf from the resources folder
 22 * and prints details about it to the standard output.
 23 *
 24 * @author Michael Tandy
 25 */
 26public class RelationTest {
 27    public static record ExpectedRelation(long id, List<RelationMemberType> memberTypes, List<Long> memberIDs,
 28            List<String> tags) {
 29        public Map<String, String> tagMap() {
 30            Map<String, String> map = new HashMap<>();
 31            for (int i = 0; i < tags.size(); i += 2) {
 32                map.put(tags.get(i), tags.get(i + 1));
 33            }
 34            return map;
 35        }
 36    }
 37
 38    @Test
 39    public void test() throws Exception {
 40        ExpectedRelation[] expected = new ExpectedRelation[] {
 41                new ExpectedRelation(21855, List.of(RelationMemberType.WAY, RelationMemberType.WAY),
 42                        List.of(156255508l, 156255507l),
 43                        List.of("name", "Hatfield Tunnel", "type", "tunnel")),
 44                new ExpectedRelation(31640,
 45                        Stream.generate(() -> RelationMemberType.WAY).limit(234).toList(),
 46                        List.of(24541150l, 25896432l, 25896435l, 136990875l, 25896366l, 25896438l, 136990877l,
 47                                22329168l, 136990870l, 136990880l, 136990882l, 3220127l, 136990873l, 136990872l,
 48                                121267851l, 121267847l, 4515378l, 8145197l, 19745207l, 19745206l, 4518667l, 113946003l,
 49                                136990884l, 8126872l, 1019866l, 1935841l, 1935842l, 2837807l, 3617718l, 3617750l,
 50                                157868716l, 157868712l, 4232426l, 4232450l, 4232523l, 4234302l, 4234304l, 140969196l,
 51                                4274177l, 4288798l, 4288804l, 4288817l, 115708422l, 4290564l, 4290566l, 146405866l,
 52                                4290567l, 4290569l, 104494931l, 37726893l, 4300013l, 4327072l, 4385337l, 4515181l,
 53                                4719529l, 4719530l, 5924137l, 136990881l, 135775543l, 135775531l, 6006524l, 9227120l,
 54                                9228211l, 9228215l, 9228221l, 9228224l, 9265930l, 9362548l, 10943268l, 10943269l,
 55                                10944242l, 10989591l, 10991833l, 10996488l, 11070521l, 11071125l, 71403686l, 11071127l,
 56                                11071128l, 11071130l, 11071828l, 11071937l, 11071938l, 11071941l, 11072077l, 11072080l,
 57                                15091285l, 15091286l, 15091321l, 15091324l, 15091328l, 15091364l, 15091378l, 73271466l,
 58                                73271488l, 58718382l, 15091546l, 15091557l, 15091559l, 15091596l, 15091603l, 15091606l,
 59                                48827325l, 15091609l, 164157963l, 15091656l, 43026009l, 43026010l, 15091678l, 19826860l,
 60                                19826861l, 164157965l, 19973630l, 22277500l, 22277501l, 22277790l, 22277845l, 22277849l,
 61                                22278089l, 22278090l, 22278109l, 22278111l, 22278116l, 22278120l, 22278128l, 22278626l,
 62                                22328765l, 22328772l, 22328778l, 22328779l, 22330396l, 22679421l, 23040738l, 23040842l,
 63                                23040846l, 23040858l, 23117246l, 23294746l, 23506818l, 23506829l, 23517268l, 23517271l,
 64                                157868707l, 23598320l, 23598322l, 23598454l, 23737552l, 23737553l, 23737667l,
 65                                145738805l, 24541552l, 24541863l, 24541865l, 25681100l, 25681103l, 25681278l, 25681279l,
 66                                25681280l, 25681403l, 25681444l, 25681445l, 25690338l, 25690339l, 25690340l, 25690749l,
 67                                43023896l, 43023897l, 25690753l, 25690754l, 25690755l, 25690809l, 25690810l, 38328051l,
 68                                38328052l, 25691524l, 101712789l, 101712803l, 25692910l, 25698340l, 25698341l,
 69                                25698342l, 25698343l, 25698344l, 25698346l, 25698348l, 25698352l, 25698353l, 25698354l,
 70                                25698355l, 26263982l, 26955717l, 28366733l, 33971858l, 33971859l, 145738820l, 33995569l,
 71                                23737666l, 41093668l, 44051303l, 44131233l, 44131234l, 25690748l, 25690752l, 44033101l,
 72                                44202299l, 44317183l, 44317184l, 44834939l, 45317529l, 45675161l, 4275500l, 53574779l,
 73                                55041560l, 62941752l, 70914841l, 100443831l, 103058540l, 103058539l, 25691035l,
 74                                145809031l, 140969180l, 140969209l, 10989259l, 141570176l, 157621618l, 157868715l,
 75                                157868706l, 157868717l, 157868710l, 157868709l, 157868714l, 157868705l, 157868718l,
 76                                157868711l),
 77                        List.of("name", "NCN National Route 61", "network", "ncn", "ref", "61", "route", "bicycle",
 78                                "type", "route")),
 79                new ExpectedRelation(267403, List.of(RelationMemberType.NODE, RelationMemberType.NODE),
 80                        List.of(502550970l, 502552090l),
 81                        List.of("name", "Oaktree Close", "naptan:StopAreaCode", "210G896", "naptan:StopAreaType",
 82                                "GPBS", "naptan:verified", "no", "site", "stop_area", "source", "naptan_import", "type",
 83                                "site")),
 84                new ExpectedRelation(267404, List.of(RelationMemberType.NODE, RelationMemberType.NODE),
 85                        List.of(502550921l, 502552074l),
 86                        List.of("name", "Burfield Close", "naptan:StopAreaCode", "210G897", "naptan:StopAreaType",
 87                                "GPBS", "naptan:verified", "no", "site", "stop_area", "source", "naptan_import", "type",
 88                                "site")),
 89                new ExpectedRelation(267400, List.of(RelationMemberType.NODE, RelationMemberType.NODE),
 90                        List.of(502550963l, 502552081l),
 91                        List.of("name", "Jasmine Gardens", "naptan:StopAreaCode", "210G895", "naptan:StopAreaType",
 92                                "GPBS", "naptan:verified", "no", "site", "stop_area", "source", "naptan_import", "type",
 93                                "site")),
 94        };
 95
 96        URI testFileURL = RelationTest.class.getClassLoader().getResource("protobuf-test.osm.pbf").toURI();
 97
 98        Iterator<Entity> entities = new BlobSpliterator(
 99                new RandomAccessFile(new File(testFileURL), "r"),
100                System.out::println)
101                .stream()
102                .flatMap(List::stream)
103                .filter(Relation.class::isInstance)
104                .iterator();
105
106        int expectedIndex = 0;
107        while (entities.hasNext() && expectedIndex < expected.length) {
108            Relation relation = (Relation) entities.next();
109            ExpectedRelation ex = expected[expectedIndex];
110
111            Assert.assertEquals(ex.id, relation.getID());
112            Assert.assertEquals(ex.tagMap(), relation.getTags());
113            Assert.assertEquals("amount of members", ex.memberIDs.size(), relation.getMembers().size());
114
115            for (int i = 0; i < ex.memberIDs.size(); i++) {
116                Assert.assertEquals((long) ex.memberIDs.get(i), relation.getMembers().get(i).getID());
117                Assert.assertEquals(ex.memberTypes.get(i), relation.getMembers().get(i).getType());
118            }
119
120            expectedIndex++;
121        }
122    }
123}