简体   繁体   中英

Sectors representing and intersections in shapely

I am trying to use shapely to identify the area that intersact between sectors and rectangle. So , my question is divide to two sections:

  1. How to define (create, representing) sector as shapely object ( also triangle is sufficient), my input is coordinate x,y , start angle, end angle , radius.

  2. How to calculate the area that intersact between list of sectors and polygon (rectangle)

Thanks

You can create a sector as a shapely object with the following function:

from shapely.geometry import Point, Polygon
import math

def sector(center, start_angle, end_angle, radius, steps=200):
    def polar_point(origin_point, angle,  distance):
        return [origin_point.x + math.sin(math.radians(angle)) * distance, origin_point.y + math.cos(math.radians(angle)) * distance]

    if start_angle > end_angle:
        start_angle = start_angle - 360
    else:
        pass
    step_angle_width = (end_angle-start_angle) / steps
    sector_width = (end_angle-start_angle) 
    segment_vertices = []

    segment_vertices.append(polar_point(center, 0,0))
    segment_vertices.append(polar_point(center, start_angle,radius))

    for z in range(1, steps):
        segment_vertices.append((polar_point(center, start_angle + z * step_angle_width,radius)))
    segment_vertices.append(polar_point(center, start_angle+sector_width,radius))
    segment_vertices.append(polar_point(center, 0,0))
    return Polygon(segment_vertices)

The center is a shapely point object and steps define the resolution of the curve.

So you can create a sector in this way:

center = Point(0,0)
sect = sector(center, 10, 60, 20)

在此处输入图片说明

To calculate the area of the intersection first you calculate the shape of the intersection in this way:

square = Polygon([(0,0), (0,10),(10,10), (10,0)])

在此处输入图片说明

intersection = sect.intersection(square)

在此处输入图片说明

at this point you obtain the area in this way:

calculated_area = intersection.area

The sector function is deliberately inspired by https://gis.stackexchange.com/questions/67478/how-to-create-a-circle-vector-layer-with-12-sectors-with-python-pyqgis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM