1

I would like to create a pie chart-shaped polygon with concentric layers like so: pie example

I can create this simple object with:

from math import sin, cos, radians 
from shapely.geometry import Point,LineString

#variables for buffers
coordinates = (0,0)
point = Point(coordinates)
buffers=[]
outerRadius=100
numberOfCircles=5
radiusStep= outerRadius/numberOfCircles
innerRadius=radiusStep
pi=3.1415


#create the buffers
while innerRadius<=outerRadius:
    buffers.append(point.buffer(innerRadius,200))
    innerRadius+=radiusStep


#variables for linestrings 
centerX = coordinates[0]
centerY = coordinates[1]
coordX=[]
coordY=[]
lines=[]

#create linestrings for circles
for i in xrange(1,361,45):
    xCoord = centerX + outerRadius * cos(radians(i))
    coordX.append(xCoord)
    yCoord = centerY + outerRadius * sin(radians(i))
    coordY.append(yCoord)
    line = LineString([(centerX,centerY),(xCoord,yCoord)])
    lines.append(line)

My question is this:

How can I take multiple shapely objects of different geometries and create a geofile (geojson, shapefile, etc...) so that each section of the "pie" is treated as a separate polygon.

2

You should use Fiona and the mapping function of shapely

from shapely.geometry import mapping
import fiona
# schema of the shapefile (or GeoJSON file, or...) for the lines
schema = {'geometry': 'LineString','properties': {'test': 'int'}}
with fiona.open('lines.shp','w','ESRI Shapefile', schema) as e:
   for i in lines:
       e.write({'geometry':mapping(i), 'properties':{'test':1}})

Result:

enter image description here

You can do the same thing with the buffers but you'll never get a ring polygon as result with your solution (only a superposition of polygons)

You need to use the solution given by MappaGnosis in Does shapely within function identify inner holes?

one = list(buffers[1].exterior.coords)
interior = LinearRing(one)
exterior = LinearRing(list(buffers[2].exterior.coords)
ring = Polygon(exterior,[interior])
#  new schema
schema = {'geometry': 'Polygon','properties': {'test': 'int'}}
# write the shapefile
with fiona.open('ring.shp','w','ESRI Shapefile', schema) as e:
    e.write({'geometry':mapping(ring), 'properties':{'test':1}})

Result: enter image description here

  • Interesting feature of the library, thanks for your help. – camdenl Jan 27 '14 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.