[GIS] How to convert longitude & latitude to a 3d (geocentric x,y,z) coordinate

centroidscoordinate systemqgisr

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?

Best Answer

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)
Related Question