Skip to content

Commit

Permalink
single raster instead of multi
Browse files Browse the repository at this point in the history
  • Loading branch information
ghtmtt committed Jul 12, 2018
1 parent eca6691 commit a4c0c2e
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions python/plugins/processing/algs/qgis/RasterSampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
QgsFeatureSink,
QgsRaster,
QgsProcessing,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterRasterLayer,
QgsFields,
QgsProcessingUtils,
QgsProcessingException,
Expand Down Expand Up @@ -77,10 +77,9 @@ def initAlgorithm(self, config=None):
)

self.addParameter(
QgsProcessingParameterMultipleLayers(
QgsProcessingParameterRasterLayer(
self.RASTERCOPY,
self.tr('Raster Layer to sample'),
QgsProcessing.TypeRaster
)
)

Expand All @@ -99,7 +98,7 @@ def processAlgorithm(self, parameters, context, feedback):
context
)

sampled_rasters = self.parameterAsLayerList(
sampled_rasters = self.parameterAsRasterLayer(
parameters,
self.RASTERCOPY,
context
Expand All @@ -112,11 +111,11 @@ def processAlgorithm(self, parameters, context, feedback):
raster_fields = QgsFields()

# append field to vector as rasterName_bandCount
for i in sampled_rasters:
for b in range(i.bandCount()):
raster_fields.append(QgsField(
os.path.splitext(os.path.basename(i.source()))[0] + str('_{}'.format(b + 1)), QVariant.Double)
for b in range(sampled_rasters.bandCount()):
raster_fields.append(QgsField(
'rvalue_' + str('{}'.format(b + 1)), QVariant.Double
)
)

# combine all the vector fields
out_fields = QgsProcessingUtils.combineFields(source_fields, raster_fields)
Expand All @@ -138,24 +137,22 @@ def processAlgorithm(self, parameters, context, feedback):

for n, i in enumerate(source.getFeatures()):

for rr in sampled_rasters:

attrs = i.attributes()
attrs = i.attributes()

if rr.bandCount() > 1:
if sampled_rasters.bandCount() > 1:

for b in range(rr.bandCount()):
attrs.append(
rr.dataProvider().identify(i.geometry().asPoint(),
QgsRaster.IdentifyFormatValue).results()[b + 1]
)
for b in range(sampled_rasters.bandCount()):
attrs.append(
sampled_rasters.dataProvider().identify(i.geometry().asPoint(),
QgsRaster.IdentifyFormatValue).results()[b + 1]
)

attrs.append(
rr.dataProvider().identify(i.geometry().asPoint(),
QgsRaster.IdentifyFormatValue).results()[1]
)
attrs.append(
sampled_rasters.dataProvider().identify(i.geometry().asPoint(),
QgsRaster.IdentifyFormatValue).results()[1]
)

i.setAttributes(attrs)
i.setAttributes(attrs)

sink.addFeature(i, QgsFeatureSink.FastInsert)
feedback.setProgress(int(n * total))
Expand Down

0 comments on commit a4c0c2e

Please sign in to comment.