QGIS/python/plugins/sextante/ftools/FToolsUtils.py
Victor Olaya 5c762ef741 applied path for #6574
applied patch for multiple grass sessions
added points in polygon weighted
2012-10-27 10:59:23 +02:00

120 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
FToolsUtils.py
---------------------
Date : September 2012
Copyright : (C) 2012 by Carson Farmer, Victor Olaya
Email : volayaf 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__ = 'Carson, Farmer, Victor Olaya'
__date__ = 'September 2012'
__copyright__ = '(C) 2012, Carson Farmer, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from PyQt4.QtCore import *
from qgis.core import *
def createSpatialIndex(provider):
ft = QgsFeature()
idx = QgsSpatialIndex()
provider.rewind()
provider.select()
while provider.nextFeature(ft):
idx.insertFeature(ft)
return idx
def createUniqueFieldName(fieldName, fieldList):
shortName = fieldName[:10]
if len(fieldList) == 0:
return shortName
if shortName not in fieldList:
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:
idx = len(fieldList)
if idx == max(fieldList.keys()):
idx += 1
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
fieldList[idx] = 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 getUniqueValuesCount(layer, fieldIndex, useSelection):
count = 0
values = []
layer.select([fieldIndex], QgsRectangle(), False)
if useSelection:
selection = layer.selectedFeatures()
for f in selection:
if f.attributeMap()[fieldIndex].toString() not in values:
values.append(f.attributeMap()[fieldIndex].toString())
count += 1
else:
feat = QgsFeature()
while layer.nextFeature(feat):
if feat.attributeMap()[fieldIndex].toString() not in values:
values.append(feat.attributeMap()[fieldIndex].toString())
count += 1
return count