-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5functions.py
82 lines (68 loc) · 2.36 KB
/
lab5functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Author: Originally created by Galen Maclaurin, updated by Ricardo Oliveira
Created: Created on 3.15.16, updated on 10.17.19
Purpose: Helper functions to get started with Lab 5
"""
import numpy as np
def slopeAspect(dem, cs):
"""Calculates slope and aspect using the 3rd-order finite difference method
Parameters
----------
dem : numpy array
A numpy array of a DEM
cs : float
The cell size of the original DEM
Returns
-------
numpy arrays
Slope and Aspect arrays
"""
from math import pi
from scipy import ndimage
kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
dzdx = ndimage.convolve(dem, kernel, mode='mirror') / (8 * cs)
dzdy = ndimage.convolve(dem, kernel.T, mode='mirror') / (8 * cs)
slp = np.arctan((dzdx ** 2 + dzdy ** 2) ** 0.5) * 180 / pi
ang = np.arctan2(-dzdy, dzdx) * 180 / pi
aspect = np.where(ang > 90, 450 - ang, 90 - ang)
return slp, aspect
def reclassAspect(npArray):
"""Reclassify aspect array to 8 cardinal directions (N,NE,E,SE,S,SW,W,NW),
encoded 1 to 8, respectively (same as ArcGIS aspect classes).
Parameters
----------
npArray : numpy array
numpy array with aspect values 0 to 360
Returns
-------
numpy array
numpy array with cardinal directions
"""
return np.where((npArray > 22.5) & (npArray <= 67.5), 2,
np.where((npArray > 67.5) & (npArray <= 112.5), 3,
np.where((npArray > 112.5) & (npArray <= 157.5), 4,
np.where((npArray > 157.5) & (npArray <= 202.5), 5,
np.where((npArray > 202.5) & (npArray <= 247.5), 6,
np.where((npArray > 247.5) & (npArray <= 292.5), 7,
np.where((npArray > 292.5) & (npArray <= 337.5), 8, 1)))))))
def reclassByHisto(npArray, bins):
"""Reclassify np array based on a histogram approach using a specified
number of bins. Returns the reclassified numpy array and the classes from
the histogram.
Parameters
----------
npArray : numpy array
Array to be reclassified
bins : int
Number of bins
Returns
-------
numpy array
umpy array with reclassified values
"""
histo = np.histogram(npArray, bins)[1]
rClss = np.zeros_like(npArray)
for i in range(bins):
rClss = np.where((npArray >= histo[i]) & (npArray <= histo[i + 1]),
i + 1, rClss)
return rClss