Ejemplo n.º 1
0
 def __init__(self, id=-1):
     pyOSM.Way.__init__(self, id)
     self.ref = 0
     self.population = 0
     self.node = pyOSM.Node()
     self.area = pyOSM.Area()
     self.level = 4  # level 4 = region in OSM
Ejemplo n.º 2
0
    def getinsee(self, sql, name, departement):
        c = sql.cursor()
        c.execute(
            """SELECT communes.name,communes.population,communes.departement,departements.name,regions.name,communes.id,communes.latitude,communes.longitude FROM communes,departements,regions WHERE communes.name LIKE "%s" AND communes.departement=departements.id AND communes.region=regions.id;"""
            % name)
        datas = c.fetchall()
        data = None
        if len(datas) > 0:
            if len(datas) == 1:
                data = datas[0]
            else:
                for d in datas:
                    if departement == d[3]:
                        data = d
                        break
                if data == None:
                    print "> more than one results", len(datas)
        if data:
            node = pyOSM.Node()
            node.name = data[0]
            node.population = data[1]
            node.dep_id = data[2]
            node.dep_name = data[3]
            node.reg_name = data[4]
            node.insee = data[5]
            node.location = (data[6], data[7])
        else:
            node = None

        c.close()

        return node
Ejemplo n.º 3
0
 def __init__(self, id=-1):
     pyOSM.Way.__init__(self, id)
     self.ref = 0
     self.population = 0
     self.region = None
     self.node = pyOSM.Node()
     self.area = pyOSM.Area()
     self.level = 6  # level 6 = county in OSM
Ejemplo n.º 4
0
 def __init__(self, id=-1):
     pyOSM.Way.__init__(self, id)
     self.ref = 0
     self.population = 0
     self.region = None
     self.departement = None
     self.node = pyOSM.Node()
     self.area = pyOSM.Area()
     self.level = 8
Ejemplo n.º 5
0
    def parse_osm(self,
                  fname,
                  target,
                  dname="area",
                  get_area=False,
                  override_area=False):
        tree = ElementTree.parse(fname)
        root = tree.getroot()
        relations = root.getiterator("relation")
        ways = root.getiterator("way")
        nodes = root.getiterator("node")
        print "scanning", len(relations), "relations"
        nbAdmin = 0
        for r in relations:  # scan each relation
            isAdmin = False
            inCountry = False
            name = ""
            pop = 0
            ref = 0
            admin_level = 0
            country = ""
            try:
                id = long(r.get("id"))
            except:
                id = -1
                print "* error retrieving attribute 'id' for admin relation"

            for tag in r.getiterator(
                    "tag"):  # for one relation, scan each tags
                k = tag.get("k")

                if k == 'admin_level':
                    nbAdmin = nbAdmin + 1
                    try:
                        v = int(tag.get("v"))
                        isAdmin = ((target <> 0) and (v == target)) or (
                            (target == 0) and (v == 4 or v == 6 or v == 8))
                        admin_level = v
                    except:
                        print "* error relation #%d : retrieving 'place'" % id
                if k == 'name':
                    name = tag.get("v")
                if k == 'population':
                    try:
                        pop = int(tag.get("v"))
                    except:
                        pop = -1
                if k == 'ref' or k == 'ref:INSEE':
                    try:
                        ref = int(tag.get("v"))
                        inCountry = True
                    except:
                        ref = -1
            if isAdmin and inCountry:
                t0 = time.time()
                print "> %s (%d), admin=%d" % (name, ref, v)
                # create the object to store data
                if admin_level == 4:  # region
                    o = OSMRegion(id)
                    self.regions.append(o)
                if admin_level == 6:  # departement
                    o = OSMDepartement(id)
                    self.departements.append(o)
                if admin_level == 8:  # communes
                    o = OSMCommune(id)
                    self.communes.append(o)
                o.name = name
                o.population = pop
                o.ref = ref
                o.level = admin_level

                # get the admin_centre node or compute the barycentre + store nodes (if get_area==True)
                waysID = []
                nodesID = []
                if get_area:
                    nodearea = []
                    wayarea = []
                is_node = -1
                for m in r.getiterator("member"):
                    ref = long(m.get("ref"))
                    type = m.get("type")
                    role = m.get("role")
                    if type == "node" and (
                            role == "admin_centre"
                            or role == "admin_center"):  # has an admin_centre
                        is_node = ref
                    if type == "way":
                        waysID.append(ref)
                        if get_area:
                            wayarea.append(pyOSM.Way(ref))
                if is_node > 0:  # store the admin_centre
                    o.node.osm_id = is_node
                    try:
                        o.node.name = "not found"
                        for n in nodes:
                            ref = long(n.get("id"))
                            if ref == is_node:
                                ll = float(n.get("lat"))
                                lo = float(n.get("lon"))
                                o.node.location = (ll, lo)
                                try:
                                    for tag in n.getiterator("tag"):
                                        k = tag.get("k")
                                        if k == "name":
                                            o.node.name = tag.get("v")
                                except:
                                    o.node.name = "admin_centre has no name"
                                break
                    except:
                        o.node.location = (0.0, 0.0)
                        print "admin_centre %d for %s has no location" % (
                            is_node, o.name)
                if is_node < 0 or get_area:  # compute a barycentre and store nodes
                    if len(waysID) > 0:
                        for w in ways:
                            wr = long(w.get("id"))
                            if get_area:
                                wo = pyOSM.is_in(wayarea, wr)
                            else:
                                wo = None
                            if wr in waysID:
                                for n in w.getiterator("nd"):
                                    nr = long(n.get("ref"))
                                    nodesID.append(nr)
                                    if wo:
                                        nodearea.append((wr, nr))
                                        wo.add_node(pyOSM.Node(nr))
                    if len(nodesID):
                        nb_nodes = 0
                        lat, lon = (0.0, 0.0)
                        for n in nodes:
                            ref = long(n.get("id"))
                            if ref in nodesID:
                                nb_nodes = nb_nodes + 1
                                ll = float(n.get("lat"))
                                lo = float(n.get("lon"))
                                if ll and lo:
                                    lat = lat + ll
                                    lon = lon + lo
                                if get_area:
                                    for wr, nr in nodearea:
                                        if nr == ref:
                                            wo = pyOSM.is_in(wayarea, wr)
                                            if wo:
                                                no = wo.get_node(ref)
                                                if ll == 0.0 or lo == 0.0:
                                                    print "bad location for node %d in way %d" % (
                                                        wr, nr)
                                                no.location = (ll, lo)
                                            else:
                                                print "wo %d not found for node %d" % (
                                                    wr, nr)
                        if nb_nodes > 0:
                            lat = lat / nb_nodes
                            lon = lon / nb_nodes
                        if get_area:
                            o.area.add_sorted_ways(wayarea)
                            noloc = []
                            for n in o.area.nodes:
                                ll, lo = n.location
                                if ll == 0.0 or lo == 0.0:
                                    noloc.append(n.osm_id)
                            print "\t%d node missing" % len(noloc)
                            if len(noloc) > 0:
                                print "\t", noloc
                            o.area.osm_id = o.osm_id
                            o.area.name = o.name
                            filename = os.path.join(
                                dname,
                                "fr%d_%d_%s.xml" % (o.level, o.ref, o.name))
                            ensure_dir(filename)
                            if not os.path.exists(filename) or override_area:
                                o.area.save(filename)
                        o.node.osm_id = is_node
                        if is_node < 0:
                            o.node.name = "barycenter"
                            o.node.location = (lat, lon)
                t0 = time.time() - t0
                o.show(False)
                print "\t>%.1f seconds" % t0

        return nbAdmin
Ejemplo n.º 6
0
def get_area(relations, ways, nodes, name):
    """
		get_area
		extract from relations, ways and nodes list an admin boundary (level=8 for city)
		build a way (osm data structure) for this boundary
	"""
    id = -1
    area = pyOSM.Area()
    # scan relations to find the right boundary=administrative (admin_level=8 + name)
    for r in relations:
        is_zone = False
        is_subzone = False
        is_name = False
        for tag in r.getiterator("tag"):
            k = tag.get("k")
            if k == zone_tag:
                if tag.get("v") == zone_value:
                    is_zone = True
            if k == zone_subtag:
                if tag.get("v") == zone_subvalue:
                    is_subzone = True
            if k == "name":
                if tag.get("v") == name:
                    is_name = True
        # we match the correct relation : handle it
        if is_zone and is_subzone and is_name:
            id = long(r.get("id"))
            if _debug_:
                print "\tfound relation:", id, "build way(s) and node(s)"
            waylist = []
            nodelist = []
            # extract members to build in memory the way list and node(s) corresponding (with location)
            for m in r.getiterator("member"):
                if m.get("type") == "way":
                    ref = long(m.get("ref"))
                    w = pyOSM.Way(ref)
                    waylist.append(w)
            if len(waylist) > 0:  # scan way list and pre-build nodes
                print "\t\textract", len(waylist), "way(s)"
                for w in ways:
                    wid = long(w.get("id"))
                    wo = pyOSM.is_in(waylist, wid)
                    if wo != None:
                        for t in w.getiterator("tag"):
                            name = t.get("name")
                            if name != None:
                                wo.name = name
                        nb = 0
                        for n in w.getiterator("nd"):
                            nid = long(n.get("ref"))
                            nodelist.append((wid, nid))
                            n0 = pyOSM.Node(nid)
                            wo.add_node(n0)
                            nb = nb + 1
            if len(
                    nodelist
            ) > 0:  # complete node informations for way list by scanning all the nodes and extract location
                print "\t\textract", len(nodelist), "node(s)"
                for node in nodes:
                    ref = long(node.get("id"))
                    for i in nodelist:
                        if i[1] == ref:
                            wo = pyOSM.is_in(waylist, i[0])
                            if wo:
                                n = wo.get_node(ref)
                                if n != None:
                                    ll = float(node.get("lat"))
                                    lo = float(node.get("lon"))
                                    n.location = (ll, lo)
            if len(waylist) > 0:
                # ordered the way in the logical order to describe the polygon (the last node of a way is the first of the next)
                print "\t\tordering", len(waylist), "ways"
                area.add_sorted_ways(waylist)

    return area