5

I've downloaded the great britain .osm.pbf file from http://download.geofabrik.de/europe.html and I want to be able to pull out all the lat and lons of every node. Is this possible?

If I could get it into python format of some sort that would be great

1 Answer 1

8

You can use pyosmium to parse an .osm.pbf file.

This simple example just prints the location and name of every node that has a name tag:

import osmium
import sys

class NamesHandler(osmium.SimpleHandler):
    def node(self, n):
        if 'name' in n.tags:
            print(f'{n.location}: ' + n.tags['name'])

def main(osmfile):
    NamesHandler().apply_file(osmfile)
    return 0

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("Usage: python %s <osmfile>" % sys.argv[0])
        sys.exit(-1)

    exit(main(sys.argv[1]))

Of course, you'll probably want to do something more sophisticated with the data depending on your use case. Check the documentation for a basic usage tutorial and reference, and the README of the pyosmium GitHub repository for installation instructions.

2
  • Where would I put the file location in to here?
    – DRobins
    Commented Aug 6, 2021 at 13:02
  • @DRobins With this small example script, the file is provided as a command line parameter. It's taken from sys.argv[1] in the final line, passed to main, and finally passed to the handler's apply_file. Only that last step is specific to pyosmium, the rest is just there so that the example script can be executed.
    – Tordanik
    Commented Aug 6, 2021 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.