Skip to main content
25 votes
Accepted

Is it ok to group by geometry in PostGIS?

There's almost certainly nothing wrong with grouping by geometry in this case, since you're already grouping by a unique ID (nbhd_id). And, as you point out, it saves you a join and makes the query ...
dbaston's user avatar
  • 13.1k
13 votes

Clustering points/polygons based on proximity (within specifed distance) using QGIS?

you can try this (did this in QGIS 2.16) fixed distance buffer each point by 250m (this is half the required distance between points) then dissolve all on the result of that then use multipart to ...
Steven Kay's user avatar
  • 20.5k
12 votes
Accepted

Counting fire occurences per county per year using QGIS

You can use the "Statistics by categories" from the Vector analysis toolbox in QGIS. Select your layer, leave 'Field to calculate statistics on' empty, select "COUNTY_NM" and "...
jbalk's user avatar
  • 7,583
11 votes
Accepted

Adding unique ID for groups of items using Field calculator in QGIS

You can use arrays for this purpose: array_find(array_distinct(array_agg("Name")), "Name") This returns for example this "id" column: So how does it work: First an ...
MrXsquared's user avatar
  • 34.5k
9 votes
Accepted

Calculating Convex Hull based on attributes in QGIS

There is another Convex Hull function in the "Vector geometry" -" "Minimum bounding geometry" tool. That one allows you to specify a field by with the geometries should be grouped.
bugmenot123's user avatar
  • 10.9k
8 votes
Accepted

Summarizing fields with same ID in QGIS Field Calculator

As an alternative you may try this expression: if("WEA" IS NOT NULL, array_sum( array_agg( "Baulast", group_by:="ogc_fid", ...
Taras's user avatar
  • 33.1k
8 votes

Categorising data points along transect using QGIS

I just wanted to add another solution in case the intervals are not intended to be equal, the interval 'names' are not necessarily sequential the intervals are represented by line geometries, and you ...
she_weeds's user avatar
  • 12.6k
7 votes

Copying all visible layers to new group

Use the following script. root = QgsProject.instance().layerTreeRoot() group = root.addGroup("group") for layer in root.children(): if layer.isVisible() and layer != group: _layer ...
Kadir Şahbaz's user avatar
7 votes

Counting fire occurences per county per year using QGIS

There is a possibility using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... Let's assume there is a layer called 'points' with its attribute table, see image ...
Taras's user avatar
  • 33.1k
7 votes

Calculating two columns at once in QGIS

QGIS has an algorithm for that : Statistics by categories (official documentation) You have to select your layer, your category field (here "Cover") and the field to calculate statistics (...
J. Monticolo's user avatar
  • 15.7k
6 votes
Accepted

How to group by attributes and give unique categories in QGIS?

There is an easy way that needs two steps from the db-manager and two tables that can be deleted in the end(there is a way with virtual tables, but i don´t get them to work properly from the db-...
Matte's user avatar
  • 6,245
6 votes
Accepted

Load Named Style within a group on QGIS

Change this line child.loadNamedStyle('*.qml') by: child.layer().loadNamedStyle('/some/real/path/to/a/file.qml') Your child object is of type QgsLayerTreeLayer, but you need to access layer() to ...
Germán Carrillo's user avatar
6 votes

Summing area of by several attributes in QGIS

Use a combination of the two fields like concat: "id" || "function" for the group_by() part of the expression: sum("area", "id" || "function")
Babel's user avatar
  • 72k
6 votes
Accepted

Area of each polygon that intersects another layer and total area of intersections in QGIS

You can do this using QGIS expressions and e.g. create a new field with the Field Calculator. If you have a layer 'polygon' (orange in the screenshot) and another layer 'overlay' (blue outlined), on ...
Babel's user avatar
  • 72k
6 votes

Area of each polygon that intersects another layer and total area of intersections in QGIS

Other solution is to use your SQL statement for the "Virtual Layer" and change it to: SELECT fid, group_concat(b_full) as formated, sum(area) from ( SELECT a.fid, st_area(b.geometry) ...
eurojam's user avatar
  • 11.1k
6 votes
Accepted

Unsplitting/merging lines that have coincident endpoints using PostGIS

The graph-theoretical answer for simplifying edge topologies and decreasing the size of a graph would be contraction - implemented as stable function as of pgRouting 3.0. To perform a linear graph ...
geozelot's user avatar
  • 30.3k
6 votes

Selected item in layertree group or layer?

You can check with the QgsLayerTree.isGroup() / .isLayer() static methods, the .nodeType() method, or by using isinstance(). p = QgsProject.instance() root = p.layerTreeRoot() for c in root.children()...
Matt's user avatar
  • 17.4k
6 votes

Filling attributes in one field based on second field with same values in QGIS

If you want to update SID_1 with the value of SID_2 if SID_1 is NULL choose Update existing field in the Field Calculator execute a simple conditional: IF( "SID_1" IS NULL, "SID_2"...
geozelot's user avatar
  • 30.3k
6 votes
Accepted

Categorising data points along transect using QGIS

Use this expression: it will automatically adapt if you change the interval (2nd last line). See below for explanation: array_foreach( generate_series(65,90), char(@element) )[floor ( ...
Babel's user avatar
  • 72k
6 votes
Accepted

Selecting N points within each geometry using SQL

You want a correlated sub-query for this - a query that essentially does a for-each loop over the running table - and I would suggest a LATERAL join for flexibility. Here we find (up to) 50 pseudo-...
geozelot's user avatar
  • 30.3k
5 votes

Creating population dot density map at the block group level in QGIS

Use Vector -> Research Tools -> Random Points Inside Polygons. Select the variable, which contains the number of people in each Block (=polygon) under expression. leave sampling strategy ...
Kurt's user avatar
  • 7,107
5 votes

Clustering geographical data based on point location and associated point values

The rioja package provides functionality for constrained hierarchical clustering. For what your are thinking of as "spatially constrained" your would specify your cuts based on distance whereas for "...
Jeffrey Evans's user avatar
5 votes
Accepted

Centroid of multiple polygons based on value in attribute table

Try the natively supported SpatiaLite SQL, either via Virtual Layer, or the DB Manager; run SELECT "AreaChar", ST_Centroid(ST_Collect(geometry)) AS geometry FROM <your_layer> GROUP BY ...
geozelot's user avatar
  • 30.3k
5 votes

Adding unique ID for groups of items using Field calculator in QGIS

You can use PyQGIS: lyr = QgsProject.instance().mapLayersByName('lmv ok_my_riks_sample')[0] #Change name to match your data category_field = 'kategori' #Same field_to_calculate = 'cat' #Same. This ...
Bera's user avatar
  • 74.2k
5 votes

Adding unique ID for groups of items using Field calculator in QGIS

This is a typical CASE...WHEN..THEN solution. In the field calculator, you can use the following statement to update the ID field: Case When "Name" = 'loam' then 1 When "Name&...
ahmadhanb's user avatar
  • 41k
5 votes
Accepted

Adding unique ID for consecutive groups using Field calculator in QGIS

create an ID field following the GpsTime field, then create the group1 field and populate it with the following expression (can be improved): with_variable('cucu', aggregate( layer:=@layer_name,...
pigreco's user avatar
  • 4,500
5 votes

Grouping and measuring total length of road based on specific attribute in QGIS

To get the km of cycleway for each type of highway, use this expression: sum("lunghezza", group_by:="highway") To get the percentage of each highway type, use this expression: sum(...
Babel's user avatar
  • 72k
5 votes

Grouping and measuring total length of road based on specific attribute in QGIS

You can create a virtual layer with the query: select row_number() over() as id, highway, sum(lunghezza) as lunghezza_sum, sum(lunghezza)/sum(sum(lunghezza)) over () * 100 as ...
Bera's user avatar
  • 74.2k
5 votes

Filling attributes in one field based on second field with same values in QGIS

You can use a short PyQGIS script. First create your new field e.g. "ID_2" and fill it with the values from "ID_1" (the expression to do that is simply "ID_1"). Next open ...
Ben W's user avatar
  • 21.8k
5 votes

Get a group from layerTreeRoot using its unique id using PyQGIS

I've written a script to access easily to layer(s) and group(s) in QGIS Layer Panel. Here's the group part: def G(group_path: str = None): """ Return the group given in path ...
Kadir Şahbaz's user avatar

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