示例#1
0
文件: py.py 项目: ponyatov/osmos
# -*- coding: utf8 -*-
 
import os,time
yy,mm,dd,hh=time.localtime()[:4]
NOW='%.2i%.2i%.2i%.2i'%(yy%100,mm,dd,hh)
  
import cfg,osm,cgmp,lxml
 
SAMARA_OBL = osm.relation(id=72194)#osm.xapi('relation[id=72194]')#osm.relation(id=72194)
print SAMARA_OBL.lat,SAMARA_OBL.lon

def lxm(xxx):
    return lxml.etree.tostring(xxx)

OSM = SAMARA_OBL.xpath('/osm')[0]

MP=cgmp.MP('../map/mp.mp')

MP.IMGID.Name='Samara globe (c) osm.org'
MP.IMGID.LocalName='глобус Самары (c) osm.org'
MP.IMGID.Version=OSM.attrib['version']
MP.IMGID.Copyright='(c) osm.org & [email protected]'#OSM.attrib['copyright']
 
R=SAMARA_OBL.xpath('/osm/relation[@id=72194]/member[@type="node"][@role="label"]')[0].attrib['ref']
C=SAMARA_OBL_CENTER=osm.node(R)
print C.id,C.lat,C.lon,C.name
MP.IMGID.PointView='%s %s'%(C.lat,C.lon)
MP.add(cgmp.AdminCenter(C.id,C.lat,C.lon,C.name))

R=SAMARA_OBL.xpath('/osm/relation[@id=72194]/member[@type="node"][@role="admin_centre"]')[0].attrib['ref']
C=osm.node(R)
def start_element(name, attrs):
    global n,parsing_node,w,parsing_way,r,parsing_relation,noderefs,sequence_id,cut_nodes,sequence_id,wnc
    if name == 'node':
        parsing_node=True
        n = node()
        n.lat = 'lat' in attrs and float(attrs['lat'])
        n.lon = 'lon' in attrs and float(attrs['lon'])
        n.version = 'version' in attrs and int(attrs['version'])
        n.user_id = 'uid' in attrs and int(attrs['uid']) or 0
        n.user = '******' in attrs and unicode(attrs['user']) or "__anonymous__"
        n.id = 'id' in attrs and int(attrs['id'])
        n.visible = 'visible' in attrs and attrs['visible'] == "true"
        n.timestamp = 'timestamp' in attrs and iso8601.parse_date(attrs['timestamp'])
        n.changeset_id = 'changeset' in attrs and int(attrs['changeset'])
    elif name == 'way':
        sequence_id = 0
        parsing_way=True
        w = way()
        w.version = 'version' in attrs and int(attrs['version'])
        w.user_id = 'uid' in attrs and int(attrs['uid']) or 0
        w.user = '******' in attrs and unicode(attrs['user']) or "__anonymous__"
        w.id = 'id' in attrs and int(attrs['id'])
        w.visible = 'visible' in attrs and attrs['visible'] == "true"
        w.timestamp = 'timestamp' in attrs and iso8601.parse_date(attrs['timestamp'])
        w.changeset_id = 'changeset' in attrs and int(attrs['changeset'])
    elif name == 'relation':
        parsing_relation=True
        r = relation()
        r.version = 'version' in attrs and int(attrs['version'])
        r.user_id = 'uid' in attrs and int(attrs['uid']) or 0
        r.user = '******' in attrs and unicode(attrs['user']) or "__anonymous__"
        r.id = 'id' in attrs and int(attrs['id'])
        r.visible = 'visible' in attrs and attrs['visible'] == "true"
        r.timestamp = 'timestamp' in attrs and iso8601.parse_date(attrs['timestamp'])
        r.changeset_id = 'changeset' in attrs and int(attrs['changeset'])
    elif name == 'tag':
        if parsing_node and not n:
            print "\ttag outside of node"
        elif parsing_way and not w:
            print "\ttag outside of way"
        elif parsing_relation and not r:
            print "\ttag outside of relation"
        else:
            if parsing_node:
                p = n
            elif parsing_way:
                p = w
            elif parsing_relation:
                p = r
            if 'k' in attrs and 'v' in attrs and p:
                p.tags[attrs['k']] = attrs['v']
    elif name == 'nd' and parsing_way:
        #print '\tnoderef start:', attrs['ref']
        if not w:
            print "\tnoderef outside of way"
        else:
            noderef = 'ref' in attrs and int(attrs['ref'])
            timestamp_way = w.timestamp.strftime("%Y%m%d%H%M%S")
            # find node version that corresponds to way version
            # that is the last node version that has creation date before this way version creation date.
            cursor_nodememstore.execute("select max(timestamp),version from nodes where id = ? and timestamp <= ?",(noderef,timestamp_way))
            row = cursor_nodememstore.fetchone()
            if row[0] == None:
            	# Node does not exist in file, possibly because this version was not in the extract's bbox
            	# Solution for now is to cut this node out for this way-version.
            	cut_nodes += 1
            	logging.warn("node ref %i was cut from way %i version %i because the node was not in the file" % (noderef,w.id,w.version))
            else:
                noderefs.append(noderef)
                wn.append((w.id,w.version,noderef,row[1],sequence_id))
                sequence_id += 1
                wnc += 1
    elif name == 'member' and parsing_relation:
        #print '\tmember start:', attrs['ref']
        if not r:
            print "\tnmember outside of relation"
        else:
            m = member()
            m.type = 'type' in attrs and attrs['type']
            m.ref = 'ref' in attrs and attrs['ref']
            m.role = 'role' in attrs and attrs['role']
            m.sequence_id = sequence_id
            sequence_id += 1
            r.members.append(m)