Urban Flood Risk Mitigation Batch Processing

Hi!
I am currently trying to run a Batch Processing for the Urban Flood Risk Mitigation Model. I can run the model from the GUI but when i save it as a python script and try to run it it gives me this error:

SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xfc in position 38: invalid start byte

Do you maybe know what the problem is and how to fix it?

I would be grateful for help!

Best Regards,
Felix

Hi @Felix,

Thanks for posting. This can when there is a non ascii character and it can’t be encoded / decoded properly. But InVEST should be handling this.

Within your python script you should see a line like this:
logging.basicConfig(level=logging.INFO, handlers=[handler])
Could you edit the logging level to debug, so it looks like this:
logging.basicConfig(level=logging.DEBUG, handlers=[handler])

Could you then capture the output from the model run and upload it as a text file or paste it here?

One way to do that from the command line would be to:
python my_script.py > urban_log.txt

Thanks!

Doug

Hi @Doug,
thanks for the answer. I tried to change the logging to debug, but it still gives me the same error as before. I don’t think the model creates any output. Here is the complete log of the model run:

Traceback (most recent call last):
File “C:\Program Files\Python38\lib\runpy.py”, line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File “C:\Program Files\Python38\lib\runpy.py”, line 86, in run_code
exec(code, run_globals)
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy_main
.py", line 45, in
cli.main()
File “c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/…\debugpy\server\cli.py”, line 444, in main
run()
File “c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/…\debugpy\server\cli.py”, line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str(“main”))
File “C:\Program Files\Python38\lib\runpy.py”, line 262, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File “C:\Program Files\Python38\lib\runpy.py”, line 237, in _get_code_from_file
code = compile(f.read(), fname, ‘exec’)
Der Thread 0x1 hat mit Code 0 (0x0) geendet.
File “C:\Users\felix\OneDrive\Documents\Uni\Masterarbeit\inVEST\Flood Risk Migitation\Python\Flood Risk Batch Processing\Flood_Risk_Batch_Processing.py”, line 24
‘aoi_watersheds_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
^
SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xfc in position 38: invalid start byte

Hi @Felix,

Thanks for posting that, it’s helpful. Could you attach your python script file or paste it here? It actually looks like the issue is happening even before an InVEST model is being run.

Thanks,

Doug

Hi Doug,
sure:

coding=UTF-8

-----------------------------------------------

Generated by InVEST 3.9.2 on Wed Mar 30 10:57:13 2022

Model: Urban Flood Risk Mitigation

import logging
import sys

import natcap.invest.urban_flood_risk_mitigation
import natcap.invest.utils

LOGGER = logging.getLogger(name)
root_logger = logging.getLogger()

handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(
fmt=natcap.invest.utils.LOG_FMT,
datefmt=’%m/%d/%Y %H:%M:%S ')
handler.setFormatter(formatter)
logging.basicConfig(level=logging.DEBUG, handlers=[handler])

args = {
‘aoi_watersheds_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
'Risk Migitation/Input Data/Schablone für ’
‘Schmittsche Karte Floodplains1.shp’,
‘built_infrastructure_vector_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
'Risk Migitation/Input Data/Buildings ’
‘actual State final.shp’,
‘curve_number_table_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
'Risk Migitation/Input Data/Biophysical table ’
‘soil group.csv’,
‘infrastructure_damage_loss_table_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
'Risk Migitation/Input ’
‘Data/Damage Loss Today.csv’,
‘lulc_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
‘Risk Migitation/Input Data/LULC clipped to floodplains.tif’,
‘rainfall_depth’: ‘10’,
‘results_suffix’: ‘1’,
‘soils_hydrological_group_raster_path’: 'C:/Users/felix/OneDrive/Documents/Uni/Masterarbeit/inVEST/Flood ’
‘Risk Migitation/Input Data/Soil ’
‘Groups Floodplains undrained.tif’,
‘workspace_dir’: r’C:\Users\felix\OneDrive\Documents\Uni\Masterarbeit\inVEST\Flood Risk Migitation\Work Spaces\3003’,
}

if name == ‘main’:
natcap.invest.urban_flood_risk_mitigation.execute(args)

I actually didn’t write any code on my own, i just saved it from the inVest GUI.

Hi @Felix ,

This might be because of the formatting here in the topic, but I’m noticing two types of single-quotation marks in your source code:

  • straight quotation marks (')
  • curly quotation marks ()

Although python source code does support many types of unicode characters, I don’t believe that curly quotation marks are a supported means of identifying strings. If you change them all to straight quotation marks (either ' or "), I wonder if that will correct this issue.

If that doesn’t resolve the issue, could you either attach the file itself or else share the textfile directly with us somehow? I think the forums software’s rendering here might be getting in the way of identifying what’s wrong.

Thanks,
James

1 Like

Hi,
sorry for not replying. I could solve the problem in the mean time, i just had to set the default encoding of visual studio to utf8. Thanks for the help anyways!
Best Regards
Felix

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.