metaodi / osmapi

Python wrapper for the OpenStreetMap API
http://osmapi.metaodi.ch/
GNU General Public License v3.0
213 stars 41 forks source link

Error during changeset uploading #175

Open stalker314314 opened 1 month ago

stalker314314 commented 1 month ago

Hi, I was using this pattern:

    api = osmapi.OsmApi(session=oauth_session, changesetauto=True, changesetautosize=500, changesetautotags={...})
    api.WayUpdate(entity)

and it worked when I was using username/password (v3.1.0). Nowdays same code is returning:

Traceback (most recent call last):
  ...
  File "OsmApi.py", line 363, in NodeUpdate
    return self._do("modify", "node", NodeData)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "OsmApi.py", line 1822, in _do
    self._changesetautoflush()
  File "OsmApi.py", line 1911, in _changesetautoflush
    self.ChangesetUpload(self._changesetautodata[:autosize])
  File "OsmApi.py", line 1366, in ChangesetUpload
    data += self._add_changeset_data(changeData, change["type"])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "OsmApi.py", line 1925, in _add_changeset_data
    changedElement["changeset"] = self._CurrentChangesetId
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
TypeError: 'str' object does not support item assignment

I cannot easily debug, seems like code iterates for dict and trying to treat changedElement as dictionary (while those are keys of changeData dict. I guess I am on some wrong path completely.

I would be suspicious of my code, but this pattern works:

    api = osmapi.OsmApi(session=oauth_session)
    api.ChangesetCreate({...})
    api.WayUpdate(entity)
    api.ChangesetClose()

I am not blocked, but I need to manually open/close changesets. Is it just me, or autochangeset break for you too? I am using latest osmapi wrongly?

metaodi commented 1 month ago

I've not used the autochangeset in quite a while.

I created a contextmanager so you can actually use a the with keyword to open and automatically close your changesets like this:

api = osmapi.OsmApi(api="https://api06.dev.openstreetmap.org", session=auth.session)
with api.Changeset({"comment": "My first test"}) as changeset_id:
    print(f"Part of Changeset {changeset_id}")
    node1 = api.NodeCreate({"lon": 1, "lat": 1, "tag": {}})

See this script for a more complete example.