I have loaded a csv file (includes points with elevation)in QGIS. I want to select exactly 1000 points in a specific area. My idea is to select one point and then to include all nearst points to him until a theshold of 1000 points is hit. How can I do this? Is this possible in basic QGIS or do I need a plugin first?
Since you tagged python, here is a solution using PyQGIS:
layer = iface.activeLayer() # get features from the currently selected layer feature = layer.selectedFeatures() # only get the first feature of those that are currently selected spatial_idx = QgsSpatialIndex(layer.getFeatures()) # build a spatial index for the layer nearestneighbors = spatial_idx.nearestNeighbor(feature.geometry(), neighbors=1000) # build a list of the nearest 1000 neigbors layer.selectByIds(nearestneighbors) # select the nearest neighbors
Below an example of how you can do this in QGIS without using Python:
I would use the tool called Distance Matrix which comes standard with QGIS. It will return you a list of locations and you can determine the N number of nearest target points.
See the screenshots for the parameters. My workflow was as followed:
- Import your CSV file as points
- Select the point you want to use as your starting reference
- Open the Distance Matrix tool
- Input point layer as well as the target point layer are the same layer.
- For the input layer, make sure to select "selected features only
- Specify a unique ID field (probably object ID or zone id)
- Keep default output matrix type
- Set the Nearest (k) target points to 1000
This will produce the result you are asking for. In addition, the list provides the distance from the starting point to each individual point, as well as the corresponding target ID. You can use this ID to join and query the original dataset if desired (for example if you would like to move over original column names).
Using only the QGIS expressions and
Select by Expression, the expression to use is:
/*Select the first 'n' points closest to the selected point*/ -- fid selection with_variable ('selected_fid', array_find( array_agg(geom_to_wkt($geometry,6)), array_agg(geom_to_wkt($geometry,6), filter:=is_selected())), -- selection circle with_variable ('circle', make_circle ( $geometry, aggregate( layer:=@layer_name, aggregate:='array_agg', expression:= array_max(array_foreach ( overlay_nearest (@layer_name, $geometry,limit:=10), distance($geometry,@element))))[@selected_fid]), -- verify intersects (@circle,array_agg($geometry)[@selected_fid])) )
selected_fid variable selects the index value of the selected geometry within the array;
circle creates a circle with the center of the selected point and the radius equal to the furthest point within 10 points.
NB: the value of the
limit: = argument depends on the provider:
- if shapefile
limit: = value + 1
- if geopackage
limit: = value
this is because
$id starts from scratch in shapefiles
The value to be changed is the limit, for example if you want to select the first 1000 points you have to write limit: = 1000