# How do I convert longitude & latitude to a 3d (geocentric x,y,z) coordinate?

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? Oct 8, 2013 at 15:42
• 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. 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!) 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. Oct 9, 2013 at 8:15
• 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. Oct 9, 2013 at 16:17

import pyproj