Scala library
With Osm4scala, you can forget about complexity of the osm.pbf
format and think about a scala iterators of primitives
(nodes, ways and relations) or blob blocks.
For example, counting all node primitives in a file is so simple as:
This is a simple example, but because the iterator nature, you can use it for more complex tasks and patterns, to process data at the same time you are reading with near zero memory usage, for example.
The library allows to read the pbf file on two different ways:
#
Entity iteration.OpenStreetMap is based in three different type of primitives:
- Nodes: Represented by the
NodeEntity
case class. - Ways: Represented by the
WayEntity
case class. - Relations: Represented by the
RelationEntity
case class.
To iterate over all primitives in a file, the only step is create an iterator using the EntityIterator
factory:
EntityIterator.fromPbf(pbfInputStream: InputStream): EntityIterator
allows to create an iterator from any Java InputStream.EntityIterator.fromPbf(pbfInputStream: InputStreamSentinel): EntityIterator
allows to create an iterator from any Java InputStream with a sentinel to stop under the defined condition.EntityIterator.def fromBlob(blob: Blob): EntityIterator
allows to read a Blob block as and iterator.
Every element in the iterator will be an Entity case class implementing an OSMEntity
trait.
There is an example in the repository that count all primitives with optional filtering by primitive type (Way, Node or Relation).
#
Blob iterationSometimes, it is better to split the read in two steps. Iterate over all blobs and, secondly, iterate over all entities in avery blob.
To iterate over all blobs in a file, the only step is create an iterator using the BlobTupleIterator
factory:
BlobTupleIterator.fromPbf(pbfInputStream: InputStream): BlobTupleIterator
allows to create an iterator from any Java InputStream.BlobTupleIterator.fromPbf(pbfInputStream: InputStreamSentinel): BlobTupleIterator
allows to create an iterator from any Java InputStream with a sentinel to stop under the defined condition.
Every element in the iterator will be a (BlobHeader, Blob)
tuple.
There is an example in the repository that extract all block from a pbf file, and store them in a folder.
#
Dependencies:Import the library using sbt.
Import the library using maven.
Only if you have problems resolving dependencies without it, add my bintray repo: