Skip to content

Commit

Permalink
more api updates in ftools
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Feb 2, 2013
1 parent 63cc49b commit 2a2d944
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion python/plugins/fTools/tools/doGeoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ def union( self ):
# in geometries of different types
# produced by the intersection
# fix #3549
gList = ftools_utils.getGeomType( geom.wkbType() )
gList = ftools_utils.getGeomType( geom.wkbType() )
if int_geom.wkbType() in gList:
try:
outFeat.setGeometry( int_geom )
Expand Down
4 changes: 3 additions & 1 deletion python/plugins/fTools/tools/doRandPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
sGeom = QgsGeometry()
sPoints = []
if design == self.tr("field"):
for (i, attr) in sProvider.fields().iteritems():
i = 0
for attr in sProvider.fields():
if (unicode(numRand) == attr.name()):
index = i #get input field index
break
i += 1
count = 10.00
add = 60.00 / sProvider.featureCount()
sFit = sProvider.getFeatures()
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/fTools/tools/ftools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def createUniqueFieldName( field ):
# Return list of field names with more than 10 characters length
def checkFieldNameLength( fieldList ):
longNames = QStringList()
for num, field in fieldList.iteritems():
for field in fieldList:
if field.name().size() > 10:
longNames << unicode( field.name() )
return longNames
Expand All @@ -209,7 +209,7 @@ def getLayerNames( vTypes ):
def getFieldNames( vlayer ):
fieldmap = getFieldList( vlayer )
fieldlist = []
for name, field in fieldmap.iteritems():
for field in fieldmap:
if not field.name() in fieldlist:
fieldlist.append( unicode( field.name() ) )
return sorted( fieldlist, cmp=locale.strcoll )
Expand Down Expand Up @@ -299,14 +299,12 @@ def openDialog( parent, filtering="Shapefiles (*.shp *.SHP)", dialogMode="Single
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
encode = settings.value( "/UI/encoding" ).toString()
fileDialog = QgsEncodingFileDialog( parent, "Save output shapefile", dirName, QString(filtering), encode )
#fileDialog.setFileMode( QFileDialog.AnyFile )
fileDialog.setFileMode( QFileDialog.ExistingFiles )
fileDialog.setAcceptMode( QFileDialog.AcceptOpen )
if not fileDialog.exec_() == QDialog.Accepted:
return None, None
files = fileDialog.selectedFiles()
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( files.first() ) ).absolutePath() ) )
#return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
if dialogMode == "SingleFile":
return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
else:
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/mapserver_export/ms_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def writeMapLayers(self):
# TODO: add epsg to all METADATA tags??
except:
print "ERROR while trying to write ows_srs METADATA"
pass
pass
layer_def += " 'wms_format' '" + format + "'\n"
layer_def += " 'wms_style' '" + ','.join(wmsStyles) + "'\n"
layer_def += " END\n"
Expand Down Expand Up @@ -687,9 +687,11 @@ def getPrimaryKey(self, layerId, tableName):
intTypes = [QVariant.Int, QVariant.LongLong, QVariant.UInt, QVariant.ULongLong]

integerFields = []
for id, field in fields.iteritems():
id = 0
for field in fields:
if field.type() in intTypes:
integerFields.append(id)
id += 1

# fid end
fidIntegerFields = []
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/Intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def processAlgorithm(self, progress):
int_geom = QgsGeometry( int_com.difference( int_sym ) )
try:
outFeat.setGeometry( int_geom )
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
outFeat.setAttributeMap( atMapA.extend( atMapB ) )
writer.addFeature( outFeat )
except:
FEATURE_EXCEPT = False
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/Union.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def processAlgorithm(self, progress):
int_geom = QgsGeometry( i )
try:
outFeat.setGeometry( int_geom )
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
outFeat.setAttributes( atMapA.extend( atMapB ) )
writer.addFeature( outFeat )
except Exception, err:
FEATURE_EXCEPT = False
Expand Down
114 changes: 58 additions & 56 deletions python/plugins/sextante/algs/ftools/ftools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""

from sextante.core.QGisLayers import QGisLayers

__author__ = 'Carson Farmer, Victor Olaya'
Expand All @@ -27,7 +28,6 @@
# Utility functions
# -------------------------------------------------
#
# combineVectorAttributes( QgsAttributeMap, QgsAttributeMap )
# convertFieldNameType( QgsField.name() )
# combineVectorFields( QgsVectorLayer, QgsVectorLayer )
# checkCRSCompatibility( QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem )
Expand All @@ -42,12 +42,12 @@
# getFieldNames( QgsVectorLayer )
# getVectorLayerByName( QgsVectorLayer.name() )
# getFieldList( QgsVectorLayer )
# createIndex( QgsVectorDataProvider )
# createIndex( QgsFeatureList )
# addShapeToCanvas( QString *file path )
# getUniqueValues( QgsVectorDataProvider, int *field id )
# saveDialog( QWidget *parent )
# getFieldType( QgsVectorLayer, QgsField.name() )
# getUniqueValuesCount( QgsVectorLayer, int fieldIndex, bool useSelection ):
# getUniqueValuesCount( QgsVectorLayer, int fieldIndex )
#
# -------------------------------------------------

Expand All @@ -56,14 +56,7 @@
from qgis.core import *
from qgis.gui import *

# From two input attribute maps, create single attribute map
def combineVectorAttributes( atMapA, atMapB ):
attribA = atMapA.values()
lengthA = len(attribA)
attribB = atMapB.values()
lengthB = len(attribB)
attribA.extend( attribB )
return dict( zip( range( 0, lengthB + lengthA ), attribA ) )
import locale

# For use with memory provider/layer, converts full field type to simple string
def convertFieldNameType( inName ):
Expand All @@ -76,12 +69,10 @@ def convertFieldNameType( inName ):

# From two input field maps, create single field map
def combineVectorFields( layerA, layerB ):
fieldsA = layerA.dataProvider().fields().values()
fieldsB = layerB.dataProvider().fields().values()
fieldsA = layerA.dataProvider().fields()
fieldsB = layerB.dataProvider().fields()
fieldsB = testForUniqueness( fieldsA, fieldsB )
seq = range( 0, len( fieldsA ) + len( fieldsB ) )
fieldsA.extend( fieldsB )
fieldsA = dict( zip ( seq, fieldsA ) )
return fieldsA

# Check if two input CRSs are identical
Expand Down Expand Up @@ -188,7 +179,7 @@ def createUniqueFieldName( field ):
# Return list of field names with more than 10 characters length
def checkFieldNameLength( fieldList ):
longNames = QStringList()
for num, field in fieldList.iteritems():
for field in fieldList:
if field.name().size() > 10:
longNames << unicode( field.name() )
return longNames
Expand All @@ -208,16 +199,16 @@ def getLayerNames( vTypes ):
elif layer.type() == QgsMapLayer.RasterLayer:
if "Raster" in vTypes:
layerlist.append( unicode( layer.name() ) )
return layerlist
return sorted( layerlist, cmp=locale.strcoll )

# Return list of names of all fields from input QgsVectorLayer
def getFieldNames( vlayer ):
fieldmap = getFieldList( vlayer )
fieldlist = []
for name, field in fieldmap.iteritems():
for field in fieldmap:
if not field.name() in fieldlist:
fieldlist.append( unicode( field.name() ) )
return fieldlist
return sorted( fieldlist, cmp=locale.strcoll )

# Return QgsVectorLayer from a layer name ( as string )
def getVectorLayerByName( myName ):
Expand All @@ -229,6 +220,16 @@ def getVectorLayerByName( myName ):
else:
return None

# Return QgsRasterLayer from a layer name ( as string )
def getRasterLayerByName( myName ):
layermap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layermap.iteritems():
if layer.type() == QgsMapLayer.RasterLayer and layer.name() == myName:
if layer.isValid():
return layer
else:
return None

# Return QgsMapLayer from a layer name ( as string )
def getMapLayerByName( myName ):
layermap = QgsMapLayerRegistry.instance().mapLayers()
Expand All @@ -241,19 +242,13 @@ def getMapLayerByName( myName ):

# Return the field list of a vector layer
def getFieldList( vlayer ):
vprovider = vlayer.dataProvider()
feat = QgsFeature()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
myFields = vprovider.fields()
return myFields
return vlayer.dataProvider().fields()

# Convinience function to create a spatial index for input QgsVectorDataProvider
def createIndex( features ):
feat = QgsFeature()
index = QgsSpatialIndex()
for feature in features:
index.insertFeature( feature )
for feat in features:
index.insertFeature( feat )
return index

# Convinience function to add a vector layer to canvas based on input shapefile path ( as string )
Expand All @@ -273,11 +268,10 @@ def addShapeToCanvas( shapefile_path ):

# Return all unique values in field based on field index
def getUniqueValues( provider, index ):
values = provider.uniqueValues( index )
return values
return provider.uniqueValues( index )

# Generate a save file dialog with a dropdown box for choosing encoding style
def saveDialog( parent, filtering="Shapefiles (*.shp)"):
def saveDialog( parent, filtering="Shapefiles (*.shp *.SHP)"):
settings = QSettings()
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
encode = settings.value( "/UI/encoding" ).toString()
Expand All @@ -293,7 +287,7 @@ def saveDialog( parent, filtering="Shapefiles (*.shp)"):
return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )

# Generate a save file dialog with a dropdown box for choosing encoding style
def openDialog( parent, filtering="Shapefiles (*.shp)"):
def openDialog( parent, filtering="Shapefiles (*.shp *.SHP)" ):
settings = QSettings()
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
encode = settings.value( "/UI/encoding" ).toString()
Expand All @@ -318,21 +312,17 @@ def dirDialog( parent ):
if not fileDialog.exec_() == QDialog.Accepted:
return None, None
folders = fileDialog.selectedFiles()
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( folders.first() ) ) ) )
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( folders.first() ) ).absolutePath() ) )
return ( unicode( folders.first() ), unicode( fileDialog.encoding() ) )

# Return field type from it's name
def getFieldType(vlayer, fieldName):
fields = vlayer.dataProvider().fields()
for name, field in fields.iteritems():
for field in vlayer.dataProvider().fields():
if field.name() == fieldName:
return field.typeName()

# return the number of unique values in field
def getUniqueValuesCount( layer, fieldIndex):
vprovider = layer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
def getUniqueValuesCount( layer, fieldIndex ):
count = 0
values = []
features = QGisLayers.features(layer)
Expand All @@ -342,21 +332,33 @@ def getUniqueValuesCount( layer, fieldIndex):
count += 1
return count

def getGeomType(gT):
if gT == 3 or gT == 6:
gTypeListPoly = [ QGis.WKBPolygon, QGis.WKBMultiPolygon ]
return gTypeListPoly
elif gT == 2 or gT == 5:
gTypeListLine = [ QGis.WKBLineString, QGis.WKBMultiLineString ]
return gTypeListLine
elif gT == 1 or gT == 4:
gTypeListPoint = [ QGis.WKBPoint, QGis.WKBMultiPoint ]
return gTypeListPoint

def getShapesByGeometryType( baseDir, inShapes, geomType ):
outShapes = QStringList()
for fileName in inShapes:
layerPath = QFileInfo( baseDir + "/" + fileName ).absoluteFilePath()
vLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
if not vLayer.isValid():
continue
layerGeometry = vLayer.geometryType()
if layerGeometry == QGis.Polygon and geomType == 0:
outShapes << fileName
elif layerGeometry == QGis.Line and geomType == 1:
outShapes << fileName
elif layerGeometry == QGis.Point and geomType == 2:
outShapes << fileName

if outShapes.count() == 0:
return None
return outShapes
outShapes = QStringList()
for fileName in inShapes:
layerPath = QFileInfo( baseDir + "/" + fileName ).absoluteFilePath()
vLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
if not vLayer.isValid():
continue
layerGeometry = vLayer.geometryType()
if layerGeometry == QGis.Polygon and geomType == 0:
outShapes << fileName
elif layerGeometry == QGis.Line and geomType == 1:
outShapes << fileName
elif layerGeometry == QGis.Point and geomType == 2:
outShapes << fileName

if outShapes.count() == 0:
return None

return outShapes

0 comments on commit 2a2d944

Please sign in to comment.