当前位置: 首页>>代码示例>>Python>>正文


Python OSMParser.parse_pbf_file方法代码示例

本文整理汇总了Python中imposm.parser.OSMParser.parse_pbf_file方法的典型用法代码示例。如果您正苦于以下问题:Python OSMParser.parse_pbf_file方法的具体用法?Python OSMParser.parse_pbf_file怎么用?Python OSMParser.parse_pbf_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在imposm.parser.OSMParser的用法示例。


在下文中一共展示了OSMParser.parse_pbf_file方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from imposm.parser import OSMParser [as 别名]
# 或者: from imposm.parser.OSMParser import parse_pbf_file [as 别名]
def main():

    file_name = sys.argv[1]
    filter = AmenityFilter()
    nodes = NodesCoords()
    p = OSMParser(nodes_callback=nodes.nodes_callback,
                  nodes_tag_filter=filter.amenity_filter)
    p.parse_pbf_file(file_name)
    print yaml.dump(nodes.points)
开发者ID:camelia-groza,项目名称:OsmPoint,代码行数:11,代码来源:pbf_parser.py

示例2: OpenStreetMapImporter

# 需要导入模块: from imposm.parser import OSMParser [as 别名]
# 或者: from imposm.parser.OSMParser import parse_pbf_file [as 别名]
class OpenStreetMapImporter(object):
    """
    Imports points of interest from OpenStreetMap
    """

    IMPORTER_NAME = 'openstreetmap'
    IMPORT_SCHEDULE = schedule(run_every=timedelta(weeks=1))

    _ATTRIBUTION = Attribution(
        licence_name='Open Database Licence',
        licence_url='http://www.opendatacommons.org/licenses/odbl',
        attribution_text='OpenStreetMap contributors',
        attribution_url='http://www.openstreetmap.org'
    )

    def __init__(self, config):
        self._parser = OSMParser(
            coords_callback=self.handle_coords,
            nodes_callback=self.handle_nodes,
            ways_callback=self.handle_ways,
            nodes_tag_filter=self.filter_tags,
            ways_tag_filter=self.filter_tags,
        )

        self.poi_service = None

        self._interesting_tags = set(OSM_TAGS_TO_AMENITIES.keys() + OSM_TAGS_TO_CATEGORIES.keys())
        self.pois = []
        self._coords = {}

        try:
            self._url = config['url']
        except KeyError:
            raise ConfigError('OpenStreetMap importer must have url config element set')

    def load(self):
        if not self.poi_service:
            raise RuntimeError("POI Service must be configured before load")
        self.pois = []
        self._coords = {}
        self._source = Source(
            url=self._url,
            version=int(time.time()),
            attribution=self._ATTRIBUTION
        )
        with NamedTemporaryFile() as protobuf_file:
            protobuf_file.write(urlopen(self._url).read())
            protobuf_file.flush()
            self._parser.parse_pbf_file(protobuf_file.name)
        for poi in self.pois:
            self.poi_service.add_or_update(poi)

    def handle_coords(self, coords):
        for id, lat, lon in coords:
            self._coords[id] = (lat, lon)

    def handle_nodes(self, nodes):
        for id, tags, coords in nodes:
            self._add_poi(
                poi_id='N{}'.format(id),
                geography=Point(coords),
                tags=tags
            )

    def handle_ways(self, ways):
        for id, tags, nodes in ways:
            if len(tags) > 0:
                if nodes[0] == nodes[-1]:
                    geography_type = Polygon
                else:
                    geography_type = LineString

                for node_id in nodes:
                    if node_id not in self._coords:
                        LOGGER.warning(
                            'Way %d from file %s has invalid co-ordinate reference',
                            id, self._url
                        )
                        break
                else:
                    self._add_poi(
                        poi_id='W{}'.format(id),
                        geography=geography_type([self._coords[node_id] for node_id in nodes]),
                        tags=tags
                    )

    def filter_tags(self, tags):
        if len(set(tags.items()) & self._interesting_tags) == 0:
            for k in tags.keys():
                del tags[k]

    def _add_poi(self, poi_id, geography, tags):
        self.pois.append(
            PointOfInterest(
                slug='osm:{}'.format(poi_id),
                geography=geography,
                identifiers=Identifiers({Identifier(namespace='osm', value=poi_id)}),
                sources=[self._source],
                amenities=[OSM_TAGS_TO_AMENITIES[tag] for tag in tags.items() if tag in OSM_TAGS_TO_AMENITIES.keys()],
                categories=list(chain.from_iterable(
#.........这里部分代码省略.........
开发者ID:ManchesterIO,项目名称:mollyproject-next,代码行数:103,代码来源:openstreetmap.py


注:本文中的imposm.parser.OSMParser.parse_pbf_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。