Skip to content
Permalink
Browse files

review Lines to polygons and Polygons to lines tools

  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 39ecc61 commit 3fbc3af901caf316cb510e2690096d3ee9352a5c
Showing with 115 additions and 87 deletions.
  1. +58 −44 python/plugins/sextante/ftools/LinesToPolygons.py
  2. +57 −43 python/plugins/sextante/ftools/PolygonsToLines.py
@@ -1,11 +1,15 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers

from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector

class LinesToPolygons(GeoAlgorithm):
@@ -16,52 +20,62 @@ class LinesToPolygons(GeoAlgorithm):
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/to_lines.png")

def defineCharacteristics(self):
self.name = "Lines to polygons"
self.group = "Geometry tools"

self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_LINE))
self.addOutput(OutputVector(self.OUTPUT, "Output layer"))

def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(LinesToPolygons.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = self.getOutputFromName(LinesToPolygons.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, vprovider.crs() )
settings = QSettings()
encoding = settings.value("/UI/encoding", "System").toString()

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputValue(self.OUTPUT)

provider = layer.dataProvider()
layer.select(layer.pendingAllAttributesList())

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
QGis.WKBPolygon, provider.crs())

inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
outGeomList = []
multi = False
nElement += 1
progress.setPercentage(int(nElement/nFeat * 100))
if inFeat.geometry().isMultipart():
outGeomList = inFeat.geometry().asMultiPolyline()
multi = True
else:
outGeomList.append( inFeat.geometry().asPolyline() )
polyGeom = self.remove_bad_lines( outGeomList )
if len(polyGeom) <> 0:
outFeat.setGeometry( QgsGeometry.fromPolygon( polyGeom ) )
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
writer.addFeature( outFeat )
del writer

current = 0
total = 100.0 / float(provider.featureCount())

def remove_bad_lines( self, lines ):
temp_geom = []
if len(lines)==1:
if len(lines[0]) > 2:
temp_geom = lines
else:
temp_geom = []
else:
temp_geom = [elem for elem in lines if len(elem) > 2]
return temp_geom
while layer.nextFeature(inFeat):
outGeomList = []
multi = False

if inFeat.geometry().isMultipart():
outGeomList = inFeat.geometry().asMultiPolyline()
multi = True
else:
outGeomList.append(inFeat.geometry().asPolyline())

def defineCharacteristics(self):
self.name = "Lines to polygons"
self.group = "Geometry tools"
self.addParameter(ParameterVector(LinesToPolygons.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_LINE))
self.addOutput(OutputVector(LinesToPolygons.OUTPUT, "Output layer"))
#=========================================================
polyGeom = self.removeBadLines(outGeomList)
if len(polyGeom) <> 0:
outFeat.setGeometry(QgsGeometry.fromPolygon(polyGeom))
atMap = inFeat.attributeMap()
outFeat.setAttributeMap(atMap)
writer.addFeature(outFeat)

current += 1
progress.setPercentage(int(current * total))

del writer

def removeBadLines(self, lines):
geom = []
if len(lines) == 1:
if len(lines[0]) > 2:
geom = lines
else:
geom = []
else:
geom = [elem for elem in lines if len(elem) > 2]
return geom
@@ -1,12 +1,15 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector

class PolygonsToLines(GeoAlgorithm):
@@ -17,52 +20,63 @@ class PolygonsToLines(GeoAlgorithm):
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/to_lines.png")

def defineCharacteristics(self):
self.name = "Polygons to lines"
self.group = "Geometry tools"

self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))

self.addOutput(OutputVector(self.OUTPUT, "Output layer"))

def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(PolygonsToLines.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QGis.WKBLineString, vprovider.crs() )
settings = QSettings()
encoding = settings.value("/UI/encoding", "System").toString()

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputValue(self.OUTPUT)

provider = layer.dataProvider()
layer.select(layer.pendingAllAttributesList())

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
QGis.WKBLineString, provider.crs())

inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
multi = False
nElement += 1
progress.setPercentage(int(nElement/nFeat * 100))
inGeom = inFeat.geometry()
if inGeom.isMultipart():
multi = True
atMap = inFeat.attributeMap()
lineList = self.extractAsLine( inGeom )
outFeat.setAttributeMap( atMap )
for h in lineList:
outFeat.setGeometry( outGeom.fromPolyline( h ) )
writer.addFeature( outFeat )

current = 0
total = 100.0 / float(provider.featureCount())

while layer.nextFeature(inFeat):
multi = False
inGeom = inFeat.geometry()
if inGeom.isMultipart():
multi = True
atMap = inFeat.attributeMap()
lineList = self.extractAsLine(inGeom)
outFeat.setAttributeMap(atMap)
for h in lineList:
outFeat.setGeometry(outGeom.fromPolyline(h))
writer.addFeature(outFeat)

current += 1
progress.setPercentage(int(current * total))

del writer

def extractAsLine( self, geom ):
multi_geom = QgsGeometry()
temp_geom = []
if geom.type() == 2:
def extractAsLine(self, geom):
multiGeom = QgsGeometry()
lines = []
if geom.type() == QGis.Polygon:
if geom.isMultipart():
multi_geom = geom.asMultiPolygon()
for i in multi_geom:
temp_geom.extend(i)
multiGeom = geom.asMultiPolygon()
for i in multiGeom:
lines.extend(i)
else:
multi_geom = geom.asPolygon()
temp_geom = multi_geom
return temp_geom
multiGeom = geom.asPolygon()
lines = multiGeom
return lines
else:
return []

def defineCharacteristics(self):
self.name = "Polygons to lines"
self.group = "Geometry tools"
self.addParameter(ParameterVector(PolygonsToLines.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
self.addOutput(OutputVector(PolygonsToLines.OUTPUT, "Output layer"))
#=========================================================
return []

0 comments on commit 3fbc3af

Please sign in to comment.
You can’t perform that action at this time.