# World Elevation Data as CSV

Hi I need For each Lat and Long value also the Alt value! However, I am having problems extracting these values from a Geotiff and wonder if this data is publicly available in Excel or CSV file format? Unfortunately I have not found anything myself!

import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import pandas as pd

data = Dataset("C:/Users/Oliver Weisser/Desktop/Bachelor/Programm/Daten/Daten/ETOPO1_Bed_g_gdal.grd",'r')
print(data.variables.keys())

lon_range = data.variables['x_range'][:]
lat_range = data.variables['y_range'][:]
topo_range = data.variables['z_range'][:]
spacing = data.variables['spacing'][:]
dimension = data.variables['dimension'][:]
z = data.variables['z'][:]
lon_num =  dimension
lat_num =  dimension

lon = np.linspace(lon_range,lon_range,dimension)
lat = np.linspace(lat_range,lat_range,dimension)

topo = np.reshape(z, (lat_num, lon_num))

dfl = pd.DataFrame({
'Latitude': lat.reshape(-1),
'Longitude': lon.reshape(-1),
'Altitude': topo.reshape(-1)
})
print(dfl)



This is the code I have written so far, maybe someone will find a bug there

• Comments are not for extended discussion; this conversation has been moved to chat.
– gerrit
Jun 24 at 8:04

So I hope I finally understood what your problem really was and this gives you the height information at your specific lat lon values from your csv data.

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt

num_el = csv_data[:,0]
lat = csv_data[:,1]
lon = csv_data[:,2]
value = csv_data[:,3]

data = Dataset("ETOPO1_Bed_g_gdal.grd",'r')
lon_range = data.variables['x_range'][:]
lat_range = data.variables['y_range'][:]
topo_range = data.variables['z_range'][:]
spacing = data.variables['spacing'][:]
dimension = data.variables['dimension'][:]
z = data.variables['z'][:]
lon_num =  dimension
lat_num =  dimension

etopo_lon = np.linspace(lon_range,lon_range,dimension)
etopo_lat = np.linspace(lat_range,lat_range,dimension)
topo = np.reshape(z, (lat_num, lon_num))

height = np.empty_like(num_el)
for i in range(len(num_el)):  #in this loop we search for the height values for the specific lat and lon values from your csv
desired_lat_idx = np.abs(etopo_lat - lat[i]).argmin()
desired_lon_idx = np.abs(etopo_lon - lon[i]).argmin()
height[i] = topo[desired_lat_idx,desired_lon_idx]

plt.figure()
height[height<0]=0 # if you want to disregard values below 0
plt.imshow(np.reshape(height,(180,360))) # if the resolution of your csv data changes from 1 degree to something different you need to chance the values in the reshape. For simplicity I hardcoded the numbers.


The searching for the height levels is not that efficient but I didnt have the time to do a proper numpy style solution.

• I will have a detailed look at it! but it looks very promising! Thank you very much for your time and for answering me so patiently! Jun 22 at 12:08
• You are welcome, maybe as an advice in the future try to better formulate your issues and questions. Then people can help you more quickly :) Jun 22 at 12:13
• OK I will do that! You may notice that I am new to coding and still need some help :-) Jun 22 at 12:14
• Yeah, no worries. Everyone started at one point. Jun 22 at 12:25
• Hi @smichel I'm afraid I don't quite understand the probgram and I still want to ask how I can extract the data from this figure that I have a table that looks like this: Long/lat/Value(CSV)/Altitude? If something is unclear, I will try to ask the question again in more detail in a new question. Jun 23 at 10:08