Skip to content

Commit

Permalink
allow sampling with different CRS
Browse files Browse the repository at this point in the history
  • Loading branch information
ghtmtt committed Jul 12, 2018
1 parent efa2365 commit b8ce7fa
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions python/plugins/processing/algs/qgis/RasterSampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
QgsProcessingParameterRasterLayer,
QgsProcessingParameterString,
QgsProcessingParameterDefinition,
QgsCoordinateTransform,
QgsFields,
QgsProcessingUtils,
QgsProcessingException,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b8ce7fa

Please sign in to comment.