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 ...
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)
...
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:='...
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....
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 ...
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 ...
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 ...
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)
# ...
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 ...
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:
...
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 ...
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:=...
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 ...
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 ...
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 ...
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 ...
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.
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 ...
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))
+ ')'
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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) ...
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, ...
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", ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
point-in-polygon × 488qgis × 188
polygon × 69
point × 55
r × 50
postgis × 49
arcgis-desktop × 45
overlapping-features × 44
spatial-join × 38
python × 37
count × 35
shapefile × 27
arcmap × 21
qgis-3 × 17
pyqgis × 15
postgresql × 15
fields-attributes × 15
intersection × 14
field-calculator × 13
geopandas × 13
shapely × 13
expression × 11
select-by-location × 11
coordinate-system × 10
sql × 10