35 votes
Accepted

Check if a point falls within a multipolygon with Python

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one feature = list of polygons, look at Converting huge multipolygon to polygons) The ...
gene's user avatar
  • 54.4k
24 votes
Accepted

Looking for GeoPandas QGIS equivalent of ArcGIS "Select by location" tool

If poly is a GeoDataFrame with a single geometry, extract this: polygon = poly.geometry[0] Then, you can use the within method to check which points are within the polygon: points.within(polygon) ...
joris's user avatar
  • 3,853
21 votes

Refering to another layer with Field Calculator in QGIS

It can be done in Field Calculator with function aggregate(). In point layer create new field with field calculator expression like this: aggregate( layer:= 'polygon_layer_name', aggregate:='...
Oto Kaláb's user avatar
  • 6,835
21 votes
Accepted

Converting cluster of Points to Polygons using QGIS

Convex hull - as mentioned by Kazuhito - is one option, but - depending on the cluster shape - you will get more appropriate polygons using concave hulls, for example implemented in ConcaveHull plugin....
underdark's user avatar
  • 83.8k
20 votes

Counting points in polygons using QGIS

The tool you're looking for is now called "Count points in polygons", and it can be found in the QGIS's Processing Toolbox (Ctrl+Alt+T) under Vector analysis tools or can be also launched ...
HDunn's user avatar
  • 8,556
20 votes
Accepted

Re-locating points randomly at certain distance in QGIS

This answer has been updated several times to get a solution that gradually adds complexity to the expression. For the final solution, you should go directely to the very bottom (step 5). The history ...
Babel's user avatar
  • 67.2k
15 votes
Accepted

Applying labelling with obstacles in QGIS

Why obstacles do not work The setting Features act as obstacles only works for features of the same layer. Use Geometry generator to define label placement Go to Label / Placement tab and use Geometry ...
Babel's user avatar
  • 67.2k
13 votes

Overlaying spatial polygon with grid and checking in which grid element specific coordinates are located using R

The New York dataset provided in the question is no longer available for download. I use the nc dataset from sf package to demonstrate a solution using sf package: library(sf) library(ggplot2) # ...
sebdalgarno's user avatar
  • 1,703
13 votes
Accepted

How to find points within a polygon in PostGIS

You should use the ST_within function and not the ST_intersect function. Here the documentation. Here the sql code. You want only the points within greatermanchester select a.* from "Nov01" as a join ...
Mazu_R's user avatar
  • 1,092
12 votes
Accepted

Using qgis:selectbylocation in PyQGIS

I used this code: mapcanvas = iface.mapCanvas() layers = mapcanvas.layers() processing.runalg('qgis:selectbylocation', layers[0], layers[1], u'within', 0) with this situation: and it worked: ...
xunilk's user avatar
  • 29.7k
12 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
  • 67.2k
11 votes
Accepted

Counting number of features within another feature using aggregate function in the QGIS Field calculator

This expression will count the number of populated places for each country: aggregate(layer:='ne_110m_populated_places', aggregate:='count', expression:=name, filter:=...
eurojam's user avatar
  • 9,799
11 votes
Accepted

Count points in polygon grouping by attribute using PyQGIS

Use this script: from PyQt5.QtCore import QVariant # layers polygon_lyr = QgsProject.instance().mapLayersByName("Polygons")[0] point_lyr = QgsProject.instance().mapLayersByName("Points")[0] # index ...
Kadir Şahbaz's user avatar
11 votes
Accepted

Adding new field in point layer with attribute from another layer in QGIS

This plugin is deprecated! The plugin is outdated as of QGIS 3.14 due to the introduction of the new functions for geometry overlay expressions You can use "refFunctions" Plugin. It adds ...
Kadir Şahbaz's user avatar
11 votes

Adding new field in point layer with attribute from another layer in QGIS

There is also a possibility without need to install a plugin, simply using this expression. overlay_within() is available since QGIS 3.16, in fact implementing the functions of the refFunctions plugin ...
Babel's user avatar
  • 67.2k
10 votes

Converting cluster of Points to Polygons using QGIS

You may be interested in Convex Hull which is in Processing | QGIS geoalgorithms | Vector geometry tools. There is Field option which can be used with Method Create convex hulls based on field. Or ...
Kazuhito's user avatar
  • 30.5k
10 votes
Accepted

Counting unique features of points inside polygon in QGIS

Count Points In Polygon tool in QGIS 3 counts the unique values, too. But first you have to select 'Class Field' parameter which is used to specify a unique field name.
Kadir Şahbaz's user avatar
10 votes
Accepted

NULL values when using overlay_within function in Field Calculator

overlay_within returns Sektor value of the related polygon feature(s) as a list/array (even if it returns one polygon). You should convert to string. Set Output field type to Text and use this ...
Kadir Şahbaz's user avatar
10 votes

Showing data from attribute table in legend in QGIS

You can use the following expression if there is one polygon. @layer_name + ' (' + to_string(array_get(array_agg( "NUMPOINTS" ), 0)) + ')'
Kadir Şahbaz's user avatar
9 votes

Check if a point falls within a multipolygon with Python

The problem in your first example is in this loop: ... shpfilePoints = [] for shape in polygon: shpfilePoints = shape.points ... It only appends the last feature points. I tried out my approach ...
xunilk's user avatar
  • 29.7k
9 votes

Selecting and exporting points inside specific polygon using QGIS

There is a direct and very easy way to do this. In the Clip tool, press the green cyclic arrows button next to the Overlay layer dropdown menu. That instructs to iterate the operation over the ...
Vajira's user avatar
  • 91
9 votes
Accepted

Counting points in adjacent polygons using QGIS

Using calculate field you could use the following expression for points inside the polygon: array_length(overlay_contains( 'points',$geometry)) You can see in the preview the results for the selected ...
Dror Bogin's user avatar
  • 3,380
9 votes
Accepted

Counting distinct values per polygon in QGIS

Perhaps the simplest way is to use the Statistics by category tool. It creates a new table, summarising your input. Select the fields by which to categorise, and leave Field to calculate statistics on ...
Matt's user avatar
  • 14.7k
8 votes

Creating regular point grid inside polygon in PostGIS

Three algorithms using different methods. Github Repo: https://github.com/imran-5/Postgis-Custom The simple and best approach, using the actual earth distance of coordinates from the x and y ...
Muhammad Imran Siddique's user avatar
8 votes

Intersection between points and polygons in QGIS

Contrary to original poster, the simple intersect function does work for capturing points in polygons, in newer version of QGIS (2.14.3-Essen). Using this method also gives you the attribute data ...
Timbit's user avatar
  • 81
8 votes
Accepted

Counting number of points within a grid

Spatial Join your points to your polygons, use INTERSECT or WITHIN, no need to keep all the attributes just the OID of the polygon is what's needed on the joined points. Using summary statistics you ...
Michael Stimson's user avatar
8 votes

How to subset point data by outside of polygon data in R

You can see which polygons are under points with: st_intersects(nc_point, nc1) which returns a list of the same length as nc_point. If the point isn't in a polygon then it returns integer(0) ...
Spacedman's user avatar
  • 62.5k
8 votes

Counting unique features of points inside polygon in QGIS

You can run a SQL query in QGIS/database/DB Manager, something like the following : SELECT polygon_id, COUNT (DISTINCT car_id) FROM point_layer, polygon_layer where st_intersects(point.geometry, ...
Snaileater's user avatar
  • 5,703
8 votes

Preventing labels from overlapping certain features using QGIS

Using QGIS 3.16.0 Assuming your labels are stored in "name" field, go to Layer Properties > Labels and set the labeling expression as below: if(overlay_disjoint('area'), "name", ...
Kazuhito's user avatar
  • 30.5k
8 votes
Accepted

Selecting and exporting points inside specific polygon using QGIS

You can do this in two steps using QGIS. (1) First, use the tool the "Join Attributes by Location" to create a new field in your points layer that describes which polygon each point falls ...
Kartograaf's user avatar
  • 2,862

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