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 nearest points to him until a threshold of 1000 points is hit.

How can I do this? Is this possible in basic QGIS or do I need a plugin first?

3 Answers 3


Since you tagged python, here is a solution using PyQGIS:

layer = iface.activeLayer() # get features from the currently selected layer
feature = layer.selectedFeatures()[0] # 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).

Example of Distance Matrix


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 (
        array_agg(geom_to_wkt($geometry, 6)),
        array_agg(geom_to_wkt($geometry, 6), filter:=is_selected())[0]
    -- selection circle 
                                overlay_nearest(@layer_name, $geometry,limit:=10),
    -- verify
        intersects(@circle, array_agg($geometry)[@selected_fid])

the selected_fid variable selects the index value of the selected geometry within the array; the variable 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.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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