2012-09-27 08:53:23 +03:00
|
|
|
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
|
2012-09-28 08:59:26 +03:00
|
|
|
|
|
|
|
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
|
2012-09-28 15:35:31 +03:00
|
|
|
|
|
|
|
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
|