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....
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 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 ...
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.
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 ...
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 > ...
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 ...
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?.
You can use Python only (versions 2.7.x and 3.x) without QGIS:
1) With Fiona, shapely and itertools
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
# schema of the dual graph ...
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 ...
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 ...
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, ...
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 ...
here is an approach using sf. As I demonstrate, results from sf::st_centroid and rgeos::gCentroid are the same.
# 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")) %>%
# using rgeos
sp_cent <- gCentroid(as(nc,...
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*)'))
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 ...
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 ...
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 )...
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 ))
You can retrieve this information using R like this:
# 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)
#> x y
#> Aruba -69.97345 12.51678
I think it is better to store your points as geometries because you can use a spatial index for speeding up queries.
WITH points AS
ST_SetSRID(ST_MakePoint(x,y), 4326) AS geom
ST_Centroid(ST_Union(points.geom)) AS geom
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 ...
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 :
CASE WHEN (SELECT the_geom FROM points WHERE gid = d.gid) IS NOT ...
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 ...