示例#1
0
def roadsGraph(filename):
    roads = Roads()
    osmParser = OSMParser(concurrency=4,
                          coords_callback=roads.nodes,
                          ways_callback=roads.edges)
    osmParser.parse(filename)
    return roads.getGraph()
示例#2
0
def retrieve_highway(osm_file, selected_zone, tolerance, Ncore = 1):
    # Parses the OSM file.
    point = Point(selected_zone, tolerance)
    p = OSMParser(concurrency = Ncore, coords_callback = point.select)
    p.parse(osm_file)

    highway = Highway(point)
    p = OSMParser(concurrency = Ncore, ways_callback = highway.select)
    p.parse(osm_file)

    highway.point_set = set([item for refs in highway.point for item in refs])

    # Getting all coordinates.
    point_coordinate = []
    for way in highway.point_set:
        try:
            point_coordinate.append(point.coordinate[way])
        except:
            pass

    # Defining the set of OSM way id and coordinates.
    highway_coordinate = []
    highway_osmid = []
    for refs, osmid in zip(highway.point, highway.osmid):
        try:
            highway_coordinate.append([point.coordinate[n] for n in refs])
            highway_osmid.append(osmid)
        except:
            pass

    return highway_coordinate, highway_osmid
示例#3
0
def read(fname, bb=None, ll_bb=None):
	g = graph.Graph()
	vertex_map = {}
	def coords_callback(clist):
		for osmid, lon, lat in clist:
			p = geom.FPoint(lon, lat)
			if ll_bb is not None and not ll_bb.contains(p):
				continue
			p = coords.lonLatToMeters(p).to_point()
			if bb is not None and not bb.contains(p):
				continue
			vertex_map[osmid] = g.add_vertex(p)
	def ways_callback(ways):
		for osmid, tags, refs in ways:
			if 'highway' not in tags or tags['highway'] in BLACKLIST:
				continue
			for i in range(len(refs) - 1):
				src = refs[i]
				dst = refs[i + 1]
				if src not in vertex_map or dst not in vertex_map:
					continue
				g.add_bidirectional_edge(vertex_map[src], vertex_map[dst])
	p = OSMParser(coords_callback=coords_callback)
	p.parse(fname)
	p = OSMParser(ways_callback=ways_callback)
	p.parse(fname)
	return g
示例#4
0
def extractCities(countries_dir, output_dir):
    print "extracting cities"
    global CC
    global citiesFile

    if os.path.exists(cities_path):
        os.remove(cities_path)

    citiesFile = codecs.open(cities_path, encoding='utf-8', mode='wb')

    prepare_CC_map()

    p = OSMParser(concurrency=4, nodes_callback=handle_nodes)

    for dirname, dirnames, filenames in os.walk(countries_dir):
        #    for subdirname in dirnames:
        baseDirName = os.path.basename(dirname)
        basePath = os.path.dirname(dirname)

        for country_file in filenames:
            if country_file != ".DS_Store":
                country = os.path.splitext(os.path.basename(country_file))[0]
                CC = map_to_CC(country)
                #			print country + " " + CC
                inputFile = dirname + "/" + country_file
                print inputFile
                p.parse(inputFile)

    citiesFile.close()

    print "done"
示例#5
0
def extractCities( countries_dir, output_dir ):
	print "extracting cities"
	global CC
	global citiesFile

	if os.path.exists( cities_path ):
		os.remove( cities_path )

	citiesFile = codecs.open( cities_path, encoding='utf-8', mode='wb' ) 

	prepare_CC_map( )

	p = OSMParser( concurrency=4, nodes_callback = handle_nodes )

	for dirname, dirnames, filenames in os.walk( countries_dir ):
	#    for subdirname in dirnames:
		baseDirName = os.path.basename( dirname )
		basePath = os.path.dirname( dirname )

		for country_file in filenames:
			if country_file != ".DS_Store":
				country = os.path.splitext( os.path.basename( country_file ) )[0]
				CC = map_to_CC( country )
	#			print country + " " + CC
				inputFile = dirname + "/" + country_file
				print inputFile
				p.parse( inputFile )

	citiesFile.close()

	print "done"
def main():
    file_path = sys.argv[1]
    if not os.path.exists(file_path):
        print("{} doesn't exist.".format(file_path))
        exit(1)
    else:
        print("Extracting buildings from |{}|...".format(file_path))
        root = et.Element('environment')
        comment = et.Comment(
            'Generated obstacle configurations for {}'.format(file_path))
        root.append(comment)

        coord_parser = CoordGeo()
        p = OSMParser(concurrency=4,
                      coords_callback=coord_parser.coords_callback)
        p.parse(file_path)

        bldg_parser = BuildingShapes(coord_parser.coords)

        p = OSMParser(concurrency=4, ways_callback=bldg_parser.ways_callback)
        p.parse(file_path)

        print('# Coords: {}'.format(len(coord_parser.coords)))
        print('# Buildings: {}'.format(len(bldg_parser.xml_elmts)))
        # Extent the children to the root element
        root.extend(bldg_parser.xml_elmts)
        #print(prettify(root))
        # Dump to file
        print('Writing result to {}'.format(output_file))
        output = et.ElementTree(root)
        output.write(output_file, pretty_print=True)
示例#7
0
	def __init__(self, osmfile):
		# parse the input file and save its contents in memory
		# road ids and node refs as returned from imposm
		self.roads = dict()

		p = OSMParser(ways_callback = self.ways_callback)
		p.parse(osmfile)
示例#8
0
def retrieve_highway(osm_file, selected_zone, tolerance, Ncore=1):
    # Parses the OSM file.
    point = Point(selected_zone, tolerance)
    p = OSMParser(concurrency=Ncore, coords_callback=point.select)
    p.parse(osm_file)

    highway = Highway(point)
    p = OSMParser(concurrency=Ncore, ways_callback=highway.select)
    p.parse(osm_file)

    highway.point_set = set([item for refs in highway.point for item in refs])

    # Getting all coordinates.
    point_coordinate = []
    for way in highway.point_set:
        try:
            point_coordinate.append(point.coordinate[way])
        except:
            pass

    # Defining the set of OSM way id and coordinates.
    highway_coordinate = []
    highway_osmid = []
    for refs, osmid in zip(highway.point, highway.osmid):
        try:
            highway_coordinate.append([point.coordinate[n] for n in refs])
            highway_osmid.append(osmid)
        except:
            pass

    return highway_coordinate, highway_osmid
示例#9
0
	def load_osm(self, filename):
		# Reinitialize if we have a new file
		ways = []
		coords = {}
		num_coords = 0
		num_ways = 0
		
		# status output
		if self.verbose:
			sys.stderr.write("loading ways, each '-' is 100 ways, each row is 10,000 ways\n")
		
		posm = OSMParser(ways_callback=self.ways_callback)
		posm.parse(filename)
		#print {}
		# status output
		if self.verbose:
			sys.stderr.write("\n{} ways matched in {}, {} coordinates will be loaded, each '.' is 1% complete\n".format(len(self.ways), filename, len(self.coords)))
			total = len(self.coords)
			if total < 100:
				self.coords_marker = 1
			else:
				self.coords_marker = round(total/100)

		posm = OSMParser(coords_callback=self.coords_callback)
		posm.parse(filename)
		
		# status output
		if self.verbose:
			sys.stderr.write("\ncoordinates loaded. calculating curvature, each '.' is 1% complete\n")
			sys.stderr.flush()
示例#10
0
    def __init__(self, filename):
        self.nodes = {}
        self.coords = {}
        self.positions = {}
        self.ways = {}
        self.edges = []
        parser = OSMParser(concurrency=1,
                           ways_callback=self.ways_cb,
                           nodes_callback=self.nodes_cb,
                           coords_callback=self.coords_cb)
        parser.parse(filename)

        for osmid in self.ways:
            tags, refs = self.ways[osmid]
            if not 'highway' in tags:
                continue
            # unpack the ways in to edges that will be used for the graphs later
            # we store the osm id of the way in the edge, but prolly not needed
            for i in xrange(len(refs) - 1):
                from_node, to_node = refs[i], refs[i + 1]
                from_coords, _ = self.coords[from_node]
                to_coords, _ = self.coords[to_node]
                d = dist(from_coords, to_coords)
                # FIXME: make a class or namedtuple from edge
                self.edges.append(
                    (osmid, from_node, to_node, from_coords, to_coords, d))
示例#11
0
def remove_tags(input_filename, output_fp, tags_to_keep, close_output_fp=True):

    output_writer = OSMWriter(fp=output_fp)
    remover = TagRemover(output_writer, tags_to_keep)
    parser = OSMParser(ways_callback=remover.ways, nodes_callback=remover.nodes, concurrency=1)
    parser.parse(input_filename)

    output_writer.close(close_file=close_output_fp)
示例#12
0
文件: streets.py 项目: ijoo2/streets
    def __init__(self):
        print "Reading OpenStreetMap data from file=", config['osm_data']
        # data = Graph(config['osm_data'])

        data = Graph()
        p = OSMParser(concurrency=4, ways_callback=data.ways)
        p.parse(config['osm_data'])
        print data.highways
示例#13
0
def buildingsPolygons(filename):
    buildings = Buildings()
    osmparser = OSMParser(concurrency=4,
                          coords_callback=buildings.nodes,
                          ways_callback=buildings.osmways,
                          relations_callback=buildings.osmrelations)
    osmparser.parse(filename)
    return buildings.getPolygons()
示例#14
0
    def __init__(self, osmfile):
        # parse the input file and save its contents in memory

        # initialize street network
        self.street_network = StreetNetwork()

        # coord pairs as returned from imposm
        self.coords = dict()

        # max and min latitude and longitude
        self.bounds = dict()
        self.bounds["min_lat"] = 9999
        self.bounds["max_lat"] = -9999
        self.bounds["min_lon"] = 9999
        self.bounds["max_lon"] = -9999

        # active copy of OSM data indexed by OSM id
        self.all_osm_relations = dict()
        self.all_osm_ways = dict()
        self.all_osm_nodes = dict()

        # nodes with specific landuse tags
        self.residential_nodes = set()
        self.industrial_nodes = set()
        self.commercial_nodes = set()

        # subset that is also connected to the street network
        self.connected_residential_nodes = set()
        self.connected_industrial_nodes = set()
        self.connected_commercial_nodes = set()

        # mapping from highway types to max speeds
        # we do this so there"s always a speed limit for every edge, even if
        # none is in the OSM data
        self.max_speed_map = dict()
        self.max_speed_map["motorway"] = 140
        self.max_speed_map["trunk"] = 120
        self.max_speed_map["primary"] = 100
        self.max_speed_map["secondary"] = 80
        self.max_speed_map["tertiary"] = 70
        self.max_speed_map["road"] = 50
        self.max_speed_map["minor"] = 50
        self.max_speed_map["unclassified"] = 50
        self.max_speed_map["residential"] = 30
        self.max_speed_map["track"] = 30
        self.max_speed_map["service"] = 20
        self.max_speed_map["path"] = 10
        self.max_speed_map["cycleway"] = 1   # >0 to prevent infinite weights
        self.max_speed_map["bridleway"] = 1  # >0 to prevent infinite weights
        self.max_speed_map["pedestrian"] = 1 # >0 to prevent infinite weights
        self.max_speed_map["footway"] = 1    # >0 to prevent infinite weights

        p = OSMParser(concurrency = 1,
                      coords_callback = self.coords_callback,
                      nodes_callback = self.nodes_callback,
                      ways_callback = self.ways_callback,
                      relations_callback = self.relations_callback)
        p.parse(osmfile)
示例#15
0
    def __init__(self, osmfile):
        # parse the input file and save its contents in memory

        # initialize street network
        self.street_network = StreetNetwork()

        # coord pairs as returned from imposm
        self.coords = dict()

        # max and min latitude and longitude
        self.bounds = dict()
        self.bounds["min_lat"] = 9999
        self.bounds["max_lat"] = -9999
        self.bounds["min_lon"] = 9999
        self.bounds["max_lon"] = -9999

        # active copy of OSM data indexed by OSM id
        self.all_osm_relations = dict()
        self.all_osm_ways = dict()
        self.all_osm_nodes = dict()

        # nodes with specific landuse tags
        self.residential_nodes = set()
        self.industrial_nodes = set()
        self.commercial_nodes = set()

        # subset that is also connected to the street network
        self.connected_residential_nodes = set()
        self.connected_industrial_nodes = set()
        self.connected_commercial_nodes = set()

        # mapping from highway types to max speeds
        # we do this so there"s always a speed limit for every edge, even if
        # none is in the OSM data
        self.max_speed_map = dict()
        self.max_speed_map["motorway"] = 140
        self.max_speed_map["trunk"] = 120
        self.max_speed_map["primary"] = 100
        self.max_speed_map["secondary"] = 80
        self.max_speed_map["tertiary"] = 70
        self.max_speed_map["road"] = 50
        self.max_speed_map["minor"] = 50
        self.max_speed_map["unclassified"] = 50
        self.max_speed_map["residential"] = 30
        self.max_speed_map["track"] = 30
        self.max_speed_map["service"] = 20
        self.max_speed_map["path"] = 10
        self.max_speed_map["cycleway"] = 1   # >0 to prevent infinite weights
        self.max_speed_map["bridleway"] = 1  # >0 to prevent infinite weights
        self.max_speed_map["pedestrian"] = 1 # >0 to prevent infinite weights
        self.max_speed_map["footway"] = 1    # >0 to prevent infinite weights

        p = OSMParser(concurrency = 1,
                      coords_callback = self.coords_callback,
                      nodes_callback = self.nodes_callback,
                      ways_callback = self.ways_callback,
                      relations_callback = self.relations_callback)
        p.parse(osmfile)
示例#16
0
class p:
	def __init__(self):
		server = Server()
		self.db=server.get_or_create_db("b")
		self.i=0
		self.parser=OSMParser(nodes_callback=self.coord, ways_callback=self.way, relations_callback=self.rel, coords_callback=self.coord)
	def parse(self,x):
		self.parser.parse(x)
	def coord(self,c):
		out=dict()
		for osm_id,lat,lng in c:
			out["type"]="coord"
			out["_id"]=str(osm_id)
			out["geometry"]={"type":"Point","coordinates":lng}
			self.db["c"+str(osm_id)]=out
		self.i=self.i+1
		print "cached a coord totoal now "+str(self.i)
	def node(self,n):
		out=dict()
		for osm_id,tags,coord in n:
			c=[coord[0],coord[1]]
			out["type"]="node"
			out["_id"]=str(osm_id)
			out["geometry"]={"type":"Point","coordinates":c}
			for k in tags:
				if k !="type" and k!="_id"and k!="geometry":
					out[k]=tags[k]
			self.db[str(osm_id)]=out
		self.i=self.i+1
		print "cached a node totoal now "+str(self.i)
	def way(self,w):
		out=dict()
		for osm_id,tags,mem in w:
			out["type"]="way"
			out["_id"]=str(osm_id)
			out["members"]=mem
			for k in tags:
				if k !="type" and k!="_id"and k!="members":
					out[k]=tags[k]
			self.db[str(osm_id)]=out
		self.i=self.i+1
		print "cached a way totoal now "+str(self.i)
	def rel(self,r):
		out=dict()
		for osm_id,tags,mem in r:
			out["type"]="relation"
			out["_id"]=str(osm_id)
			out["members"]=mem
			for k in tags:
				if k !="type" and k!="_id"and k!="members":
					out[k]=tags[k]
			self.db[str(osm_id)]=out
		self.i=self.i+1
		print "cached a rel totoal now "+str(self.i)
	def clean(self):
		for k in self.nodes:
			if self.coords.has_key(k):
				del self.coords[k]
示例#17
0
def fetch_and_parse_pbf():
    print 'Fetching', PBF_URL
    pbf_file = 'brandenburg-latest.osm.pbf'
    urllib.urlretrieve(PBF_URL, pbf_file)
    np = NodesParser()
    p = OSMParser(concurrency=2, nodes_callback=np.nodes)
    p.parse(pbf_file)
    np.close()
    print 'Dataset', DATASET_PATH, 'with', np.count, 'nodes parsed from OSM xml'
    def load_file(self, filename):
        # Reinitialize if we have a new file
        ways = []
        coords = {}
        num_coords = 0
        num_ways = 0

        # status output
        if self.verbose:
            sys.stderr.write("loading ways, each '-' is 100 ways, each row is 10,000 ways\n")

        p = OSMParser(ways_callback=self.ways_callback)
        p.parse(filename)

        # status output
        if self.verbose:
            sys.stderr.write(
                "\n{} ways matched in {} {mem:.1f}MB memory used, {} coordinates will be loaded, each '.' is 1% complete\n".format(
                    len(self.ways),
                    filename,
                    len(self.coords),
                    mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576,
                )
            )

            total = len(self.coords)
            if total < 100:
                self.coords_marker = 1
            else:
                self.coords_marker = round(total / 100)

        p = OSMParser(coords_callback=self.coords_callback)
        p.parse(filename)

        # Filter out ways that are missing coordinates (because they are

        # status output
        if self.verbose:
            sys.stderr.write(
                "\ncoordinates loaded {mem:.1f}MB memory used, calculating curvature, each '.' is 1% complete\n".format(
                    mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576
                )
            )
            sys.stderr.flush()

            # Loop through the ways and calculate their curvature
        self.calculate()

        # status output
        if self.verbose:
            sys.stderr.write(
                "calculation complete, {mem:.1f}MB memory used\n".format(
                    mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576
                )
            )
            sys.stderr.flush()
示例#19
0
文件: parse.py 项目: xtompok/osmawalk
def parseOSMfile(filename,waysConf,areasConf,bridgesConf,tunnelsConf,srtm):	
	""" Parse OSM file """
	clas = classifier(waysConf,areasConf,bridgesConf,tunnelsConf,srtm)
	p = OSMParser(concurrency=4, ways_callback=clas.ways_cb, nodes_callback=clas.nodes_cb, relations_callback=clas.relations_cb, coords_callback=clas.coords_cb)
	p.parse(filename)
	clas.Map.nodes.sort(key=lambda node: node.id)
	clas.Map.ways.sort(key=lambda way: way.id)
	for i in range(len(clas.Map.nodes)):
		clas.Map.nodesIdx[clas.Map.nodes[i].id]=i
	return clas.Map
示例#20
0
  def __init__(self, osmfile):
    self.nodes = dict()
    self.bounds = dict()
    self.bounds["min_lat"] =-9999 # 58.3773000
    self.bounds["max_lat"] = 9999 # 58.4236000
    self.bounds["min_lng"] =-9999 # 15.5207000
    self.bounds["max_lng"] = 9999 # 15.6248000

    p=OSMParser(coords_callback = self.coords_callback)
    p.parse(osmfile)
示例#21
0
def add_churches(filename):
    # get churches
    churches = GetChurches()
    p = OSMParser(concurrency=4,
                  nodes_callback=churches.nodes,
                  ways_callback=churches.ways)
    p.parse(filename)
    
    update_refs.apply_async(args=[filename])
    return True
示例#22
0
	def calc_all_ways(self):
		p = OSMParser(concurrency=4, coords_callback=self._read_coords,
			ways_callback=self._read_ways)
		if not self.silent:
			print("Reading file")
		p.parse(self.filepath)
		if not self.silent:
			print("Summing")
		self._calc_ways()
		return sum(self.way_distances)
示例#23
0
文件: roads.py 项目: Rollmops/OSM2P3D
    def __init__(self, osm_file, coords, nodes, config):

        self.tmp_dir = tempfile.mkdtemp()
        
        self.config = config
        self.osm_file = osm_file
        self.coords = dict(coords.items() + nodes.items())
        
        osm_parser = OSMParser(concurrency=1, ways_callback=self._ways_callback)
        osm_parser.parse(osm_file)
示例#24
0
    def load_osm_features(self, pbf_path , families):
        logger.info("    Adding feature from OSM.")
        pbf_path, pbf_name = os.path.split(pbf_path)
        categories = {}
        for family in families:
            locations = {}

            class FamilyParser(object):
                ways_objects = {}
                nodes_objects = {}
                all_nodes = {}

                def ways(self, ways):
                    for osmid, tags, refs in ways:
                        for key in families[family].keys():
                            if key in tags.keys():
                                if families[family][key] == "*" or tags[key] in families[family][key]:
                                    self.ways_objects[int(osmid)] = {"nd" : refs, "tag" : tags[key]}

                def nodes(self, nodes):
                    for osmid, tags, coords in nodes:
                        for key in families[family].keys():
                            if key in tags.keys():
                                if families[family][key] == "*" or tags[key] in families[family][key]:
                                    self.nodes_objects[int(osmid)] = {"lon" : coords[0], "lat" : coords[1], "tag" : tags[key]}

                def coords(self, coords):
                    for coords in coords:
                        self.all_nodes[int(coords[0])] = (coords[1], coords[2])

            parser = FamilyParser()
            p = OSMParser(concurrency = 2, ways_callback = parser.ways, nodes_callback = parser.nodes, coords_callback = parser.coords)
            p.parse("%s/%s.osm.pbf" % (pbf_path, self.name))

            for k in parser.nodes_objects.keys():
                locations[k] = (parser.nodes_objects[k]["lon"], parser.nodes_objects[k]["lat"], parser.nodes_objects[k]["tag"])
            for k in parser.ways_objects.keys():
                # compute mean postion
                m_lon, m_lat = 0, 0
                for nd in parser.ways_objects[k]["nd"]:
                    node = parser.all_nodes[int(nd)]
                    m_lon += node[0]
                    m_lat += node[1]
                m_lon /= float(len(parser.ways_objects[k]["nd"]))
                m_lat /= float(len(parser.ways_objects[k]["nd"]))
                locations[nd] = (m_lon, m_lat, parser.ways_objects[k]["tag"]) #a way can have the same id as a node so we take the id of the last node

            logger.info("Number of elements in family %s : %s" % (family, str(len(locations.keys()))))

            categories[family] = locations

        for category in categories.keys():
            for id in categories[category].keys():
                building = Building(int(id), categories[category][id][0], categories[category][id][1], category, None)
                self.features += [building]
示例#25
0
def add_churches(filename, invalidate_refs=True):
    # get churches
    churches = GetChurches(invalidate_refs=invalidate_refs)
    p = OSMParser(concurrency=4,
                  nodes_callback=churches.nodes,
                  ways_callback=churches.ways)
    p.parse(filename)

    # don't run as task anymore:
#    update_refs(filename)
    return True
示例#26
0
def update_refs(filename):
    # get refs
    refs = GetRefs()
    p = OSMParser(concurrency=4,
                  coords_callback=refs.coords)
    p.parse(filename)

    if len(Ref.objects.filter(need_update=True))>0:
        current_task.retry(args=[filename])

    return True
示例#27
0
    def reload_position_data(self):
        print "Reloading (OSM) position data"
        osm_stations = BusStationGetter()
        p = OSMParser(concurrency=4, nodes_callback=osm_stations.nodes)
        p.parse(OSM_file)

        osm_stations = sorted(osm_stations.stations, key=itemgetter(0))
        lpp_stations = sorted(get_lpp_stations(), key=itemgetter(0))
        stations = get_coordinates_for_stations(lpp_stations, osm_stations)
        pickle.dump(stations, open("lpp-station-geo-data.pypickle", "wb"))
        print "Reloaded position data"
示例#28
0
文件: geo.py 项目: uskudnik/trola.si
    def reload_position_data(self):
        print "Reloading (OSM) position data"
        osm_stations = BusStationGetter()
        p = OSMParser(concurrency=4, nodes_callback=osm_stations.nodes)
        p.parse(OSM_file)

        osm_stations = sorted(osm_stations.stations, key=itemgetter(0))
        lpp_stations = sorted(get_lpp_stations(), key=itemgetter(0))
        stations = get_coordinates_for_stations(lpp_stations, osm_stations)
        pickle.dump(stations, open("lpp-station-geo-data.pypickle", "wb"))
        print "Reloaded position data"
示例#29
0
 def import_file(self, file_name):
     """Parses a file and feeds and imports the parsed osm primitives
     into a remote crdb cluster
     
     Parameters:
     file_name -- the name of the input file
     """
     parser = OSMParser(concurrency=10,
                        nodes_callback=self._on_nodes,
                        ways_callback=self._on_ways,
                        relations_callback=self._on_relations)
     parser.parse(file_name)
示例#30
0
def init_mongo():
    osm_parser = OSMParser(nodes_callback=save_places)
    osm_parser.parse('belarus-latest.osm.pbf')

    mongo_client = pymongo.MongoClient()
    places_collection = mongo_client.map.places
    places_collection.remove()
    places_collection.create_index([('location', pymongo.GEOSPHERE)])
    try:
        places_collection.insert([p.as_dict() for p in places], continue_on_error=True)
    except pymongo.errors.DuplicateKeyError:
        pass
示例#31
0
 def parse(self):
     parser = OSMParser(2,
         nodes_callback=self.parse_nodes,
         coords_callback=self.parse_coords,
         ways_callback=self.parse_ways,
         relations_callback=self.parse_relations,
         nodes_tag_filter=self.nodes_filter,
         ways_tag_filter=self.ways_filter,
         relations_tag_filter=self.relations_filter,
     )
     osm_filename = os.path.join(os.path.dirname(__file__), self.osm_filename)
     parser.parse(osm_filename)
示例#32
0
def test_save_bank(capfd):
    """Test saving a bank node
    """
    # set up to parse `bank.osm`
    input_filepath = os.path.join(os.path.dirname(__file__),"data","bank.osm")
    node_handler = NodeHandler()
    p = OSMParser(nodes_callback=node_handler.nodes)
    p.parse(input_filepath)

    # don't use db, so expect tab-separated STDOUT
    resout, reserr = capfd.readouterr()
    assert resout == '1\tBank of Testing\tbank\tPOINT(10.0 50.0)\n'
示例#33
0
	def __init__(self, osmfile) :
		# parse the input file and save its contents in memory
		# node ids and coord pairs as returned from imposm
		self.nodes = dict()
		# actual min and max latitude and longitude of coordinates
		self.bounds = dict()
		self.bounds["min_lat"] = 9999
		self.bounds["max_lat"] = -9999
		self.bounds["min_lng"] = 9999
		self.bounds["max_lng"] = -9999
		p = OSMParser(coords_callback = self.coords_callback)
		p.parse(osmfile)
示例#34
0
def parse(fpath):

    # instantiate counter and parser and start parsing
    osm = Parser()
    p = OSMParser(concurrency=2,
    	ways_callback=osm.ways_callback,
    	coords_callback=osm.coords_callback,
    	nodes_callback=osm.nodes_callback)

    p.parse(fpath)

    return osm
示例#35
0
    def __init__(self, path, offset):
        self.__processor = DataProcessor()
        self.__path = path
        way = OSMParser(concurrency=4,
                        ways_callback=self.__processor.callback_ways)
        coord = OSMParser(concurrency=4,
                          coords_callback=self.__processor.callback_coords)

        way.parse(path)
        coord.parse(path)

        self.__structure = self.__processor.pre_processing(offset)
示例#36
0
 def parse_osm_file(self):
     osm_parser = OSMParser(
         concurrency=2,
         coords_callback=self.parse_points,
         nodes_callback=self.parse_nodes,
         ways_callback=self.parse_ways,
         # relations_ callback=self.relations,
         # nodes_tag_filter=self.nodes_filter,
         # ways_tag_filter=self.ways_filter,
         # relations_tag_filter=self.relations_filter
     )
     osm_parser.parse(self.osm_filename)
示例#37
0
def processShed(pbfFile, outFile, distance, mode, concurrent, stopNodes):
    # instantiate map and parser and start parsing
    osmap = Map()
    p = OSMParser(concurrency=concurrent, ways_callback=osmap.ways_callback, coords_callback=osmap.nodes_callback)
    p.parse(pbfFile)

    osmap.calculateWalkDistances(stopNodes)
    walkshed = osmap.generateWalkShed(distance, {'distance': distance, 'mode': mode})
    
    import json
    with open(outFile, 'w') as f:
        json.dump(walkshed, f)
示例#38
0
文件: import.py 项目: rodo/rosarks
    def handle(self, *args, **options):
        """
        Handle the munin command
        """
        fpath = sys.argv[2]
        self.stdout.write("import {}\n".format(fpath))

        # instantiate counter and parser and start parsing
        chker = RosaParse()
        p = OSMParser(concurrency=4, nodes_callback=chker.pnodes, relations_callback=chker.prelations)

        # Read filename on cmd arg
        p.parse(fpath)
def generateCache(filename = 'berlin.osm.pbf'):
    cache = OSMCache('.')
    coords_queue = JoinableQueue(512)
    coords_writer = CacheWriterProcess(coords_queue, cache.coords_cache, None, marshaled_data=True)
    coords_writer.start()

    parser = OSMParser(coords_callback=coords_queue.put)

    parser.parse(filename)

    coords_queue.put(None)
    coords_writer.join()
    return cache
示例#40
0
def get_graph(filename):
    grabber = OSMloader()
    p = OSMParser(concurrency=1,
                  coords_callback=grabber.nodes,
                  ways_callback=grabber.ways)
    p.parse(filename)
    grabber.spinner.finish()
    print ''

    print 'Simplifying graph... '
    graph = max(nx.strongly_connected_component_subgraphs(grabber.graph,
                                                          copy=True),
                key=len)
    return nx.DiGraph(simplify_graph(graph, strict=False))
 def parse(self):
     parser = OSMParser(
         2,
         nodes_callback=self.parse_nodes,
         coords_callback=self.parse_coords,
         ways_callback=self.parse_ways,
         relations_callback=self.parse_relations,
         nodes_tag_filter=self.nodes_filter,
         ways_tag_filter=self.ways_filter,
         relations_tag_filter=self.relations_filter,
     )
     osm_filename = os.path.join(os.path.dirname(__file__),
                                 self.osm_filename)
     parser.parse(osm_filename)
示例#42
0
def update_refs(filename):
    # get refs
    missing_refs = Ref.objects.filter(need_update=True).count()
    while missing_refs>0:
        refs = GetRefs()
        p = OSMParser(concurrency=4,
                      coords_callback=refs.coords)
        p.parse(filename)

        missing_refs_neu = Ref.objects.filter(need_update=True).count()
        if missing_refs_neu == missing_refs:
            return False

    return True
示例#43
0
    def __call__(self, filename):

        client.init(splitext(basename(filename))[0], self.level)

        if self.drop:
            client.dropdb(self.level)

        parser = OSMParser(concurrency=4,
                           ways_callback=self.way,
                           coords_callback=self.coord,
                           nodes_callback=self.node,
                           relations_callback=self.relation)
        parser.parse(filename)
        self.flush_buffer()
示例#44
0
	def load_file(self, filename):
		# Reinitialize if we have a new file
		ways = []
		coords = {}
		num_coords = 0
		num_ways = 0

		# status output
		if self.verbose:
			sys.stderr.write("\nLoading ways, each '-' is 100 ways, each row is 10,000 ways\n")

		p = OSMParser(ways_callback=self.ways_callback)
		p.parse(filename)

		# status output
		if self.verbose:
			sys.stderr.write("\n{} ways matched in {} {mem:.1f}MB memory used,\n{} coordinates will be loaded, each '.' is 1% complete\n".format(len(self.ways), filename, len(self.coords), mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576))

			total = len(self.coords)
			if total < 100:
				self.coords_marker = 1
			else:
				self.coords_marker = round(total/100)

		p = OSMParser(coords_callback=self.coords_callback)
		p.parse(filename)

		# status output
		if self.verbose:
			sys.stderr.write("\nCoordinates loaded {mem:.1f}MB memory used.".format(mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576))
			sys.stderr.flush()

		# Join routes end-to-end and add them to the way list.
		self.join_ways()

		# status output
		if self.verbose:
			sys.stderr.write("\nJoining complete. {mem:.1f}MB memory used.".format(mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576))
			sys.stderr.write("\nCalculating curvature, each '.' is 1% complete\n")
			sys.stderr.flush()

		# Loop through the ways and calculate their curvature
		start_time = time.time()
		self.calculate()

		# status output
		if self.verbose:
			sys.stderr.write("\nCalculation complete, {mem:.1f}MB memory used".format(mem=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1048576))
			sys.stderr.write('\nCalculation completed in {time:.1f} seconds'.format(time=(time.time() - start_time)))
			sys.stderr.flush()
def parse_file(filename, scraper):
    """
    parse_file
    """
    print('Started parsing file: {}'.format(filename))
    p = OSMParser(concurrency=4, nodes_callback=scraper.nodes)
    start_time = time.clock()
    p.parse(filename)
    proc_time = time.clock()-start_time

    # Add file to processed list, and update the progress file
    scraper.add_file_processed(filename)
    scraper.save_progress()
    print(u"\n \u2713 Processed {} records in {} seconds".format(scraper.processed, proc_time))
示例#46
0
def parse(fname, bb=None, ll_bb=None):
    from imposm.parser import OSMParser

    vertex_set = set([])
    vertices = deque([])  # (osm_id, lon, lat)

    used_vertices = set([])
    out_ways = deque([])
    edges = deque([])  # (osm_id, index, src, dst)

    def coords_callback(clist):
        for osmid, lon, lat in clist:
            p = geom.FPoint(lon, lat)
            if ll_bb is not None and not ll_bb.contains(p):
                continue
            p = coords.lonLatToMeters(p).to_point()
            if bb is not None and not bb.contains(p):
                continue

            vertices.append((osmid, np.double(lon), np.double(lat)))
            vertex_set.add(osmid)

    def ways_callback(ways):
        for osmid, tags, refs in ways:
            if 'highway' not in tags or tags['highway'] in BLACKLIST:
                continue
            for i in range(len(refs) - 1):
                src = refs[i]
                dst = refs[i + 1]
                if src not in vertex_set or dst not in vertex_set:
                    continue

                edges.append((osmid, i, src, dst))
                used_vertices.add(src)
                used_vertices.add(dst)
                out_ways.append((osmid, refs))

    p = OSMParser(coords_callback=coords_callback)
    p.parse(fname)
    p = OSMParser(ways_callback=ways_callback)
    p.parse(fname)

    final_vertices = deque([])
    for v in vertices:
        (osm_id, _, _) = v
        if osm_id in used_vertices:
            final_vertices.append(v)

    return final_vertices, edges, out_ways
示例#47
0
    def handle(self, *args, **options):
        """
        Handle the munin command
        """
        fpath = sys.argv[2]
        self.stdout.write('import {}\n'.format(fpath))

        # instantiate counter and parser and start parsing
        chker = RosaParse()
        p = OSMParser(concurrency=4,
                      nodes_callback=chker.pnodes,
                      relations_callback=chker.prelations)

        # Read filename on cmd arg
        p.parse(fpath)
示例#48
0
文件: osm.py 项目: meyersj/geotweet
 def extract(self, pbf, output):
     """ extract POI nodes from osm pbf extract """
     logging.info("Extracting POI nodes from {0} to {1}".format(pbf, output))
     with open(output, 'w') as f:
         # define callback for each node that is processed
         def nodes_callback(nodes):
             for node in nodes:
                 node_id, tags, coordinates = node
                 # if any tags have a matching key then write record
                 if any([t in tags for t in POI_TAGS]):
                     f.write(json.dumps(dict(tags=tags, coordinates=coordinates)))
                     f.write('\n')
         parser = OSMParser(concurrency=4, nodes_callback=nodes_callback)
         parser.parse(pbf)
     return output
class OSMPbfParserImpl:
    def __init__(self,interface,osmPbfFile,callback_type=None,repository=None):
        #print(IOSMPbfParser)#
        if not isinstance(interface, IOSMPbfParser): raise Exception("Bad Interface")
        self.osmPbfFile = osmPbfFile
        if(callback_type== "nodes_callback"):
            self.parser = OSMParser(concurrency=4,nodes_callback=self.fetchNodes)
            print ("Initializing Nodes Callback......")
        elif(callback_type== "ways_callback"):
            self.parser = OSMParser(concurrency=4,ways_callback=self.fetchWays)
            print ("Initializing Ways Callback......")
        elif(callback_type== "relations_callback"):
            self.parser = OSMParser(concurrency=4,relations_callback=self.fetchRelations)
            print ("Initializing Relations Callback......")
        elif(callback_type== "coords_callback"):
            self.parser = OSMParser(concurrency=4,coords_callback=self.fetchCoords)
            print ("Initializing Coords Callback......")
        else:
            self.parser = OSMParser(concurrency=4,nodes_callback=self.fetchNodes)
        self.repository= repository
    def start(self):
        print ("Started Parser.......")
        self.parser.parse(self.osmPbfFile)    
    def fetchNodes(self,nodes_tuple):
        for osmId, meta_data,coords in nodes_tuple:
            #print (osmId)
            Graph.getInstance().addNode(osmId,Node(osmId,meta_data,coords))
        #print(len(Graph.getInstance().getNodes()))
    def fetchWays(self,ways_tuple,collection=None):
        nodes = Graph.getInstance().getNodes().keys()
        nodes_all = Graph.getInstance().getNodes()
        for osmId, meta_data,refs in ways_tuple:
            refs_set =set(refs)
            #print ('Refs who are nodes')
            common_nodes=list(refs_set.intersection(nodes))
            if len(common_nodes)>1:
                street_edges = []
                for i in range(0,len(common_nodes)-1):
                    edge = Edge(Haversian.getInstance().calculateDistance(nodes_all[common_nodes[i]].getCoords(),nodes_all[common_nodes[i+1]].getCoords()),common_nodes[i],common_nodes[i+1])
                    #print('Haversian distance from '+str(common_nodes[i])+", "+str(common_nodes[i+1]))
                    #print(edge.getDistance())
                    street_edges.append(edge)
                Graph.getInstance().addStreet(osmId, Street(osmId,meta_data,street_edges))
        
    def fetchCoords(self,coords_tuple):
        graph=Graph.getInstance()
        for osmId,lng,lat in coords_tuple:
            graph.addPoint(osmId, Point(lng,lat))
示例#50
0
def generateCache(filename='berlin.osm.pbf'):
    cache = OSMCache('.')
    coords_queue = JoinableQueue(512)
    coords_writer = CacheWriterProcess(coords_queue,
                                       cache.coords_cache,
                                       None,
                                       marshaled_data=True)
    coords_writer.start()

    parser = OSMParser(coords_callback=coords_queue.put)

    parser.parse(filename)

    coords_queue.put(None)
    coords_writer.join()
    return cache
示例#51
0
def main():
    cli = axi.cli()
    cli.add_argument("osm_file", help=".osm map file to read from")
    args = cli.parse_args()

    handler = Handler()
    p = OSMParser(concurrency=1,
                  nodes_callback=handler.on_nodes,
                  ways_callback=handler.on_ways,
                  relations_callback=handler.on_relations,
                  coords_callback=handler.on_coords)
    p.parse(args.osm_file)

    projection = LambertAzimuthalEqualAreaProjection(LAT, LNG)

    w = MAP_WIDTH_KM
    #h = w * 12 / 8.5
    h = w * args.height / args.width

    gs = []
    for key, ways in handler.ways.items():
        if key not in WEIGHTS:
            continue
        weight = WEIGHTS[key]
        paths = []
        for way in ways:
            coords = [projection.project(*handler.coords[x]) for x in way]
            paths.append(coords)
        g = paths_to_shapely(paths)
        if weight:
            g = g.buffer(LANE_WIDTH_M / 1000.0 * weight)
        g = crop(g, w * MARGIN, h * MARGIN)
        gs.append(g)
    g = ops.cascaded_union(gs)
    g = crop(g, w, h)

    paths = shapely_to_paths(g)
    #paths.append(circle(0, 0, 9 / 1000.0, 1000))
    #paths.append(circle(0, 0, 6 / 1000.0, 1000))
    #paths.append(circle(0, 0, 3 / 1000.0, 1000))

    paths.append(box(w, h))
    paths.append(box(w - 5.0 / 1000, h - 5.0 / 1000))
    paths.append(box(w - 10.0 / 1000, h - 10.0 / 1000))

    d = axi.Drawing(paths)
    cli.draw(d)
示例#52
0
def importdb(filename):
    global buf, buffered_coords, buffered_nodes, pushed_coords, pushed_nodes
    buf=[]
    buffered_coords=set()
    buffered_nodes=set()
    pushed_coords=set()
    pushed_nodes=set()
    global waycount, nodecount, coordcount
    nodecount = waycount = coordcount = 0

    p = OSMParser(concurrency=4, 
                  ways_callback=way,
                  coords_callback=coord,
                  nodes_callback=node,
                  relations_callback=relation)
    p.parse(filename)
    flush_buffer()
示例#53
0
    def load_from_file(fname):
        ways = Ways()
        p = OSMParser(concurrency=4, ways_callback=ways.get_ways)
        p.parse(fname)

        coords = Coords()
        p = OSMParser(concurrency=4, coords_callback=coords.get_coords)
        p.parse(fname)
        id2coords = coords.coords

        roads = []
        for tags, refs, weight in ways.ways.values():
            for i in range(1, len(refs)):
                road = (id2coords[refs[i - 1]][0], id2coords[refs[i - 1]][1],
                        id2coords[refs[i]][0], id2coords[refs[i]][1], weight)
                roads.append(road)
        print len(roads)
        return RoadNetwork2(roads)
示例#54
0
def main(filename, output):
    #cache = generateCache(filename)
    cache = OSMCache('.')
    coords_queue = JoinableQueue(512)
    coords_writer = CacheWriterProcess(coords_queue,
                                       cache.coords_cache,
                                       None,
                                       marshaled_data=True)
    coords_writer.start()

    handler = Handler(cache, output)
    parser = OSMParser(coords_callback=coords_queue.put,
                       nodes_callback=handler.nodes_callback,
                       ways_callback=handler.ways_callback)
    parser.parse(filename)

    coords_queue.put(None)
    coords_writer.join()
示例#55
0
文件: osm.py 项目: meyersj/geotweet
    def extract(self, pbf, output):
        """ extract POI nodes from osm pbf extract """
        logging.info("Extracting POI nodes from {0} to {1}".format(
            pbf, output))
        with open(output, 'w') as f:
            # define callback for each node that is processed
            def nodes_callback(nodes):
                for node in nodes:
                    node_id, tags, coordinates = node
                    # if any tags have a matching key then write record
                    if any([t in tags for t in POI_TAGS]):
                        f.write(
                            json.dumps(dict(tags=tags,
                                            coordinates=coordinates)))
                        f.write('\n')

            parser = OSMParser(concurrency=4, nodes_callback=nodes_callback)
            parser.parse(pbf)
        return output
示例#56
0
def get_graph(filename):
    node_coords = {}
    edges = []
    etags = {}

    def node(id, *coords):
        node_coords[id] = coords

    def way(id, tags, refs):
        is_way = False
        if 'highway' in tags:
            is_way = True
        if 'busway' in tags:
            is_way = True
        if not is_way: return
        if tags['highway'] == 'footway': return
        if tags['highway'] == 'cycleway': return
        if tags['highway'] == 'steps': return

        for i in range(len(refs) - 1):
            edge = refs[i], refs[i + 1]
            etags[edge] = tags
            edges.append(edge)

        if not is_oneway(tags):
            tags['oneway'] = 'yes'
            way(None, tags, refs[::-1])

    def nodes(nodes):
        for args in nodes:
            node(*args)

    def ways(ways):
        for args in ways:
            way(*args)

    parser = OSMParser(
        ways_callback=ways,
        coords_callback=nodes,
    )
    parser.parse(filename)

    return node_coords, edges, etags