2014-10-03 11:09:06 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Gridify.py
|
|
|
|
---------------------
|
|
|
|
Date : May 2010
|
|
|
|
Copyright : (C) 2010 by Michael Minn
|
|
|
|
Email : pyqgis at michaelminn dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Michael Minn'
|
|
|
|
__date__ = 'May 2010'
|
|
|
|
__copyright__ = '(C) 2010, Michael Minn'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
|
|
|
QgsGeometry,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsApplication)
|
2014-10-03 11:09:06 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-10-03 11:09:06 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-10-03 11:09:06 +03:00
|
|
|
class GeometryConvert(GeoAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
TYPE = 'TYPE'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
2014-10-03 11:09:06 +03:00
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Convert geometry type')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
|
2014-10-03 11:09:06 +03:00
|
|
|
|
2015-08-31 16:59:11 +02:00
|
|
|
self.types = [self.tr('Centroids'),
|
|
|
|
self.tr('Nodes'),
|
|
|
|
self.tr('Linestrings'),
|
|
|
|
self.tr('Multilinestrings'),
|
|
|
|
self.tr('Polygons')]
|
|
|
|
|
2014-10-03 11:09:06 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2014-10-03 11:09:06 +03:00
|
|
|
self.addParameter(ParameterSelection(self.TYPE,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('New geometry type'), self.types))
|
2014-10-03 11:09:06 +03:00
|
|
|
|
2015-05-22 15:56:45 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Converted')))
|
2014-10-03 11:09:06 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
def processAlgorithm(self, feedback):
|
2014-10-03 11:09:06 +03:00
|
|
|
layer = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(self.INPUT))
|
|
|
|
index = self.getParameterValue(self.TYPE)
|
|
|
|
|
|
|
|
splitNodes = False
|
|
|
|
if index == 0:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.Point
|
2014-10-03 11:09:06 +03:00
|
|
|
elif index == 1:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.Point
|
2014-10-03 11:09:06 +03:00
|
|
|
splitNodes = True
|
|
|
|
elif index == 2:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.LineString
|
2014-10-03 11:09:06 +03:00
|
|
|
elif index == 3:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.MultiLineString
|
2014-10-03 11:09:06 +03:00
|
|
|
elif index == 4:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.Polygon
|
2014-10-03 11:09:06 +03:00
|
|
|
else:
|
2016-08-04 09:10:08 +02:00
|
|
|
newType = QgsWkbTypes.Point
|
2014-10-03 11:09:06 +03:00
|
|
|
|
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
2016-08-04 07:37:22 +10:00
|
|
|
layer.fields(), newType, layer.crs())
|
2014-10-03 11:09:06 +03:00
|
|
|
|
|
|
|
features = vector.features(layer)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
2014-10-03 11:09:06 +03:00
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-10-03 11:09:06 +03:00
|
|
|
geom = f.geometry()
|
|
|
|
geomType = geom.wkbType()
|
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
if geomType in [QgsWkbTypes.Point, QgsWkbTypes.Point25D]:
|
|
|
|
if newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
writer.addFeature(f)
|
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType in [QgsWkbTypes.MultiPoint, QgsWkbTypes.MultiPoint25D]:
|
|
|
|
if newType == QgsWkbTypes.Point and splitNodes:
|
2014-10-03 11:09:06 +03:00
|
|
|
points = geom.asMultiPoint()
|
|
|
|
for p in points:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(p))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(geom.centroid())
|
|
|
|
writer.addFeature(feat)
|
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType in [QgsWkbTypes.LineString, QgsWkbTypes.LineString25D]:
|
|
|
|
if newType == QgsWkbTypes.Point and splitNodes:
|
2014-10-03 11:09:06 +03:00
|
|
|
points = geom.asPolyline()
|
|
|
|
for p in points:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(p))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(geom.centroid())
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.LineString:
|
2014-10-03 11:09:06 +03:00
|
|
|
writer.addFeature(f)
|
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType in [QgsWkbTypes.MultiLineString, QgsWkbTypes.MultiLineString25D]:
|
|
|
|
if newType == QgsWkbTypes.Point and splitNodes:
|
2014-10-03 11:09:06 +03:00
|
|
|
lines = geom.asMultiPolyline()
|
|
|
|
for line in lines:
|
|
|
|
for p in line:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(p))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(geom.centroid())
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.LineString:
|
2014-10-03 11:09:06 +03:00
|
|
|
lines = geom.asMultiPolyline()
|
|
|
|
for line in lines:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPolyline(line))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.MultiLineString:
|
2014-10-03 11:09:06 +03:00
|
|
|
writer.addFeature(f)
|
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType in [QgsWkbTypes.Polygon, QgsWkbTypes.Polygon25D]:
|
|
|
|
if newType == QgsWkbTypes.Point and splitNodes:
|
2014-10-03 11:09:06 +03:00
|
|
|
rings = geom.asPolygon()
|
|
|
|
for ring in rings:
|
|
|
|
for p in ring:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(p))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(geom.centroid())
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.MultiLineString:
|
2014-10-03 11:09:06 +03:00
|
|
|
rings = geom.asPolygon()
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromMultiPolyline(rings))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Polygon:
|
2014-10-03 11:09:06 +03:00
|
|
|
writer.addFeature(f)
|
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType in [QgsWkbTypes.MultiPolygon, QgsWkbTypes.MultiPolygon25D]:
|
|
|
|
if newType == QgsWkbTypes.Point and splitNodes:
|
2014-10-03 11:09:06 +03:00
|
|
|
polygons = geom.asMultiPolygon()
|
|
|
|
for polygon in polygons:
|
|
|
|
for line in polygon:
|
|
|
|
for p in line:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(p))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Point:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(geom.centroid())
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.LineString:
|
2014-10-03 11:09:06 +03:00
|
|
|
polygons = geom.asMultiPolygon()
|
|
|
|
for polygons in polygons:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPolyline(polygon))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType == QgsWkbTypes.Polygon:
|
2014-10-03 19:15:26 +02:00
|
|
|
polygons = geom.asMultiPolygon()
|
|
|
|
for polygon in polygons:
|
2014-10-03 11:09:06 +03:00
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(f.attributes())
|
|
|
|
feat.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
|
|
writer.addFeature(feat)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif newType in [QgsWkbTypes.MultiLineString, QgsWkbTypes.MultiPolygon]:
|
2014-10-03 19:15:26 +02:00
|
|
|
writer.addFeature(f)
|
2014-10-03 11:09:06 +03:00
|
|
|
else:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Cannot convert from {0} to {1}').format(geomType, newType))
|
2014-10-03 11:09:06 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-03 11:09:06 +03:00
|
|
|
|
|
|
|
del writer
|