annotate land_cover.py @ 0:828c02027180 draft default tip

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
author ecology
date Mon, 21 Oct 2024 16:47:10 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
1 # Land Cover TE Algorithm Without GEE
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
2
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
3 # How to Execute:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
4 # - Load the two images (.TIFF) of the rasters
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
5 # to be processed in the "/data/land_cover/input" folder
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
6 # - Setting the two filenames in the specific cell of this script (0)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
7 # - Run all cells of the script
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
8 # - Check the script results in the "/data/land_cover/output" folder
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
9 # - Land Cover Degradation "/data/land_cover/output/lc_dg.tiff"
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
10 # - Land Cover Transition "/data/land_cover/output/lc_tr.tiff"
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
11
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
12 # Librairies import
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
13 import argparse
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
14 import json
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
15 import os
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
16 import sys
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
17
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
18 import matplotlib.pyplot as plt
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
19
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
20 import numpy as np
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
21
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
22 import rasterio
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
23 from rasterio.plot import show
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
24
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
25 from te_schemas.land_cover import LCLegendNesting
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
26 from te_schemas.land_cover import LCTransitionDefinitionDeg
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
27
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
28 # Methods to manage Rasters
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
29
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
30
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
31 def remap(raster, problem_numbers, alternative_numbers):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
32 n_min, n_max = raster.min(), raster.max()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
33 replacer = np.arange(n_min, n_max + 1)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
34 mask = (problem_numbers >= n_min) & (problem_numbers <= n_max)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
35 replacer[problem_numbers[mask] - n_min] = alternative_numbers[mask]
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
36 raster_replaced = replacer[raster - n_min]
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
37 return raster_replaced
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
38
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
39
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
40 def saveRaster(dataset, datasetPath, cols, rows, projection, namedataset=None):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
41 if namedataset:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
42 rasterSet = rasterio.open(datasetPath,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
43 'w',
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
44 driver='GTiff',
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
45 height=rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
46 width=cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
47 count=1,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
48 dtype=np.int8,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
49 crs=projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
50 transform=transform, )
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
51 rasterSet.write(dataset, 1)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
52 rasterSet.close()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
53 else:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
54 rasterSet = rasterio.open(datasetPath,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
55 'w',
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
56 driver='GTiff',
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
57 height=rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
58 width=cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
59 count=1,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
60 dtype=np.int8,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
61 crs=projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
62 transform=transform, )
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
63 rasterSet.write(dataset, 1)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
64 rasterSet.set_band_description(1, namedataset)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
65 rasterSet.close()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
66
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
67
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
68 def plot(ndviImage, cmap):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
69 src = rasterio.open(ndviImage, 'r')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
70 fig, ax = plt.subplots(1, figsize=(12, 12))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
71 show(src, cmap=cmap, ax=ax)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
72 ax.set_xlabel('Est')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
73 ax.set_ylabel('Nord')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
74 plt.show()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
75
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
76
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
77 def plotContour(ndviImage, cmap):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
78 src = rasterio.open(ndviImage, 'r')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
79 fig, ax = plt.subplots(1, figsize=(12, 12))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
80 show(src, cmap=cmap, contour=True, ax=ax)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
81 ax.set_xlabel('Est')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
82 ax.set_ylabel('Nord')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
83 plt.show()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
84
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
85
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
86 # Setting inputs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
87 command_line_args = sys.argv[1:]
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
88
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
89
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
90 parser = argparse.ArgumentParser(description="landcover inputs and outputs")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
91 # Add arguments
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
92 parser.add_argument("--raster_1", help="raster 1")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
93 parser.add_argument("--raster_2", help="raster 2")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
94 parser.add_argument("--json", help="json")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
95
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
96 args = parser.parse_args(command_line_args)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
97
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
98 # Parse the command line arguments
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
99
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
100 # Import data
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
101
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
102 path_raster_yi = args.raster_1
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
103 path_raster_yf = args.raster_2
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
104 fjson = args.json
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
105 # Input Rasters
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
106
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
107 # Outputs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
108 out_dir = os.path.join(os.getcwd(), "data/land_cover/output/")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
109 if not os.path.exists(out_dir):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
110 os.makedirs(out_dir)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
111
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
112 # Output Rasters
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
113 path_lc_tr = os.path.join(out_dir, 'lc_tr.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
114 path_lc_bl = os.path.join(out_dir, 'lc_bl.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
115 path_lc_dg = os.path.join(out_dir, 'lc_dg.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
116 path_lc_tg = os.path.join(out_dir, 'lc_tg.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
117 path_change_yf_yi = os.path.join(out_dir, 'change_yf_yi.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
118 path_lc_baseline_esa = os.path.join(out_dir, 'lc_baseline_esa.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
119 path_lc_target_esa = os.path.join(out_dir, 'lc_target_esa.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
120 path_out_img = os.path.join(out_dir, 'stack.tiff')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
121
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
122 # Contours
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
123 contours_change_yf_yi = os.path.join(out_dir, '/change_yf_yi0.shp')
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
124
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
125
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
126 # Parsing Inputs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
127 # Load static inputs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
128 # Transition Matrix, ESA Legend, IPCC Legend
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
129 # Input Raster and Vector Paths
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
130 params = json.load(open(fjson))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
131
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
132 crs = params.get("crs")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
133
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
134 trans_matrix_val = params.get("trans_matrix")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
135 trans_matrix = LCTransitionDefinitionDeg.Schema().load(trans_matrix_val)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
136
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
137 esa_to_custom_nesting = LCLegendNesting.Schema().load(
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
138 params.get("legend_nesting_esa_to_custom")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
139 )
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
140
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
141 ipcc_nesting = LCLegendNesting.Schema().load(
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
142 params.get("legend_nesting_custom_to_ipcc")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
143 )
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
144
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
145 class_codes = sorted([c.code for c in esa_to_custom_nesting.parent.key])
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
146 class_positions = [*range(1, len(class_codes) + 1)]
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
147
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
148 # Load dynamic inputs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
149 # Baseline ESA Raster
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
150 raster_yi = rasterio.open(path_raster_yi)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
151 lc_baseline_esa = raster_yi.read(1)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
152 yi_dict_profile = dict(raster_yi.profile)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
153
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
154 for k in yi_dict_profile:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
155 print(k.upper(), yi_dict_profile[k])
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
156
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
157 # Target ESA Raster
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
158 raster_yf = rasterio.open(path_raster_yf)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
159 lc_target_esa = raster_yf.read(1)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
160 yf_dict_profile = dict(raster_yf.profile)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
161
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
162 for k in yf_dict_profile:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
163 print(k.upper(), yf_dict_profile[k])
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
164
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
165 cols = raster_yi.width
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
166 rows = raster_yf.height
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
167 transform = raster_yi.transform
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
168 projection = raster_yi.crs
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
169
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
170 # Setting up output
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
171 saveRaster(lc_baseline_esa.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
172 path_lc_baseline_esa,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
173 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
174 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
175 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
176 "Land_cover_yi")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
177
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
178 saveRaster(lc_target_esa.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
179 path_lc_target_esa,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
180 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
181 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
182 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
183 "Land_cover_yf")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
184
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
185 # Algorithm execution
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
186 # Transition codes are based on the class code indices
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
187 # (i.e. their order when sorted by class code) -
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
188 # not the class codes themselves.
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
189 # So need to reclass the land cover used for the transition calculations
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
190 # from the raw class codes to the positional indices of those class codes.
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
191 # And before doing that, need to reclassified initial and
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
192 # final layers to the IPCC (or custom) classes.
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
193
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
194 # Processing baseline raster
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
195 bl_remap_1 = remap(lc_baseline_esa,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
196 np.asarray(esa_to_custom_nesting.get_list()[0]),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
197 np.asarray(esa_to_custom_nesting.get_list()[1]))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
198 lc_bl = remap(bl_remap_1, np.asarray(class_codes), np.asarray(class_positions))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
199
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
200 saveRaster(lc_bl.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
201 path_lc_bl,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
202 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
203 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
204 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
205 "Land_cover_yi")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
206
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
207 # Processing Target Raster
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
208 tg_remap_1 = remap(lc_target_esa,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
209 np.asarray(esa_to_custom_nesting.get_list()[0]),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
210 np.asarray(esa_to_custom_nesting.get_list()[1]))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
211 lc_tg = remap(tg_remap_1, np.asarray(class_codes), np.asarray(class_positions))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
212
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
213 saveRaster(lc_tg.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
214 path_lc_tg,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
215 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
216 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
217 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
218 "Land_cover_yf")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
219
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
220 # Processing Transition Map
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
221 # Compute transition map (first digit for baseline land cover,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
222 # and second digit for target year land cover)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
223 lc_tr = (lc_bl * esa_to_custom_nesting.parent.get_multiplier()) + lc_tg
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
224
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
225 # Compute land cover transition
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
226 # Remap persistence classes so they are sequential.
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
227 # This makes it easier to assign a clear color ramp in QGIS.
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
228 lc_tr_pre_remap = remap(lc_tr,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
229 np.asarray(trans_matrix.get_persistence_list()[0]),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
230 np.asarray(trans_matrix.get_persistence_list()[1]))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
231
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
232 saveRaster(lc_tr_pre_remap.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
233 path_lc_tr,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
234 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
235 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
236 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
237 "Land_cover_transitions_yi-yf")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
238
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
239 # Compute land cover degradation
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
240 # definition of land cover transitions as degradation (-1),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
241 # improvement (1), or no relevant change (0)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
242 lc_dg = remap(lc_tr,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
243 np.asarray(trans_matrix.get_list()[0]),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
244 np.asarray(trans_matrix.get_list()[1]))
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
245
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
246 saveRaster(lc_dg.astype('int8'),
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
247 path_lc_dg,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
248 cols,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
249 rows,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
250 projection,
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
251 "Land_cover_degradation")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
252
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
253 # Compute Mutlibands stack
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
254 # Land Cover Degradation + Baseline ESA
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
255 # + Target ESA + Land Cover Transition
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
256 dg_raster = rasterio.open(path_lc_dg, "r")
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
257 dg = dg_raster.read(1, masked=True)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
258 baseline_esa = rasterio.open(path_lc_baseline_esa, "r").read(1, masked=True)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
259 target_esa = rasterio.open(path_lc_target_esa, "r").read(1, masked=True)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
260 tr = rasterio.open(path_lc_tr, "r").read(1, masked=True)
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
261
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
262 band_list = [dg, lc_baseline_esa, lc_target_esa, tr]
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
263 out_meta = dg_raster.meta.copy()
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
264 out_meta.update({"count": 4})
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
265
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
266 with rasterio.open(path_out_img, 'w', **out_meta) as dest:
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
267 for band_nr, src in enumerate(band_list, start=1):
828c02027180 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/earth commit 4cbace8c3a71b7a1638cfd44a21f5d4b84d2fd15
ecology
parents:
diff changeset
268 dest.write(src, band_nr)