forked from pbranson/dembuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdembuilder_example.py
102 lines (72 loc) · 2.53 KB
/
dembuilder_example.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
"""
Example script demonstrating functionality of dembuilder
Code copied from P Branson's jupyter notebook - basically just so MC doesn't have to keep flipping between
google chrome and spyder
M Cuttler
May 2019
"""
#%% Example from P Branson notebook
#import python modules
import os
import sys
import numpy as np
#import p branson modules
import dembuilder as db
sys.path.append('../')
#%%
#define bounding box of new raster
bbox=np.zeros(4)
bbox[0] = 220000 #left
bbox[1] = 7570000 #bottom
bbox[2] = 270000 #right
bbox[3] = 7620000 #top
newRaster = db.Raster(bbox=bbox, resolution=500, epsgCode=28350)
#read bathy file and crop to bounding box
filepath = os.path.join(os.getcwd(),'notebooks')
filename = 'Pilbara_200m_Composite_Linear.tif'
sampleReader = db.SamplePointReader(os.path.join(filepath, filename),cropTo=bbox)
samples = sampleReader.load()
#dispay samples (x,y,z, arrays)
samples
#%%
#show options for boundary types
[t for t in db.BoundaryPolygonType]
#use Box boundary
samples.generateBoundary(type=db.BoundaryPolygonType.Box)
#plot samples
samples.plot()
#%% create 'random' secondary bathymetry points
#define random samples to incorporate into bathy; could also load another bathy file (.tif, .xyz, .mat, .nc)
x = np.random.rand(100) * 10000 + 240000
y = np.random.rand(100) * 10000 + 7590000
z = np.random.rand(100) * 10 - 15
#create samples from x,y,z arrays defined above
randomSamples = db.SamplePoints(x,y,z)
#show random samples
randomSamples.plot()
#generate a concave hull boundary and thresold distance to 1500
randomSamples.generateBoundary(type=db.BoundaryPolygonType.ConcaveHull,threshold=1500)
#%% interpolate new bathy to raster object
#show resampling methods
[t for t in db.ResampleMethods]
#resample using linear interpolation
randomSamples.resample(newRaster,method=db.ResampleMethods.Linear)
#plot new raster with interpolated bathy (only has randomSamples)
newRaster.plot()
#%% compile multiple bathy sets together
#include bathy from Pilbara_200m_composite
samples.resample(newRaster,method=db.ResampleMethods.BlockAvg)
#display composite bathy dataset
newRaster.plot()
#save raster to .tif
newRaster.saveToFile('tempRaster.tiff')
#export raster as .xyz
newRaster.getSamples().saveXYZ('tempRaster.xyz')
#%% save raster to current directory as .tif
newRaster.saveToFile('tempRaster.tiff')
#export raster to current directory as .xyz
newRaster.getSamples().saveXYZ('tempRaster.xyz')
#%% can also load existing rasters
loadedRaster = db.Raster.loadFromFile('tempRaster.tiff')
loadedRaster.plot()