Skip to content

Commit

Permalink
Merge pull request #40 from dfguerrerom/master
Browse files Browse the repository at this point in the history
Included new reclassify tab
  • Loading branch information
12rambau authored Jun 3, 2021
2 parents e786bb2 + 162f7e1 commit 31b3d41
Show file tree
Hide file tree
Showing 10 changed files with 1,006 additions and 43 deletions.
85 changes: 56 additions & 29 deletions acc_ui.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "honey-roller",
"metadata": {},
"outputs": [],
Expand All @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "associate-terry",
"metadata": {},
"outputs": [],
Expand All @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "seven-terry",
"metadata": {},
"outputs": [],
Expand All @@ -36,7 +36,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "tested-pension",
"metadata": {},
"outputs": [],
Expand All @@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "early-tiger",
"metadata": {},
"outputs": [],
Expand All @@ -56,10 +56,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "metropolitan-great",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "de9343ce2ea3452f98ae2b52e5e84480",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Tile(align_center=True, children=[Card(children=[Html(children=['ACC: Accounting of image objects and areas'],…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"acc_title"
]
Expand All @@ -83,41 +98,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "accessible-folder",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74e80d8770164d9a96234386e6483da6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ConvertByte(align_center=True, children=[Card(children=[Html(children=['Select input map classes'], tag='h2'),…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"acc_convert"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "beginning-engagement",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "237453fa868b4ab08e7c631396787fbc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"AccTile(align_center=True, children=[Card(children=[Html(children=['Run Process'], tag='h2'), Flex(children=[S…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"acc_process"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "declared-pound",
"metadata": {},
"outputs": [],
"source": [
"acc_io.__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "sexual-playing",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
7 changes: 7 additions & 0 deletions component/message/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"parc": "PARC",
"rss": "RSS",
"spa": "SPA",
"rcl":"Reclassify",
"about": "About",
"licence": "Licence"
},
Expand All @@ -21,6 +22,10 @@
"btn": "Accept the term of licence"
}
},
"reclassify": {
"title": "Reclassify local rasters",
"description": "This module will allow you to reclassify your local raster values into a new custom codes. <h2>Usage</h2></br>Navigate over your sepal storage environment and select the input `.tif` image, in the next dropdown use a manual classification if you don't have a classification `.csv` table, otherwise select the corresponding file. </br> Go to the 'Customize Classification' tab in order to create or modify existing classification files, and use them after in the workflow.<br><br>Once you have updated your files, click over the 'Get reclassify table' and choose the target values for each class."
},
"acc": {
"title": "ACC: Accounting of image objects and areas",
"description": "This module will conduct the Accounting analysis. Accounting will label and calculate the area of all foreground objects (coded with 2 bytes). The results are spatially explicit maps and tabular summary statistics. Details on the methodology can be found in the (mac: right+click or windows: cmd+click to open in new tab) [Accounting product sheet](https://ies-ows.jrc.ec.europa.eu/gtb/GTB/psheets/GTB-Objects-Accounting.pdf).",
Expand Down Expand Up @@ -102,6 +107,8 @@
"btn": "Convert the input image classes",
"title": "Select input map classes",
"file_exist": "the file {} already exists, no new bin maps have been created",
"reclassify_exist": "The file {} already exists.",
"no_exists" : "The provided {} file doesn't exists.",
"running": "The bin map is being processed",
"finished": "the bin map is ready to be used",
"no_file": "Please provide a file to be reclassified",
Expand Down
55 changes: 53 additions & 2 deletions component/scripts/pre_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def set_byte_map(class_list, raster, process, output):
each nested list is the value of the class that need to be included into each byte value.
raster (pathlib.Path): the path to the original image
"""
print('class_list',class_list)
print('raster',raster)
print('process',process)
print('output',output)
# check that the inputs are all separated
if is_overlap(class_list):
raise Exception(cm.bin.overlap)
Expand Down Expand Up @@ -78,7 +82,6 @@ def set_byte_map(class_list, raster, process, output):
data_value = (bool_data * (index + 1)).astype(np.uint8)
data = data + data_value



data = data.astype(out_meta['dtype'])

Expand Down Expand Up @@ -108,4 +111,52 @@ def unique(raster):
features = np.where(count!=0)[0]
features = features.tolist()

return features
return features

def reclassify_from_map(in_raster, map_values, dst_raster=None, overwrite=False):
""" Remap raster values from map_values dictionary. If the
are missing values in the dictionary 0 value will be returned
Args:
in_raster (path to raster): Input raster to reclassify
map_values (dict): Dictionary with origin:target values
"""

# Get reclassify path raster
filename = Path(in_raster).stem
dst_raster = Path('~').expanduser()/f'downloads/{filename}_reclassified.tif' if not dst_raster else dst_raster

if not overwrite:
if dst_raster.is_file():
raise Warning(cm.bin.reclassify_exist.format(dst_raster))
else:
raise Exception(cm.bin.no_exists.format(dst_raster))

if not all(list(map_values.values())):
raise Exception('All new values has to be filled, try it again.')

# Cast to integer map_values

map_values = {k: int(v) for k, v in map_values.items()}

with rio.open(in_raster) as src:

raw_data = src.read()
profile = src.profile
profile.update(compress='lzw', dtype=np.uint8)

data = np.zeros_like(raw_data, dtype=np.uint8)

for origin, target in map_values.items():

bool_data = np.zeros_like(raw_data, dtype=np.bool_)
bool_data = bool_data + (raw_data == origin)

data_value = (bool_data * target).astype(np.uint8)

data += data_value

with rio.open(dst_raster, 'w', **profile) as dst:
dst.write(data)

return data
2 changes: 2 additions & 0 deletions component/tile/reclassify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .reclassify_tile import *
from .datatable import *
Loading

0 comments on commit 31b3d41

Please sign in to comment.