Search in sources :

Example 6 with Node

use of com.baremaps.osm.domain.Node in project baremaps by baremaps.

the class ImportUpdateDataTest method data.

@Test
@Tag("integration")
void data() throws Exception {
    LongDataMap<Coordinate> coordinates = new LongDataOpenHashMap<>(new DataStore<>(new CoordinateDataType(), new OnHeapMemory()));
    LongDataMap<List<Long>> references = new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
    // Import data
    new ImportService(new URI("res://simple/data.osm.pbf"), blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
    headerRepository.put(new Header(0l, LocalDateTime.of(2020, 1, 1, 0, 0, 0, 0), "res://simple", "", ""));
    // Check node importation
    assertNull(nodeRepository.get(0l));
    assertNotNull(nodeRepository.get(1l));
    assertNotNull(nodeRepository.get(2l));
    assertNotNull(nodeRepository.get(3l));
    assertNull(nodeRepository.get(4l));
    // Check way importation
    assertNull(wayRepository.get(0l));
    assertNotNull(wayRepository.get(1l));
    assertNull(wayRepository.get(2l));
    // Check relation importation
    assertNull(relationRepository.get(0l));
    assertNotNull(relationRepository.get(1l));
    assertNull(relationRepository.get(2l));
    // Check node properties
    Node node = nodeRepository.get(1l);
    Assertions.assertEquals(1, node.getLon());
    Assertions.assertEquals(1, node.getLat());
    // Check way properties
    Way way = wayRepository.get(1l);
    assertNotNull(way);
    // Update the database
    new UpdateService(blobStore, new PostgresCoordinateMap(dataSource), new PostgresReferenceMap(dataSource), headerRepository, nodeRepository, wayRepository, relationRepository, 3857).call();
    // Check deletions
    assertNull(nodeRepository.get(0l));
    assertNull(nodeRepository.get(1l));
    // Check insertions
    assertNotNull(nodeRepository.get(2l));
    assertNotNull(nodeRepository.get(3l));
    assertNotNull(nodeRepository.get(4l));
}
Also used : LongListDataType(com.baremaps.store.type.LongListDataType) Node(com.baremaps.osm.domain.Node) URI(java.net.URI) Way(com.baremaps.osm.domain.Way) ImportService(com.baremaps.osm.repository.ImportService) Header(com.baremaps.osm.domain.Header) UpdateService(com.baremaps.osm.repository.UpdateService) Coordinate(org.locationtech.jts.geom.Coordinate) CoordinateDataType(com.baremaps.store.type.CoordinateDataType) List(java.util.List) LongDataOpenHashMap(com.baremaps.store.LongDataOpenHashMap) OnHeapMemory(com.baremaps.store.memory.OnHeapMemory) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 7 with Node

use of com.baremaps.osm.domain.Node in project baremaps by baremaps.

the class OpenStreetMapBenchmark method entityStream.

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void entityStream() throws IOException {
    AtomicLong nodes = new AtomicLong(0);
    AtomicLong ways = new AtomicLong(0);
    AtomicLong relations = new AtomicLong(0);
    try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(path))) {
        new OsmPbfParser().entities(inputStream).forEach(new EntityConsumerAdapter() {

            @Override
            public void match(Node node) {
                nodes.incrementAndGet();
            }

            @Override
            public void match(Way way) {
                ways.incrementAndGet();
            }

            @Override
            public void match(Relation relation) {
                relations.incrementAndGet();
            }
        });
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Relation(com.baremaps.osm.domain.Relation) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Node(com.baremaps.osm.domain.Node) EntityConsumerAdapter(com.baremaps.osm.function.EntityConsumerAdapter) Way(com.baremaps.osm.domain.Way) OsmPbfParser(com.baremaps.osm.pbf.OsmPbfParser) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 8 with Node

use of com.baremaps.osm.domain.Node in project baremaps by baremaps.

the class PostgresNodeRepository method get.

/**
 * {@inheritDoc}
 */
@Override
public List<Node> get(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return List.of();
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(selectIn)) {
        statement.setArray(1, connection.createArrayOf("int8", keys.toArray()));
        try (ResultSet result = statement.executeQuery()) {
            Map<Long, Node> values = new HashMap<>();
            while (result.next()) {
                Node value = getValue(result);
                values.put(value.getId(), value);
            }
            return keys.stream().map(values::get).collect(Collectors.toList());
        }
    } catch (SQLException | JsonProcessingException e) {
        throw new RepositoryException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Node(com.baremaps.osm.domain.Node) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 9 with Node

use of com.baremaps.osm.domain.Node in project baremaps by baremaps.

the class PostgresNodeRepository method copy.

/**
 * {@inheritDoc}
 */
@Override
public void copy(List<Node> values) throws RepositoryException {
    if (values.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection()) {
        PGConnection pgConnection = connection.unwrap(PGConnection.class);
        try (CopyWriter writer = new CopyWriter(new PGCopyOutputStream(pgConnection, copy))) {
            writer.writeHeader();
            for (Node value : values) {
                writer.startRow(9);
                writer.writeLong(value.getId());
                writer.writeInteger(value.getInfo().getVersion());
                writer.writeInteger(value.getInfo().getUid());
                writer.writeLocalDateTime(value.getInfo().getTimestamp());
                writer.writeLong(value.getInfo().getChangeset());
                writer.writeJsonb(toJson(value.getTags()));
                writer.writeDouble(value.getLon());
                writer.writeDouble(value.getLat());
                writer.writeGeometry(value.getGeometry());
            }
        }
    } catch (IOException | SQLException e) {
        throw new RepositoryException(e);
    }
}
Also used : PGConnection(org.postgresql.PGConnection) PGCopyOutputStream(org.postgresql.copy.PGCopyOutputStream) SQLException(java.sql.SQLException) Node(com.baremaps.osm.domain.Node) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) RepositoryException(com.baremaps.osm.repository.RepositoryException) IOException(java.io.IOException) CopyWriter(com.baremaps.postgres.jdbc.CopyWriter)

Example 10 with Node

use of com.baremaps.osm.domain.Node in project baremaps by baremaps.

the class OpenStreetMapTest method process.

void process(Stream<Entity> stream, long headerCount, long boundCount, long nodeCount, long wayCount, long relationCount) {
    AtomicLong headers = new AtomicLong(0);
    AtomicLong bounds = new AtomicLong(0);
    AtomicLong nodes = new AtomicLong(0);
    AtomicLong ways = new AtomicLong(0);
    AtomicLong relations = new AtomicLong(0);
    stream.forEach(new EntityConsumer() {

        @Override
        public void match(Header header) {
            assertNotNull(header);
            assertEquals("osmium/1.8.0", header.getWritingProgram());
            headers.incrementAndGet();
        }

        @Override
        public void match(Bound bound) {
            assertNotNull(bound);
            assertEquals(43.75169, bound.getMaxLat(), 0.000001);
            assertEquals(7.448637, bound.getMaxLon(), 0.000001);
            assertEquals(43.72335, bound.getMinLat(), 0.000001);
            assertEquals(7.409205, bound.getMinLon(), 0.000001);
            bounds.incrementAndGet();
        }

        @Override
        public void match(Node node) {
            assertNotNull(node);
            nodes.incrementAndGet();
        }

        @Override
        public void match(Way way) {
            assertNotNull(way);
            ways.incrementAndGet();
        }

        @Override
        public void match(Relation relation) {
            assertNotNull(relation);
            relations.incrementAndGet();
        }
    });
    assertEquals(headerCount, headers.get());
    assertEquals(boundCount, bounds.get());
    assertEquals(nodeCount, nodes.get());
    assertEquals(wayCount, ways.get());
    assertEquals(relationCount, relations.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Relation(com.baremaps.osm.domain.Relation) Header(com.baremaps.osm.domain.Header) Node(com.baremaps.osm.domain.Node) Bound(com.baremaps.osm.domain.Bound) EntityConsumer(com.baremaps.osm.function.EntityConsumer) Way(com.baremaps.osm.domain.Way)

Aggregations

Node (com.baremaps.osm.domain.Node)16 Way (com.baremaps.osm.domain.Way)8 Relation (com.baremaps.osm.domain.Relation)7 List (java.util.List)6 Coordinate (org.locationtech.jts.geom.Coordinate)6 Info (com.baremaps.osm.domain.Info)5 HashMap (java.util.HashMap)4 BlobStore (com.baremaps.blob.BlobStore)3 PostgresHeaderRepository (com.baremaps.osm.postgres.PostgresHeaderRepository)3 PostgresNodeRepository (com.baremaps.osm.postgres.PostgresNodeRepository)3 PostgresRelationRepository (com.baremaps.osm.postgres.PostgresRelationRepository)3 PostgresWayRepository (com.baremaps.osm.postgres.PostgresWayRepository)3 HeaderRepository (com.baremaps.osm.repository.HeaderRepository)3 RepositoryException (com.baremaps.osm.repository.RepositoryException)3 LongListDataType (com.baremaps.store.type.LongListDataType)3 Path (java.nio.file.Path)3 Osmformat (com.baremaps.osm.binary.Osmformat)2 PrimitiveGroup (com.baremaps.osm.binary.Osmformat.PrimitiveGroup)2 Header (com.baremaps.osm.domain.Header)2 EntityConsumerAdapter (com.baremaps.osm.function.EntityConsumerAdapter)2