0

I have a geo-referenced tiff file and a point shapefile. I want to ignore all the points in the shapefile which are outside raster's boundary.

  • Duplicate? gis.stackexchange.com/questions/57964/… – RoperMaps Jun 21 '17 at 13:05
  • @GeoWill the answer to that post is bit unclear to me, how did he manage to calculate the bounding box, there is not much explanation. – FJ_Abbasi Jun 21 '17 at 13:36
  • Either scroll down to the second answer which references this method of getting raster extent. Or open your raster in qgis and look at the properties. – RoperMaps Jun 21 '17 at 14:17
  • what is your meaning of "ignore" all the points in the shapefile which are outside raster's boundary? – xunilk Jun 22 '17 at 2:28
  • @xunilk deleting the points. – FJ_Abbasi Jun 22 '17 at 8:37
1

Based in your more recent commentary, I think that is preferable to produce a new point vector layer, with points overlay raster, for preserving original point layer. Next PyQGIS code produces it as memory layer in QGIS. You only have to change my name layers for yours.

registry = QgsMapLayerRegistry.instance()

raster = registry.mapLayersByName('noise_linear_raster')
vector = registry.mapLayersByName('random_points3')

extent_polygon = raster[0].extent().asWktPolygon()

geom_extent = QgsGeometry.fromWkt(extent_polygon)

feat_vector = [ feat for feat in vector[0].getFeatures() ]

new_feat_vector = [ feat for feat in feat_vector if feat.geometry().within(geom_extent) ]

epsg = vector[0].crs().postgisSrid()

uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'no_ignored_points',
                           'memory')

prov = mem_layer.dataProvider()

for i, feat in enumerate(new_feat_vector):
    feat.setAttributes([i])

prov.addFeatures(new_feat_vector)

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

I tried out above code in Python Console of QGIS and I got:

enter image description here

Red points are corresponding to memory layer and all of them are within raster layer.

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.