Search in sources :

Example 1 with StreamException

use of com.baremaps.stream.StreamException in project baremaps by baremaps.

the class CreateGeometryConsumer method createLine.

private LineString createLine(Member member) {
    try {
        List<Long> refs = this.references.get(member.getRef());
        List<Coordinate> coords = refs.stream().map(coordinates::get).collect(Collectors.toList());
        Coordinate[] array = coords.toArray(new Coordinate[coords.size()]);
        return geometryFactory.createLineString(array);
    } catch (Exception e) {
        throw new StreamException(e);
    }
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) StreamException(com.baremaps.stream.StreamException) StreamException(com.baremaps.stream.StreamException)

Example 2 with StreamException

use of com.baremaps.stream.StreamException in project baremaps by baremaps.

the class OsmChangeSpliterator method tryAdvance.

@Override
public boolean tryAdvance(Consumer<? super Change> consumer) {
    try {
        if (reader.hasNext()) {
            int event = reader.next();
            switch(event) {
                case START_ELEMENT:
                    if (ELEMENT_NAME_OSMCHANGE.equals(reader.getLocalName())) {
                        return true;
                    }
                    Change entity = readChange();
                    consumer.accept(entity);
                    return true;
                case END_DOCUMENT:
                    return false;
                default:
                    return true;
            }
        } else {
            return false;
        }
    } catch (XMLStreamException e) {
        throw new StreamException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Change(com.baremaps.osm.domain.Change) XMLStreamException(javax.xml.stream.XMLStreamException) StreamException(com.baremaps.stream.StreamException)

Example 3 with StreamException

use of com.baremaps.stream.StreamException in project baremaps by baremaps.

the class Execute method call.

@Override
public Integer call() throws Exception {
    DataSource datasource = PostgresUtils.datasource(database);
    BlobStore blobStore = options.blobStore();
    for (URI file : files) {
        logger.info("Execute {}", file);
        String blob = new String(blobStore.get(file).getInputStream().readAllBytes(), StandardCharsets.UTF_8);
        blob = interpolate(System.getenv(), blob);
        StreamUtils.batch(Splitter.on(";").splitToStream(blob), 1).forEach(query -> {
            try (Connection connection = datasource.getConnection();
                Statement statement = connection.createStatement()) {
                statement.execute(query);
            } catch (SQLException e) {
                throw new StreamException(e);
            }
        });
    }
    logger.info("Done");
    return 0;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) URI(java.net.URI) BlobStore(com.baremaps.blob.BlobStore) DataSource(javax.sql.DataSource) StreamException(com.baremaps.stream.StreamException)

Example 4 with StreamException

use of com.baremaps.stream.StreamException in project baremaps by baremaps.

the class OsmPbfParser method entities.

/**
 * Creates an ordered stream of entities.
 *
 * @param inputStream an osm pbf {@link InputStream}
 * @return a stream of blocks
 */
public Stream<Entity> entities(InputStream inputStream) {
    return blocks(inputStream).flatMap(block -> {
        try {
            Stream.Builder<Entity> entities = Stream.builder();
            block.visit(new BlockEntityConsumer(entities::add));
            return entities.build();
        } catch (Exception e) {
            throw new StreamException(e);
        }
    });
}
Also used : Entity(com.baremaps.osm.domain.Entity) BlockEntityConsumer(com.baremaps.osm.function.BlockEntityConsumer) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) StreamException(com.baremaps.stream.StreamException) StreamException(com.baremaps.stream.StreamException)

Example 5 with StreamException

use of com.baremaps.stream.StreamException in project baremaps by baremaps.

the class OsmChangeSpliterator method readChange.

private Change readChange() throws XMLStreamException {
    switch(reader.getLocalName()) {
        case ELEMENT_NAME_CREATE:
        case ELEMENT_NAME_DELETE:
        case ELEMENT_NAME_MODIFY:
            ChangeType type = ChangeType.valueOf(reader.getLocalName().toUpperCase());
            List<Entity> elements = new ArrayList<>();
            reader.nextTag();
            while (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                elements.add(readElement());
                reader.nextTag();
            }
            return new Change(type, elements);
        default:
            throw new StreamException("Unexpected XML element: " + reader.getLocalName());
    }
}
Also used : Entity(com.baremaps.osm.domain.Entity) ChangeType(com.baremaps.osm.domain.Change.ChangeType) ArrayList(java.util.ArrayList) Change(com.baremaps.osm.domain.Change) XMLStreamException(javax.xml.stream.XMLStreamException) StreamException(com.baremaps.stream.StreamException)

Aggregations

StreamException (com.baremaps.stream.StreamException)5 Change (com.baremaps.osm.domain.Change)2 Entity (com.baremaps.osm.domain.Entity)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 BlobStore (com.baremaps.blob.BlobStore)1 ChangeType (com.baremaps.osm.domain.Change.ChangeType)1 BlockEntityConsumer (com.baremaps.osm.function.BlockEntityConsumer)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Stream (java.util.stream.Stream)1 DataSource (javax.sql.DataSource)1 Coordinate (org.locationtech.jts.geom.Coordinate)1