python DEM
时间: 2023-10-29 19:54:01 浏览: 112
1python_gis_DEM生成_DEM_
5星 · 资源好评率100%
DEMs (Digital Elevation Models) are commonly used in geospatial analysis and represent the topography of a specific area. In Python, you can work with DEM data using libraries like GDAL, NumPy, and Matplotlib. Here's a basic example of loading and visualizing a DEM using these libraries:
```python
import gdal
import numpy as np
import matplotlib.pyplot as plt
# Load DEM data
dem_path = 'path/to/your/dem.tif'
dem_dataset = gdal.Open(dem_path)
dem_array = dem_dataset.ReadAsArray()
# Get metadata
geotransform = dem_dataset.GetGeoTransform()
x_min = geotransform[0]
y_max = geotransform[3]
pixel_size = geotransform[1]
# Create meshgrid for plotting
x = np.linspace(x_min, x_min + pixel_size * dem_array.shape[1], dem_array.shape[1])
y = np.linspace(y_max - pixel_size * dem_array.shape[0], y_max, dem_array.shape[0])
X, Y = np.meshgrid(x, y)
# Plot DEM using Matplotlib
fig, ax = plt.subplots()
im = ax.imshow(dem_array, cmap='terrain', extent=[x.min(), x.max(), y.min(), y.max()])
ax.set_aspect('equal')
plt.colorbar(im, ax=ax, label='Elevation (m)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('Digital Elevation Model')
plt.show()
```
This code loads a DEM from a GeoTIFF file, extracts the elevation values as a NumPy array, and plots it using Matplotlib. You can customize the visualization by modifying the colormap (`cmap`) or adding contour lines or hillshading effects. Make sure to replace `'path/to/your/dem.tif'` with the actual path to your DEM file.
阅读全文