[Qgis-user] divide poligons

Carson Farmer carson.farmer at gmail.com
Thu Feb 24 08:04:41 PST 2011


Hi Dario,

> I have a layer of polygons, these polygons are cut by two parallel lines.
> Sometimes just one line crosses the polygon, dividing it into two.
> need to create new polygons, formed by the polygon area that is contained
> between the lines, and the areas that are outside the inner lines, and the
> second case to create new polygons formed by the two areas divided by the
> line.
> Is there any plugin or geoprocess  in quantum gis what running this?
> I can only do the reverse process, which is the polygon cut the lines that
> intersect with it, but I can not do otherwise with the lines dividing the
> polygon.
I don't know of any plugin that will do this, but it can certainly be
done using the Python scripting interface in QGIS. The attached Python
script *should* do what you want for most basic applications. A usage
example is given at the top of the script, but basically you just save
the script somewhere where the QGIS Python console can find it (best
bet is your ~/.qgis/python folder where all your plugins are
installed, if you don't know where this is, the Python plugin
installer should indicate this on the main dialog). Then simply type
(into the QGIS Python console):

>>> canvas = qgis.utils.iface.mapCanvas()
>>> layer_a = canvas.layer(0) # polygon layer
>>> layer_b = canvas.layer(1) # line layer
>>> from split_polygons import split_polygons
>>> split_polygons(layer_a, layer_b, "/home/username/Desktop/output.shp")

This assume that your polygon layer in the top layer in the layer list
(and is visible), and the line layer in the second from the top (and
is also visible). The 3rd argument to split_polygons is the output
path for the resultant shapefile, and can be any valid path string, so
on Windows it might be something like: C:\workingfolder\output.shp

Let me know if you need any further help with the script. It's really
just a quick stab at this. I tested it using very basic polygons and
lines that I hand digitised for testing purposes. Eventually I'll try
to make it into a plugin, but until then it should at least get you
going.

Regards,

Carson

-- 
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation
National University of Ireland, Maynooth,
http://www.carsonfarmer.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: split_polygons.py
Type: application/octet-stream
Size: 2897 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20110224/37942e67/attachment.obj>


More information about the Qgis-user mailing list