Certainly! To generate masks highlighting specific terrain features from a 2D numpy array of elevation values, you can compute various terrain attributes such as slope, aspect, curvature, etc. Each of these attributes can reveal different features of the terrain. Here's a step-by-step guide on how to do this:
-
Load Necessary Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter, sobel
-
Compute Terrain Attributes
We'll compute various terrain derivatives that can be used to highlight features.
-
Gradient (Slope):
The slope represents the rate of change of elevation. Areas with high slope values are steep terrains.
def compute_slope(elevation):
dx, dy = np.gradient(elevation)
slope = np.sqrt(dx**2 + dy**2)
return slope
-
Aspect:
Aspect represents the direction the slope is facing.
def compute_aspect(elevation):
dx, dy = np.gradient(elevation)
aspect = np.arctan2(dy, -dx) * 180 / np.pi
aspect = (aspect + 360) % 360 # Convert to 0-360 degrees
return aspect
-
Curvature:
Curvature indicates the convexity or concavity of the terrain.
def compute_curvature(elevation):
dx = sobel(elevation, axis=1)
dy = sobel(elevation, axis=0)
dxx = sobel(dx, axis=1)
dyy = sobel(dy, axis=0)
curvature = dxx + dyy
return curvature
-
Generate Masks
Depending on the terrain features you want to highlight, you can create masks based on thresholds or specific criteria.
-
Steep Slopes Mask:
slope = compute_slope(elevation_array)
steep_slopes_mask = (slope > slope_threshold).astype(float)
-
Flat Areas Mask:
flat_areas_mask = (slope < slope_threshold).astype(float)
-
North-Facing Slopes Mask:
aspect = compute_aspect(elevation_array)
north_facing_mask = (((aspect >= 315) | (aspect <= 45))).astype(float)
-
Curvature Masks:
-
Normalize Masks
Ensure all mask values are between 0 and 1.
def normalize_mask(mask):
mask_min = mask.min()
mask_max = mask.max()
normalized_mask = (mask - mask_min) / (mask_max - mask_min)
return normalized_mask
Apply normalization:
normalized_steep_slopes_mask = normalize_mask(steep_slopes_mask)
-
Generate Eight Specific Masks
Here’s an example of eight terrain masks you might generate:
- Steep Slopes Mask
- Gentle Slopes Mask
- North-Facing Slopes Mask
- South-Facing Slopes Mask
- Concave Terrain Mask
- Convex Terrain Mask
- High Elevation Mask
- Low Elevation Mask
Example Code for All Masks:
# Compute necessary attributes
elevation = elevation_array
slope = compute_slope(elevation)
aspect = compute_aspect(elevation)
curvature = compute_curvature(elevation)
# Define thresholds
slope_threshold = np.percentile(slope, 75)
curvature_threshold = np.percentile(np.abs(curvature), 75)
elevation_threshold_high = np.percentile(elevation, 75)
elevation_threshold_low = np.percentile(elevation, 25)
# 1. Steep Slopes Mask
steep_slopes_mask = (slope > slope_threshold).astype(float)
# 2. Gentle Slopes Mask
gentle_slopes_mask = (slope <= slope_threshold).astype(float)
# 3. North-Facing Slopes Mask
north_facing_mask = (((aspect >= 315) | (aspect <= 45))).astype(float)
# 4. South-Facing Slopes Mask
south_facing_mask = ((aspect >= 135) & (aspect <= 225)).astype(float)
# 5. Concave Terrain Mask
concave_mask = (curvature < -curvature_threshold).astype(float)
# 6. Convex Terrain Mask
convex_mask = (curvature > curvature_threshold).astype(float)
# 7. High Elevation Mask
high_elevation_mask = (elevation > elevation_threshold_high).astype(float)
# 8. Low Elevation Mask
low_elevation_mask = (elevation < elevation_threshold_low).astype(float)
# Normalize all masks
masks = [steep_slopes_mask, gentle_slopes_mask, north_facing_mask, south_facing_mask,
concave_mask, convex_mask, high_elevation_mask, low_elevation_mask]
normalized_masks = [normalize_mask(mask) for mask in masks]
-
Visualize the Masks
mask_titles = ['Steep Slopes', 'Gentle Slopes', 'North-Facing', 'South-Facing',
'Concave Terrain', 'Convex Terrain', 'High Elevation', 'Low Elevation']
fig, axs = plt.subplots(2, 4, figsize=(15, 7))
axs = axs.ravel()
for i in range(8):
axs[i].imshow(normalized_masks[i], cmap='gray')
axs[i].set_title(mask_titles[i])
axs[i].axis('off')
plt.tight_layout()
plt.show()
-
Apply Gaussian Smoothing (Optional)
If you want the masks to have smooth transitions (values between 0 and 1), you can apply Gaussian filtering.
def smooth_mask(mask, sigma=1):
return gaussian_filter(mask, sigma=sigma)
smoothed_masks = [smooth_mask(mask) for mask in normalized_masks]
Visualize smoothed masks similarly.
-
Save or Use the Masks
You can save the masks for further processing or directly use them in your application.
for i, mask in enumerate(normalized_masks):
np.save(f'mask_{i}.npy', mask)
By following these steps, you'll generate eight masks that highlight specific terrain features based on elevation, slope, aspect, and curvature. You can adjust the criteria and thresholds to focus on the features most relevant to your analysis.
If you have specific terrain features in mind that are not covered here, please let me know, and I can provide guidance on how to generate masks for those features.