In [137]:
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline
Shuttle Radar Topography Mission (SRTM) free digital elevation
https://www2.jpl.nasa.gov/srtm/
https://earthexplorer.usgs.gov/
https://gdal.gloobe.org/install.html
sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update
sudo apt-get install gdal-bin
...
!! hors virtualenv...
In [2]:
import gdal
In [3]:
filename = 'elevation_data/n45_e005_1arc_v3.tif'
In [4]:
dataset = gdal.Open(filename)
In [5]:
# see http://www.gdal.org/gdal_tutorial.html
In [6]:
dataset.RasterCount
Out[6]:
In [7]:
dataset.RasterXSize
Out[7]:
In [8]:
print( 'Projection is ',dataset.GetProjection() )
In [9]:
geotransform = dataset.GetGeoTransform()
if not geotransform is None:
print( 'Origin = (',geotransform[0], ',',geotransform[3],')' )
print( 'Pixel Size = (',geotransform[1], ',',geotransform[5],')' )
In [10]:
geotransform
Out[10]:
In [11]:
band = dataset.GetRasterBand(1)
arr = band.ReadAsArray()
In [12]:
print( 'Band Type=',gdal.GetDataTypeName(band.DataType) )
In [13]:
Bmin = band.GetMinimum()
Bmax = band.GetMaximum()
print(Bmin, Bmax)
(Bmin,Bmax) = band.ComputeRasterMinMax(1)
print(Bmin, Bmax)
In [14]:
band.GetOverviewCount()
Out[14]:
In [138]:
elevation = band.ReadAsArray()
print(elevation.shape)
print( elevation )
In [17]:
plt.imshow(elevation, cmap='gist_earth')
plt.show()
In [18]:
plt.figure(figsize=(10, 10))
plt.imshow(elevation[ 2500:3500, 2000:3000], cmap='gist_earth')
plt.show()
In [139]:
Rterre = 6371.009 # km
In [141]:
geotransform = dataset.GetGeoTransform()
mapZeroGPS = ( geotransform[0], geotransform[3] )
pixelSize = ( geotransform[1], geotransform[5] )
mapSize = ( dataset.RasterXSize, dataset.RasterYSize )
In [142]:
pixelSize # deg
Out[142]:
In [143]:
mapZeroGPS # def
Out[143]:
In [144]:
mapSize
Out[144]:
In [145]:
imgsize_deg = 0.0002777777777777778*3601
In [147]:
imgsize_km = imgsize_deg * Rterre*np.pi/180
print( imgsize_km )
In [29]:
pixelsize_km = pixelSize[0]*Rterre*np.pi/180
print( 'résolution: %i m' % (pixelsize_km*1000) )
In [148]:
# Centre
GPScoords = 5.7103223, 45.1973288
In [31]:
mapZeroGPS = np.array( mapZeroGPS )
coordsCentreGPS = np.array( GPScoords )
pixelSize = np.array( pixelSize )
In [32]:
coordsCentrePx = np.round( (coordsCentreGPS - mapZeroGPS)/pixelSize )
print( coordsCentrePx )
In [33]:
plt.figure(figsize=(10, 10))
plt.imshow(elevation, cmap='gist_earth')
plt.plot(coordsCentrePx[0], coordsCentrePx[1], ',r' )
plt.show()
In [130]:
xyres = pixelSize *Rterre*np.pi/180 *1000 # m
print( xyres )
In [51]:
mY, mX = np.mgrid[ 0:mapSize[0], 0:mapSize[1] ]
In [149]:
Distance = np.sqrt( (mX - coordsCentrePx[0] )**2 + (mY-coordsCentrePx[1])**2 )*xyres[0]
In [150]:
plt.imshow(Distance)
plt.plot(coordsCentrePx[0], coordsCentrePx[1], '.r' )
Out[150]:
In [152]:
Azimuth = np.arctan2( mX-coordsCentrePx[0], mY-coordsCentrePx[1] )*180/np.pi # zero au sud
k = 0.5
Azimuth = np.round( Azimuth/k )*k
In [153]:
plt.figure(figsize=(10, 10))
plt.imshow(Azimuth)
plt.plot(coordsCentrePx[0], coordsCentrePx[1], '.r' );
In [154]:
altitudeCentre = elevation[ int(coordsCentrePx[1]), int(coordsCentrePx[0]) ] + 10
print(altitudeCentre)
In [155]:
Horizon = np.arctan2( elevation - altitudeCentre, Distance )*180/np.pi
In [156]:
Horizon.max()
Out[156]:
In [157]:
Horizon.min()
Out[157]:
In [159]:
plt.imshow(Horizon)
plt.plot(coordsCentrePx[0], coordsCentrePx[1], '.r' )
Out[159]:
In [160]:
Horizon
Out[160]:
In [158]:
plt.figure(figsize=(10, 10))
plt.imshow(Horizon[ 2500:3500, 2000:3000], cmap='gist_earth')
plt.show()
In [161]:
azimuth_span = np.unique( Azimuth )
print( len( azimuth_span ) )
In [162]:
horizon_profil = []
for az in azimuth_span:
h = Horizon[ Azimuth == az ].max()
horizon_profil.append( h )
In [163]:
plt.plot( azimuth_span, horizon_profil )
Out[163]:
In [174]:
horizon_data = np.array( [ azimuth_span, horizon_profil ] ).T
In [175]:
np.savetxt('horizon.csv', horizon_data, fmt='%3.4f', delimiter=', ' )
In [176]:
horizon_data
Out[176]:
In [ ]: