2

Which function and library do I use to find elevation of a set of points from shapefile in Python QGIS?

I have point shapefile and DEM with me.

1
  • By using 'identify' method of QgsRasterDataProvider.
    – xunilk
    Jul 29 '17 at 0:57
2

By using 'identify' method of QgsRasterDataProvider. Next code can do that (you only need to change names of each layer for names of your layers):

registry = QgsMapLayerRegistry.instance()

point_layer = registry.mapLayersByName('random_points')
raster_layer = registry.mapLayersByName('utah_demUTM2')
rprovider = raster_layer[0].dataProvider()

points = [ feat.geometry().asPoint() for feat in point_layer[0].getFeatures() ]

values = [ rprovider.identify(point, QgsRaster.IdentifyFormatValue).results()[1]
           for point in points ]

for i, point in enumerate(points):
    print 'point: {}, value: {:.0f}'.format(point, values[i])

After running the code at Python Console of QGIS, it was obtained these values:

point: (390945,4.45115e+06), value: 1550
point: (459500,4.43215e+06), value: 1657
point: (428310,4.4389e+06), value: 1580
point: (440696,4.42605e+06), value: 1668
point: (455931,4.43519e+06), value: 2387
point: (462461,4.4274e+06), value: 1596
point: (474903,4.44233e+06), value: 2156
point: (447042,4.44411e+06), value: 1387
point: (445900,4.46419e+06), value: 1605
point: (411695,4.46731e+06), value: 1535
point: (439640,4.45529e+06), value: 1376
point: (369118,4.45867e+06), value: 2163
point: (364344,4.41719e+06), value: 1646
point: (389235,4.4248e+06), value: 1973
point: (461011,4.45046e+06), value: 2018
point: (438474,4.45699e+06), value: 1377
point: (366826,4.45125e+06), value: 2099
point: (361502,4.46407e+06), value: 1560
point: (452052,4.45705e+06), value: 2493
point: (448242,4.42587e+06), value: 2967
point: (366311,4.43337e+06), value: 2247
point: (368971,4.46071e+06), value: 1967
point: (393487,4.43411e+06), value: 1720
point: (400026,4.46377e+06), value: 2079
point: (449569,4.42784e+06), value: 2804

for layers in next image:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.