Search in sources :

Example 6 with Relation

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

the class PostgresRelationRepository method put.

/**
 * {@inheritDoc}
 */
@Override
public void put(List<Relation> values) throws RepositoryException {
    if (values.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(insert)) {
        for (Relation value : values) {
            statement.clearParameters();
            setValue(statement, value);
            statement.addBatch();
        }
        statement.executeBatch();
    } catch (SQLException | JsonProcessingException e) {
        throw new RepositoryException(e);
    }
}
Also used : Relation(com.baremaps.osm.domain.Relation) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with Relation

use of com.baremaps.osm.domain.Relation 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 Relation

use of com.baremaps.osm.domain.Relation 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)

Example 9 with Relation

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

the class RelationGeometryTest method handleRelation.

Geometry handleRelation(String file) throws IOException {
    InputStream input = new GZIPInputStream(this.getClass().getResourceAsStream(file));
    List<Entity> entities = new OsmXmlParser().entities(input).collect(Collectors.toList());
    LongDataMap<Coordinate> coordinates = new MockLongDataMap<>(entities.stream().filter(e -> e instanceof Node).map(e -> (Node) e).collect(Collectors.toMap(n -> n.getId(), n -> new Coordinate(n.getLon(), n.getLat()))));
    LongDataMap<List<Long>> references = new MockLongDataMap<>(entities.stream().filter(e -> e instanceof Way).map(e -> (Way) e).collect(Collectors.toMap(w -> w.getId(), w -> w.getNodes())));
    Relation relation = entities.stream().filter(e -> e instanceof Relation).map(e -> (Relation) e).findFirst().get();
    new CreateGeometryConsumer(coordinates, references).match(relation);
    return relation.getGeometry();
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) GZIPInputStream(java.util.zip.GZIPInputStream) Relation(com.baremaps.osm.domain.Relation) LongDataMap(com.baremaps.store.LongDataMap) Coordinate(org.locationtech.jts.geom.Coordinate) IOException(java.io.IOException) Disabled(org.junit.jupiter.api.Disabled) Way(com.baremaps.osm.domain.Way) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Entity(com.baremaps.osm.domain.Entity) OsmXmlParser(com.baremaps.osm.xml.OsmXmlParser) Node(com.baremaps.osm.domain.Node) Geometry(org.locationtech.jts.geom.Geometry) MockLongDataMap(com.baremaps.osm.store.MockLongDataMap) InputStream(java.io.InputStream) Entity(com.baremaps.osm.domain.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) OsmXmlParser(com.baremaps.osm.xml.OsmXmlParser) Node(com.baremaps.osm.domain.Node) Way(com.baremaps.osm.domain.Way) GZIPInputStream(java.util.zip.GZIPInputStream) Relation(com.baremaps.osm.domain.Relation) Coordinate(org.locationtech.jts.geom.Coordinate) List(java.util.List) MockLongDataMap(com.baremaps.osm.store.MockLongDataMap)

Example 10 with Relation

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

the class PostgresRelationRepository method copy.

/**
 * {@inheritDoc}
 */
@Override
public void copy(List<Relation> 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 (Relation value : values) {
                writer.startRow(10);
                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.writeLongList(value.getMembers().stream().map(Member::getRef).collect(Collectors.toList()));
                writer.writeIntegerList(value.getMembers().stream().map(Member::getType).map(MemberType::ordinal).collect(Collectors.toList()));
                writer.writeStringList(value.getMembers().stream().map(Member::getRole).collect(Collectors.toList()));
                writer.writeGeometry(value.getGeometry());
            }
        }
    } catch (IOException | SQLException ex) {
        throw new RepositoryException(ex);
    }
}
Also used : PGConnection(org.postgresql.PGConnection) PGCopyOutputStream(org.postgresql.copy.PGCopyOutputStream) Relation(com.baremaps.osm.domain.Relation) MemberType(com.baremaps.osm.domain.Member.MemberType) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) RepositoryException(com.baremaps.osm.repository.RepositoryException) IOException(java.io.IOException) Member(com.baremaps.osm.domain.Member) CopyWriter(com.baremaps.postgres.jdbc.CopyWriter)

Aggregations

Relation (com.baremaps.osm.domain.Relation)15 Node (com.baremaps.osm.domain.Node)8 Way (com.baremaps.osm.domain.Way)8 Member (com.baremaps.osm.domain.Member)6 List (java.util.List)6 Coordinate (org.locationtech.jts.geom.Coordinate)6 ArrayList (java.util.ArrayList)5 Info (com.baremaps.osm.domain.Info)4 BlobStore (com.baremaps.blob.BlobStore)3 MemberType (com.baremaps.osm.domain.Member.MemberType)3 EntityConsumerAdapter (com.baremaps.osm.function.EntityConsumerAdapter)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 Geometry (org.locationtech.jts.geom.Geometry)3 OsmPbfParser (com.baremaps.osm.pbf.OsmPbfParser)2 PostgresCoordinateMap (com.baremaps.osm.postgres.PostgresCoordinateMap)2