<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get the elevation in an ASCII raster DEM format using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-the-elevation-in-an-ascii-raster-dem-format/m-p/1188215#M64870</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I do not understand why the index calculated for the reference or the maximum is 1000... It should be 999.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;When you use Max_X as GPS_X input, dx will be equal to nc*CellsizeX, which would be 1000. index_x = dx/CellsizeX will still be 1000. See lines 16, 22, and 25 in your code, analogous for dy.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Does anybody have an idea about how to fix this problem ?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;The root of the problem are lines 16 and 17. Here, you're telling Python that your raster has 1001 rows/columns: the start row + 1000 rows after that.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This should fix it:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Max_x = Ref_X + (nc - 1) * CellsizeX&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jun 2022 09:56:10 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-06-30T09:56:10Z</dc:date>
    <item>
      <title>Get the elevation in an ASCII raster DEM format using python</title>
      <link>https://community.esri.com/t5/python-questions/get-the-elevation-in-an-ascii-raster-dem-format/m-p/1188208#M64869</link>
      <description>&lt;P&gt;Hi guys !&lt;/P&gt;&lt;P&gt;I have some problems to get the elevation, based on the latitude and longitude of one point, from an ASCII DEM file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The .asc file looks like this :&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ncols 1000&lt;BR /&gt;nrows 1000&lt;BR /&gt;xllcorner 1213999.500000000000&lt;BR /&gt;yllcorner 6153000.500000000000&lt;BR /&gt;cellsize 1.000000000000&lt;BR /&gt;NODATA_value -99999.00&lt;BR /&gt;486.41 486.57 486.47 486.27 486.17 485.98 485.74 485.66&lt;/P&gt;&lt;P&gt;485.57 485.51 485.43 485.34 485.18 485.06 485.07 485.05&lt;/P&gt;&lt;P&gt;484.85 484.84 485.04 485.37 485.73 485.98 486.15 486.51&lt;/P&gt;&lt;P&gt;486.85 487.12 487.53 487.93 488.19 488.56 488.89 489.23&lt;/P&gt;&lt;P&gt;489.65 490.02 490.37 490.72 491.05 491.31 491.69 492.07&lt;/P&gt;&lt;P&gt;492.41 492.82 493.31 493.76 494.10 494.51 494.90 495.27&lt;/P&gt;&lt;P&gt;495.45 495.83 496.35 496.72 497.06 497.17 497.26 497.42&lt;/P&gt;&lt;P&gt;497.68 497.92 498.04 498.15 498.32 498.56 498.77 499.02&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First I import the file in a numpy array, having 1000 rows and 1000 columns.&lt;/P&gt;&lt;P&gt;Based on the latitude and longitude of one point I calculate the index on x and y to find the altitude in the&amp;nbsp; numpy array.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code to do this is as follow :&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#! usr/bin/env python3
#! -*- coding : utf-8 -*-

import numpy as np

Tile = 'T5821_1213999.5_6153000.5.asc'
rep = 'PathToFile' + Tile
 
par = open(rep)
dat = np.loadtxt(par, skiprows=6)

nc, nr = 1000, 1000
CellsizeX, CellsizeY = 1, 1
Ref_x = 1213999.5
Ref_y = 6153000.5
Max_x = 1213999.5 + nc*CellsizeX
Max_y = 6153000.5 + nr*CellsizeY

def Get_Altitude(dat, nr, GPS_x, GPS_y, Ref_x, Ref_y, CellsizeX, CellsizeY, index_x=None, index_y=None):
    if ((index_x==None) and (index_y==None)):
        # Get the distance between tile reference and point
        dx = np.abs(GPS_x - Ref_x)
        dy = np.abs(GPS_y - Ref_y)
        # Get the index of the current point
        index_x = int(np.round((dx/CellsizeX),0))
        index_y = int(np.round((dy/CellsizeY),0))

        # Get the altitude in the DEM 
        alt = dat[np.abs(-nr+index_y)][index_x]

    else:
        # Get the altitude in the DEM 
        alt = dat[np.abs(-nr+index_y)][index_x]

    if ((alt == -99999.00) or (alt == -9999)):
        alt = 0

    return alt

lt = Get_Altitude(dat, nr, Max_x, Max_y, Ref_x, Ref_y, CellsizeX, CellsizeY, index_x=None, index_y=None)
print(lt)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I want to get the altitude of the reference of the tile (bottom left corner), or the maximum (top right corner),&amp;nbsp;I have an error saying that the index is out of the array :&amp;nbsp;&lt;/P&gt;&lt;P&gt;alt = dat[np.abs(-nr+index_y)][index_x]&lt;BR /&gt;IndexError: index 1000 is out of bounds for axis 0 with size 1000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The index of my numpy array goes from to 0 to 999, so 1000 columns (and rows).&lt;/P&gt;&lt;P&gt;I do not understand why the index calculated for the reference or the maximum is 1000... It should be 999.&lt;/P&gt;&lt;P&gt;Does anybody have an idea about how to fix this problem ? Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 09:22:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-the-elevation-in-an-ascii-raster-dem-format/m-p/1188208#M64869</guid>
      <dc:creator>Titiiii</dc:creator>
      <dc:date>2022-06-30T09:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: Get the elevation in an ASCII raster DEM format using python</title>
      <link>https://community.esri.com/t5/python-questions/get-the-elevation-in-an-ascii-raster-dem-format/m-p/1188215#M64870</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I do not understand why the index calculated for the reference or the maximum is 1000... It should be 999.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;When you use Max_X as GPS_X input, dx will be equal to nc*CellsizeX, which would be 1000. index_x = dx/CellsizeX will still be 1000. See lines 16, 22, and 25 in your code, analogous for dy.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Does anybody have an idea about how to fix this problem ?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;The root of the problem are lines 16 and 17. Here, you're telling Python that your raster has 1001 rows/columns: the start row + 1000 rows after that.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This should fix it:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Max_x = Ref_X + (nc - 1) * CellsizeX&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 09:56:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-the-elevation-in-an-ascii-raster-dem-format/m-p/1188215#M64870</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-06-30T09:56:10Z</dc:date>
    </item>
  </channel>
</rss>

