def fresh_elems_uuid(db, uuid, fix_num=None): data_type = {"N": "node", "W": "way", "R": "relation", "I": "infos"} marker = _get(db, uuid=uuid) if not marker: abort(410, "Id is not present in database.") def expand_tags(tags): t = [] for (k, v) in tags.items(): t.append({"k": k, "v": v}) return t elems = {} for elem in marker['elems']: if elem['type']: fresh_elem = utils.fetch_osm_elem(data_type[elem['type']], elem['id']) if fresh_elem and len(fresh_elem) > 0: tmp_elem = { data_type[elem['type']]: True, "type": data_type[elem['type']], "id": elem["id"], "version": fresh_elem["version"], "tags": fresh_elem[u'tag'], } elems[data_type[elem['type']] + str(elem['id'])] = tmp_elem ret = { "uuid": uuid, "elems": list(elems.values()), } if fix_num != None: ret["fix"] = {} for res in marker['fixes'][fix_num]: tid = data_type[res['type']] + str(res['id']) if tid in elems: fix_elem_tags = copy.copy(elems[tid]["tags"]) for k in res['delete']: if k in fix_elem_tags: del fix_elem_tags[k] for (k, v) in res['create'].items(): fix_elem_tags[k] = v for (k, v) in res['modify'].items(): fix_elem_tags[k] = v ret["fix"][tid] = fix_elem_tags for elem in ret['elems']: elem["tags"] = expand_tags(elem["tags"]) return ret
def save(db, lang): json = request.json if 'tag' not in json: abort(422) # Changeset tags tags = json['tag'] if 'comment' not in tags or tags['comment'].strip() == '': tags['comment'] = u'Fixed with Osmose' if 'source' not in tags or tags['source'].strip() == '': tags['source'] = u'Osmose' if 'type' not in tags or tags['type'].strip() == '': tags['type'] = u'fix' tags['created_by'] = u'Osmose Editor' reuse_changeset = json.get('reuse_changeset', True) != False # Get an open changeset changeset = request.session.get('changeset') if changeset and not reuse_changeset: try: _changeset_close(changeset) except: pass changeset = None del request.session['changeset'] request.session.save() elif changeset: try: _changeset_update(changeset, tags) except: changeset = None request.session['changeset'] = changeset request.session.save() if not changeset: changeset = _changeset_create(tags) request.session['changeset'] = changeset request.session.save() # OsmChange out = io.StringIO() o = OsmSax.OsmSaxWriter(out, "UTF-8") o.startDocument() o.startElement('osmChange', {"version": "0.6", "generator": "OsmSax"}) methode = { 'node': o.NodeCreate, 'way': o.WayCreate, 'relation': o.RelationCreate } for action in ('modify', 'delete'): if action in json and len(json[action]) > 0: o.startElement(action, {}) for e in json[action]: try: ee = utils.fetch_osm_elem(e['type'], e["id"]) except: ee = None if ee and ee['version'] == int(e['version']): ee[u'changeset'] = changeset ee['tag'] = e['tags'] methode[e['type']](ee) else: # FIXME reject pass o.endElement(action) o.endElement('osmChange') osmchange = out.getvalue() # Fire the changeset _changeset_upload(changeset, osmchange)