Skip to content

Commit

Permalink
Merge pull request #3346 from nyalldawson/processing
Browse files Browse the repository at this point in the history
Some processing features + fixes
  • Loading branch information
nyalldawson committed Aug 2, 2016
2 parents e259e62 + 4bfdcf0 commit f9fabb8
Show file tree
Hide file tree
Showing 21 changed files with 678 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ tests/testdata/raster/band1_float32_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band1_int16_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band3_float32_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band3_int16_noct_epsg4326.tif.aux.xml
python/plugins/processing/tests/testdata/custom/grass7/float_raster.tif.aux.xml
python/plugins/processing/tests/testdata/custom/grass7/raster_1class.tif.aux.xml
Thumb.db
5 changes: 5 additions & 0 deletions python/plugins/processing/algs/help/qgis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ qgis:meancoordinates: >

If an attribute is selected in the <Unique ID field> parameters, features will be grouped according to values in this field. Instead of a single point with the center of mass of the whole layer, the output layer will contain a center of mass for the features in each category.

qgis:mergelines: >
This algorithm joins all connected parts of MultiLineString geometries into single LineString geometries.

If any parts of the input MultiLineString geometries are not connected, the resultant geometry will be a MultiLineString containing any lines which could be merged and any non-connected line parts.

qgis:mergevectorlayers: >
This algorithm combines two vector layer of the same geometry type into a single one.

Expand Down
50 changes: 25 additions & 25 deletions python/plugins/processing/algs/qgis/Dissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

import os
from collections import defaultdict

from qgis.PyQt.QtGui import QIcon

Expand All @@ -36,7 +37,7 @@
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterTableMultipleField
from processing.core.outputs import OutputVector
from processing.tools import vector, dataobjects

Expand All @@ -60,22 +61,24 @@ def defineCharacteristics(self):
self.tr('Input layer'),
[ParameterVector.VECTOR_TYPE_POLYGON, ParameterVector.VECTOR_TYPE_LINE]))
self.addParameter(ParameterBoolean(Dissolve.DISSOLVE_ALL,
self.tr('Dissolve all (do not use field)'), True))
self.addParameter(ParameterTableField(Dissolve.FIELD,
self.tr('Unique ID field'), Dissolve.INPUT, optional=True))
self.tr('Dissolve all (do not use fields)'), True))
self.addParameter(ParameterTableMultipleField(Dissolve.FIELD,
self.tr('Unique ID fields'), Dissolve.INPUT, optional=True))
self.addOutput(OutputVector(Dissolve.OUTPUT, self.tr('Dissolved')))

def processAlgorithm(self, progress):
useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
fieldname = self.getParameterValue(Dissolve.FIELD)
field_names = self.getParameterValue(Dissolve.FIELD)
vlayerA = dataobjects.getObjectFromUri(
self.getParameterValue(Dissolve.INPUT))
vproviderA = vlayerA.dataProvider()
fields = vlayerA.fields()

writer = self.getOutputFromName(
Dissolve.OUTPUT).getVectorWriter(fields,
vproviderA.geometryType(),
vproviderA.crs())
Dissolve.OUTPUT).getVectorWriter(
vlayerA.fields().toList(),
vproviderA.geometryType(),
vlayerA.crs())

outFeat = QgsFeature()
features = vector.features(vlayerA)
total = 100.0 / len(features)
Expand Down Expand Up @@ -125,20 +128,16 @@ def processAlgorithm(self, progress):
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
else:
fieldIdx = vlayerA.fieldNameIndex(fieldname)
unique = vector.getUniqueValues(vlayerA, int(fieldIdx))
nFeat = len(unique)
myDict = {}
attrDict = {}
for item in unique:
myDict[unicode(item).strip()] = []
attrDict[unicode(item).strip()] = None
field_indexes = [vlayerA.fieldNameIndex(f) for f in field_names.split(';')]

unique = None
attribute_dict = {}
geometry_dict = defaultdict(lambda: [])

for inFeat in features:
attrs = inFeat.attributes()
tempItem = attrs[fieldIdx]

index_attrs = tuple([attrs[i] for i in field_indexes])

tmpInGeom = QgsGeometry(inFeat.geometry())
if tmpInGeom.isGeosEmpty():
continue
Expand All @@ -152,16 +151,17 @@ def processAlgorithm(self, progress):
'geometry: ')
+ error.what())

if attrDict[unicode(tempItem).strip()] is None:
if not index_attrs in attribute_dict:
# keep attributes of first feature
attrDict[unicode(tempItem).strip()] = attrs
attribute_dict[index_attrs] = attrs

myDict[unicode(tempItem).strip()].append(tmpInGeom)
geometry_dict[index_attrs].append(tmpInGeom)

features = None
nFeat = len(attribute_dict)

nElement = 0
for key, value in myDict.items():
for key, value in geometry_dict.items():
outFeat = QgsFeature()
nElement += 1
progress.setPercentage(int(nElement * 100 / nFeat))
try:
Expand All @@ -170,7 +170,7 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Geometry exception while dissolving'))
outFeat.setGeometry(tmpOutGeom)
outFeat.setAttributes(attrDict[key])
outFeat.setAttributes(attribute_dict[key])
writer.addFeature(outFeat)

del writer
90 changes: 90 additions & 0 deletions python/plugins/processing/algs/qgis/MergeLines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
Smooth.py
---------
Date : July 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail 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__ = 'Nyall Dawson'
__date__ = 'July 2016'
__copyright__ = '(C) 2016, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive323

__revision__ = '$Format:%H$'

import os

from qgis.core import QgsFeature

from qgis.PyQt.QtGui import QIcon

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class MergeLines(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'

def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Merge lines')
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.fields().toList(),
provider.geometryType(),
layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)

for current, inFeat in enumerate(features):
outFeat = QgsFeature()
attrs = inFeat.attributes()
outFeat.setAttributes(attrs)

inGeom = inFeat.geometry()
if inGeom:
outGeom = inGeom.mergeLines()
if outGeom is None:
raise GeoAlgorithmExecutionException(
self.tr('Error merging lines'))

outFeat.setGeometry(outGeom)

writer.addFeature(outFeat)
progress.setPercentage(int(current * total))

del writer
18 changes: 10 additions & 8 deletions python/plugins/processing/algs/qgis/MultipartToSingleparts.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,22 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.pendingFields().toList(), geomType, layer.crs())

outFeat = QgsFeature()
inGeom = QgsGeometry()

features = vector.features(layer)
total = 100.0 / len(features)
for current, f in enumerate(features):
inGeom = f.geometry()
outFeat = QgsFeature()
attrs = f.attributes()

geometries = self.extractAsSingle(inGeom)
outFeat.setAttributes(attrs)

for g in geometries:
outFeat.setGeometry(g)
inGeom = f.geometry()
if inGeom:
geometries = self.extractAsSingle(inGeom)

for g in geometries:
outFeat.setGeometry(g)
writer.addFeature(outFeat)
else:
#input feature with null geometry
writer.addFeature(outFeat)

progress.setPercentage(int(current * total))
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
from .DefineProjection import DefineProjection
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
from .MergeLines import MergeLines

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
Expand Down Expand Up @@ -197,7 +198,7 @@ def __init__(self):
CheckValidity(), OrientedMinimumBoundingBox(), Smooth(),
ReverseLineDirection(), SpatialIndex(), DefineProjection(),
RectanglesOvalsDiamondsVariable(),
RectanglesOvalsDiamondsFixed()
RectanglesOvalsDiamondsFixed(), MergeLines()
]

if hasMatplotlib:
Expand Down
10 changes: 5 additions & 5 deletions python/plugins/processing/gui/ListMultiselectWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
QListWidget,
QAbstractItemView)
from qgis.PyQt.QtGui import QFont
from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtCore import Qt, QSize, pyqtSignal


class ListMultiSelectWidget(QGroupBox):
Expand Down Expand Up @@ -163,7 +163,7 @@ def _setupUI(self):
self._set_list_widget_defaults(self.unselected_widget)
unselected_label = QLabel()
unselected_label.setText('Unselected')
unselected_label.setAlignment(Qt.Qt.AlignCenter)
unselected_label.setAlignment(Qt.AlignCenter)
unselected_label.setFont(italic_font)
unselected_v_layout = QVBoxLayout()
unselected_v_layout.addWidget(unselected_label)
Expand All @@ -174,7 +174,7 @@ def _setupUI(self):
self._set_list_widget_defaults(self.selected_widget)
selected_label = QLabel()
selected_label.setText('Selected')
selected_label.setAlignment(Qt.Qt.AlignCenter)
selected_label.setAlignment(Qt.AlignCenter)
selected_label.setFont(italic_font)
selected_v_layout = QVBoxLayout()
selected_v_layout.addWidget(selected_label)
Expand Down Expand Up @@ -215,7 +215,7 @@ def _set_list_widget_defaults(self, widget):
widget.setDragEnabled(True)
widget.setDragDropMode(QAbstractItemView.DragDrop)
widget.setDragDropOverwriteMode(False)
widget.setDefaultDropAction(QtCore.Qt.MoveAction)
widget.setDefaultDropAction(Qt.MoveAction)
widget.setSelectionMode(QAbstractItemView.MultiSelection)


Expand All @@ -227,4 +227,4 @@ def __init__(self, text):
buttons_size_policy = QSizePolicy(
QSizePolicy.Fixed, QSizePolicy.Fixed)
self.setSizePolicy(buttons_size_policy)
self.setMaximumSize(QtCore.QSize(30, 30))
self.setMaximumSize(QSize(30, 30))
31 changes: 31 additions & 0 deletions python/plugins/processing/tests/testdata/dissolve_polys.gfs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>dissolve_polys</Name>
<ElementPath>dissolve_polys</ElementPath>
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>10</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>9.16296</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>6.08868</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>2</Width>
</PropertyDefn>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
Loading

0 comments on commit f9fabb8

Please sign in to comment.