예제 #1
0
def fresh_elems(db, lang, err_id, fix_num=None):
    (marker, columns_marker, elements, columns_elements, fix,
     columns_fix) = _get(db, err_id)

    data_type = {"N": "node", "W": "way", "R": "relation", "I": "infos"}

    def expand_tags(tags):
        t = []
        for (k, v) in tags.items():
            t.append({"k": k, "v": v})
        return t

    elems = {}
    for elem in elements:
        if elem["data_type"]:
            fresh_elem = utils.fetch_osm_elem(data_type[elem["data_type"]],
                                              elem["id"])

            if fresh_elem and len(fresh_elem) > 0:
                tmp_elem = {
                    data_type[elem["data_type"]]: True,
                    "type": data_type[elem["data_type"]],
                    "id": elem["id"],
                    "version": fresh_elem["version"],
                    "tags": fresh_elem[u'tag'],
                }
                elems[data_type[elem["data_type"]] +
                      str(elem["id"])] = tmp_elem

    if fix_num != None:
        res = _get_fix(db, err_id, fix_num)
        tid = data_type[res["elem_data_type"]] + str(res["elem_id"])
        if elems.has_key(tid):
            fix_elem_tags = copy.copy(elems[tid]["tags"])
            for k in res["tags_delete"]:
                if fix_elem_tags.has_key(k):
                    del fix_elem_tags[k]
            for (k, v) in res["tags_create"].items():
                fix_elem_tags[k] = v
            for (k, v) in res["tags_modify"].items():
                fix_elem_tags[k] = v

            ret = {
                "error_id": err_id,
                "elems": elems.values(),
                "fix": {
                    tid: fix_elem_tags
                }
            }

            for elem in ret['elems']:
                elem["tags"] = expand_tags(elem["tags"])
            return ret

    ret = {"error_id": err_id, "elems": elems.values()}

    for elem in ret['elems']:
        elem["tags"] = expand_tags(elem["tags"])

    return ret
예제 #2
0
def fresh_elems(db, lang, err_id, fix_num=None):
    (marker, columns_marker, elements, columns_elements, fix, columns_fix) = _get(db, err_id)

    data_type = { "N": "node", "W": "way", "R": "relation", "I": "infos"}

    def expand_tags(tags):
      t = []
      for (k, v) in tags.items():
        t.append({"k": k, "v": v})
      return t

    elems = {}
    for elem in elements:
      if elem["data_type"]:
        fresh_elem = utils.fetch_osm_elem(data_type[elem["data_type"]], elem["id"])

        if fresh_elem and len(fresh_elem) > 0:
            tmp_elem = {data_type[elem["data_type"]]: True,
                    "type": data_type[elem["data_type"]],
                    "id": elem["id"],
                    "version": fresh_elem["version"],
                    "tags": fresh_elem[u'tag'],
                   }
            elems[data_type[elem["data_type"]] + str(elem["id"])] = tmp_elem

    if fix_num != None:
        res = _get_fix(db, err_id, fix_num)
        tid = data_type[res["elem_data_type"]] + str(res["elem_id"])
        if elems.has_key(tid):
            fix_elem_tags = copy.copy(elems[tid]["tags"])
            for k in res["tags_delete"]:
                if fix_elem_tags.has_key(k):
                    del fix_elem_tags[k]
            for (k, v) in res["tags_create"].items():
                fix_elem_tags[k] = v
            for (k, v) in res["tags_modify"].items():
                fix_elem_tags[k] = v

            ret = {
                "error_id": err_id,
                "elems": elems.values(),
                "fix": {tid: fix_elem_tags}
            }

            for elem in ret['elems']:
                elem["tags"] = expand_tags(elem["tags"])
            return ret

    ret = {
        "error_id": err_id,
        "elems": elems.values()
    }

    for elem in ret['elems']:
        elem["tags"] = expand_tags(elem["tags"])

    return ret
예제 #3
0
def save(db, lang):
    json = request.json
    if not json.has_key('tag'):
        abort(422)

    # Changeset tags
    tags = json['tag']
    if not tags.has_key('comment') or tags['comment'].strip() == '':
        tags['comment'] = u'Fix with Osmose'
    if not tags.has_key('source') or tags['source'].strip() == '':
        tags['source'] = u'Osmose'
    if not tags.has_key('type') 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']
    elif changeset:
        try:
            _changeset_update(changeset, tags)
        except:
            changeset = None
            request.session['changeset'] = changeset

    if not changeset:
        changeset = _changeset_create(tags)
        request.session['changeset'] = changeset

    # OsmChange
    out = StringIO.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 json.has_key(action) and len(json[action]) > 0:
            o.startElement(action, {})
            for (k, e) in json[action].items():
                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['tag']
                    methode[e['type']](ee)
                else:
                    # FIXME reject
                    pass
            o.endElement(action)

    o.endElement('osmChange')
    osmchange = out.getvalue()

    # Fire the changeset
    _changeset_upload(changeset, osmchange)