Ejemplo n.º 1
0
 def CopyWayTo(self, output):
     self._output = output
     self.parser = OSMParser(concurrency=2,
                             nodes_callback=None,
                             ways_callback=self.WayParse,
                             relations_callback=None)
     self.parser.parse(self._pbf_file)
     del self.parser
     if self._got_error:
         raise Exception()
Ejemplo n.º 2
0
 def CopyTo(self, output):
     self._debug_in_way = False
     self._debug_in_relation = False
     self._output = output
     self.parser = OSMParser(
         concurrency=3,
         coords_callback=self.CoordParse,
         #                                nodes_callback=self.NodeParse,
         ways_callback=self.WayParse,
         relations_callback=self.RelationParse)
     self.parser.parse(self._pbf_file)
     if self._got_error:
         raise Exception()
Ejemplo n.º 3
0
 def CopyRelationTo(self, output):
     self._output = output
     self.parser = OSMParser(
         concurrency=2, nodes_callback=None, ways_callback=None, relations_callback=self.RelationParse
     )
     self.parser.parse(self._pbf_file)
     if self._got_error:
         raise Exception()
Ejemplo n.º 4
0
 def CopyTo(self, output):
     self._output = output
     self.parser = OSMParser(
         concurrency=2,
         nodes_callback=self.NodeParse,
         ways_callback=self.WayParse,
         relations_callback=self.RelationParse,
     )
     self.parser.parse(self._pbf_file)
     del self.parser
     if self._got_error:
         raise Exception()
Ejemplo n.º 5
0
    def CopyTo(self, output):
        self._debug_in_way      = False
        self._debug_in_relation = False
        self._output = output
        self.parser = OSMParser(concurrency=3,
                                coords_callback=self.CoordParse,
#                                nodes_callback=self.NodeParse,
                                ways_callback=self.WayParse,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        if self._got_error:
            raise Exception()
Ejemplo n.º 6
0
class OsmPbfReader:

    def log(self, txt):
        self._logger.log(txt)
    
    def __init__(self, pbf_file, logger = dummylog()):
        self._pbf_file = pbf_file
        self._logger   = logger
        self._got_error = False


    def CopyTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=self.NodeParse,
                                ways_callback=self.WayParse,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyWayTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=self.WayParse,
                                relations_callback=None)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyRelationTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=None,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def NodeParse(self, nodes):
        if self._got_error:
            return
        for node in nodes:
            data = {}
            data["id"] = node[0]
            data["tag"] = node[1]
            data["lon"] = node[2][0]
            data["lat"] = node[2][1]
            if 3 in node:
                data["version"] = node[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(node[4]))
                data["uid"] = node[5]
            try:
                self._output.NodeCreate(data)
            except:
                print node, data
                print traceback.format_exc()
                self._got_error = True

    def WayParse(self, ways):
        if self._got_error:
            return
        for way in ways:
            data = {}
            data["id"] = way[0]
            data["tag"] = way[1]
            data["nd"] = way[2]
            if 3 in way:
                data["version"] = way[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(way[4]))
                data["uid"] = way[5]
            try:
                self._output.WayCreate(data)
            except:
                print way, data
                print traceback.format_exc()
                self._got_error = True

    def RelationParse(self, relations):
        if self._got_error:
            return
        for relation in relations:
            data = {}
            data["id"] = relation[0]
            data["tag"] = relation[1]
            if 3 in relation:
                data["version"] = relation[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(relation[4]))
                data["uid"] = relation[5]
            data["member"] = []
            for (ref, type, role) in relation[2]:
                attrs = { "ref": int(ref),
                          "role": role,
                          "type": type,
                        }

                data["member"].append(attrs)

            try:
                self._output.RelationCreate(data)
            except:
                print data
                print traceback.format_exc()
                self._got_error = True
Ejemplo n.º 7
0
class OsmPbfReader:

    def log(self, txt):
        self._logger.log(txt)

    def __init__(self, pbf_file, state_file, logger = dummylog()):
        self._pbf_file = pbf_file
        self._state_file = state_file
        self._logger = logger
        self._got_error = False

    def timestamp(self):
        if self._state_file:
            osm_state = OsmState(self._state_file)
            return osm_state.timestamp()

        else:
            try:
                # Try to get timestamp from metadata
                res = getstatusoutput("%s %s --out-timestamp" % (config.bin_osmconvert, self._pbf_file))
                if not res[0]:
                    d = dateutil.parser.parse(res[1]).replace(tzinfo=None)
                    if not d:
                        raise ValueError()
                    return d
            except:
                pass

            try:
                # Compute max timestamp from data
                res = getstatusoutput("%s %s --out-statistics | grep 'timestamp max'" % (config.bin_osmconvert, self._pbf_file))
                if not res[0]:
                    s = res[1].split(' ')[2]
                    return dateutil.parser.parse(s).replace(tzinfo=None)

            except:
                return


    def CopyTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=self.NodeParse,
                                ways_callback=self.WayParse,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyWayTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=self.WayParse,
                                relations_callback=None)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyRelationTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=None,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def NodeParse(self, nodes):
        if self._got_error:
            return
        for node in nodes:
            data = {}
            data["id"] = node[0]
            data["tag"] = node[1]
            data["lon"] = node[2][0]
            data["lat"] = node[2][1]
            if len(node) > 3:
                data["version"] = node[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(node[4]))
                data["uid"] = node[5]
            try:
                self._output.NodeCreate(data)
            except:
                print(node, data)
                print(traceback.format_exc())
                self._got_error = True

    def WayParse(self, ways):
        if self._got_error:
            return
        for way in ways:
            data = {}
            data["id"] = way[0]
            data["tag"] = way[1]
            data["nd"] = way[2]
            if len(way) > 3:
                data["version"] = way[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(way[4]))
                data["uid"] = way[5]
            try:
                self._output.WayCreate(data)
            except:
                print(way, data)
                print(traceback.format_exc())
                self._got_error = True

    def RelationParse(self, relations):
        if self._got_error:
            return
        for relation in relations:
            data = {}
            data["id"] = relation[0]
            data["tag"] = relation[1]
            if len(relation) > 3:
                data["version"] = relation[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(relation[4]))
                data["uid"] = relation[5]
            data["member"] = []
            for (ref, type, role) in relation[2]:
                attrs = { "ref": int(ref),
                          "role": role,
                          "type": type,
                        }

                data["member"].append(attrs)

            try:
                self._output.RelationCreate(data)
            except:
                print(data)
                print(traceback.format_exc())
                self._got_error = True
Ejemplo n.º 8
0
from imposm.parser.simple import OSMParser 
# Google protocol buffers -> OSM data
import json

z = []

def find_water(ways):
    # callback method for ways
    for v in ways:
    	tags = v[1]; refs = v[2]; timestamp = v[4];
        if 'man_made' in tags and ('water_well' == tags['man_made'] or 'water_tap' == tags['man_made']):
        	assert(len(refs) == 2)
        	z.append((refs, timestamp, tags['man_made']))
        	print(tags['man_made'], refs, timestamp);

# init parser
p = OSMParser(concurrency=4, nodes_callback=find_water)
p.parse('africa-latest.osm.pbf')

z.sort()

f = open("water_sources.txt", "w");
f.write("{\"data\": [")
for refs, timestamp, tag in z:
	f.write("{\"coords\": ["+str(refs[0])+","+str(refs[1])+"], \"timestamp\":"+str(timestamp)+", \"tag\":\""+tag+"\"},\n");

f.write("]}");
f.close();
# done
print "done"
Ejemplo n.º 9
0
class OsmPbfReader:

    def log(self, txt):
        self._logger.log(txt)
    
    def __init__(self, pbf_file, logger = dummylog()):
        self._pbf_file = pbf_file
        self._logger   = logger
        self._got_error = False


    def CopyTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=self.NodeParse,
                                ways_callback=self.WayParse,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyWayTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=self.WayParse,
                                relations_callback=None)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def CopyRelationTo(self, output):
        self._output = output
        self.parser = OSMParser(concurrency=2,
                                nodes_callback=None,
                                ways_callback=None,
                                relations_callback=self.RelationParse)
        self.parser.parse(self._pbf_file)
        del self.parser
        if self._got_error:
            raise Exception()

    def NodeParse(self, nodes):
        if self._got_error:
            return
        for node in nodes:
            data = {}
            data["id"] = node[0]
            data["tag"] = node[1]
            data["lon"] = node[2][0]
            data["lat"] = node[2][1]
            if len(node) > 3:
                data["version"] = node[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(node[4]))
                data["uid"] = node[5]
            try:
                self._output.NodeCreate(data)
            except:
                print(node, data)
                print(traceback.format_exc())
                self._got_error = True

    def WayParse(self, ways):
        if self._got_error:
            return
        for way in ways:
            data = {}
            data["id"] = way[0]
            data["tag"] = way[1]
            data["nd"] = way[2]
            if len(way) > 3:
                data["version"] = way[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(way[4]))
                data["uid"] = way[5]
            try:
                self._output.WayCreate(data)
            except:
                print(way, data)
                print(traceback.format_exc())
                self._got_error = True

    def RelationParse(self, relations):
        if self._got_error:
            return
        for relation in relations:
            data = {}
            data["id"] = relation[0]
            data["tag"] = relation[1]
            if len(relation) > 3:
                data["version"] = relation[3]
                data["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(relation[4]))
                data["uid"] = relation[5]
            data["member"] = []
            for (ref, type, role) in relation[2]:
                attrs = { "ref": int(ref),
                          "role": role,
                          "type": type,
                        }

                data["member"].append(attrs)

            try:
                self._output.RelationCreate(data)
            except:
                print(data)
                print(traceback.format_exc())
                self._got_error = True