55

Firstly, I can't find any documentation that says that coordinates or getSpPPolygonsLabptSlots returns the centre-of-mass centroid. In fact the latter function now shows up as 'Deprecated' and should issue a warning. What you want for computing the centroid as the centre-of-mass of a feature is the gCentroid function from the rgeos package. Doing help....


44

Every polygon has, at a minimum, four distinct "centers": The barycenter of its vertices. The barycenter of its edges. Its barycenter as a polygon. A GIS-specific "center" useful for labeling (usually calculated with undocumented proprietary methods). (They may accidentally coincide in special cases, but for "generic" polygons they are distinct points.) A ...


33

A centroid is per definition a point layer and not a polygon. Therefor you need to create a new layer, which is easy as pie in QGIS 1.8 and higher versions. Load in your polygon Go to the menu -> Vector -> Geometry tools -> polygon centroid and create a centroid point layer Export the coordinates of the created centroid to the attribute table by clicking on ...


20

you have to use the union function like this SELECT att1, st_centroid(st_union(geom)) as geom FROM schema.table GROUP BY att1; so you can obtain centroid of point that have same attribute.


19

Try: UPDATE polygon_layer SET longitude=ST_X(ST_Centroid(geom)), Latitude=ST_Y(ST_Centroid(geom));


17

Frank Donnelly provides a CSV file of country centroids that's based on data taken from the GeoNames Server, but hand curated by Frank. The data was last updated in February 2012. May 2018 The former source isn't available anymore, here is a newer one, with lots of infos on the countries (incl. Centroids), and possibility to download the data in several ...


16

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). If you want the calculate the centroid in terms of its dimensions, you could use the realcentroid plugin instead which you can download from: Plugins > ...


14

This perhaps is most easily done with raster calculations, because the centroid of the land surface is found (by definition) by averaging the geocentric coordinates (X, Y, Z), weighting them by land area. Averaging is done using zonal statistics. To obtain these coordinates and the area scale factor, compute the following grids in geographic coordinates ...


13

If QGIS is computing the centroid with GEOS which is a JTS port then the algorithm is this http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/algorithm/CentroidArea.html. About the theory there is a link in the javadoc into http://www.faqs.org/faqs/graphics/algorithms-faq/, see section 2.02: How can the centroid of a polygon be computed?. The ...


12

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 = MultiPolygon([shape(poly['geometry']) for poly in fiona.open("polygons.shp")]) # creation of the dual graph shapefile import itertools # schema of the dual graph ...


10

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 above:


10

Really, making a new shapefile is one of your easiest options. However, you don't need to copy and paste coordinates. Do this: Make your centroids file using the Polygon Centroids tool. Open the centroids attribute table and make it editable (pencil icon at the bottom). Open the field calculator (calculator icon at the bottom) and choose the Create New ...


9

Create 2 new columns (x and y) type real. In field calculator use option update existing field and execute following expressions: $x (for x column) and $y (for y column). Actually you can create new columns in field calculator directly using create new column option. Now you can export your layer as CSV (save layer as) and work with it in Excell or whatever ...


9

It won't work consistently even when you perform all triangulations relative to a single, fixed point. The problem is that spherical and Euclidean calculations are being mixed without any consideration of what they might mean. One way to make this obvious is to consider a rather extreme triangle, such as almost one-half of a hemisphere. For instance, ...


9

You can do this analysis in the "spdep" package. In the relevant neighbor functions, if you use "longlat=TRUE", the function calculates great circle distance and returns kilometers as the distance unit. In the below example you could coerce the resulting distance list object ("dist.list") to a matrix or data.frame however, it is quite efficient calculating ...


9

From the doc: ST_PointOnSurface — Returns a POINT guaranteed to lie on the surface.


9

Proessing QGIS Algorithms has "Point on Surface" too, under Vector geometry tools group. No need to use external plugins nor DBs


8

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 recommended for use on long/lat nc <- st_read(system.file('shape/nc.shp', package = "sf")) %>% st_transform(32617) # using rgeos sp_cent <- gCentroid(as(nc,...


7

Try this: http://download.geonames.org/export/zip/ It is licensed under a Creative Commons Attribution 3.0 License. They even have zip codes of various other countries around the world.


7

A more robust soulution to mike's answer: long = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*')) lat = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)'))


7

You could use the Shapely python library, which provides a function representative_point() that is guaranteed to lie within the polygon. Here's a Python script that can be run in the QGIS Python console. The polygon layer for which you want to create the attribute should be selected. The function takes the name of the attribute you want to update. The ...


7

The centroid function returns the centroid of the vertices within a geometry (see the documentation). It was originally only intended for use with polygons but can now be used with other geometries. So, the start-point of a line gives no indication of where the centroid of its vertices should be and is therefore irrelevant in your example above (I'm not ...


7

The centroid function returns an Point geometry object that represents the centroid of the input geometry. You'd have to give the centroid function a geometry to chew on (most likely your feature's geometry, from $geometry), then parse that geometry out however you'd like. One example would be getting the centroid as WKT: geomToWKT( centroid( $geometry )...


7

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


6

How about using this in the 2.2 Field Calculator? Long field = substr(geomToWKT( centroid( $geometry )), 7, 12) Lat field = substr(geomToWKT( centroid( $geometry )), strpos(geomToWKT( centroid( $geometry )), ' ')+2, 12) Seems this is addressed in 2.6 with the xmin option xmin(centroid( $geometry ))


6

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) # get a data.frame with centroids df <- as.data.frame(centroids) head(df) #> x y #> Aruba -69.97345 12.51678 #&...


6

I think it is better to store your points as geometries because you can use a spatial index for speeding up queries. The query: WITH points AS (SELECT ST_SetSRID(ST_MakePoint(x,y), 4326) AS geom FROM your_xy_table ), centroid AS (SELECT ST_Centroid(ST_Union(points.geom)) AS geom FROM points), multiobject AS (SELECT ...


6

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. The alternative,ST_Union, will "dissolve" multiple geometries and remove redundant components. This is probably not what you want for this application. To see ...


6

In my case I have each geometry in disctint tables. What I did was : For lines -> ST_LineInterpolatePoint() with 0.5 factor. For polygons -> Test if ST_Centroid() is inside its geometry. If so, ST_Centroid() is the best choice, if not I choose PointOnSurface(). Here's the query : SELECT CASE WHEN (SELECT the_geom FROM points WHERE gid = d.gid) IS NOT ...


6

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, the initial data is a river as an areal object, see the figure below. 2) Vector > Geometry processing > Convert polygons into lines, see image below 3) Open the ...


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