Search in sources :

Example 11 with Relation

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

the class PostgresRelationRepository method getValue.

private Relation getValue(ResultSet resultSet) throws SQLException, JsonProcessingException {
    long id = resultSet.getLong(1);
    int version = resultSet.getInt(2);
    int uid = resultSet.getInt(3);
    LocalDateTime timestamp = resultSet.getObject(4, LocalDateTime.class);
    long changeset = resultSet.getLong(5);
    Map<String, String> tags = toMap(resultSet.getString(6));
    Long[] refs = (Long[]) resultSet.getArray(7).getArray();
    Integer[] types = (Integer[]) resultSet.getArray(8).getArray();
    String[] roles = (String[]) resultSet.getArray(9).getArray();
    List<Member> members = new ArrayList<>();
    for (int i = 0; i < refs.length; i++) {
        members.add(new Member(refs[i], MemberType.forNumber(types[i]), roles[i]));
    }
    Geometry geometry = GeometryUtils.deserialize(resultSet.getBytes(10));
    Info info = new Info(version, timestamp, changeset, uid);
    return new Relation(id, info, tags, members, geometry);
}
Also used : LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) Info(com.baremaps.osm.domain.Info) Geometry(org.locationtech.jts.geom.Geometry) Relation(com.baremaps.osm.domain.Relation) Member(com.baremaps.osm.domain.Member)

Example 12 with Relation

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

the class CreateGeometryConsumer method createPolygons.

private Set<Polygon> createPolygons(Relation relation, String role) {
    Set<Polygon> polygons = new HashSet<>();
    LineMerger lineMerger = new LineMerger();
    relation.getMembers().stream().filter(m -> MemberType.WAY.equals(m.getType())).filter(m -> role.equals(m.getRole())).forEach(member -> {
        LineString line = createLine(member);
        if (line.isClosed()) {
            Polygon polygon = geometryFactory.createPolygon(line.getCoordinates());
            polygons.add(polygon);
        } else {
            lineMerger.add(line);
        }
    });
    lineMerger.getMergedLineStrings().stream().forEach(geometry -> {
        LineString line = (LineString) geometry;
        if (line.isClosed()) {
            Polygon polygon = geometryFactory.createPolygon(line.getCoordinates());
            polygons.add(polygon);
        }
    });
    return polygons;
}
Also used : CascadedPolygonUnion(org.locationtech.jts.operation.union.CascadedPolygonUnion) EntityConsumerAdapter(com.baremaps.osm.function.EntityConsumerAdapter) LinearRing(org.locationtech.jts.geom.LinearRing) LoggerFactory(org.slf4j.LoggerFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Member(com.baremaps.osm.domain.Member) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) MemberType(com.baremaps.osm.domain.Member.MemberType) LineMerger(org.locationtech.jts.operation.linemerge.LineMerger) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Relation(com.baremaps.osm.domain.Relation) LongDataMap(com.baremaps.store.LongDataMap) Set(java.util.Set) Point(org.locationtech.jts.geom.Point) Way(com.baremaps.osm.domain.Way) Collectors(java.util.stream.Collectors) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) StreamException(com.baremaps.stream.StreamException) Node(com.baremaps.osm.domain.Node) Polygon(org.locationtech.jts.geom.Polygon) Geometry(org.locationtech.jts.geom.Geometry) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) PolygonExtracter(org.locationtech.jts.geom.util.PolygonExtracter) LineMerger(org.locationtech.jts.operation.linemerge.LineMerger) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) HashSet(java.util.HashSet)

Example 13 with Relation

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

the class OsmXmlSpliterator method readRelation.

private Relation readRelation() throws XMLStreamException {
    long id = Long.parseLong(reader.getAttributeValue(null, ATTRIBUTE_NAME_ID));
    Info info = readInfo();
    // read the content of the node
    Map<String, String> tags = new HashMap<>();
    List<Member> members = new ArrayList<>();
    reader.nextTag();
    while (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
        switch(reader.getLocalName()) {
            case ELEMENT_NAME_TAG:
                readTag(tags);
                break;
            case ELEMENT_NAME_MEMBER:
                readRelationMember(members);
                break;
            default:
                readUnknownElement();
                break;
        }
    }
    return new Relation(id, info, tags, members);
}
Also used : Relation(com.baremaps.osm.domain.Relation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Info(com.baremaps.osm.domain.Info) Member(com.baremaps.osm.domain.Member)

Example 14 with Relation

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

the class Update method call.

@Override
public Integer call() throws Exception {
    BlobStore blobStore = options.blobStore();
    DataSource datasource = PostgresUtils.datasource(database);
    LongDataMap<Coordinate> coordinates = new PostgresCoordinateMap(datasource);
    LongDataMap<List<Long>> references = new PostgresReferenceMap(datasource);
    HeaderRepository headerRepository = new PostgresHeaderRepository(datasource);
    Repository<Long, Node> nodeRepository = new PostgresNodeRepository(datasource);
    Repository<Long, Way> wayRepository = new PostgresWayRepository(datasource);
    Repository<Long, Relation> relationRepository = new PostgresRelationRepository(datasource);
    logger.info("Importing changes");
    new UpdateService(blobStore, coordinates, references, headerRepository, nodeRepository, wayRepository, relationRepository, srid).call();
    logger.info("Done");
    return 0;
}
Also used : PostgresHeaderRepository(com.baremaps.osm.postgres.PostgresHeaderRepository) HeaderRepository(com.baremaps.osm.repository.HeaderRepository) PostgresWayRepository(com.baremaps.osm.postgres.PostgresWayRepository) PostgresReferenceMap(com.baremaps.osm.postgres.PostgresReferenceMap) Node(com.baremaps.osm.domain.Node) PostgresCoordinateMap(com.baremaps.osm.postgres.PostgresCoordinateMap) Way(com.baremaps.osm.domain.Way) DataSource(javax.sql.DataSource) Relation(com.baremaps.osm.domain.Relation) UpdateService(com.baremaps.osm.repository.UpdateService) Coordinate(org.locationtech.jts.geom.Coordinate) PostgresNodeRepository(com.baremaps.osm.postgres.PostgresNodeRepository) List(java.util.List) PostgresRelationRepository(com.baremaps.osm.postgres.PostgresRelationRepository) BlobStore(com.baremaps.blob.BlobStore) PostgresHeaderRepository(com.baremaps.osm.postgres.PostgresHeaderRepository)

Example 15 with Relation

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

the class OpenStreetMapGeometriesBenchmark method store.

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
@Warmup(iterations = 0)
@Measurement(iterations = 1)
public void store() throws IOException {
    Path directory = Files.createTempDirectory(Paths.get("."), "benchmark_");
    LongDataMap<Coordinate> coordinates = new LongDataOpenHashMap<>(new DataStore<>(new CoordinateDataType(), new OnDiskMemory(directory)));
    LongDataMap<List<Long>> references = new LongDataOpenHashMap<>(new DataStore<>(new LongListDataType(), new OnHeapMemory()));
    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().coordinates(coordinates).references(references).projection(4326).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 : Path(java.nio.file.Path) LongListDataType(com.baremaps.store.type.LongListDataType) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Node(com.baremaps.osm.domain.Node) OnDiskMemory(com.baremaps.store.memory.OnDiskMemory) EntityConsumerAdapter(com.baremaps.osm.function.EntityConsumerAdapter) Way(com.baremaps.osm.domain.Way) AtomicLong(java.util.concurrent.atomic.AtomicLong) Relation(com.baremaps.osm.domain.Relation) Coordinate(org.locationtech.jts.geom.Coordinate) BufferedInputStream(java.io.BufferedInputStream) CoordinateDataType(com.baremaps.store.type.CoordinateDataType) List(java.util.List) LongDataOpenHashMap(com.baremaps.store.LongDataOpenHashMap) OnHeapMemory(com.baremaps.store.memory.OnHeapMemory) 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)

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