Skip to main content
55 votes

Finding nearest point in other GeoDataFrame using GeoPandas

If you have large dataframes, I've found that scipy's cKDTree spatial index .query method returns very fast results for nearest neighbor searches. As it uses a spatial index it's orders of magnitude ...
JHuw's user avatar
  • 659
41 votes

Finding all neighbors using GeoPandas

The following script finds and adds neighbors' names joined by comma as a new field value. import geopandas as gpd file = "C:/path/to/shapefile.shp" # open file gdf = gpd.read_file(...
Kadir Şahbaz's user avatar
31 votes
Accepted

Finding nearest point in other GeoDataFrame using GeoPandas

You can directly use the Shapely function Nearest points (the geometries of the GeoSeries are Shapely geometries): from shapely.ops import nearest_points # unary union of the gpd2 geomtries pts3 = ...
gene's user avatar
  • 55.1k
20 votes

Finding all neighbors using GeoPandas

This is an addendum to @Kadir's answer (which works great). For one, instead of using not disjoint you can just use touches directly, which does the same thing but is easier to read. If, as the OP ...
Aaron Bramson's user avatar
19 votes

Finding nearest point in other GeoDataFrame using GeoPandas

As of v0.10.0 geopandas supports sjoin_nearest natively - see here. Example: Get the nearest distances between points of two different GeoDataFrames or within one GeoDataFrame. Note that you should ...
rdmolony's user avatar
  • 313
19 votes
Accepted

How to UPDATE with LATERAL Nearest-Neighbour query?

No need for JOIN LATERAL (or do you really just want to use it?); an UPDATE will pass each processing row to the following query, which is the same concept as using a JOIN LATERAL.[*]Try UPDATE gps ...
geozelot's user avatar
  • 30.3k
16 votes

Joining attributes from nearest point in QGIS

In QGIS 3.8 and above the "Join attributes by nearest" native tool in Processing Toolbox does the job easily!
Antonis Michalakis's user avatar
14 votes
Accepted

Find nearest line feature from point in QGIS

In the Processing Toolbox use the Vector general > Join attributes by nearest tool with the following settings: Input layer = your point layer Input layer 2 = your line layer Layer 2 fields to ...
TeddyTedTed's user avatar
  • 6,120
13 votes
Accepted

Getting values from adjacent polygons or nearest point

You can use QGIS expressions with the new overlay_nearest function, availble since QGIS 3.16 and the array_mean() function, available since QGIS 3.18. If you already have an attribute in the polygon ...
Babel's user avatar
  • 72.4k
13 votes
Accepted

How to create variable buffers with nearest mean distance radius value

You can use this expression: buffer( $geometry, array_mean( -- lengths mean array_foreach( overlay_nearest( -- get nearest points 'CONTENIDORS_GEO', ...
Kadir Şahbaz's user avatar
12 votes

Counting number of points per line in QGIS

You can use Join by location to join polyline with point shapefiles. However, the point should be exactly snapped to the line in order to be joined, otherwise, it is better to create a buffer of ...
ahmadhanb's user avatar
  • 41k
12 votes
Accepted

Finding nearest neighbor with smaller attribute value in QGIS for a huge dataset: make expression more efficient

Update: a very similar processing tool as the below processing tool is now part of the ProcessX Plug-In. You can find it in your Processing Toolbox in ProcessX --> Vector - Conditionals --> Join ...
MrXsquared's user avatar
  • 34.6k
11 votes

Nearest distance between point and line layers in QGIS

In QGIS I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer.... Let's assume we have two layers 'points' and 'river' with their corresponding ...
Taras's user avatar
  • 33.2k
11 votes
Accepted

Selecting features within certain buffer distance of selected feature using PyQGIS?

If you want to select all features that are within 100 m of the selected "buffer" feature you can use next code: layer = iface.activeLayer() feats = [ feat for feat in layer.getFeatures() ] #...
xunilk's user avatar
  • 30k
11 votes

How do I interpret the results from the distance matrix?

You can use a heatmap renderer for your point layers (in the symbology, change from Single Symbol to Heatmap) to see how/if the patterns overlap. That's probably the easiest and fastest way to get a ...
Babel's user avatar
  • 72.4k
10 votes
Accepted

ArcGIS: table statistics range, method?

Here is a python solution, using arcpy to access the data and numpy to calculate the statistical values. Using arcpy.da.SearchCursor() write the values to a list. Use python.numpy.percentile() to ...
bebego's user avatar
  • 1,173
10 votes
Accepted

What QGIS tool or plugin is equivalent to the ESRI "Near" tool?

The NNJoin QGIS Plugin finds the closest line for each point. The resulting point layer will for each point contain the distance to the closest line and all of the attributes of that line. NNJoin ...
csk's user avatar
  • 24.9k
10 votes
Accepted

Computing areas of influence using QGIS

the grass algorithm v.net.alloc can produce the subnets - you can call it from the Processing toolbox (tested in QGIS 2.16) You'll need a point layer (for facilities) and a lines layer with costs (...
Steven Kay's user avatar
  • 20.5k
10 votes

Drawing lines to nearest points in QGIS

This should be possible without any plugin using the default Processing tools, particularly Distance to nearest hub: https://docs.qgis.org/testing/en/docs/user_manual/processing_algs/qgis/...
underdark's user avatar
  • 84.2k
10 votes
Accepted

Calculating the shortest distance between polygons in QGIS

If only length values are needed, please use the following expression in the "Field Calculator" length( shortest_line( $geometry, geometry(get_feature('building', 'osm_id'...
Taras's user avatar
  • 33.2k
10 votes
Accepted

Aggregating result of many to many join of points closest to line in QGIS

Giving you only one point id is sensible. Otherwise, join operation would generate hundreds/thousands of columns in the line layer for each near point. One solution would be to add all near point ids ...
Kadir Şahbaz's user avatar
10 votes

Finding the shortest distance between two selected polygons using PyQGIS

In your script, you always get the features whose ids are 0 and 1, which are always the same features, not the selected features. Instead, you need to get selected features. Use this script: layer = ...
Kadir Şahbaz's user avatar
9 votes

Finding nearest point in other GeoDataFrame using GeoPandas

Figured it out: def min_dist(point, gpd2): gpd2['Dist'] = gpd2.apply(lambda row: point.distance(row.geometry),axis=1) geoseries = gpd2.iloc[gpd2['Dist'].argmin()] return geoseries Of ...
RedM's user avatar
  • 1,535
9 votes
Accepted

Deleting "lonely" points in QGIS?

As I understand it, v.neighbors outputs a raster that can hold the number of points within a set distance from each cell, but does not identify whether or not the points themselves have any neighbours ...
humperderp's user avatar
8 votes
Accepted

Why do "stripes" appear on raster?

The resampling method 'near' or 'nearest' is generally to be considered only for succinct/classified data, it attempts to assign a cell value based on the closest source pixel: This is most commonly ...
Michael Stimson's user avatar
8 votes
Accepted

Finding distance between buildings and nearest road using QGIS

While in general this would be better handled in PostGIS, there are plug-ins which do the heavy lifting for you: NNJoin MMQGis Hub Distance. NNJoin This is the preferable and correct way, however ...
RafDouglas C. Tommasi's user avatar
8 votes
Accepted

Connecting lines with point in QGIS

This is not taking any attributes into account (for example road name stored in each address point), just finds closest road using closestSegmentWithContext. Then you can merge (or spatial join etc.) ...
Bera's user avatar
  • 74.3k
8 votes

Find nearest line feature from point in QGIS

You can try a Virtual Table of the Data Source Manager like this: select p.myid, l.myid, min(st_distance(p.geometry, l.geometry)) dist from mypoints p, mylines l group by p.myid; Where ...
Jakob's user avatar
  • 7,481
8 votes
Accepted

Get the attribute of the nearest line to a polygon

The following expression should work : array_to_string(overlay_nearest('Streets', StreetNameField)) Adapt the expression to your data structure. From QGIS 3.16 on, i think.
Snaileater's user avatar
  • 5,743
8 votes
Accepted

Creating list of adjacent polygons in QGIS

You can use SQL and join the layer to itself based on a maximum distance and where the ids are different (to prevent joining polygons to themselves). Change mylayer to the name of your layer, and 30 ...
Bera's user avatar
  • 74.3k

Only top scored, non community-wiki answers of a minimum length are eligible