osmcode / pyosmium

Python bindings for libosmium
https://osmcode.org/pyosmium
BSD 2-Clause "Simplified" License
318 stars 65 forks source link

Storing a list of lon-lat-coordinates #203

Closed cowolff closed 2 years ago

cowolff commented 2 years ago

I want to use pyosmium to get the coordinates of all schools in the state that I am living in. So far, I have written this code:

class DataHandler(osmium.SimpleHandler):
     def __init__(self):
          osmium.SimpleHandler.__init__(self)
          self.schools = []

     def node(self, n):
          if n.tags.get('amenity') == 'school' and 'name' in n.tags:
               self.schools.append((n.location.lat, n.location.lon))

h = DataHandler()
h.apply_file(FILE_PATH, locations=True)
print(sorted(h.schools))

I have also played around with copy.deepcopy to create deep copies of those coordinates before adding them to the list but I keep running into this error:

Node callback keeps reference to OSM object. This is not allowed

I know that this error means that I can only store deep copies of those informations but still, I can't figure out how to do so in a way, that doesn't raise this exception.

lonvia commented 2 years ago

Your script works for me. What Python and OS are you using?

cowolff commented 2 years ago

Well, that pretty much solved it for me. The problem apparently was my Ubuntu VM. I just switched to MacOS and there this code works as well. Sorry for wasting your time with this!

lonvia commented 2 years ago

This was an Ubuntu 22.04?

cowolff commented 2 years ago

No, it was an Ubuntu 21.10

lonvia commented 2 years ago

I've just tried with a vanilla VM with Ubuntu 21.10 and the script worked fine, too. So it is probably something special in the setup of the VM that caused the error. It would be interesting to get to the root of this but it's probably too difficult to find.