计算polygon面积和判断顺逆时针方向的方法

11 篇文章 1 订阅

一.利用shapely求polygon面积

  import shapely
    from shapely.geometry import Polygon, MultiPoint  # 多边形
    # box1 = [2, 0, 4, 2, 2, 4, 0, 2, 0, 0]
    box1 = [2, 0, 4, 2, 2, 4, 0, 2, 2, 2]
    poly_box1 = Polygon(np.array(box1).reshape(-1,2))
    print(poly_box1)
    print(poly_box1.area)

二.逆时针调整为顺时针

1.四个点 

def polygon_area1():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针
    poly = np.array([[2, 0],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    poly_h = poly.shape[0]
    edge = []
    for i in range(poly_h - 1):
        edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))
    edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))
    # edge = [
    #     (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
    #     (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
    #     (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
    #     (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
    # ]
    area = np.sum(edge) / 2.
    print('=====The first way======')
    print('area:', area)
    if area <0:
        index = [0] + [i for i in range(poly_h - 1, 0, -1)]
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
def polygon_area2():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针
    poly = np.array([[2, 0],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    edge = []
    poly_h = poly.shape[0]
    for i in range(poly_h-1):
        edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])
    edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])
    print('=====The second way======')
    # print('edge:', edge)
    area = np.sum(edge)/2.
    print('area:', area)
    #如果是逆时针 调整为顺时针
    if area < 0:
        index = [0] + [i for i in range(poly_h-1, 0, -1)]
        # print(index)
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
if __name__ == '__main__':
    # show_image()
    # compute_IOU_()
    polygon_area1()
    polygon_area2()

2.多个点

def polygon_area1():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针 凹多边形
    print('凹多边形')
    poly = np.array([[2, 0],
                     [2, 2],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    # 逆时针 凸多边形
    # print('凸多边形')
    # poly = np.array([[2, 0],
    #                  [0, 0],
    #                  [0, 2],
    #                  [2, 4],
    #                  [4, 2]])
    poly_h = poly.shape[0]
    edge = []
    for i in range(poly_h - 1):
        edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))
    edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))
    # edge = [
    #     (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
    #     (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
    #     (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
    #     (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
    # ]
    area = np.sum(edge) / 2.
    print('=====The first way======')
    print('area:', area)
    if area <0:
        print('此为逆时针,调整为顺时针')
        index = [0] + [i for i in range(poly_h - 1, 0, -1)]
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
def polygon_area2():
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    # # #顺时针
    # poly = np.array([[2, 0],
    #                  [4, 2],
    #                  [2, 4],
    #                  [0, 2]])
    # 逆时针 凹多边形
    print('凹多边形')
    poly = np.array([[2, 0],
                     [2, 2],
                     [0, 2],
                     [2, 4],
                     [4, 2]])
    # 逆时针 凸多边形
    # print('凸多边形')
    # poly = np.array([[2, 0],
    #                  [0, 0],
    #                  [0, 2],
    #                  [2, 4],
    #                  [4, 2]])
    edge = []
    poly_h = poly.shape[0]
    for i in range(poly_h-1):
        edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])
    edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])
    print('=====The second way======')
    # print('edge:', edge)
    area = np.sum(edge)/2.
    print('area:', area)
    #如果是逆时针 调整为顺时针
    if area < 0:
        print('此为逆时针,调整为顺时针')
        index = [0] + [i for i in range(poly_h-1, 0, -1)]
        # print(index)
        fix_poly = poly[index, :]
        print('fix_poly:', fix_poly)
if __name__ == '__main__':
    # show_image()
    # compute_IOU_()
    polygon_area1()
    polygon_area2()

           

三.计算不规则多边形面积

分解成梯形,面积依次相加

import numpy as np
points = np.array([[0.72, 2.28],
                   [2.66, 4.71],
                   [5,	 3.5],
                   [3.63, 2.52],
                   [4,	1.6],
                   [1.9,	1]])
print(points.shape)
h, w = points.shape
areas = []
for i in range(0, points.shape[0]):
    area = (points[(i+1) % h][-1] + points[i][-1])*(points[(i+1) % h][0] - points[i][0])/2
    areas.append(area)
    print(points[i])
    print(points[(i+1) % h])
print('==areas:', areas)
print(sum(areas))

 

参考:https://www.cnblogs.com/TenosDoIt/p/4047211.html

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中,我们可以使用高德地图的API来根据经纬度计算面积判断一个点是否在圈内。 首先,我们需要导入高德地图的相关库文件,并获取一个AMap对象,用来进行地图操作。 然后,我们可以使用AMap对象中的AMapUtils类中的calculateArea()方法计算多边形的面积。这个方法接收一个List<LatLng>类型的参数,用于传入多边形的顶点经纬度坐标。返回的面积单位为平方米。 另外,要判断一个点是否在圈内,我们可以使用AMap对象中的AMapUtils类中的isPolygonContainsPoint()方法。这个方法接收一个LatLng类型的点坐标和一个List<LatLng>类型的多边形顶点坐标。返回一个布尔值,表示该点是否在多边形内部。 具体代码如下: ``` // 导入相关库文件 import com.amap.api.maps.AMap; import com.amap.api.maps.AMapUtils; import com.amap.api.maps.model.LatLng; // 获取AMap对象 AMap aMap = mapView.getMap(); // 计算多边形面积 List<LatLng> polygonPoints = new ArrayList<>(); polygonPoints.add(new LatLng(39.90923, 116.397428)); polygonPoints.add(new LatLng(39.90823, 116.397428)); polygonPoints.add(new LatLng(39.90823, 116.398428)); polygonPoints.add(new LatLng(39.90923, 116.398428)); float area = AMapUtils.calculateArea(polygonPoints); // 判断一个点是否在圈内 LatLng point = new LatLng(39.90923, 116.397428); boolean isContains = AMapUtils.isPolygonContainsPoint(point, polygonPoints); ``` 以上就是使用Android Studio中的高德地图API根据经纬度计算面积判断一个点是否在圈内的方法。注意,要在项目中正确引入高德地图的API,并在AndroidManifest.xml文件中注册相关权限和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值