Skip to main content
25 votes

How to calculate polygon centroids in R (for non-contiguous shapes)

here is an approach using sf. As I demonstrate, results from sf::st_centroid and rgeos::gCentroid are the same. library(sf) library(ggplot2) # I transform to utm because st_centroid is not ...
sebdalgarno's user avatar
  • 1,733
21 votes

Using Field Calculator in QGIS to update columns with centroid coordinates

Not sure if it worked 5 years ago, but now the solution looks like this: x(centroid($geometry)) and y(centroid($geometry)) Results are the same as while using the "Join" option mentioned ...
Mykola Kozyr's user avatar
20 votes
Accepted

Centroid coordinates for odd-shaped polygons

I think the standard QGIS polygon centroid tools calculate the centre of mass in order to determine the polygon's centroid. So this could lie outside the polygon itself (nothing to do with projection)....
Joseph's user avatar
  • 76.3k
20 votes

How to get the centroid of a set of points in PostGIS?

PostGIS has two functions for combining multiple geometries into a single geometry that you can use as an input to ST_Centroid. ST_Collect simply combines a set of geometries without modifying them. ...
dbaston's user avatar
  • 13.2k
19 votes

Creating centerline of river in QGIS?

The surest method for solving your question from the point of view of the cartographic approach is to use the Voronoi Polygons. I offer one of the solutions to your question using QGIS tools. So, ...
Cyril Mikhalchenko's user avatar
16 votes
Accepted

Getting lat/long for centroids using QGIS

Inserting new fields with the field calculator and the expression $x and $y on the layer with your centroids should do the job. If you use the polygon layer of your lakes, you might include there lat/...
Babel's user avatar
  • 74.9k
16 votes

Creating centroid inside of polygon

If you want a point that is for sure inside the polygon use representative point: Returns a cheaply computed point that is guaranteed to be within the geometric object. import geopandas as gpd ...
Bera's user avatar
  • 77.8k
14 votes

List of central coordinates (centroid) for all countries?

You can retrieve this information using R like this: library(rgeos) library(rworldmap) # get world map wmap <- getMap(resolution="high") # get centroids centroids <- gCentroid(wmap, byid=TRUE)...
rafa.pereira's user avatar
  • 1,277
13 votes
Accepted

Getting dual graph from QGIS

You can use Python only (versions 2.7.x and 3.x) without QGIS: 1) With Fiona, shapely and itertools import fiona from shapely.geometry import MuliPolygon, LineString, mapping Multi = ...
gene's user avatar
  • 55.4k
13 votes
Accepted

QGIS - how to use geometry generator to place a centroid in each segment of a multiline?

You can use the following expression for Geometry Generator. with_variable( 'segments', segments_to_lines($geometry), collect_geometries( array_foreach( ...
Kadir Şahbaz's user avatar
12 votes

Determining the centroid of polygons in QGIS

I just came across this post nearly 5 years late (!), but here's what I do to calculate polygon centroids at version 2.18.14: Begin editing the polygon layer Create a new text field, call it ...
Stu Smith's user avatar
  • 8,386
12 votes

Centroid coordinates for odd-shaped polygons

Proessing QGIS Algorithms has "Point on Surface" too, under Vector geometry tools group. No need to use external plugins nor DBs
giohappy's user avatar
  • 1,262
12 votes
Accepted

Generation of centroids outside polygon

There is no problem, a centroid doesn't need to be within the polygon. If you need a point that must be inside the polygon then you should use either pointOnSurface or poleOfInacessability which do ...
Ian Turton's user avatar
  • 83.2k
11 votes
Accepted

Creating point on surface in QGIS

In QGIS 3.10 this option is not available using Vector > Geometry Tools > Centroids. However if you search for "Point on surface" in the Processing Toolbox (Ctrl+Shift+T) using QGIS ...
Walshe_d's user avatar
  • 739
9 votes
Accepted

Getting centroids for multiple lines in QGIS

In tool "Mean coordinate(s)" from the QGIS's Processing Toolbox (Ctrl+Alt+T) use Unique "ID" field.
tomalos's user avatar
  • 311
9 votes

Getting centroids for multiple lines in QGIS

In the Field Calculator, create a new String field and set the length to be something high like 99. Then use the expression: geomToWKT(centroid($geometry)) This should give centroid coordinate values ...
Joseph's user avatar
  • 76.3k
9 votes
Accepted

Creating radial lines from center of polygon to edge in QGIS

You can use "Geometry by expression" followed by "Multipart to singleparts". For each angle from 0 to 360-22.5 at 22.5 increments, a line is created from the centroid to a point ...
Bera's user avatar
  • 77.8k
8 votes

How to get the centroid of a set of points in PostGIS?

If you're looking for performance use this query: SELECT avg(ST_X(the_geom)) as lon, avg(ST_Y(the_geom)) as lat FROM table it gives you more or less the same output as: SELECT ST_AsText(...
gemo1011's user avatar
  • 115
8 votes

Real centroid plugin QGIS3

Third party plugins like this are dependant on their author to update for new versions. I'd suggest using the built in "point on surface" algorithm from the processing toolbox instead
ndawson's user avatar
  • 27.9k
8 votes
Accepted

R st_centroid GEOS error Unknown WKB type 12

To handle the conversion mentioned by @user30184 within R, you can use the ogr2ogr() function provided by my gdalUtilities package. To demonstrate its use, here is a little utility function adapted ...
Josh O'Brien's user avatar
8 votes
Accepted

Creating centroid on curved surface in QGIS

I don't know a tool to have directly what you want but I propose here a workflow to have the following result : Names like this refers to QGIS processing algorithms Compute the Minimum Bounding ...
J. Monticolo's user avatar
  • 15.9k
7 votes

Centering polygon on specific coordinates using QGIS

This answer is for QGIS 3.4. See this answer for earlier versions: Moving vectors to specified coordinates in QGIS? I was able to do this by creating a centroid for a polygon: Then moving the ...
Jay Cummins's user avatar
  • 14.7k
7 votes

How do I create centroids inside polygons in Python?

You just need to use the method representative_point that borrows the shapely method (shapely library borrows the method from GEOS library PointOnSurface method e.g https://github.com/Toblerity/...
ThomasG77's user avatar
  • 31.2k
7 votes
Accepted

How to ensure centroid is calculated within polygon?

You do not say whether you are using ArcPy with the ArcGIS Pro or ArcMap application of the ArcGIS Desktop product so I'll assume that it is ArcGIS Pro and reference that documentation. In the ...
PolyGeo's user avatar
  • 65.4k
7 votes

Ordering polygons from north to south in QGIS

You can create a new field with this expression : array_find( -- find the current feature id in the list array_reverse( -- reverse the order of the list, so greastest Y coordinate (= Northest) is ...
J. Monticolo's user avatar
  • 15.9k
6 votes

How to calculate polygon centroids in R (for non-contiguous shapes)

What I did to overcome this problem is to generate a function which negatively buffers the polygon until it is small enough to expect a convex polygon. The function to use iscentroid(polygon) #' find ...
Gert's user avatar
  • 727
6 votes

List of central coordinates (centroid) for all countries?

You can get country centroids using Python and GeoPandas. import geopandas as gpd import pandas as pd # Access built-in Natural Earth data via GeoPandas world = gpd.read_file(gpd.datasets.get_path('...
Aaron's user avatar
  • 51.8k
6 votes

Determining the centroid of polygons in QGIS

Came across this post and was hard to follow, suggested update: Note: This will calculate the latitude and longitude in your projection coordinates (which may be meters). If you want the ...
GreenTurtle's user avatar
6 votes
Accepted

Bind polygon centroid to polygon attribute data

Load libraries and example data # Load libraries library('raster') library('geosphere') library('mapview') # incredible interactive map visualization in R # Get SpatialPolygonsDataFrame object ...
Guz's user avatar
  • 3,176
6 votes
Accepted

Scaling more than 1000 polygons in QGIS

In QGIS you can create a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer.... with a SpatiaLite expression to perform the transformation that you need: SELECT ...
Gabriel De Luca's user avatar

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