2 changes: 1 addition & 1 deletion python/plugins/processing/algs/ftools/MeanCoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from processing.parameters.ParameterTableField import ParameterTableField
from processing.parameters.ParameterVector import ParameterVector
from processing.outputs.OutputVector import OutputVector
from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class MeanCoords(GeoAlgorithm):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from processing.parameters.ParameterVector import ParameterVector
from processing.outputs.OutputHTML import OutputHTML
from processing.outputs.OutputNumber import OutputNumber
from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class NearestNeighbourAnalysis(GeoAlgorithm):

Expand Down Expand Up @@ -67,7 +67,7 @@ def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
output = self.getOutputValue(self.OUTPUT)

spatialIndex = utils.createSpatialIndex(layer)
spatialIndex = utils.spatialindex(layer)

neighbour = QgsFeature()
distance = QgsDistanceArea()
Expand Down
25 changes: 7 additions & 18 deletions python/plugins/processing/algs/ftools/PointDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import math

from qgis.core import *

from processing.tools import dataobjects, vector
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers

from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterSelection import ParameterSelection
from processing.parameters.ParameterTableField import ParameterTableField

from processing.outputs.OutputTable import OutputTable

from processing.algs.ftools import FToolsUtils as utils

class PointDistance(GeoAlgorithm):

Expand All @@ -54,11 +48,6 @@ class PointDistance(GeoAlgorithm):
"Summary distance matrix (mean, std. dev., min, max)"
]

#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/matrix.png")
#===========================================================================

def defineCharacteristics(self):
self.name = "Distance matrix"
self.group = "Vector analysis tools"
Expand All @@ -73,17 +62,17 @@ def defineCharacteristics(self):
self.addOutput(OutputTable(self.DISTANCE_MATRIX, "Distance matrix"))

def processAlgorithm(self, progress):
inLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
inLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
inField = self.getParameterValue(self.INPUT_FIELD)
targetLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.TARGET_LAYER))
targetLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.TARGET_LAYER))
targetField = self.getParameterValue(self.TARGET_FIELD)
matType = self.getParameterValue(self.MATRIX_TYPE)
nPoints = self.getParameterValue(self.NEAREST_POINTS)

outputFile = self.getOutputFromName(self.DISTANCE_MATRIX)

if nPoints < 1:
nPoints = len(QGisLayers.features(targetLayer))
nPoints = len(vector.features(targetLayer))

self.writer = outputFile.getTableWriter([])

Expand All @@ -100,7 +89,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
else:
self.writer.addRecord(["InputID", "MEAN", "STDDEV", "MIN", "MAX"])

index = utils.createSpatialIndex(targetLayer);
index = vector.spatialindex(targetLayer);

inIdx = inLayer.fieldNameIndex(inField)
inLayer.select([inIdx])
Expand All @@ -111,7 +100,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
outGeom = QgsGeometry()
distArea = QgsDistanceArea()

features = QGisLayers.features(inLayer)
features = vector.features(inLayer)
current = 0
total = 100.0 / float(len(features))
for inFeat in features:
Expand Down Expand Up @@ -142,7 +131,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
progress.setPercentage(int(current * total))

def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, progress):
index = utils.createSpatialIndex(targetLayer)
index = vector.spatialindex(targetLayer)

inIdx = inLayer.fieldNameIndex(inField)
outIdx = targetLayer.fieldNameIndex(inField)
Expand Down
10 changes: 2 additions & 8 deletions python/plugins/processing/algs/ftools/PointsInPolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *

from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers
from processing.core.ProcessingLog import ProcessingLog

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString

from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class PointsInPolygon(GeoAlgorithm):

Expand Down Expand Up @@ -71,7 +65,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
spatialIndex = utils.spatialindex(pointLayer)

ftPoly = QgsFeature()
ftPoint = QgsFeature()
Expand Down
10 changes: 2 additions & 8 deletions python/plugins/processing/algs/ftools/PointsInPolygonUnique.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString

from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class PointsInPolygonUnique(GeoAlgorithm):

Expand Down Expand Up @@ -75,7 +69,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
spatialIndex = utils.spatialindex(pointLayer)

ftPoint = QgsFeature()
outFeat = QgsFeature()
Expand Down
10 changes: 2 additions & 8 deletions python/plugins/processing/algs/ftools/PointsInPolygonWeighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString

from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class PointsInPolygonWeighted(GeoAlgorithm):

Expand Down Expand Up @@ -76,7 +70,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
spatialIndex = utils.spatialindex(pointLayer)

ftPoint = QgsFeature()
outFeat = QgsFeature()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.QGisLayers import QGisLayers
from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils
from processing.parameters.ParameterSelection import ParameterSelection
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterNumber import ParameterNumber
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/processing/algs/ftools/SelectByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
from processing.core.QGisLayers import QGisLayers
from processing.parameters.ParameterSelection import ParameterSelection
from processing.parameters.ParameterVector import ParameterVector

from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class SelectByLocation(GeoAlgorithm):

Expand Down Expand Up @@ -67,7 +65,7 @@ def processAlgorithm(self, progress):
selectLayer = QGisLayers.getObjectFromUri(filename)

oldSelection = set(inputLayer.selectedFeaturesIds())
index = spatialIndex = utils.createSpatialIndex(inputLayer)
index = spatialIndex = utils.spatialindex(inputLayer)

feat = QgsFeature()
geom = QgsGeometry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.QGisLayers import QGisLayers
from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterTableField import ParameterTableField

from processing.outputs.OutputVector import OutputVector

class SinglePartsToMultiparts(GeoAlgorithm):
Expand Down
11 changes: 2 additions & 9 deletions python/plugins/processing/algs/ftools/SumLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers
from processing.core.ProcessingLog import ProcessingLog

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString
from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class SumLines(GeoAlgorithm):

Expand Down Expand Up @@ -78,7 +71,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(lineLayer)
spatialIndex = utils.spatialindex(lineLayer)

ftLine = QgsFeature()
ftPoly = QgsFeature()
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/ftools/Union.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.outputs.OutputVector import OutputVector
from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils
from processing.core.ProcessingLog import ProcessingLog
from processing.core.GeoAlgorithm import GeoAlgorithm

Expand Down Expand Up @@ -59,8 +59,8 @@ def processAlgorithm(self, progress):
inFeatA = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
indexA = utils.createSpatialIndex(vlayerB)
indexB = utils.createSpatialIndex(vlayerA)
indexA = utils.spatialindex(vlayerB)
indexB = utils.spatialindex(vlayerA)

count = 0
nElement = 0
Expand Down
6 changes: 1 addition & 5 deletions python/plugins/processing/algs/ftools/UniqueValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
__revision__ = '$Format:%H$'

import codecs

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterTableField import ParameterTableField

from processing.outputs.OutputHTML import OutputHTML
from processing.outputs.OutputNumber import OutputNumber

from processing.algs.ftools import FToolsUtils as utils
from processing.tools import vector as utils

class UniqueValues(GeoAlgorithm):

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/core/LayerExporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def exportTable( table):
isASCII=False
isDbf = unicode(table.source()).endswith("dbf") or unicode(table.source()).endswith("shp")
if (not isDbf or not isASCII):
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), QGis.WKBNoGeometry, layer.crs() )
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), QGis.WKBNoGeometry, QgsCoordinateReferenceSystem('4326') )
for feat in table.getFeatures():
writer.addFeature(feat)
del writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
classes = {}

#Iterate over input layer to count unique values in each class

feats = processing.getfeatures(layer)
nFeat = len(feats)
for inFeat in feats:
Expand Down
130 changes: 129 additions & 1 deletion python/plugins/processing/tools/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from processing.core.QGisLayers import QGisLayers
from qgis.core import *
from PyQt4.QtCore import *

def uniquevalues(layer, attribute):
'''Returns a list of unique values for a given attribute.
Expand Down Expand Up @@ -84,4 +85,131 @@ def spatialindex(layer):

def getfeatures(layer):
'''returns an iterator over the features of a vector layer, considering the existing selection'''
return QGisLayers.features(layer)
return QGisLayers.features(layer)


def createUniqueFieldName(fieldName, fieldList):
shortName = fieldName[:10]

if len(fieldList) == 0:
return shortName

fieldNames = [f.name() for f in fieldList]

if shortName not in fieldNames:
return shortName

shortName = fieldName[:8] + "_1"
changed = True
while changed:
changed = False
for n in fieldList:
if n == shortName:
# create unique field name
num = int(shortName[-1:])
if num < 9:
shortName = shortName[:8] + "_" + str(num + 1)
else:
shortName = shortName[:7] + "_" + str(num + 1)

changed = True

return shortName

def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fieldNameIndex(fieldName)
if idx == -1:
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
idx = len(fieldList)
fieldList.append(field)

return idx, fieldList

def extractPoints(geom):
points = []
if geom.type() == QGis.Point:
if geom.isMultipart():
points = geom.asMultiPoint()
else:
points.append(geom.asPoint())
elif geom.type() == QGis.Line:
if geom.isMultipart():
lines = geom.asMultiPolyline()
for line in lines:
points.extend(line)
else:
points = geom.asPolyline()
elif geom.type() == QGis.Polygon:
if geom.isMultipart():
polygons = geom.asMultiPolygon()
for poly in polygons:
for line in poly:
points.extend(line)
else:
polygon = geom.asPolygon()
for line in polygon:
points.extend(line)

return points

def simpleMeasure(geom, method=0, ellips=None, crs=None):
# method defines calculation type:
# 0 - layer CRS
# 1 - project CRS
# 2 - ellipsoidal
if geom.wkbType() in [QGis.WKBPoint, QGis.WKBPoint25D]:
pt = geom.asPoint()
attr1 = pt.x()
attr2 = pt.y()
elif geom.wkbType() in [QGis.WKBMultiPoint, QGis.WKBMultiPoint25D]:
pt = inGeom.asMultiPoint()
attr1 = pt[0].x()
attr2 = pt[0].y()
else:
measure = QgsDistanceArea()

if method == 2:
measure.setSourceCrs(crs)
measure.setEllipsoid(ellips)
measure.setEllipsoidalMode(True)

attr1 = measure.measure(geom)
if geom.type() == QGis.Polygon:
attr2 = measure.measurePerimeter(geom)
else:
attr2 = None

return (attr1, attr2)

def getUniqueValues(layer, fieldIndex):
values = []
features = QGisLayers.features(layer)
for feat in features:
if feat.attributes()[fieldIndex] not in values:
values.append(feat.attributes()[fieldIndex])
return values

def getUniqueValuesCount(layer, fieldIndex):
return len(getUniqueValues(layer, fieldIndex))

# From two input field maps, create single field map
def combineVectorFields(layerA, layerB):
fields = []
fieldsA = layerA.dataProvider().fields()
fields.extend(fieldsA)
namesA = [unicode(f.name()).lower() for f in fieldsA]
fieldsB = layerB.dataProvider().fields()
for field in fieldsB:
name = unicode(field.name()).lower()
if name in namesA:
idx=2
newName = name + "_" + unicode(idx)
while newName in namesA:
idx += 1
newName = name + "_" + unicode(idx)
field = QgsField(newName, field.type(), field.typeName())
fields.append(field)

return fields