# How to create a circle vector layer with 12 sectors with Python/pyqgis?

From a given coordinate and radius I need to create a circle with 12 sectors. The sectors should start from north (0 degrees) in 30 degree steps clockwise. I could also imagine to create the sectors by triangles, if that would be easier.

I wrote a small script that generates polygonal sectors (for now it exports the data as GeoJSON, but you should easily be able to export the data as a Shapefile etc.). You can easily adjust the parameters as you can see on the screenshot:

``````from shapely.geometry import Point, Polygon
import math
import geojson

# initial parameters for segmentation
steps = 90 # subdivision of circle. The higher, the smoother it will be
sectors = 12.0 # number of sectors in the circle (12 means 30 degrees per sector)
start = 315.0 # start of circle in degrees
end = 45.0 # end of circle in degrees
center = Point(23,42)

# prepare parameters
if start > end:
start = start - 360
else:
pass

step_angle_width = (end-start) / steps
sector_width = (end-start) / sectors
steps_per_sector = int(math.ceil(steps / sectors))

# helper function to calculate point from relative polar coordinates (degrees)
def polar_point(origin_point, angle,  distance):

features = []
for x in xrange(0,int(sectors)):
segment_vertices = []

# first the center and first point
segment_vertices.append(polar_point(center, 0,0))

# then the sector outline points
for z in xrange(1, steps_per_sector):
segment_vertices.append((polar_point(center, start + x * sector_width + z * step_angle_width,radius)))

# then again the center point to finish the polygon
segment_vertices.append(polar_point(center, start + x * sector_width+sector_width,radius))
segment_vertices.append(polar_point(center, 0,0))

# create feature
features.append(geojson.Feature(
geometry=Polygon(segment_vertices))
)

# prepare geojson feature collection
res = geojson.FeatureCollection(
features = features
)

# write to file
f = open('sector.json', 'w')
f.write(geojson.dumps(res))
f.close()
``````
• wow - I will test it soon and report back... thanks! – dassau Jul 31 '13 at 13:35
• One more question, please. Would it also be possible to have the 12 sector circle starting from 345 degree to 15 degree? – dassau Jul 31 '13 at 14:53
• sure. i updated my code accordingly. – chriserik Jul 31 '13 at 15:54
• when drawing in equirectangular space (and most other CRS), just be aware the the areas of the wedges are not equal in size: gis.stackexchange.com/questions/28390/… Easiest fix would be drawing the circle in an azimuthal projection with the circle center as origin and reproject the output to the desired CRS – mxfh Oct 12 '14 at 4:05

i have written some code with javascript to test your request and got the following result. I think you can make a good start from here. you can convert it to python. (if not i can help you...)

Code:

``````var leng = 12;
var angle = 360 / leng
var power = 150;

var ang = 0;
angles = [];

while (ang < 360) {
var tx = xy.lon + (Math.cos((Math.PI / 180) * ang) * power);
var ty = xy.lat + (Math.sin((Math.PI / 180) * ang) * power);
if (ang >= 0) {
angles.push([tx, ty]);
}
ang += 360 / leng
};
``````

i hope it helps you...

• Thanks a lot, but answer 1 seems to be exactly what I am looking for. – dassau Jul 31 '13 at 13:36