__init__.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:python-fhrs-osm 作者: gregrs-uk 项目源码 文件源码
def get_way_centroid(self, way):
        """Calculate the centroid of a way

        way (object): overpy.Way object
        Returns dict of lat/lon
        """
        # Polygon has to have at least 3 points
        if len(way.nodes) >= 3:
            geom = []
            for node in way.nodes:
                geom.append((node.lon, node.lat))
            poly = Polygon(geom)
            cent = poly.centroid
            return {'lat': cent.y, 'lon': cent.x}
        elif len(way.nodes) == 2:
            # if way has 2 nodes, use average position
            lat = (way.nodes[0].lat + way.nodes[1].lat) / 2
            lon = (way.nodes[0].lon + way.nodes[1].lon) / 2
            return {'lat': lat, 'lon': lon}
        elif len(way.nodes) == 1:
            # if way has 1 node, use that position
            # (unusual and certainly a bug but possible)
            return {'lat': way.nodes[0].lat, 'lon': way.nodes[0].lon}
        else:
            raise RuntimeError
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号