I'm interested in finding the centroid of a point cloud. I understand that to do this I need to convert latitude and longitude values to 3d coordinates, average them and then convert the resulting coordinate back to latitude and longitude (From a previous answer: https://gis.stackexchange.com/a/6026/8891).

How would I go about doing this (preferably in either QGIS or R)? Is there an appropriate formula which I can recreate in R or a function already available in QGIS?

The main aim of this is to identify the following: If each point in the point cloud was a person, and all the people had to travel (as the crow flies) to a single point, which point would minimise the total distance travelled by the whole group?

  • The formulas are available in many threads that can be found by searching our site on ["geocentric"](gis.stackexchange.com/search?q=+geocentric). However, it is unclear that the answer you reference is the "centroid" you seek, because there are many different definitions for points on (or related to) a spheroid. What precisely do you mean by this term?
    – whuber
    Oct 8, 2013 at 15:42
  • 1
    Probably best if I explain the root of the question! If each point in the point cloud was a person, and all the people had to travel (as the crow flies) to a single point, which point would minimise the total distance travelled by the whole group? Imagine you had the address of everyone who had bought cheese in the last 6 months and wanted to make an educated guess on where might be a good place to set up your new cheese shop. That's what I'm trying to get at.
    – Tumbledown
    Oct 8, 2013 at 16:00
  • Excellent! Please state that in your question, because it substantially changes the possible answers. The "centroid" you linked to minimizes the average squared Euclidean distance to the central point. Squaring the distances makes a difference (and hugely simplifies the solution when Euclidean distances are involved). But: do you want to use Euclidean distance to approximate travel, or distance along a network, or--even more realistically--cost or time to travel along a network? (Actually, you really want to maximize the number of customers, which is none of the above!)
    – whuber
    Oct 8, 2013 at 16:27
  • I would love to do drive-times/network routing but time, resources and capacity are against me. I believe pgRouting is the package I could use but I simply don't have time (or the expertise) to sit and get this set up and running. What I'm planning on doing is starting off with simple Euclidean and potentially also making use of R's ggmaps package to query Google's Distance Matrix for distance/time. I'm hoping in combination these will allow some sensible options to be identified. On your final point on maximising customers, they're captive customers but we want to make their lives easier.
    – Tumbledown
    Oct 9, 2013 at 8:15
  • 1
    OK. You still have some decisions to make. One way to make lives easier is to minimize the mean travel time of all customers. But which mean--do you average over all customers or do you average over all trips? The latter average will provide higher weights to those customers making more trips. There is also an issue of equitability: a solution that minimizes the average (however it is computed) may cause a few customers to make extremely long trips (and you might lose them). Instead you might want to minimize the longest trip anyone takes. In any event, finding the solution is hard.
    – whuber
    Oct 9, 2013 at 16:17

1 Answer 1


if you can use python, you can convert coordinates from WGS84 to geocentric coordinate system (EPSG:4978) using this snippet of python code.

import pyproj

wgs84 = pyproj.Proj('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')##4326
geocentric= pyproj.Proj('+proj=geocent +datum=WGS84 +units=m +no_defs') ##4978

x2 , y2  , z2= pyproj.transform( wgs84, geocentric, x, y, z)
  • Thanks @geogeek, I'll have to dust off my python notes. Will this work in Python 3.x or is it a 2.x job?
    – Tumbledown
    Oct 10, 2013 at 9:30
  • 1
    this will work in 3.x and 2.x, you can download pyproj library from this link code.google.com/p/pyproj/downloads/list
    – geogeek
    Oct 10, 2013 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.