├── src
├── test
│ ├── resources
│ │ └── sample.pbf
│ └── java
│ │ └── com
│ │ └── wolt
│ │ └── osm
│ │ └── parallelpbf
│ │ ├── encoder
│ │ ├── OsmEncoderTest.java
│ │ ├── OsmEntityEncoderTest.java
│ │ ├── WayEncoderTest.java
│ │ ├── OsmHeaderEncoderTest.java
│ │ ├── RelationEncoderTest.java
│ │ ├── StringTableEncoderTest.java
│ │ └── DenseNodesEncoderTest.java
│ │ ├── parser
│ │ ├── WayParserTest.java
│ │ ├── TagParserTest.java
│ │ ├── RelationParserTest.java
│ │ ├── NodeParserTest.java
│ │ └── InfoParserTest.java
│ │ ├── ParallelBinaryWriterExample.java
│ │ ├── blob
│ │ ├── ReadHeaderLengthTest.java
│ │ ├── ReadBlobTest.java
│ │ ├── BlobWriterTest.java
│ │ └── ReadHeaderTest.java
│ │ ├── ParalelBinaryParserExample.java
│ │ ├── io
│ │ ├── OSMDataReaderTest.java
│ │ ├── OSMHeaderReaderTest.java
│ │ ├── OSMReaderTest.java
│ │ └── OSMWriterTest.java
│ │ ├── TestObjectsFactory.java
│ │ └── ParallelBinaryParserIT.java
└── main
│ ├── java
│ └── com
│ │ └── wolt
│ │ └── osm
│ │ └── parallelpbf
│ │ ├── encoder
│ │ ├── package-info.java
│ │ ├── OsmEncoder.java
│ │ ├── OsmEntityEncoder.java
│ │ ├── OsmHeaderEncoder.java
│ │ ├── StringTableEncoder.java
│ │ ├── WayEncoder.java
│ │ ├── RelationEncoder.java
│ │ └── DenseNodesEncoder.java
│ │ ├── io
│ │ ├── package-info.java
│ │ ├── OSMReader.java
│ │ ├── OSMHeaderReader.java
│ │ ├── OSMDataReader.java
│ │ └── OSMWriter.java
│ │ ├── parser
│ │ ├── package-info.java
│ │ ├── WayParser.java
│ │ ├── RelationParser.java
│ │ ├── BaseParser.java
│ │ └── NodeParser.java
│ │ ├── blob
│ │ ├── package-info.java
│ │ ├── BlobInformation.java
│ │ ├── BlobWriter.java
│ │ └── BlobReader.java
│ │ ├── package-info.java
│ │ ├── entity
│ │ ├── package-info.java
│ │ ├── BoundBox.java
│ │ ├── OsmEntity.java
│ │ ├── Way.java
│ │ ├── Relation.java
│ │ ├── Node.java
│ │ ├── Info.java
│ │ ├── Header.java
│ │ └── RelationMember.java
│ │ └── ParallelBinaryWriter.java
│ └── proto
│ ├── fileformat.proto
│ └── osmformat.proto
├── spotbugsExclude.xml
├── .gitignore
├── checkstyle.xml
├── README.md
└── pom.xml
/src/test/resources/sample.pbf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/woltapp/parallelpbf/HEAD/src/test/resources/sample.pbf
--------------------------------------------------------------------------------
/spotbugsExclude.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/encoder/OsmEncoderTest.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import static org.junit.jupiter.api.Assertions.*;
6 |
7 | class OsmEncoderTest {
8 |
9 | @Test
10 | void testNanoScale() {
11 | double input = 100.5009;
12 |
13 | long actual = OsmEncoder.doubleToNanoScaled(input);
14 | assertEquals(100500900000L, actual);
15 | }
16 | }
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/encoder/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * Binary blobs encoding.
20 | */
21 |
22 | package com.wolt.osm.parallelpbf.encoder;
23 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/io/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * Binary blobs reading, writing and processing.
20 | */
21 | package com.wolt.osm.parallelpbf.io;
22 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/parser/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * Collection of OSM primitives parsers.
20 | */
21 | package com.wolt.osm.parallelpbf.parser;
22 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/blob/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * BlobHeader, Blob and stream reading operations.
20 | */
21 | package com.wolt.osm.parallelpbf.blob;
22 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * Parallel OSM PBF format parser.
20 | *
21 | * See https://github.com/akashihi/parallelpbf for the details and usage example.
22 | */
23 | package com.wolt.osm.parallelpbf;
24 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | /**
19 | * Definition of OSM data entities (v0.6 API compatible),
20 | * that will be extracted from the PBF file for the future
21 | * processing.
22 | */
23 | package com.wolt.osm.parallelpbf.entity;
24 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/encoder/OsmEncoder.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | /**
4 | * Base class for all encoders, provides common encoding functions.
5 | */
6 | public abstract class OsmEncoder {
7 | /**
8 | * Coordinates grid default granularity.
9 | */
10 | public static final int GRANULARITY = 100;
11 |
12 | /**
13 | * Single tag entry (key or value) is a integer index,
14 | * so 4 bytes per entry.
15 | */
16 | protected static final int TAG_ENTRY_SIZE = 4;
17 |
18 | /**
19 | * Single member entry (key or value) is a long value,
20 | * so 8 bytes per entry keeping both of them.
21 | */
22 | protected static final int MEMBER_ENTRY_SIZE = 8;
23 |
24 | /**
25 | * Conversion from nano- to non-scaled.
26 | */
27 | private static final double NANO = 1e9;
28 |
29 | /**
30 | * Convert double to nano-scaled long.
31 | * @param value double to convert.
32 | * @return value multiplied to 1e9 and rounded then.
33 | */
34 | protected static long doubleToNanoScaled(final double value) {
35 | return Math.round(value * NANO);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/encoder/OsmEntityEncoderTest.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import com.wolt.osm.parallelpbf.TestObjectsFactory;
4 | import com.wolt.osm.parallelpbf.entity.Node;
5 | import crosby.binary.Osmformat;
6 | import org.junit.jupiter.api.Test;
7 |
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
9 |
10 | class OsmEntityEncoderTest {
11 | private static class OsmEntityEncoderImpl extends OsmEntityEncoder {
12 |
13 | @Override
14 | public void addImpl(Node entity) {
15 |
16 | }
17 |
18 | @Override
19 | public int estimateSize() {
20 | return 0;
21 | }
22 |
23 | @Override
24 | public Osmformat.PrimitiveGroup.Builder writeImpl() {
25 | return Osmformat.PrimitiveGroup.newBuilder();
26 | }
27 | }
28 |
29 | @Test
30 | public void testNoUseAfterWrite() {
31 | OsmEntityEncoderImpl testedObject = new OsmEntityEncoderImpl();
32 | testedObject.add(TestObjectsFactory.node());
33 | testedObject.write();
34 | assertThrows(IllegalStateException.class, () -> testedObject.add(TestObjectsFactory.node()));
35 | }
36 | }
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/BoundBox.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.entity;
19 |
20 | import lombok.Data;
21 |
22 | /**
23 | * Wrapper of the bounding box of the map parsed.
24 | */
25 | @Data
26 | public final class BoundBox {
27 | /**
28 | * Lesser longitude of a bounding box.
29 | */
30 | private final double left;
31 |
32 | /**
33 | * Lesser latitude of a bounding box.
34 | */
35 | private final double top;
36 |
37 | /**
38 | * Bigger longitude of a bounding box.
39 | */
40 | private final double right;
41 |
42 | /**
43 | * Bigger latitude of a bounding gox.
44 | */
45 | private final double bottom;
46 | }
47 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/encoder/WayEncoderTest.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import com.wolt.osm.parallelpbf.TestObjectsFactory;
4 | import crosby.binary.Osmformat;
5 | import org.junit.jupiter.api.BeforeEach;
6 | import org.junit.jupiter.api.Test;
7 |
8 | import static org.junit.jupiter.api.Assertions.assertEquals;
9 |
10 | class WayEncoderTest {
11 | private StringTableEncoder stringEncoder;
12 |
13 | @BeforeEach
14 | public void setUp() {
15 | stringEncoder = new StringTableEncoder();
16 | }
17 |
18 | @Test
19 | public void testWaySize() {
20 | WayEncoder testedObject = new WayEncoder(stringEncoder);
21 | testedObject.add(TestObjectsFactory.way());
22 |
23 | assertEquals(40, testedObject.estimateSize());
24 | }
25 |
26 | @Test
27 | public void testWrite() {
28 | WayEncoder testedObject = new WayEncoder(stringEncoder);
29 | testedObject.add(TestObjectsFactory.way());
30 |
31 | Osmformat.PrimitiveGroup actual = testedObject.write().build();
32 |
33 | Osmformat.Way w = actual.getWays(0);
34 | assertEquals(1, w.getId());
35 | assertEquals(1, w.getKeys(0));
36 | assertEquals(1, w.getVals(0));
37 |
38 | assertEquals(3, w.getRefs(0));
39 | assertEquals(3, w.getRefs(1));
40 | assertEquals(-4, w.getRefs(2));
41 |
42 | assertEquals(3, w.getInfo().getVersion());
43 | assertEquals(5, w.getInfo().getChangeset());
44 | assertEquals(1, w.getInfo().getUid());
45 | assertEquals(true, w.getInfo().getVisible());
46 | }
47 | }
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/OsmEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.entity;
19 |
20 | import lombok.Data;
21 |
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | /**
26 | * Base class for the all OSM entities.
27 | *
28 | * All OSM v0.6 API entities have id and tags,
29 | * presented as unique keys with their values.
30 | *
31 | * For a PBF format we also store metadata for the entity.
32 | * @see Info
33 | */
34 | @Data
35 | public abstract class OsmEntity {
36 | /**
37 | * Entry id.
38 | */
39 | private final long id;
40 |
41 | /**
42 | * Entry tags map. May be empty.
43 | */
44 | private Map tags = new HashMap<>();
45 |
46 | /**
47 | * Entry metadata, can be null.
48 | *
49 | * @see Info
50 | */
51 | private Info info;
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/blob/BlobInformation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.blob;
19 |
20 | import lombok.Data;
21 |
22 | /**
23 | * A better wrapper over BlobHeader.
24 | *
25 | * Keeps blob size and blob type together for future processing.
26 | */
27 | @Data
28 | public class BlobInformation {
29 | /* OSM PBF Fileformat block types. See https://wiki.openstreetmap.org/wiki/PBF_Format for the details */
30 | /**
31 | * OSMData type block.
32 | */
33 | public static final String TYPE_OSM_DATA = "OSMData";
34 | /**
35 | * OSMHeader type block.
36 | */
37 | public static final String TYPE_OSM_HEADER = "OSMHeader";
38 | /**
39 | * Data blob size.
40 | */
41 | private final Integer size;
42 |
43 | /**
44 | * Data blob type.
45 | */
46 | private final String type;
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/encoder/OsmHeaderEncoderTest.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import com.google.protobuf.InvalidProtocolBufferException;
4 | import com.wolt.osm.parallelpbf.entity.BoundBox;
5 | import com.wolt.osm.parallelpbf.entity.Header;
6 | import crosby.binary.Osmformat;
7 | import org.junit.jupiter.api.Test;
8 |
9 | import static org.junit.jupiter.api.Assertions.*;
10 |
11 | class OsmHeaderEncoderTest {
12 | @Test
13 | void testHeaderNoBBox() throws InvalidProtocolBufferException {
14 | byte[] blob = OsmHeaderEncoder.encodeHeader(null);
15 |
16 | Osmformat.HeaderBlock actual = Osmformat.HeaderBlock.parseFrom(blob);
17 |
18 | assertFalse(actual.hasBbox());
19 |
20 | assertTrue(actual.hasWritingprogram());
21 | assertEquals("parallelpbf", actual.getWritingprogram());
22 |
23 | assertTrue(actual.getRequiredFeaturesList().contains(Header.FEATURE_OSM_SCHEMA));
24 | assertTrue(actual.getRequiredFeaturesList().contains(Header.FEATURE_DENSE_NODES));
25 | }
26 |
27 | @Test
28 | void testHeaderBBox() throws InvalidProtocolBufferException {
29 | BoundBox bbox = new BoundBox(1, 2, 4 ,8);
30 |
31 | byte[] blob = OsmHeaderEncoder.encodeHeader(bbox);
32 |
33 | Osmformat.HeaderBlock actual = Osmformat.HeaderBlock.parseFrom(blob);
34 |
35 | assertTrue(actual.hasBbox());
36 |
37 | assertEquals(1000000000L, actual.getBbox().getLeft());
38 | assertEquals(2000000000L, actual.getBbox().getTop());
39 | assertEquals(4000000000L, actual.getBbox().getRight());
40 | assertEquals(8000000000L, actual.getBbox().getBottom());
41 | }
42 | }
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/Way.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.entity;
19 |
20 | import lombok.Data;
21 | import lombok.EqualsAndHashCode;
22 | import lombok.ToString;
23 |
24 | import java.util.LinkedList;
25 | import java.util.List;
26 |
27 | /**
28 | * OSM Way entity.
29 | *
30 | * Way is a ordered, therefore directed, collection of nodes.
31 | *
32 | * @see Node
33 | */
34 | @Data
35 | @EqualsAndHashCode(callSuper = true)
36 | @ToString(callSuper = true)
37 | public final class Way extends OsmEntity {
38 | /**
39 | * Constructs Way setting mandatory fields.
40 | * @param id Required object id.
41 | */
42 | public Way(final long id) {
43 | super(id);
44 | }
45 |
46 | /**
47 | * Ordered list of nodes, making way. Should contain at least one node.
48 | *
49 | * @see Node
50 | */
51 | private final List nodes = new LinkedList<>();
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/Relation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.entity;
19 |
20 | import lombok.Data;
21 | import lombok.EqualsAndHashCode;
22 | import lombok.ToString;
23 |
24 | import java.util.LinkedList;
25 | import java.util.List;
26 |
27 | /**
28 | * OSM Relation entity.
29 | *
30 | * Groups several OSM entities (including other relations)
31 | * to the single logical entity.
32 | *
33 | * @see RelationMember
34 | */
35 | @Data
36 | @EqualsAndHashCode(callSuper = true)
37 | @ToString(callSuper = true)
38 | public final class Relation extends OsmEntity {
39 | /**
40 | * Entity constructor.
41 | * @param id Sets required object id during construction.
42 | */
43 | public Relation(final long id) {
44 | super(id);
45 | }
46 |
47 | /**
48 | * Ordered list of relation members. Can be empty.
49 | */
50 | private final List members = new LinkedList<>();
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/entity/Node.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.entity;
19 |
20 | import lombok.Data;
21 | import lombok.EqualsAndHashCode;
22 | import lombok.ToString;
23 |
24 | /**
25 | * OSM Node entity.
26 | *
27 | * Node is a most basic building block of the OSM database.
28 | */
29 | @Data
30 | @EqualsAndHashCode(callSuper = true)
31 | @ToString(callSuper = true)
32 | public final class Node extends OsmEntity {
33 | /**
34 | * Constructs Node setting mandatory fields.
35 | * @param id Required node id.
36 | * @param latitude Node latitude.
37 | * @param longitude Node longitude
38 | */
39 | public Node(final long id, final double latitude, final double longitude) {
40 | super(id);
41 | this.lat = latitude;
42 | this.lon = longitude;
43 | }
44 |
45 | /**
46 | * Node latitude.
47 | */
48 | private final double lat;
49 |
50 | /**
51 | * Node longitude.
52 | */
53 | private final double lon;
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/encoder/OsmEntityEncoder.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import com.wolt.osm.parallelpbf.entity.OsmEntity;
4 | import crosby.binary.Osmformat;
5 |
6 | /**
7 | * Entity specific extension of OsmEncoder.
8 | * @param Type of entity that encoder supports.
9 | */
10 | public abstract class OsmEntityEncoder extends OsmEncoder {
11 | /**
12 | * 'Write was called' flag.
13 | */
14 | private boolean built = false;
15 |
16 | /**
17 | * Type specific write implementation.
18 | * @param entity Osm entity to add to the encoder.
19 | */
20 | protected abstract void addImpl(T entity);
21 |
22 | /**
23 | * Type specific group writer implementation.
24 | * @return Group with entities of T type.
25 | */
26 | protected abstract Osmformat.PrimitiveGroup.Builder writeImpl();
27 |
28 | /**
29 | * Add entity to the encoder.
30 | * @param entity Entity to add.
31 | * @throws IllegalStateException when call after write() call.
32 | */
33 | public void add(final T entity) {
34 | if (built) {
35 | throw new IllegalStateException("Encoder content is already written");
36 | }
37 | addImpl(entity);
38 | }
39 |
40 | /**
41 | * Provides approximate size of the future blob.
42 | * @return Estimated approximate maximum size of a blob.
43 | */
44 | public abstract int estimateSize();
45 |
46 | /**
47 | * Build a blob from the collected data. Encoder will become
48 | * unusable after that call.
49 | * @return OSM PBF primitiveBlock blob.
50 | */
51 | public Osmformat.PrimitiveGroup.Builder write() {
52 | built = true;
53 | return writeImpl();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/wolt/osm/parallelpbf/encoder/OsmHeaderEncoder.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf.encoder;
2 |
3 | import com.wolt.osm.parallelpbf.entity.BoundBox;
4 | import com.wolt.osm.parallelpbf.entity.Header;
5 | import crosby.binary.Osmformat;
6 |
7 | /**
8 | * HeaderBlock encoder.
9 | */
10 | public final class OsmHeaderEncoder extends OsmEncoder {
11 | /**
12 | * Wraps bound box to OSM PBF entity.
13 | * @param boundBox Bound box to wrap.
14 | * @return HeaderBBox entity.
15 | */
16 | private static Osmformat.HeaderBBox encodeBoundBox(final BoundBox boundBox) {
17 | return Osmformat.HeaderBBox.newBuilder()
18 | .setLeft(doubleToNanoScaled(boundBox.getLeft()))
19 | .setTop(doubleToNanoScaled(boundBox.getTop()))
20 | .setRight(doubleToNanoScaled(boundBox.getRight()))
21 | .setBottom(doubleToNanoScaled(boundBox.getBottom()))
22 | .build();
23 | }
24 |
25 | /**
26 | * Generates OSM PBF header and add (optional) bounding box to it.
27 | * Header values are predefined and can't be set right now.
28 | * @param boundBox Bounding box to include into header. May be null.
29 | * @return array of bytes with binary representation of the header.
30 | */
31 | public static byte[] encodeHeader(final BoundBox boundBox) {
32 | Osmformat.HeaderBlock.Builder blob = Osmformat.HeaderBlock.newBuilder();
33 |
34 | if (boundBox != null) {
35 | blob.setBbox(encodeBoundBox(boundBox));
36 | }
37 |
38 | blob.addRequiredFeatures(Header.FEATURE_OSM_SCHEMA);
39 | blob.addRequiredFeatures(Header.FEATURE_DENSE_NODES);
40 |
41 | blob.setWritingprogram("parallelpbf");
42 |
43 | return blob.build().toByteArray();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/parser/WayParserTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.parser;
19 |
20 | import com.wolt.osm.parallelpbf.entity.Way;
21 | import com.wolt.osm.parallelpbf.TestObjectsFactory;
22 | import lombok.var;
23 | import org.junit.jupiter.api.Test;
24 |
25 | import java.util.function.Consumer;
26 |
27 | import static org.junit.jupiter.api.Assertions.assertEquals;
28 | import static org.junit.jupiter.api.Assertions.assertTrue;
29 |
30 | class WayParserTest {
31 | private final Consumer checker = (way) -> {
32 | assertEquals(1, way.getId());
33 | assertEquals(TestObjectsFactory.info, way.getInfo());
34 |
35 | var tags = way.getTags();
36 | assertTrue(tags.containsKey("tag"));
37 | assertEquals("value", tags.get("tag"));
38 |
39 | assertEquals(9000, way.getNodes().get(0).longValue());
40 | };
41 |
42 | @Test
43 | void testWayParse() {
44 | var testedObject = new WayParser(checker, TestObjectsFactory.stringTable);
45 | testedObject.parse(TestObjectsFactory.wayMessage);
46 | }
47 | }
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/ParallelBinaryWriterExample.java:
--------------------------------------------------------------------------------
1 | package com.wolt.osm.parallelpbf;
2 |
3 | import ch.qos.logback.classic.Level;
4 | import ch.qos.logback.classic.Logger;
5 | import com.wolt.osm.parallelpbf.entity.*;
6 | import lombok.SneakyThrows;
7 | import org.slf4j.LoggerFactory;
8 |
9 | import java.io.*;
10 |
11 | public class ParallelBinaryWriterExample {
12 | private ParallelBinaryWriter writer;
13 |
14 | private void processNodes(Node node) {
15 | writer.write(node);
16 | }
17 |
18 | private void processWays(Way way) {
19 | writer.write(way);
20 | }
21 |
22 | private void processRelations(Relation relation) {
23 | writer.write(relation);
24 | }
25 |
26 | @SneakyThrows
27 | private void closeOnComplete() {
28 | writer.close();
29 | }
30 |
31 | private void execute() throws IOException {
32 | Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
33 | root.setLevel(Level.TRACE);
34 |
35 | String outputFilename = System.getProperty("java.io.tmpdir")+"/parallel.pbf";
36 | File outputFile = new File(outputFilename);
37 | if (outputFile.exists()) {
38 | outputFile.delete();
39 | }
40 | outputFile.createNewFile();
41 | OutputStream output = new FileOutputStream(outputFile);
42 |
43 | writer = new ParallelBinaryWriter(output,1, null);
44 | writer.start();
45 |
46 | InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("sample.pbf");
47 | new ParallelBinaryParser(input, 1)
48 | .onComplete(this::closeOnComplete)
49 | .onNode(this::processNodes)
50 | .onWay(this::processWays)
51 | .onRelation(this::processRelations)
52 | .parse();
53 | output.close();
54 | }
55 |
56 | public static void main(String[] args) throws IOException {
57 | new ParallelBinaryWriterExample().execute();
58 | }}
59 |
--------------------------------------------------------------------------------
/src/test/java/com/wolt/osm/parallelpbf/parser/TagParserTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of parallelpbf.
3 | *
4 | * parallelpbf is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * Foobar is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with Foobar. If not, see .
16 | */
17 |
18 | package com.wolt.osm.parallelpbf.parser;
19 |
20 | import com.wolt.osm.parallelpbf.TestObjectsFactory;
21 | import crosby.binary.Osmformat;
22 | import lombok.var;
23 | import org.junit.jupiter.api.Tag;
24 | import org.junit.jupiter.api.Test;
25 |
26 | import java.util.Collections;
27 | import java.util.function.Consumer;
28 |
29 | import static org.junit.jupiter.api.Assertions.assertEquals;
30 | import static org.junit.jupiter.api.Assertions.assertTrue;
31 |
32 | @Tag("BaseParser")
33 | class TagParserTest {
34 |
35 | static class TagParser extends BaseParser