本文整理汇总了Python中processing.core.QGisLayers.QGisLayers类的典型用法代码示例。如果您正苦于以下问题:Python QGisLayers类的具体用法?Python QGisLayers怎么用?Python QGisLayers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QGisLayers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleAlgorithmResults
def handleAlgorithmResults(alg, progress, showResults = True):
wrongLayers = []
htmlResults = False;
progress.setText("Loading resulting layers")
i = 0
for out in alg.outputs:
progress.setPercentage(100 * i / float(len(alg.outputs)))
if out.hidden or not out.open:
continue
if isinstance(out, (OutputRaster, OutputVector, OutputTable)):
try:
if out.value.startswith("memory:"):
layer = out.memoryLayer
QgsMapLayerRegistry.instance().addMapLayers([layer])
else:
if ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
name = os.path.basename(out.value)
else:
name = out.description
QGisLayers.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(),out.name))
except Exception, e:
wrongLayers.append(out)
#QMessageBox.critical(None, "Error", str(e))
elif isinstance(out, OutputHTML):
ProcessingResults.addResult(out.description, out.value)
htmlResults = True
示例2: processAlgorithm
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
tolerance =self.getParameterValue(self.TOLERANCE)
pointsBefore = 0
pointsAfter = 0
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
layer.wkbType(), layer.crs())
current = 0
selection = QGisLayers.features(layer)
total = 100.0 / float(len(selection))
for f in selection:
featGeometry = QgsGeometry(f.geometry())
attrs = f.attributes()
pointsBefore += self.geomVertexCount(featGeometry)
newGeometry = featGeometry.simplify(tolerance)
pointsAfter += self.geomVertexCount(newGeometry)
feature = QgsFeature()
feature.setGeometry(newGeometry)
feature.setAttributes(attrs)
writer.addFeature(feature)
current += 1
progress.setPercentage(int(current * total))
del writer
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, "Simplify: Input geometries have been simplified from"
+ str(pointsBefore) + " to " + str(pointsAfter) + " points.")
示例3: processAlgorithm
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
QGis.WKBPolygon, layer.crs())
outFeat = QgsFeature()
current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for f in features:
outGeomList = []
if f.geometry().isMultipart():
outGeomList = f.geometry().asMultiPolyline()
else:
outGeomList.append(f.geometry().asPolyline())
polyGeom = self.removeBadLines(outGeomList)
if len(polyGeom) <> 0:
outFeat.setGeometry(QgsGeometry.fromPolygon(polyGeom))
attrs = f.attributes()
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current * total))
del writer
示例4: processAlgorithm
def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''
# the first thing to do is retrieve the values of the parameters
# entered by the user
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputValue(self.OUTPUT_LAYER)
# input layers vales are always a string with its location.
# That string can be converted into a QGIS object (a QgsVectorLayer in
# this case) using the Processing.getObjectFromUri() method.
vectorLayer = QGisLayers.getObjectFromUri(inputFilename)
# And now we can process
# First we create the output layer. The output value entered by the user
# is a string containing a filename, so we can use it directly
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" )
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter(output,
systemEncoding,
provider.fields(),
provider.geometryType(),
provider.crs()
)
# Now we take the features from input layer and add them to the output.
# Method features() returns an iterator, considering the selection that
# might exist in layer and the configuration that indicates
# should algorithm use only selected features or all of them
features = QGisLayers.features(vectorLayer)
for f in features:
writer.addFeature(f)
示例5: processAlgorithm
def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
fieldindex = vlayer.fieldNameIndex(fieldname)
fields = vprovider.fields()
fields.append(QgsField("NUM_FIELD", QVariant.Int))
writer = output.getVectorWriter(fields, vprovider.geometryType(), vlayer.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
classes = {}
features = QGisLayers.features(vlayer)
nFeat = len(features)
for feature in features:
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
inGeom = feature.geometry()
outFeat.setGeometry( inGeom )
atMap = feature.attributes()
clazz = atMap[fieldindex]
if clazz not in classes:
classes[clazz] = len(classes.keys())
atMap.append(classes[clazz])
outFeat.setAttributes(atMap)
writer.addFeature( outFeat )
del writer
示例6: processAlgorithm
def processAlgorithm(self, progress):
filename = self.getParameterValue(self.INPUT)
inputLayer = QGisLayers.getObjectFromUri(filename)
method = self.getParameterValue(self.METHOD)
filename = self.getParameterValue(self.INTERSECT)
selectLayer = QGisLayers.getObjectFromUri(filename)
oldSelection = set(inputLayer.selectedFeaturesIds())
index = spatialIndex = utils.createSpatialIndex(inputLayer)
feat = QgsFeature()
geom = QgsGeometry()
selectedSet = []
current = 0
features = QGisLayers.features(selectLayer)
total = 100.0 / float(len(features))
for f in features:
geom = QgsGeometry(f.geometry())
intersects = index.intersects(geom.boundingBox())
for i in intersects:
request = QgsFeatureRequest().setFilterFid(i)
feat = inputLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(feat.geometry())
if geom.intersects(tmpGeom):
selectedSet.append(feat.id())
current += 1
progress.setPercentage(int(current * total))
if method == 1:
selectedSet = list(oldSelection.union(selectedSet))
elif method == 2:
selectedSet = list(oldSelection.difference(selectedSet))
inputLayer.setSelectedFeatures(selectedSet)
self.setOutputValue(self.OUTPUT, filename)
示例7: processAlgorithm
def processAlgorithm(self, progress):
fieldType = self.getParameterValue(self.FIELD_TYPE)
fieldName = self.getParameterValue(self.FIELD_NAME)
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
output = self.getOutputFromName(self.OUTPUT_LAYER)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()
fields = provider.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], "", fieldLength, fieldPrecision))
writer = output.getVectorWriter(fields, provider.geometryType(), layer.crs())
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = QGisLayers.features(layer)
nFeat = len(features)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry( inGeom )
atMap = inFeat.attributes()
atMap.append(None)
outFeat.setAttributes(atMap)
writer.addFeature( outFeat )
del writer
示例8: processAlgorithm
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.LAYERNAME))
idx = layer.fieldNameIndex(self.getParameterValue(self.COLUMN))
output = self.getOutputFromName(self.SAVENAME)
fields = layer.pendingFields()
newFields = []
i = 0
for field in fields:
if i != idx:
newFields.append(field)
i += 1
outfile = output.getVectorWriter(newFields, layer.wkbType(), layer.crs() )
features = QGisLayers.features(layer)
featurecount = len(features)
i = 0
outFeat = QgsFeature()
for feature in features:
progress.setPercentage(float(i) /featurecount * 100)
i += 1
outFeat.setGeometry(feature.geometry())
attributes = feature.attributes()
newAttributes = []
i = 0
for attr in attributes:
if i != idx:
newAttributes.append(attr)
i += 1
feature.setAttributes(newAttributes)
outfile.addFeature(feature)
示例9: convertUnsupportedFormats
def convertUnsupportedFormats(self, progress):
i = 0
progress.setText("Converting outputs")
for out in self.outputs:
if isinstance(out, OutputVector):
if out.compatible is not None:
layer = QGisLayers.getObjectFromUri(out.compatible)
if layer is None: # for the case of memory layer, if the getCompatible method has been called
continue
provider = layer.dataProvider()
writer = out.getVectorWriter( provider.fields(), provider.geometryType(), layer.crs())
features = QGisLayers.features(layer)
for feature in features:
writer.addFeature(feature)
elif isinstance(out, OutputRaster):
if out.compatible is not None:
layer = QGisLayers.getObjectFromUri(out.compatible)
provider = layer.dataProvider()
writer = QgsRasterFileWriter(out.value)
format = self.getFormatShortNameFromFilename(out.value)
writer.setOutputFormat(format);
writer.writeRaster(layer.pipe(), layer.width(), layer.height(), layer.extent(), layer.crs())
elif isinstance(out, OutputTable):
if out.compatible is not None:
layer = QGisLayers.getObjectFromUri(out.compatible)
provider = layer.dataProvider()
writer = out.getTableWriter(provider.fields())
features = QGisLayers.features(layer)
for feature in features:
writer.addRecord(feature)
progress.setPercentage(100 * i / float(len(self.outputs)))
示例10: processAlgorithm
def processAlgorithm(self, progress):
source = self.getParameterValue(self.INPUT)
vlayer = QGisLayers.getObjectFromUri(source)
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
writer = output.getVectorWriter(fields, QGis.WKBPoint, self.crs)
xfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.XFIELD))
yfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.YFIELD))
crsId = self.getParameterValue(self.TARGET_CRS)
targetCrs = QgsCoordinateReferenceSystem(crsId)
self.crs = targetCrs
outFeat = QgsFeature()
nElement = 0
features = QGisLayers.features(vlayer)
nFeat = len(features)
for feature in features:
nElement += 1
progress.setPercentage((nElement*100)/nFeat)
attrs = feature.attributes()
try:
x = float(attrs[xfieldindex])
y = float(attrs[yfieldindex])
except:
continue
pt = QgsPoint(x, y)
outFeat.setGeometry(QgsGeometry.fromPoint(pt))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
del writer
示例11: processAlgorithm
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
geomType = self.multiToSingleGeom(layer.dataProvider().geometryType())
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
geomType, layer.crs())
outFeat = QgsFeature()
inGeom = QgsGeometry()
current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for f in features:
inGeom = f.geometry()
attrs = f.attributes()
geometries = self.extractAsSingle(inGeom)
outFeat.setAttributes(attrs)
for g in geometries:
outFeat.setGeometry(g)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current * total))
del writer
示例12: exportVectorLayer
def exportVectorLayer(self, orgFilename):
# TODO: improve this. We are now exporting if it is not a shapefile,
# but the functionality of v.in.ogr could be used for this.
# We also export if there is a selection
if not os.path.exists(orgFilename) or not orgFilename.endswith("shp"):
layer = QGisLayers.getObjectFromUri(orgFilename, False)
if layer:
filename = LayerExporter.exportVectorLayer(layer)
else:
layer = QGisLayers.getObjectFromUri(orgFilename, False)
if layer:
useSelection = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
if useSelection and layer.selectedFeatureCount() != 0:
filename = LayerExporter.exportVectorLayer(layer)
else:
filename = orgFilename
else:
filename = orgFilename
destFilename = self.getTempFilename()
self.exportedLayers[orgFilename] = destFilename
command = "v.in.ogr"
min_area = self.getParameterValue(self.GRASS_MIN_AREA_PARAMETER)
command += " min_area=" + str(min_area)
snap = self.getParameterValue(self.GRASS_SNAP_TOLERANCE_PARAMETER)
command += " snap=" + str(snap)
command += ' dsn="' + os.path.dirname(filename) + '"'
command += " layer=" + os.path.basename(filename)[:-4]
command += " output=" + destFilename
command += " --overwrite -o"
return command
示例13: calculateResamplingExtent
def calculateResamplingExtent(self):
'''this method calculates the resampling extent, but it might set self.resample
to false if, with the current layers, there is no need to resample'''
auto = ProcessingConfig.getSetting(SagaUtils.SAGA_AUTO_RESAMPLING)
if auto:
first = True;
self.inputExtentsCount = 0
for param in self.parameters:
if param.value:
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
else:
layer = QGisLayers.getObjectFromUri(param.value)
self.addToResamplingExtent(layer, first)
first = False
if isinstance(param, ParameterMultipleInput):
if param.datatype == ParameterMultipleInput.TYPE_RASTER:
layers = param.value.split(";")
for layername in layers:
layer = QGisLayers.getObjectFromUri(layername)
self.addToResamplingExtent(layer, first)
first = False
if self.inputExtentsCount < 2:
self.resample = False
else:
self.xmin = ProcessingConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_XMIN)
self.xmax = ProcessingConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_XMAX)
self.ymin = ProcessingConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMIN)
self.ymax = ProcessingConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMAX)
self.cellsize = ProcessingConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE)
示例14: processAlgorithm
def processAlgorithm(self, progress):
inLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
inField = self.getParameterValue(self.INPUT_FIELD)
targetLayer = QGisLayers.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.getOutputValue(self.DISTANCE_MATRIX)
if nPoints < 1:
nPoints = len(QGisLayers.features(targetLayer))
# prepare CSV file writer
csvFile = open(outputFile, "wb")
self.writer = UnicodeWriter(csvFile)
if matType == 0: # Linear distance matrix
self.linearMatrix(inLayer, inField, targetLayer, targetField, matType, nPoints, progress)
elif matType == 1: # Standard distance matrix
self.regularMatrix(inLayer, inField, targetLayer, targetField, nPoints, progress)
elif matType == 2: # Summary distance matrix
self.linearMatrix(inLayer, inField, targetLayer, targetField, matType, nPoints, progress)
csvFile.close()
del self.writer
示例15: processAlgorithm
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(
layer.pendingFields().toList(), QGis.WKBPoint, layer.crs()
)
outFeat = QgsFeature()
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
current = 0
for inFeat in features:
inGeom = inFeat.geometry()
attrs = inFeat.attributes()
outGeom = QgsGeometry(inGeom.centroid())
if outGeom is None:
raise GeoAlgorithmExecutionException("Error calculating centroid")
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current * total))
del writer