Home  >  Article  >  Backend Development  >  Python implements reading elevation information in DEM using latitude and longitude point coordinates (detailed example)

Python implements reading elevation information in DEM using latitude and longitude point coordinates (detailed example)

WBOY
WBOYforward
2022-02-24 17:35:055134browse

This article brings you related issues about how to use 10 lines of code to read the elevation information in DEM using latitude and longitude point coordinates. The main idea is to use GDAL to read DEM data, obtain the first band, and read Take it as an array, then calculate the row and column numbers using the longitude and latitude, and use the row and column numbers to read the elevation information. I hope it will be helpful to everyone.

Python implements reading elevation information in DEM using latitude and longitude point coordinates (detailed example)

Recommended learning: mysql video tutorial

Implementation conditions: 1. The library to be used is GDAL;2.DEMData;

1. Idea

(1) Use GDAL to read DEM data, first get the first band and read it as an array, The functions used are:

ds.GetRasterBand(1)和band.ReadAsArray();

(2) Calculate the row and column number according to the longitude and latitude;

(3) The elevation information can be read directly according to the row and column number.

2. Code

The code and comments are as follows:

from osgeo import gdal

gdal.UseExceptions()
#以湖北DEM数据为例
ds = gdal.Open('./data/hubei_wgs84.tif')

band = ds.GetRasterBand(1)
elevation = band.ReadAsArray()
nrows, ncols = elevation.shape

x0, dx, dxdy, y0, dydx, dy = ds.GetGeoTransform()
#指定经纬度点坐标
latitude,longitude=31.15,111.24
#根据经纬度计算行列号,dx=dy为分辨率,不相等的时候(y0-latitude)/dx改为(y0-latitude)/-dy
new_ncols,new_nrows=int((y0-latitude)/dx),int((longitude-x0)/dx)

#根据行列号读取并打印输出指定坐标点高程
print(elevation[new_ncols][new_nrows])

3. The result is

932, that is, the elevation is 932m

4. Arcgis verification

Use the Go To XY tool, the rightmost tool of the toolbar

Enter the latitude and longitude coordinates to locate the point:

Add the point to the map

# Use Identity to view the point attributes:

The results are consistent.

Recommended learning:

mysql video tutorial

The above is the detailed content of Python implements reading elevation information in DEM using latitude and longitude point coordinates (detailed example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete