Skip to content
Permalink
Browse files

allow sampling with different CRS

  • Loading branch information
ghtmtt committed Jul 12, 2018
1 parent efa2365 commit b8ce7faf073d03af781cbcece2dabdb179e90018
Showing with 19 additions and 4 deletions.
  1. +19 −4 python/plugins/processing/algs/qgis/RasterSampling.py
@@ -39,6 +39,7 @@
QgsProcessingParameterRasterLayer,
QgsProcessingParameterString,
QgsProcessingParameterDefinition,
QgsCoordinateTransform,
QgsFields,
QgsProcessingUtils,
QgsProcessingException,
@@ -151,26 +152,40 @@ def processAlgorithm(self, parameters, context, feedback):
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()

# create the coordinates transformation context
ct = QgsCoordinateTransform(source.sourceCrs(), sampled_raster.crs(), context.transformContext())

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

if i.geometry().isMultipart():
raise QgsProcessingException(self.tr('''Impossible to sample data
of a Multipart layer. Please use the Multipart to single part
algorithm to transform the layer.'''))

# get the feature geometry as point
point = i.geometry().asPoint()

# reproject to raster crs
try:
point = ct.transform(point)
except QgsCsException:
feedback.reportError(self.tr('Could not reproject feature {} to raster CRS').format(i.id()))

attrs = i.attributes()

if sampled_raster.bandCount() > 1:

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

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

i.setAttributes(attrs)

0 comments on commit b8ce7fa

Please sign in to comment.
You can’t perform that action at this time.