├── icon.png ├── deploy.sh ├── .vscode └── settings.json ├── algos ├── __init__.py ├── interpolate.py ├── sampling.py └── utils.py ├── TODO.md ├── types ├── __init__.py ├── wind.py ├── texture.py ├── landuse.py ├── fds.py ├── domain.py ├── utils.py └── terrain.py ├── __init__.py ├── .gitignore ├── landuse_types ├── CIMA_Propagator.csv └── Landfire.gov_F13.csv ├── qgis2fds.py ├── README.md ├── metadata.txt ├── qgis2fds_provider.py ├── styles ├── hillshade.qml ├── Landfire F13.qml └── CIMA Propagator.qml ├── pb_tool.cfg ├── qgis2fds_algorithm.py └── LICENSE /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firetools/qgis2fds/HEAD/icon.png -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # install with: python3 -m pip install pb_tool 3 | pb_tool deploy 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "spellright.language": [ 3 | "en_US" 4 | ], 5 | "spellright.documentTypes": [ 6 | "markdown", 7 | "latex", 8 | "plaintext" 9 | ] 10 | } -------------------------------------------------------------------------------- /algos/__init__.py: -------------------------------------------------------------------------------- 1 | from .utils import ( 2 | get_pixel_aligned_extent, 3 | get_extent_layer, 4 | get_reprojected_vector_layer, 5 | ) 6 | from .interpolate import clip_and_interpolate_dem 7 | from .sampling import get_utm_fire_layers, get_sampling_point_grid_layer 8 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | # Interpolate dem 2 | 3 | - create sampling grid 4 | - drape z values from dem 5 | - tin interpolate sampling grid 6 | 7 | # Sample dem and landuse 8 | 9 | - create new sampling grid of interpolation 10 | - drape z values from interpolated dem 11 | - sample landuse 12 | - reproject sampling grid to UTM 13 | -------------------------------------------------------------------------------- /types/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | from .domain import Domain 11 | from .fds import FDSCase 12 | from .landuse import LanduseType 13 | from .terrain import GEOMTerrain, OBSTTerrain 14 | from .texture import Texture 15 | from .wind import Wind 16 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi, Ruggero Poletto" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | 11 | def classFactory(iface): 12 | """! 13 | Load qgis2fds class from file qgis2fds. 14 | 15 | @param iface: A QGIS interface instance. 16 | @type iface: QgsInterface 17 | """ 18 | # 19 | from .qgis2fds import qgis2fdsPlugin 20 | 21 | return qgis2fdsPlugin() 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # Rope project settings 7 | .ropeproject 8 | 9 | # QGIS 10 | *.qgs~ 11 | *.qgz 12 | 13 | # Deploy 14 | deploy_qgis2fds 15 | 16 | # FDS calculations 17 | *.s3d 18 | *.s3d.sz 19 | *_cpu.csv 20 | *_git.txt 21 | *_hrr.csv 22 | *_steps.csv 23 | *_devc.csv 24 | *.binfo 25 | *.end 26 | *.out 27 | *.sinfo 28 | *.smv 29 | *.ge 30 | *.ge2 31 | *.ter 32 | *.ini 33 | *.bingeom 34 | *.sf 35 | *.sf.bnd 36 | *.be 37 | *.be.bnd 38 | *.bf 39 | *.bf.bnd 40 | *.gbf 41 | *.fds 42 | *.gsf 43 | *.gcf 44 | -------------------------------------------------------------------------------- /landuse_types/CIMA_Propagator.csv: -------------------------------------------------------------------------------- 1 | landuse,SURF 2 | 0,"&SURF ID='NA' RGB=255,255,255 /" 3 | 1,"&SURF ID='C01' RGB=249,197,92 VEG_LSET_FUEL_INDEX=5 /" 4 | 2,"&SURF ID='C02' RGB=254,193,119 VEG_LSET_FUEL_INDEX=4 /" 5 | 3,"&SURF ID='C03' RGB=133,153,156 / Barren" 6 | 4,"&SURF ID='C04' RGB=114,154,85 VEG_LSET_FUEL_INDEX=10 /" 7 | 5,"&SURF ID='C05' RGB=114,154,85 VEG_LSET_FUEL_INDEX=10 /" 8 | 6,"&SURF ID='C06' RGB=255,254,212 VEG_LSET_FUEL_INDEX=1 /" 9 | 7,"&SURF ID='C07' RGB=255,254,212 VEG_LSET_FUEL_INDEX=1 /" 10 | 1000,"&SURF ID='Ignition' VEG_LSET_IGNITE_TIME=0. COLOR='RED' /" 11 | 1001,"&SURF ID='Burned' RGB=20,20,20 /" 12 | -------------------------------------------------------------------------------- /qgis2fds.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import os, sys, inspect 11 | from qgis.core import QgsApplication 12 | from .qgis2fds_provider import qgis2fdsProvider 13 | 14 | cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0] 15 | 16 | if cmd_folder not in sys.path: 17 | sys.path.insert(0, cmd_folder) 18 | 19 | 20 | class qgis2fdsPlugin(object): 21 | def __init__(self): 22 | self.provider = None 23 | 24 | def initProcessing(self): 25 | self.provider = qgis2fdsProvider() 26 | QgsApplication.processingRegistry().addProvider(self.provider) 27 | 28 | def initGui(self): 29 | self.initProcessing() 30 | 31 | def unload(self): 32 | QgsApplication.processingRegistry().removeProvider(self.provider) 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # *qgis2fds* plugin repository 2 | 3 | The open source plugin to export terrains and landuse from the [QGIS](http://www.qgis.org) 4 | geographic information system to the [NIST Fire Dynamics Simulator (FDS)](https://pages.nist.gov/fds-smv/) 5 | for wildfire simulation and atmospheric dispersion of fire pollutants. 6 | 7 | * **Learn** how to use this tool on the [wiki pages](https://github.com/firetools/qgis2fds/wiki). 8 | * **Discuss** about the usage on its [discussion group](https://groups.google.com/g/qgis2fds). 9 | * File **issues** on the [issue tracker](https://github.com/firetools/qgis2fds/issues). 10 | 11 | --- 12 | 13 | The development of *qgis2fds* has been funded by a grant from 14 | the Italian Ministry of Foreign Affairs and International Cooperation. 15 | 16 | By the research project WUIFI-21 (High fidelity computational fluid dynamics modeling of forest fires 17 | for Wildland-Urban Interface communities resilience and protection) 18 | the participating organizations intend to extend the capabilities of FDS 19 | on the prediction of wildland-urban interface fires propagation. 20 | 21 | ![MAECI](https://github.com/firetools/qgis2fds/wiki/p/web/logo-maeci.jpeg) 22 | -------------------------------------------------------------------------------- /metadata.txt: -------------------------------------------------------------------------------- 1 | # This file contains metadata for the plugin. 2 | # Mandatory items: 3 | 4 | [general] 5 | name=qgis2fds 6 | qgisMinimumVersion=3.28 7 | description=Export terrains to NIST FDS for fire simulation 8 | version=1.0.2 9 | author=Emanuele Gissi, Ruggero Poletto 10 | email=emanuele.gissi@gmail.com 11 | 12 | about=The open source plugin to export terrains and landuse to the NIST Fire Dynamics Simulator (FDS) (https://pages.nist.gov/fds-smv/) for wildfire simulation and atmospheric dispersion of fire pollutants. 13 | 14 | tracker=https://github.com/firetools/qgis2fds/issues 15 | repository=https://github.com/firetools/qgis2fds 16 | 17 | # End of mandatory metadata 18 | 19 | hasProcessingProvider=yes 20 | # changelog= 21 | tags=python, cfd, fire, wilfire, WUI 22 | homepage=https://github.com/firetools/qgis2fds/wiki 23 | category=Analysis 24 | icon=icon.png 25 | experimental=False 26 | deprecated=False 27 | 28 | # Since QGIS 3.8, a comma separated list of plugins to be installed 29 | # (or upgraded) can be specified. 30 | # plugin_dependencies= 31 | 32 | # Category of the plugin: Raster, Vector, Database or Web 33 | # category= 34 | 35 | # If the plugin can run on QGIS Server. 36 | server=False 37 | 38 | -------------------------------------------------------------------------------- /landuse_types/Landfire.gov_F13.csv: -------------------------------------------------------------------------------- 1 | landuse,SURF 2 | 0,"&SURF ID='NA' RGB=255,255,255 /" 3 | 1,"&SURF ID='A01' RGB=255,254,212 VEG_LSET_FUEL_INDEX=1 /" 4 | 2,"&SURF ID='A02' RGB=255,253,102 VEG_LSET_FUEL_INDEX=2 /" 5 | 3,"&SURF ID='A03' RGB=236,212,99 VEG_LSET_FUEL_INDEX=3 /" 6 | 4,"&SURF ID='A04' RGB=254,193,119 VEG_LSET_FUEL_INDEX=4 /" 7 | 5,"&SURF ID='A05' RGB=249,197,92 VEG_LSET_FUEL_INDEX=5 /" 8 | 6,"&SURF ID='A06' RGB=217,196,152 VEG_LSET_FUEL_INDEX=6 /" 9 | 7,"&SURF ID='A07' RGB=170,155,127 VEG_LSET_FUEL_INDEX=7 /" 10 | 8,"&SURF ID='A08' RGB=229,253,214 VEG_LSET_FUEL_INDEX=8 /" 11 | 9,"&SURF ID='A09' RGB=162,191,90 VEG_LSET_FUEL_INDEX=9 /" 12 | 10,"&SURF ID='A10' RGB=114,154,85 VEG_LSET_FUEL_INDEX=10 /" 13 | 11,"&SURF ID='A11' RGB=235,212,253 VEG_LSET_FUEL_INDEX=11 /" 14 | 12,"&SURF ID='A12' RGB=163,177,243 VEG_LSET_FUEL_INDEX=12 /" 15 | 13,"&SURF ID='A13' RGB=0,0,0 VEG_LSET_FUEL_INDEX=13 /" 16 | 91,"&SURF ID='Urban' RGB=186,119,80 /" 17 | 92,"&SURF ID='Snow-Ice' RGB=234,234,234 /" 18 | 93,"&SURF ID='Agriculture' RGB=253,242,242 /" 19 | 98,"&SURF ID='Water' RGB=137,183,221 /" 20 | 99,"&SURF ID='Barren' RGB=133,153,156 /" 21 | 1000,"&SURF ID='Ignition' VEG_LSET_IGNITE_TIME=0. COLOR='RED' /" 22 | 1001,"&SURF ID='Burned' RGB=20,20,20 /" 23 | -------------------------------------------------------------------------------- /qgis2fds_provider.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | from qgis.core import QgsProcessingProvider 11 | from .qgis2fds_algorithm import qgis2fdsAlgorithm 12 | 13 | 14 | class qgis2fdsProvider(QgsProcessingProvider): 15 | def __init__(self): 16 | """ 17 | Default constructor. 18 | """ 19 | QgsProcessingProvider.__init__(self) 20 | 21 | def unload(self): 22 | """ 23 | Unloads the provider. 24 | """ 25 | pass 26 | 27 | def loadAlgorithms(self): 28 | """ 29 | Loads all algorithms belonging to this provider. 30 | """ 31 | self.addAlgorithm(qgis2fdsAlgorithm()) 32 | 33 | def id(self): 34 | """ 35 | Returns the unique provider id. 36 | """ 37 | return "Export to NIST FDS" 38 | 39 | def name(self): 40 | """ 41 | Returns the provider name. 42 | """ 43 | return "Export to NIST FDS" 44 | 45 | def icon(self): 46 | """ 47 | Returns a QIcon which is used for your provider inside 48 | the Processing toolbox. 49 | """ 50 | return QgsProcessingProvider.icon(self) 51 | 52 | def longName(self): 53 | """ 54 | Returns the a longer version of the provider name. 55 | """ 56 | return self.name() 57 | -------------------------------------------------------------------------------- /types/wind.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import csv, os 11 | from qgis.core import QgsProcessingException 12 | 13 | 14 | class Wind: 15 | def __init__(self, feedback, project_path, filepath) -> None: 16 | self.feedback = feedback 17 | self.filepath = filepath and os.path.join(project_path, filepath) or str() 18 | self._ws, self._wd = list(), list() 19 | 20 | # Check 21 | if not filepath: 22 | self.feedback.pushInfo(f"No wind *.csv file.") 23 | return 24 | self.feedback.pushInfo(f"Import wind *.csv file: <{self.filepath}>") 25 | 26 | # Import 27 | try: 28 | with open(self.filepath) as csv_file: 29 | # wind csv file has an header line and three columns: 30 | # time in seconds, wind speed in m/s, and direction in degrees 31 | csv_reader = csv.reader(csv_file, delimiter=",") 32 | next(csv_reader) # skip header line 33 | for r in csv_reader: 34 | self._ws.append( 35 | f"&RAMP ID='ws', T={float(r[0]):.1f}, F={float(r[1]):.1f} /" 36 | ) 37 | self._wd.append( 38 | f"&RAMP ID='wd', T={float(r[0]):.1f}, F={float(r[2]):.1f} /" 39 | ) 40 | except Exception as err: 41 | raise QgsProcessingException( 42 | f"Cannot import wind *.csv file: <{self.filepath}>:\n{err}" 43 | ) 44 | 45 | def get_fds(self) -> str: 46 | result = f""" 47 | Wind 48 | &WIND SPEED=1., RAMP_SPEED_T='ws', RAMP_DIRECTION_T='wd' /\n""" 49 | if self._ws: 50 | result += "\n".join(("\n".join(self._ws), "\n".join(self._wd))) 51 | else: 52 | result += f"""! Example ramps for wind speed and direction 53 | &RAMP ID='ws', T= 0, F= 10. / 54 | &RAMP ID='ws', T= 600, F= 10. / 55 | &RAMP ID='ws', T=1200, F= 20. / 56 | &RAMP ID='wd', T= 0, F=315. / 57 | &RAMP ID='wd', T= 600, F=270. / 58 | &RAMP ID='wd', T=1200, F=360. /""" 59 | return result 60 | -------------------------------------------------------------------------------- /styles/hillshade.qml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 5 | 1 6 | 0 7 | 0 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 22 | 23 | 24 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | None 38 | WholeRaster 39 | Estimated 40 | 0.02 41 | 0.98 42 | 2 43 | 44 | 45 | 46 | 47 | 48 | resamplingFilter 49 | 50 | 0 51 | 52 | -------------------------------------------------------------------------------- /algos/interpolate.py: -------------------------------------------------------------------------------- 1 | import processing 2 | from qgis.core import QgsProcessing 3 | from .utils import ( 4 | get_pixel_center_aligned_grid_layer, 5 | set_grid_layer_z, 6 | get_reprojected_vector_layer, 7 | ) 8 | 9 | 10 | def clip_and_interpolate_dem( 11 | context, 12 | feedback, 13 | dem_layer, 14 | extent, 15 | extent_crs, 16 | pixel_size, 17 | output=QgsProcessing.TEMPORARY_OUTPUT, 18 | ): 19 | text = f"\nInterpolate <{dem_layer}> layer at <{pixel_size}> pixel size..." 20 | feedback.setProgressText(text) 21 | 22 | tmp = get_pixel_center_aligned_grid_layer( 23 | context, 24 | feedback, 25 | raster_layer=dem_layer, 26 | extent=extent, 27 | extent_crs=extent_crs, 28 | larger=2.0, # FIXME what if downsampling as in CERN? 29 | ) 30 | 31 | if feedback.isCanceled(): 32 | return {} 33 | 34 | tmp = set_grid_layer_z( 35 | context, 36 | feedback, 37 | grid_layer=tmp["OUTPUT"], 38 | raster_layer=dem_layer, 39 | ) 40 | 41 | if feedback.isCanceled(): 42 | return {} 43 | 44 | tmp = get_reprojected_vector_layer( 45 | context, 46 | feedback, 47 | vector_layer=tmp["OUTPUT"], 48 | destination_crs=extent_crs, 49 | ) 50 | 51 | if feedback.isCanceled(): 52 | return {} 53 | 54 | return _create_raster_from_grid( 55 | context, 56 | feedback, 57 | grid_layer=tmp["OUTPUT"], 58 | extent=extent, 59 | pixel_size=pixel_size, 60 | output=output, 61 | ) 62 | 63 | 64 | def _create_raster_from_grid( 65 | context, 66 | feedback, 67 | grid_layer, 68 | extent, 69 | pixel_size, 70 | output=QgsProcessing.TEMPORARY_OUTPUT, 71 | ): 72 | text = f"Interpolate elevation..." 73 | feedback.pushInfo(text) 74 | 75 | layer_source = context.getMapLayer(grid_layer).source() 76 | interpolation_source = 1 # elevation 77 | field_index = -1 # elevation 78 | input_type = 0 # points 79 | interpolation_data = ( 80 | f"{layer_source}::~::{interpolation_source}::~::{field_index}::~::{input_type}" 81 | ) 82 | feedback.pushInfo(f"interpolation_data: {interpolation_data}") # FIXME 83 | alg_params = { 84 | "INTERPOLATION_DATA": interpolation_data, 85 | "METHOD": 0, # linear 86 | "EXTENT": extent, 87 | "PIXEL_SIZE": pixel_size, 88 | "OUTPUT": output, 89 | } 90 | return processing.run( 91 | "qgis:tininterpolation", 92 | alg_params, 93 | context=context, 94 | feedback=feedback, 95 | is_child_algorithm=True, 96 | ) 97 | -------------------------------------------------------------------------------- /pb_tool.cfg: -------------------------------------------------------------------------------- 1 | #/*************************************************************************** 2 | # qgis2fds 3 | # 4 | # Configuration file for plugin builder tool (pb_tool) 5 | # Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/ 6 | # ------------------- 7 | # begin : 2020-05-04 8 | # copyright : (C) 2020 by Emanuele Gissi 9 | # email : emanuele.gissi@gmail.com 10 | # ***************************************************************************/ 11 | # 12 | #/*************************************************************************** 13 | # * * 14 | # * This program is free software; you can redistribute it and/or modify * 15 | # * it under the terms of the GNU General Public License as published by * 16 | # * the Free Software Foundation; either version 2 of the License, or * 17 | # * (at your option) any later version. * 18 | # * * 19 | # ***************************************************************************/ 20 | # 21 | # 22 | # You can install pb_tool using: 23 | # pip install http://geoapt.net/files/pb_tool.zip 24 | # 25 | # Consider doing your development (and install of pb_tool) in a virtualenv. 26 | # 27 | # For details on setting up and using pb_tool, see: 28 | # http://g-sherman.github.io/plugin_build_tool/ 29 | # 30 | # Issues and pull requests here: 31 | # https://github.com/g-sherman/plugin_build_tool: 32 | # 33 | # Sane defaults for your plugin generated by the Plugin Builder are 34 | # already set below. 35 | # 36 | # As you add Python source files and UI files to your plugin, add 37 | # them to the appropriate [files] section below. 38 | 39 | [plugin] 40 | # Name of the plugin. This is the name of the directory that will 41 | # be created in .qgis2/python/plugins 42 | name: qgis2fds 43 | 44 | # Full path to where you want your plugin directory copied. If empty, 45 | # the QGIS default path will be used. Don't include the plugin name in 46 | # the path. 47 | plugin_path:deploy_qgis2fds 48 | 49 | [files] 50 | # Python files that should be deployed with the plugin 51 | python_files: __init__.py qgis2fds.py qgis2fds_algorithm.py qgis2fds_provider.py 52 | 53 | # Other files required for the plugin 54 | extras: icon.png LICENSE metadata.txt README.md 55 | 56 | # Other directories to be deployed with the plugin. 57 | # These must be subdirectories under the plugin directory 58 | extra_dirs: algos landuse_types styles types 59 | 60 | # The main dialog file that is loaded (not compiled) 61 | main_dialog: 62 | 63 | # Other ui files for dialogs you create (these will be compiled) 64 | compiled_ui_files: 65 | 66 | # Resource file(s) that will be compiled 67 | resource_files: 68 | 69 | # ISO code(s) for any locales (translations), separated by spaces. 70 | # Corresponding .ts files must exist in the i18n directory 71 | locales: 72 | 73 | [help] 74 | # the built help directory that should be deployed with the plugin 75 | # dir: help/build/html 76 | # the name of the directory to target in the deployed plugin 77 | # target: help 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /styles/Landfire F13.qml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 5 | 1 6 | 0 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | None 19 | WholeRaster 20 | Estimated 21 | 0.02 22 | 0.98 23 | 2 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 | 0 58 | 59 | -------------------------------------------------------------------------------- /styles/CIMA Propagator.qml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 5 | 1 6 | 0 7 | 0 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | None 31 | WholeRaster 32 | Estimated 33 | 0.02 34 | 0.98 35 | 2 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | resamplingFilter 66 | 67 | 6 68 | 69 | -------------------------------------------------------------------------------- /types/texture.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import os, time 11 | from qgis.core import QgsProcessingException, QgsMapSettings, QgsMapRendererParallelJob 12 | from qgis.utils import iface 13 | from qgis.PyQt.QtCore import QSize, QCoreApplication 14 | 15 | 16 | class Texture: 17 | 18 | timeout = 30.0 19 | 20 | def __init__( 21 | self, 22 | feedback, 23 | path, 24 | name, 25 | image_type, 26 | pixel_size, 27 | tex_layer, 28 | utm_extent, 29 | utm_crs, 30 | ) -> None: 31 | self.feedback = feedback 32 | self.image_type = image_type 33 | self.pixel_size = pixel_size 34 | self.tex_layer = tex_layer 35 | self.utm_crs = utm_crs # destination_crs 36 | 37 | self.filename = f"{name}_tex.{self.image_type}" 38 | self.filepath = os.path.join(path, self.filename) 39 | self.tex_extent = utm_extent 40 | 41 | self._save() 42 | 43 | def _save(self): 44 | self.feedback.pushInfo(f"Save terrain texture file: <{self.filepath}>") 45 | # Calc tex_extent size in meters (it is in utm) 46 | tex_extent_xm = self.tex_extent.xMaximum() - self.tex_extent.xMinimum() 47 | tex_extent_ym = self.tex_extent.yMaximum() - self.tex_extent.yMinimum() 48 | # Calc tex_extent size in pixels 49 | tex_extent_xpix = int(tex_extent_xm / self.pixel_size) 50 | tex_extent_ypix = int(tex_extent_ym / self.pixel_size) 51 | # Choose exporting layers 52 | if self.tex_layer: # use user tex layer 53 | layers = (self.tex_layer,) 54 | elif iface: # no user tex layer, use map canvas 55 | canvas = iface.mapCanvas() 56 | layers = canvas.layers() 57 | else: 58 | self.feedback.pushInfo(f"No texture requested.") 59 | return 60 | # Image settings and texture layer choice 61 | settings = QgsMapSettings() # build settings 62 | settings.setDestinationCrs(self.utm_crs) # set output crs 63 | settings.setExtent(self.tex_extent) # in utm_crs 64 | settings.setOutputSize(QSize(tex_extent_xpix, tex_extent_ypix)) 65 | settings.setLayers(layers) 66 | # Render and save image 67 | render = QgsMapRendererParallelJob(settings) 68 | render.start() 69 | t0 = time.time() 70 | dt = 0. 71 | while render.isActive(): 72 | dt = time.time() - t0 73 | QCoreApplication.processEvents() 74 | if self.feedback.isCanceled(): 75 | render.cancelWithoutBlocking() 76 | return 77 | if dt >= self.timeout: 78 | render.cancelWithoutBlocking() 79 | self.feedback.reportError("Texture render timed out, no texture saved.") 80 | return 81 | image = render.renderedImage() 82 | try: 83 | os.makedirs(os.path.dirname(self.filepath), exist_ok=True) 84 | image.save(self.filepath, self.image_type) 85 | except Exception as err: 86 | raise QgsProcessingException( 87 | f"Texture file not writable to <{self.filepath}>.\n{err}" 88 | ) 89 | self.feedback.pushInfo(f"Texture saved in {dt:.2f} s") 90 | 91 | def get_fds(self): 92 | return f"TERRAIN_IMAGE='{self.filename}'" 93 | -------------------------------------------------------------------------------- /types/landuse.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import csv, re, os 11 | from qgis.core import QgsProcessingException 12 | from . import utils 13 | 14 | 15 | class LanduseType: 16 | 17 | _scan_id = re.compile( # search ID value in SURF 18 | r""" 19 | [,\s\t]+ # 1+ separator 20 | ID 21 | [,\s\t]* # 0+ separator 22 | = 23 | [,\s\t]* # 0+ separator 24 | (?:'(.+?)'|"(.+?)") # protected string 25 | [,\s\t]+ # 1+ separator 26 | """, 27 | re.VERBOSE | re.DOTALL | re.IGNORECASE, 28 | ) # no MULTILINE, so that $ is the end of the file 29 | 30 | def __init__(self, feedback, project_path, filepath) -> None: 31 | self.feedback = feedback 32 | self.filepath = filepath and os.path.join(project_path, filepath) or str() 33 | if filepath: 34 | self.feedback.pushInfo(f"Import landuse type *.csv file: <{self.filepath}>") 35 | self.surf_dict = dict() 36 | self.surf_id_dict = dict() 37 | self._import() 38 | else: 39 | self.feedback.pushInfo(f"No landuse type *.csv file.") 40 | self.surf_dict = {} # INERT is predefined, FDS SURF not needed 41 | self.surf_id_dict = {0: "INERT"} 42 | self.feedback.pushInfo( 43 | f"Default bcs for the fire layer: bc_in=<{self.bc_in_default}>, bc_out=<{self.bc_out_default}>." 44 | ) 45 | 46 | def _import(self) -> None: 47 | try: 48 | with open(self.filepath) as csv_file: 49 | # landuse csv file has an header line and two columns: 50 | # landuse integer number and corresponding FDS SURF str 51 | csv_reader = csv.reader(csv_file, delimiter=",") 52 | next(csv_reader) # skip header linelanduse_path 53 | for r in csv_reader: 54 | key, value_surf = int(r[0]), str(r[1]) 55 | found_id = re.search(self._scan_id, value_surf) 56 | if not found_id: 57 | raise QgsProcessingException( 58 | f"No FDS ID found in <{value_surf}> from landuse type *.csv file." 59 | ) 60 | value_id = found_id.groups()[0] 61 | self.surf_dict[key] = value_surf # eg: {98: "&SURF ID='A04' ... /"} 62 | self.surf_id_dict[key] = value_id # eg: {98: 'A04'} 63 | except IOError as err: 64 | raise QgsProcessingException( 65 | f"Error importing landuse type *.csv file from <{self.filepath}>:\n{err}" 66 | ) 67 | if len(set(self.surf_id_dict.values())) != len(self.surf_id_dict): 68 | raise QgsProcessingException( 69 | f"Duplicated FDS ID in landuse type *.csv file not allowed." 70 | ) 71 | 72 | def get_comment(self) -> str: 73 | return f"Landuse type file: <{self.filepath and utils.shorten(self.filepath) or 'none'}>" 74 | 75 | def get_fds(self) -> str: 76 | res = "\n".join(self.surf_dict.values()) 77 | return f""" 78 | Landuse boundary conditions 79 | {res or 'none'}""" 80 | 81 | @property 82 | def surf_id_str(self): 83 | return ",".join((f"'{s}'" for s in self.surf_id_dict.values())) 84 | 85 | @property 86 | def bc_out_default(self) -> str: 87 | try: 88 | return list(self.surf_id_dict)[-2] # eg. Ignition 89 | except IndexError: 90 | return 0 91 | 92 | @property 93 | def bc_in_default(self) -> str: 94 | try: 95 | return list(self.surf_id_dict)[-1] # eg. burned 96 | except KeyError: 97 | return 0 98 | -------------------------------------------------------------------------------- /types/fds.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import time, os 11 | from qgis.core import Qgis, QgsProject 12 | from qgis.utils import pluginMetadata 13 | from . import utils 14 | 15 | 16 | class FDSCase: 17 | def __init__( 18 | self, 19 | feedback, 20 | path, 21 | name, 22 | utm_crs, 23 | wgs84_origin, 24 | pixel_size, 25 | dem_layer, 26 | domain, 27 | terrain, 28 | texture, 29 | wind, 30 | ) -> None: 31 | self.feedback = feedback 32 | self.name = name # chid 33 | self.utm_crs = utm_crs 34 | self.wgs84_origin = wgs84_origin 35 | self.pixel_size = pixel_size 36 | self.dem_layer = dem_layer 37 | self.domain = domain 38 | self.terrain = terrain 39 | self.texture = texture 40 | self.wind = wind 41 | 42 | self.filename = f"{name}.fds" 43 | self.filepath = os.path.join(path, self.filename) 44 | 45 | def get_fds(self): 46 | # Init 47 | plugin_version = pluginMetadata("qgis2fds", "version") 48 | qgis_version = Qgis.QGIS_VERSION.encode("ascii", "ignore").decode("ascii") 49 | qgis_filepath = QgsProject.instance().fileName() or "not saved" 50 | date = time.strftime("%a, %d %b %Y, %H:%M:%S", time.localtime()) 51 | 52 | landuse_layer_desc = f"{self.terrain.landuse_layer and self.terrain.landuse_layer.name() or 'none'}" 53 | landuse_type_filepath = f"{self.terrain.landuse_type.filepath and utils.shorten(self.terrain.landuse_type.filepath) or 'none'}" 54 | fire_layer_desc = ( 55 | f"{self.terrain.fire_layer and self.terrain.fire_layer.name() or 'none'}" 56 | ) 57 | wind_filepath = ( 58 | f"{self.wind.filepath and utils.shorten(self.wind.filepath) or 'none'}" 59 | ) 60 | 61 | # Prepare fds case 62 | return f"""\ 63 | ! Generated by qgis2fds {plugin_version} on QGIS {qgis_version} 64 | ! QGIS file: {utils.shorten(qgis_filepath)} 65 | ! Date: {date} 66 | {self.domain.get_comment()} 67 | Desired resolution: {self.pixel_size:.1f}m 68 | DEM layer: {self.dem_layer.name()} 69 | Landuse layer: {landuse_layer_desc} 70 | Landuse type file: {landuse_type_filepath} 71 | Fire layer: {fire_layer_desc} 72 | FDS DEVCs layer: FIXME 73 | Wind file: {wind_filepath} 74 | 75 | &HEAD CHID='{self.name}' TITLE='Description of {self.name}' / 76 | 77 | MISC LEVEL_SET_MODE parameter 78 | 1: Wind not affected by the terrain. No fire. 79 | 2: Wind field established over the terrain, then frozen. No fire. 80 | 3: Wind field following the terrain, no fire. 81 | 4: Wind and fire fully-coupled. 82 | 83 | &MISC ORIGIN_LAT={self.wgs84_origin.y():.7f} 84 | ORIGIN_LON={self.wgs84_origin.x():.7f} 85 | NORTH_BEARING=0. 86 | {self.texture.get_fds()} 87 | LEVEL_SET_MODE=1 88 | THICKEN_OBSTRUCTIONS=T / 89 | 90 | &TIME T_END=0. / 91 | 92 | Example REAC, used when LEVEL_SET_MODE=4 93 | _REAC ID='Wood' SOOT_YIELD=0.005 O=2.5 C=3.4 H=6.2 94 | HEAT_OF_COMBUSTION=17700. / 95 | {self.domain.get_fds()} 96 | {self.terrain.landuse_type.get_fds()} 97 | 98 | Output quantities 99 | &SLCF AGL_SLICE=5. QUANTITY='LEVEL SET VALUE' / 100 | &SLCF AGL_SLICE=5. QUANTITY='TEMPERATURE' VECTOR=T / 101 | &SLCF PBX={0.:.2f} QUANTITY='TEMPERATURE' VECTOR=T / 102 | &SLCF PBY={0.:.2f} QUANTITY='TEMPERATURE' VECTOR=T / 103 | {self.wind.get_fds()} 104 | {self.terrain.get_fds()} 105 | 106 | &TAIL / 107 | """ 108 | 109 | def save(self): 110 | self.feedback.pushInfo(f"Write the fds case to <{self.filepath}>...") 111 | utils.write_file( 112 | feedback=self.feedback, 113 | filepath=self.filepath, 114 | content=self.get_fds(), 115 | ) 116 | -------------------------------------------------------------------------------- /types/domain.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | from math import sqrt 11 | from . import utils 12 | 13 | 14 | class Domain: 15 | def __init__( 16 | self, 17 | feedback, 18 | utm_crs, 19 | utm_extent, 20 | utm_origin, 21 | wgs84_origin, 22 | min_z, 23 | max_z, 24 | cell_size, 25 | nmesh, 26 | ) -> None: 27 | feedback.pushInfo("Init MESH...") 28 | 29 | self.feedback = feedback 30 | self.utm_extent = utm_extent 31 | self.utm_origin = utm_origin 32 | 33 | # Calc domain XB, relative to origin, 34 | # and a little smaller than the terrain 35 | dom_xb = ( 36 | utm_extent.xMinimum() - utm_origin.x() + 1., 37 | utm_extent.xMaximum() - utm_origin.x() - 1., 38 | utm_extent.yMinimum() - utm_origin.y() + 1., 39 | utm_extent.yMaximum() - utm_origin.y() - 1., 40 | min_z, 41 | max_z + cell_size * 10, # 10 cells over max z 42 | ) 43 | 44 | # Calc number of MESH along x and y 45 | ratio = abs((dom_xb[1] - dom_xb[0]) / (dom_xb[3] - dom_xb[2])) 46 | nmesh_y = round(sqrt(nmesh / ratio)) 47 | nmesh_x = int(nmesh / nmesh_y) 48 | 49 | # Calc MESH XB 50 | m_xb = ( 51 | dom_xb[0], 52 | dom_xb[0] + (dom_xb[1] - dom_xb[0]) / nmesh_x, 53 | dom_xb[2], 54 | dom_xb[2] + (dom_xb[3] - dom_xb[2]) / nmesh_y, 55 | dom_xb[4], 56 | dom_xb[5], 57 | ) 58 | m_xb = [round(x, 2) for x in m_xb] 59 | 60 | # Calc MESH IJK 61 | m_ijk = ( 62 | int((m_xb[1] - m_xb[0]) / cell_size), 63 | int((m_xb[3] - m_xb[2]) / cell_size), 64 | int((m_xb[5] - m_xb[4]) / cell_size), 65 | ) 66 | 67 | # Calc MESH MULT DX DY 68 | mult_dx, mult_dy = m_xb[1] - m_xb[0], m_xb[3] - m_xb[2] 69 | 70 | # Calc MESH size and cell number 71 | mesh_sizes = [m_xb[1] - m_xb[0], m_xb[3] - m_xb[2], m_xb[5] - m_xb[4]] 72 | ncell = m_ijk[0] * m_ijk[1] * m_ijk[2] 73 | 74 | # Prepare comment string 75 | utm_crs_desc = utm_crs.description() 76 | utm_origin_desc = f"{utm_origin.x():.1f}E {utm_origin.y():.1f}N" 77 | e = utm_extent 78 | domain_extent_desc = f"{e.xMinimum():.1f}-{e.xMaximum():.1f}E {e.yMinimum():.1f}-{e.yMaximum():.1f}N" 79 | 80 | self._comment = f""" 81 | Selected UTM CRS: {utm_crs_desc} 82 | Domain origin: {utm_origin_desc} 83 | <{utils.get_lonlat_url(wgs84_origin)}> 84 | Domain extent: {domain_extent_desc} 85 | """ 86 | 87 | # Prepare fds string 88 | self._fds = f""" 89 | Domain and its boundary conditions 90 | {nmesh_x:d} · {nmesh_y:d} meshes of {mesh_sizes[0]:.1f}m · {mesh_sizes[1]:.1f}m · {mesh_sizes[2]:.1f}m size and {ncell:d} cells each 91 | &MULT ID='Meshes' 92 | DX={mult_dx:.2f} I_LOWER=0 I_UPPER={nmesh_x-1:d} 93 | DY={mult_dy:.2f} J_LOWER=0 J_UPPER={nmesh_y-1:d} / 94 | &MESH IJK={m_ijk[0]:d},{m_ijk[1]:d},{m_ijk[2]:d} MULT_ID='Meshes' 95 | XB={m_xb[0]:.2f},{m_xb[1]:.2f},{m_xb[2]:.2f},{m_xb[3]:.2f},{m_xb[4]:.2f},{m_xb[5]:.2f} / 96 | &VENT ID='Domain BC XMIN' DB='XMIN' SURF_ID='OPEN' / 97 | &VENT ID='Domain BC XMAX' DB='XMAX' SURF_ID='OPEN' / 98 | &VENT ID='Domain BC YMIN' DB='YMIN' SURF_ID='OPEN' / 99 | &VENT ID='Domain BC YMAX' DB='YMAX' SURF_ID='OPEN' / 100 | &VENT ID='Domain BC ZMAX' DB='ZMAX' SURF_ID='OPEN' / 101 | 102 | Wind rose at domain origin 103 | &DEVC ID='Origin_UV' XYZ=0.,0.,{(m_xb[5]-.1):.2f} QUANTITY='U-VELOCITY' / 104 | &DEVC ID='Origin_VV' XYZ=0.,0.,{(m_xb[5]-.1):.2f} QUANTITY='V-VELOCITY' / 105 | &DEVC ID='Origin_WV' XYZ=0.,0.,{(m_xb[5]-.1):.2f} QUANTITY='W-VELOCITY' /""" 106 | 107 | def get_comment(self) -> str: 108 | return self._comment 109 | 110 | def get_fds(self) -> str: 111 | return self._fds 112 | -------------------------------------------------------------------------------- /algos/sampling.py: -------------------------------------------------------------------------------- 1 | import processing 2 | from qgis.PyQt.QtCore import QVariant 3 | from qgis.core import ( 4 | QgsProcessing, 5 | QgsProcessingException, 6 | QgsField, 7 | NULL, 8 | edit, 9 | QgsFeatureRequest, 10 | ) 11 | from .utils import ( 12 | get_pixel_center_aligned_grid_layer, 13 | set_grid_layer_z, 14 | set_grid_layer_value, 15 | get_reprojected_vector_layer, 16 | get_buffered_vector_layer, 17 | ) 18 | 19 | 20 | def get_utm_fire_layers( 21 | context, 22 | feedback, 23 | fire_layer, 24 | destination_crs, 25 | pixel_size, 26 | ): 27 | text = f"\nReproject and buffer <{fire_layer}> fire layer..." 28 | feedback.setProgressText(text) 29 | 30 | outputs = dict() 31 | 32 | if feedback.isCanceled(): 33 | return {} 34 | 35 | # Internal (burned area) 36 | tmp = get_reprojected_vector_layer( 37 | context, 38 | feedback, 39 | vector_layer=fire_layer, 40 | destination_crs=destination_crs, 41 | ) 42 | 43 | # External (fire front) 44 | tmp2 = get_buffered_vector_layer( 45 | context, 46 | feedback, 47 | vector_layer=tmp["OUTPUT"], 48 | distance=pixel_size, 49 | dissolve=False, 50 | ) 51 | 52 | return context.getMapLayer(tmp["OUTPUT"]), context.getMapLayer(tmp2["OUTPUT"]) 53 | 54 | 55 | def get_sampling_point_grid_layer( 56 | context, 57 | feedback, 58 | utm_dem_layer, 59 | landuse_layer, 60 | landuse_type, 61 | utm_fire_layer, 62 | utm_b_fire_layer, 63 | output=QgsProcessing.TEMPORARY_OUTPUT, 64 | ): 65 | text = f"\nCreate sampling grid layer for FDS geometry..." 66 | feedback.setProgressText(text) 67 | 68 | tmp = get_pixel_center_aligned_grid_layer( 69 | context, 70 | feedback, 71 | raster_layer=utm_dem_layer, 72 | extent=None, 73 | extent_crs=None, 74 | larger=0.0, 75 | ) 76 | 77 | if feedback.isCanceled(): 78 | return {} 79 | 80 | tmp = set_grid_layer_z( 81 | context, 82 | feedback, 83 | grid_layer=tmp["OUTPUT"], 84 | raster_layer=utm_dem_layer, 85 | output=output, 86 | ) 87 | 88 | if feedback.isCanceled(): 89 | return {} 90 | 91 | if landuse_layer: 92 | # Set landuse 93 | tmp = set_grid_layer_value( 94 | context, 95 | feedback, 96 | grid_layer=tmp["OUTPUT"], 97 | raster_layer=landuse_layer, 98 | column_prefix="landuse", 99 | output=output, 100 | ) 101 | if utm_fire_layer: 102 | # Set fire 103 | _load_fire_layer_bc( 104 | context, 105 | feedback, 106 | sampling_layer=tmp["OUTPUT"], 107 | fire_layer=utm_b_fire_layer, 108 | bc_field="bc_out", 109 | bc_default=landuse_type.bc_out_default, 110 | ) 111 | 112 | if feedback.isCanceled(): 113 | return {} 114 | 115 | _load_fire_layer_bc( 116 | context, 117 | feedback, 118 | sampling_layer=tmp["OUTPUT"], 119 | fire_layer=utm_fire_layer, 120 | bc_field="bc_in", 121 | bc_default=landuse_type.bc_in_default, 122 | ) 123 | 124 | if feedback.isCanceled(): 125 | return {} 126 | else: 127 | feedback.pushInfo("No fire layer provided.") 128 | else: 129 | feedback.pushInfo("No landuse layer provided.") 130 | # Add NULL field 131 | tmp_layer = context.getMapLayer(tmp["OUTPUT"]) 132 | with edit(tmp_layer): 133 | attributes = list((QgsField("landuse1", QVariant.Int),)) 134 | tmp_layer.dataProvider().addAttributes(attributes) 135 | tmp_layer.updateFields() 136 | 137 | if feedback.isCanceled(): 138 | return {} 139 | 140 | return tmp 141 | 142 | 143 | def _load_fire_layer_bc( 144 | context, 145 | feedback, 146 | sampling_layer, 147 | fire_layer, 148 | bc_field, 149 | bc_default, 150 | ): 151 | text = f"Load fire layer bc ({bc_field})..." 152 | feedback.pushInfo(text) 153 | 154 | # Edit sampling layer 155 | sampling_layer = context.getMapLayer(sampling_layer) 156 | with edit(sampling_layer): 157 | 158 | # Add new data field 159 | if sampling_layer.dataProvider().fieldNameIndex("bc") == -1: 160 | attributes = list((QgsField("bc", QVariant.Int),)) 161 | sampling_layer.dataProvider().addAttributes(attributes) 162 | sampling_layer.updateFields() 163 | output_bc_idx = sampling_layer.dataProvider().fieldNameIndex("bc") 164 | 165 | if fire_layer: 166 | # For all fire layer features 167 | bc_idx = fire_layer.fields().indexOf(bc_field) 168 | for fire_feat in fire_layer.getFeatures(): 169 | 170 | # Check if user specified per feature bc available 171 | if bc_idx != -1: 172 | bc = fire_feat[bc_idx] 173 | else: 174 | bc = bc_default 175 | 176 | # Set bc in sampling layer 177 | # for speed, preselect points 178 | fire_geom = fire_feat.geometry() 179 | fire_geom_bbox = fire_geom.boundingBox() 180 | for f in sampling_layer.getFeatures(QgsFeatureRequest(fire_geom_bbox)): 181 | g = f.geometry() 182 | if fire_geom.contains(g): 183 | if bc != NULL: 184 | sampling_layer.changeAttributeValue( 185 | f.id(), output_bc_idx, bc 186 | ) 187 | feedback.pushInfo( 188 | f" applyed from fire layer <{fire_feat.id()}> feature" 189 | ) 190 | -------------------------------------------------------------------------------- /types/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import os 11 | from qgis.core import QgsProcessingException 12 | from qgis.utils import iface 13 | 14 | 15 | # Text util 16 | 17 | 18 | def shorten(text): 19 | return len(text) > 60 and f"...{text[-57:]}" or text or "none" 20 | 21 | 22 | # Write to file 23 | 24 | 25 | def write_file(feedback, filepath, content): 26 | """ 27 | Write a text to filepath. 28 | """ 29 | feedback.pushInfo(f"Save file: <{filepath}>") 30 | os.makedirs(os.path.dirname(filepath), exist_ok=True) 31 | try: 32 | with open(filepath, "w") as f: 33 | f.write(content) 34 | except Exception as err: 35 | raise QgsProcessingException( 36 | f"File not writable to <{filepath}>, cannot proceed.\n{err}" 37 | ) 38 | 39 | 40 | # The FDS bingeom file is written from Fortran90 like this: 41 | # WRITE(731) INTEGER_ONE 42 | # WRITE(731) N_VERTS,N_FACES,N_SURF_ID,N_VOLUS 43 | # WRITE(731) VERTS(1:3*N_VERTS) 44 | # WRITE(731) FACES(1:3*N_FACES) 45 | # WRITE(731) SURFS(1:N_FACES) 46 | # WRITE(731) VOLUS(1:4*N_VOLUS) 47 | 48 | import struct 49 | import numpy as np 50 | 51 | 52 | def _write_record(f, data): 53 | """! 54 | Write a record to a binary unformatted sequential Fortran90 file. 55 | @param f: open Python file object in 'wb' mode. 56 | @param data: np.array() of data. 57 | """ 58 | # Calc start and end record tag 59 | tag = len(data) * data.dtype.itemsize 60 | # print(f"Write: record tag: {tag} dlen: {len(data)}\ndata: {data}") # TODO log debug 61 | # Write start tag, data, and end tag 62 | f.write(struct.pack("i", tag)) 63 | data.tofile(f) 64 | f.write(struct.pack("i", tag)) 65 | 66 | 67 | def write_bingeom( 68 | feedback, 69 | filepath, 70 | geom_type, 71 | n_surf_id, 72 | fds_verts, 73 | fds_faces, 74 | fds_surfs, 75 | fds_volus, 76 | ): 77 | """! 78 | Write FDS bingeom file. 79 | @param feedback: pyqgis feedback 80 | @param filepath: destination filepath 81 | @param geom_type: GEOM type (eg. 1 is manifold, 2 is terrain) 82 | @param n_surf_id: number of referred boundary conditions 83 | @param fds_verts: vertices coordinates in FDS flat format, eg. (x0, y0, z0, x1, y1, ...) 84 | @param fds_faces: faces connectivity in FDS flat format, eg. (i0, j0, k0, i1, ...) 85 | @param fds_surfs: boundary condition indexes, eg. (i0, i1, ...) 86 | @param fds_volus: volumes connectivity in FDS flat format, eg. (i0, j0, k0, w0, i1, ...) 87 | """ 88 | feedback.pushInfo(f"Save bingeom file: <{filepath}>") 89 | try: 90 | os.makedirs(os.path.dirname(filepath), exist_ok=True) 91 | with open(filepath, "wb") as f: 92 | _write_record(f, np.array((geom_type,), dtype="int32")) # was 1 only 93 | _write_record( 94 | f, 95 | np.array( 96 | ( 97 | len(fds_verts) // 3, 98 | len(fds_faces) // 3, 99 | n_surf_id, 100 | len(fds_volus) // 4, 101 | ), 102 | dtype="int32", 103 | ), 104 | ) 105 | _write_record(f, np.array(fds_verts, dtype="float64")) 106 | _write_record(f, np.array(fds_faces, dtype="int32")) 107 | _write_record(f, np.array(fds_surfs, dtype="int32")) 108 | _write_record(f, np.array(fds_volus, dtype="int32")) 109 | except Exception as err: 110 | raise QgsProcessingException( 111 | f"Bingeom file not writable to <{filepath}>, cannot proceed.\n{err}" 112 | ) 113 | 114 | 115 | # Geographic operations 116 | 117 | 118 | def get_lonlat_url(wgs84_point): 119 | return f"http://www.openstreetmap.org/?mlat={wgs84_point.y()}&mlon={wgs84_point.x()}&zoom=12" 120 | 121 | 122 | def lonlat_to_zn(lon, lat): 123 | """! 124 | Conversion from longitude/latitude to UTM zone number. 125 | @param lon: longitude in decimal degrees. 126 | @param lat: latitude in decimal degrees. 127 | @return the UTM zone number. 128 | """ 129 | if lat < -90.0 or lat > 90.0: 130 | raise Exception(f"Latitude <{lat}> out of bounds.") 131 | if lon < -180.0 or lon > 180.0: 132 | raise Exception(f"Longitude <{lon}> out of bounds.") 133 | if 56 <= lat < 64 and 3 <= lon < 12: 134 | return 32 135 | if 72 <= lat <= 84 and lon >= 0: 136 | if lon < 9: 137 | return 31 138 | elif lon < 21: 139 | return 33 140 | elif lon < 33: 141 | return 35 142 | elif lon < 42: 143 | return 37 144 | return int((lon + 180) / 6) + 1 145 | 146 | 147 | def lat_to_ne(lat): 148 | """! 149 | Detect if latitude is on the UTM north hemisphere. 150 | @param lat: latitude in decimal degrees. 151 | @return True if UTM north hemisphere. False otherwise. 152 | """ 153 | if lat < -90.0 or lat > 90.0: 154 | raise Exception(f"Latitude <{lat}> out of bounds.") 155 | if lat >= -1e-6: 156 | return True 157 | else: 158 | return False 159 | 160 | 161 | def lonlat_to_epsg(lon, lat): 162 | """! 163 | Conversion from longitude/latitude to EPSG. 164 | @param lon: longitude in decimal degrees. 165 | @param lat: latitude in decimal degrees. 166 | @return the EPSG. 167 | """ 168 | if lat < -90.0 or lat > 90.0: 169 | raise Exception(f"Latitude <{lat}> out of bounds.") 170 | if lon < -180.0 or lon > 180.0: 171 | raise Exception(f"Longitude <{lon}> out of bounds.") 172 | zn = lonlat_to_zn(lon=lon, lat=lat) 173 | if lat_to_ne(lat): 174 | return "EPSG:326" + str(zn).zfill(2) 175 | else: 176 | return "EPSG:327" + str(zn).zfill(2) 177 | -------------------------------------------------------------------------------- /algos/utils.py: -------------------------------------------------------------------------------- 1 | import processing 2 | from qgis.core import ( 3 | QgsProcessing, 4 | QgsRectangle, 5 | QgsCoordinateTransform, 6 | QgsProject, 7 | ) 8 | 9 | 10 | def get_pixel_center_aligned_grid_layer( 11 | context, 12 | feedback, 13 | raster_layer, 14 | extent, 15 | extent_crs, 16 | larger, 17 | output=QgsProcessing.TEMPORARY_OUTPUT, 18 | ): 19 | text = f"Get center aligned sampling grid..." 20 | feedback.pushInfo(text) 21 | 22 | aligned_extent = get_pixel_aligned_extent( 23 | context, 24 | feedback, 25 | raster_layer=raster_layer, 26 | extent=extent, 27 | extent_crs=extent_crs, 28 | to_centers=True, 29 | larger=larger, 30 | ) 31 | 32 | if feedback.isCanceled(): 33 | return {} 34 | 35 | return get_grid_layer( 36 | context, 37 | feedback, 38 | extent=aligned_extent, 39 | extent_crs=raster_layer.crs(), 40 | xres=raster_layer.rasterUnitsPerPixelX(), 41 | yres=raster_layer.rasterUnitsPerPixelY(), 42 | output=output, 43 | ) 44 | 45 | 46 | def get_pixel_aligned_extent( 47 | context, 48 | feedback, 49 | raster_layer, 50 | extent, 51 | extent_crs, 52 | larger, 53 | to_centers, 54 | ) -> QgsRectangle: 55 | text = f"Align extent to raster layer pixels..." 56 | feedback.pushInfo(text) 57 | 58 | if feedback.isCanceled(): 59 | return {} 60 | 61 | # Get raster_extent 62 | if not extent: # FIXME check if extent has CRS 63 | raster_extent = raster_layer.extent() 64 | else: 65 | tr = QgsCoordinateTransform( 66 | extent_crs, raster_layer.crs(), QgsProject.instance() 67 | ) 68 | raster_extent = tr.transformBoundingBox(extent) 69 | 70 | # Get raster_layer resolution 71 | xres = raster_layer.rasterUnitsPerPixelX() 72 | yres = raster_layer.rasterUnitsPerPixelY() 73 | feedback.pushInfo(f"Raster layer res: {xres}, {yres}") 74 | 75 | # Get top left extent corner coordinates, 76 | # because raster grid starts from top left corner of raster_layer extent 77 | lx0, ly1 = ( 78 | raster_layer.extent().xMinimum(), 79 | raster_layer.extent().yMaximum(), 80 | ) 81 | feedback.pushInfo(f"Raster layer extent: {lx0}, {ly1}") 82 | 83 | # Aligning raster_extent top left corner to raster_layer resolution, 84 | # never reduce its size 85 | x0, y0, x1, y1 = ( 86 | raster_extent.xMinimum(), 87 | raster_extent.yMinimum(), 88 | raster_extent.xMaximum(), 89 | raster_extent.yMaximum(), 90 | ) 91 | 92 | x0 = lx0 + (round((x0 - lx0) / xres) * xres) 93 | x1 = lx0 + (round((x1 - lx0) / xres) * xres) 94 | y0 = ly1 - (round((ly1 - y0) / yres) * yres) 95 | y1 = ly1 - (round((ly1 - y1) / yres) * yres) 96 | 97 | if to_centers: 98 | x0 += xres / 2.0 99 | x1 += -xres / 2.0 + 0.001 100 | y0 += yres / 2.0 - 0.001 101 | y1 += -yres / 2.0 102 | 103 | if larger: 104 | x0 -= xres * larger 105 | x1 += xres * larger 106 | y0 -= yres * larger 107 | y1 += yres * larger 108 | 109 | return QgsRectangle(x0, y0, x1, y1) 110 | 111 | 112 | def get_grid_layer( 113 | context, 114 | feedback, 115 | extent, 116 | extent_crs, 117 | xres, 118 | yres, 119 | output=QgsProcessing.TEMPORARY_OUTPUT, 120 | ): 121 | text = f"Get grid..." 122 | feedback.pushInfo(text) 123 | 124 | alg_params = { 125 | "CRS": extent_crs, 126 | "EXTENT": extent, 127 | "HOVERLAY": 0, 128 | "HSPACING": xres, 129 | "TYPE": 0, # Points 130 | "VOVERLAY": 0, 131 | "VSPACING": yres, 132 | "OUTPUT": output, 133 | } 134 | return processing.run( 135 | "native:creategrid", 136 | alg_params, 137 | context=context, 138 | feedback=feedback, 139 | is_child_algorithm=True, 140 | ) 141 | 142 | 143 | def set_grid_layer_z( 144 | context, 145 | feedback, 146 | grid_layer, 147 | raster_layer, 148 | output=QgsProcessing.TEMPORARY_OUTPUT, 149 | ): 150 | # It works when grid and raster share the same crs 151 | text = f"Set grid elevation..." 152 | feedback.pushInfo(text) 153 | 154 | alg_params = { 155 | "BAND": 1, 156 | "INPUT": grid_layer, 157 | "NODATA": -999.0, 158 | "RASTER": raster_layer, 159 | "SCALE": 1, 160 | "OUTPUT": output, 161 | } 162 | return processing.run( 163 | "native:setzfromraster", 164 | alg_params, 165 | context=context, 166 | feedback=feedback, 167 | is_child_algorithm=True, 168 | ) 169 | 170 | 171 | def set_grid_layer_value( 172 | context, 173 | feedback, 174 | grid_layer, 175 | raster_layer, 176 | column_prefix, 177 | output=QgsProcessing.TEMPORARY_OUTPUT, 178 | ): 179 | text = f"Set grid value ({column_prefix})..." 180 | feedback.pushInfo(text) 181 | 182 | alg_params = { 183 | "COLUMN_PREFIX": column_prefix, 184 | "INPUT": grid_layer, 185 | "RASTERCOPY": raster_layer, 186 | "OUTPUT": output, 187 | } 188 | return processing.run( 189 | "qgis:rastersampling", 190 | alg_params, 191 | context=context, 192 | feedback=feedback, 193 | is_child_algorithm=True, 194 | ) 195 | 196 | 197 | def get_reprojected_raster_layer( 198 | context, 199 | feedback, 200 | raster_layer, 201 | destination_crs, 202 | output=QgsProcessing.TEMPORARY_OUTPUT, 203 | ): 204 | text = f"Reproject <{raster_layer}> raster layer to <{destination_crs}> crs..." 205 | feedback.pushInfo(text) 206 | 207 | alg_params = { 208 | "INPUT": raster_layer, 209 | "TARGET_CRS": destination_crs, 210 | "RESAMPLING": 0, 211 | "NODATA": None, 212 | "TARGET_RESOLUTION": None, 213 | "OPTIONS": "", 214 | "DATA_TYPE": 0, 215 | "TARGET_EXTENT": None, 216 | "TARGET_EXTENT_CRS": None, 217 | "MULTITHREADING": False, 218 | "EXTRA": "", 219 | "OUTPUT": output, 220 | } 221 | return processing.run( 222 | "gdal:warpreproject", 223 | alg_params, 224 | context=context, 225 | feedback=feedback, 226 | is_child_algorithm=True, 227 | ) 228 | 229 | 230 | def get_reprojected_vector_layer( 231 | context, 232 | feedback, 233 | vector_layer, 234 | destination_crs, 235 | output=QgsProcessing.TEMPORARY_OUTPUT, 236 | ): 237 | text = f"Reproject <{vector_layer}> vector layer to <{destination_crs}> crs..." 238 | feedback.pushInfo(text) 239 | 240 | alg_params = { 241 | "INPUT": vector_layer, 242 | "TARGET_CRS": destination_crs, 243 | "OUTPUT": output, 244 | } 245 | return processing.run( 246 | "native:reprojectlayer", 247 | alg_params, 248 | context=context, 249 | feedback=feedback, 250 | is_child_algorithm=True, 251 | ) 252 | 253 | 254 | def get_buffered_vector_layer( 255 | context, 256 | feedback, 257 | vector_layer, 258 | distance, 259 | dissolve=False, 260 | output=QgsProcessing.TEMPORARY_OUTPUT, 261 | ): 262 | text = f"Buffer <{vector_layer}> vector layer..." 263 | feedback.pushInfo(text) 264 | 265 | alg_params = { 266 | "INPUT": vector_layer, 267 | "DISTANCE": distance, 268 | "SEGMENTS": 5, 269 | "END_CAP_STYLE": 0, 270 | "JOIN_STYLE": 0, 271 | "MITER_LIMIT": 2, 272 | "DISSOLVE": dissolve, 273 | "OUTPUT": output, 274 | } 275 | return processing.run( 276 | "native:buffer", 277 | alg_params, 278 | context=context, 279 | feedback=feedback, 280 | is_child_algorithm=True, 281 | ) 282 | 283 | 284 | def get_extent_layer( 285 | context, 286 | feedback, 287 | extent, 288 | extent_crs, 289 | output=QgsProcessing.TEMPORARY_OUTPUT, 290 | ): 291 | text = f"Get extent layer..." 292 | feedback.pushInfo(text) 293 | 294 | x0, y0, x1, y1 = ( 295 | extent.xMinimum(), 296 | extent.yMinimum(), 297 | extent.xMaximum(), 298 | extent.yMaximum(), 299 | ) 300 | alg_params = { 301 | "INPUT": f"{x0}, {x1}, {y0}, {y1} [{extent_crs.authid()}]", 302 | "OUTPUT": output, 303 | } 304 | return processing.run( 305 | "native:extenttolayer", 306 | alg_params, 307 | context=context, 308 | feedback=feedback, 309 | is_child_algorithm=True, 310 | ) 311 | -------------------------------------------------------------------------------- /types/terrain.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | import os 11 | import numpy as np 12 | from qgis.core import QgsProcessingException 13 | from . import utils 14 | 15 | 16 | class GEOMTerrain: 17 | def __init__( 18 | self, 19 | feedback, 20 | sampling_layer, 21 | utm_origin, 22 | landuse_layer, 23 | landuse_type, 24 | fire_layer, 25 | path, 26 | name, 27 | ) -> None: 28 | self.feedback = feedback 29 | self.sampling_layer = sampling_layer 30 | self.utm_origin = utm_origin 31 | self.landuse_layer = landuse_layer 32 | self.landuse_type = landuse_type 33 | self.fire_layer = fire_layer 34 | 35 | self._filename = f"{name}_terrain.bingeom" 36 | self._filepath = os.path.join(path, self._filename) 37 | 38 | self._m = None 39 | self.min_z = 0.0 40 | self.max_z = 0.0 41 | self._init_matrix() 42 | 43 | if self.feedback.isCanceled(): 44 | return {} 45 | 46 | self._faces = list() 47 | self._landuses = list() 48 | self._init_faces_and_landuses() 49 | 50 | if self.feedback.isCanceled(): 51 | return {} 52 | 53 | self._verts = list() 54 | self._init_verts() 55 | 56 | # The layer is a flat list of quad faces center points (z, x, y, landuse) 57 | # ordered by column. The original flat list is cut in columns, when three consecutive points 58 | # form an angle < 180°. 59 | # The returned matrix is a topological 2D representation of them by row (when transposed). 60 | 61 | # Same column: following column: 62 | # first · first · · current 63 | # | | ^ 64 | # | | | 65 | # prev · | | 66 | # | |/ 67 | # current · prev · 68 | 69 | # matrix: j 70 | # o o o o o 71 | # · · · · 72 | # o *---* o o 73 | # row · | · | · · i 74 | # o *---* o o 75 | # · · · · 76 | # o o o o o 77 | # 78 | # · center points of quad faces 79 | # o verts 80 | 81 | def _init_matrix(self) -> None: 82 | """Init the matrix from the sampling layer.""" 83 | self.feedback.pushInfo("Init the matrix of sampling points...") 84 | self.feedback.setProgress(0) 85 | 86 | # Init 87 | sampling_layer = self.sampling_layer 88 | nfeatures = sampling_layer.featureCount() 89 | partial_progress = nfeatures // 100 or 1 90 | m = np.empty((nfeatures, 4)) # allocate the np array 91 | ox, oy = self.utm_origin.x(), self.utm_origin.y() # get origin 92 | 93 | # Fill the array with point coordinates, points are listed by column 94 | # calc min and max z 95 | min_z, max_z = 1e6, -1e6 96 | for i, f in enumerate(self.sampling_layer.getFeatures()): 97 | g = f.geometry().get() # QgsPoint 98 | z = g.z() 99 | if z > max_z: 100 | max_z = z 101 | if z < min_z: 102 | min_z = z 103 | m[i] = ( 104 | g.x() - ox, # x, relative to origin 105 | g.y() - oy, # y, relative to origin 106 | g.z(), # z absolute 107 | 0, # for landuse 108 | ) 109 | if i % partial_progress == 0: 110 | self.feedback.setProgress(int(i / nfeatures * 100)) 111 | self.max_z, self.min_z = max_z, min_z 112 | 113 | # Fill the array with the landuse 114 | if self.landuse_layer: 115 | landuse_idx = self.sampling_layer.fields().indexOf("landuse1") 116 | for i, f in enumerate(self.sampling_layer.getFeatures()): 117 | a = f.attributes() 118 | m[i][3] = a[landuse_idx] or 0 119 | if i % partial_progress == 0: 120 | self.feedback.setProgress(int(i / nfeatures * 100)) 121 | 122 | # Fill the array with the fire layer bcs 123 | if self.fire_layer: 124 | bc_idx = self.sampling_layer.fields().indexOf("bc") 125 | for i, f in enumerate(self.sampling_layer.getFeatures()): 126 | a = f.attributes() 127 | if a[bc_idx]: 128 | m[i][3] = a[bc_idx] 129 | if i % partial_progress == 0: 130 | self.feedback.setProgress(int(i / nfeatures * 100)) 131 | 132 | # Get point column length 133 | column_len = 2 134 | p0, p1 = m[0, :2], m[1, :2] 135 | v0 = p1 - p0 136 | for p2 in m[2:, :2]: 137 | v1 = p2 - p1 138 | if abs(np.dot(v0, v1) / np.linalg.norm(v0) / np.linalg.norm(v1)) < 0.9: 139 | break # end of point column 140 | column_len += 1 141 | 142 | # Split matrix into columns list, get np array, and transpose 143 | # Now points are by row 144 | m = np.array(np.split(m, nfeatures // column_len)).transpose(1, 0, 2) 145 | # Check 146 | if m.shape[0] < 3 or m.shape[1] < 3: 147 | raise QgsProcessingException( 148 | f"[QGIS bug] Sampling matrix is too small: {m.shape[0]}x{m.shape[1]}" 149 | ) 150 | self._m = m 151 | 152 | def _inject_ghost_centers(self): 153 | """Inject ghost centers into the matrix.""" 154 | feedback = self.feedback 155 | feedback.pushInfo("Inject ghost centers in matrix...") 156 | feedback.setProgress(0) 157 | 158 | # Init displacements 159 | dx, dy = self._m[0, 1] - self._m[0, 0], self._m[1, 0] - self._m[0, 0] 160 | dx[2], dy[2] = 0.0, 0.0 # no z displacement 161 | dx[3], dy[3] = 0.0, 0.0 # no landuse change 162 | 163 | # Inject first row 164 | row = tuple(c - dy for c in self._m[0, :]) 165 | self._m = np.insert(self._m, 0, row, axis=0) 166 | 167 | # Append last row 168 | row = tuple((tuple(c + dy for c in self._m[-1, :]),)) 169 | self._m = np.append(self._m, row, axis=0) 170 | 171 | # Inject first col 172 | col = tuple(c - dx for c in self._m[:, 0]) 173 | self._m = np.insert(self._m, 0, col, axis=1) 174 | 175 | # Append last col 176 | col = tuple( 177 | tuple((c + dx,) for c in self._m[:, -1]), 178 | ) 179 | self._m = np.append(self._m, col, axis=1) 180 | 181 | def _init_faces_and_landuses(self): 182 | """Init GEOM faces and landuses.""" 183 | self.feedback.pushInfo("Init GEOM faces and their landuses...") 184 | self.feedback.setProgress(0) 185 | m = self._m 186 | len_vrow = m.shape[0] 187 | len_vcol = m.shape[1] + 1 # vert matrix is larger 188 | for i, row in enumerate(m): 189 | for j, p in enumerate(row): 190 | self._faces.extend( 191 | ( 192 | ( 193 | self._get_vert_index(i, j, len_vcol), # 1st face 194 | self._get_vert_index(i + 1, j, len_vcol), 195 | self._get_vert_index(i, j + 1, len_vcol), 196 | ), 197 | ( 198 | self._get_vert_index(i + 1, j + 1, len_vcol), # 2nd face 199 | self._get_vert_index(i, j + 1, len_vcol), 200 | self._get_vert_index(i + 1, j, len_vcol), 201 | ), 202 | ) 203 | ) 204 | lu = int(p[3]) 205 | self._landuses.extend((lu, lu)) 206 | self.feedback.setProgress(int(i / len_vrow * 100)) 207 | 208 | # First inject ghost centers all around the vertices 209 | # then extract the vertices by averaging the neighbour centers coordinates 210 | 211 | # · centers of quad faces + ghost centers 212 | # o verts * cs x vert 213 | # 214 | # dx j 215 | # + > + + + + + first ghost row 216 | # dy v o---o---o---o---o 217 | # + | · | · | · | · | + i center 218 | # o---o---x---o---o i vert 219 | # + | · | · | · | · | + i+1 center 220 | # o---o---o---o---o 221 | # + + + + + + last ghost row (skipped) 222 | 223 | def _init_verts(self): 224 | """Init verts as average of surrounding centers.""" 225 | self.feedback.pushInfo("Init GEOM verts...") 226 | self.feedback.setProgress(0) 227 | 228 | self._inject_ghost_centers() 229 | m = self._m 230 | ncenters = m.shape[0] * m.shape[1] 231 | partial_progress = ncenters // 100 or 1 232 | # Skip last row and last col 233 | for ip, idxs in enumerate(np.ndindex(m.shape[0] - 1, m.shape[1] - 1)): 234 | i, j = idxs 235 | self._verts.append( 236 | (m[i, j, :3] + m[i + 1, j, :3] + m[i, j + 1, :3] + m[i + 1, j + 1, :3]) 237 | / 4.0 238 | ) 239 | if ip % partial_progress == 0: 240 | self.feedback.setProgress(int(ip / ncenters * 100)) 241 | 242 | # j j j+1 243 | # *<------* i 244 | # | f1 // | 245 | # faces | /·/ | i 246 | # | // f2 | 247 | # *------>* i+1 248 | 249 | def _get_vert_index(self, i, j, len_vcol): 250 | """Get vert index in FDS notation.""" 251 | return i * len_vcol + j + 1 # F90 indexes start from 1 252 | 253 | def _save_bingeom(self) -> None: 254 | """Save the bingeom file.""" 255 | 256 | # Format in fds notation 257 | fds_verts = tuple(v for vs in self._verts for v in vs) 258 | fds_faces = tuple(f for fs in self._faces for f in fs) 259 | fds_surfs = list() 260 | 261 | # Translate landuse_layer landuses into FDS SURF index 262 | surf_id_list = list(self.landuse_type.surf_id_dict) 263 | n_surf_id = len(surf_id_list) 264 | for i, _ in enumerate(self._faces): 265 | lu = self._landuses[i] 266 | try: 267 | fds_surfs.append(surf_id_list.index(lu) + 1) # +1 for F90 268 | except ValueError: 269 | self.feedback.reportError(f"Unknown landuse index <{lu}>, setting <0>.") 270 | fds_surfs.append(1) # 0 + 1 for F90 271 | fds_surfs = tuple(fds_surfs) 272 | 273 | # Write bingeom 274 | utils.write_bingeom( 275 | feedback=self.feedback, 276 | filepath=self._filepath, 277 | geom_type=2, 278 | n_surf_id=n_surf_id, 279 | fds_verts=fds_verts, 280 | fds_faces=fds_faces, 281 | fds_surfs=fds_surfs, 282 | fds_volus=list(), 283 | ) 284 | 285 | 286 | 287 | def get_fds(self) -> str: 288 | """Get the FDS text and save.""" 289 | self._save_bingeom() 290 | self.feedback.pushInfo(f"GEOM terrain ready.") 291 | return f""" 292 | Terrain ({len(self._verts)} verts, {len(self._faces)} faces) 293 | &GEOM ID='Terrain' 294 | SURF_ID={self.landuse_type.surf_id_str} 295 | BINARY_FILE='{self._filename}' 296 | IS_TERRAIN=T EXTEND_TERRAIN=F /""" 297 | 298 | 299 | # OBST terrain 300 | 301 | 302 | class OBSTTerrain(GEOMTerrain): 303 | def __init__( 304 | self, 305 | feedback, 306 | sampling_layer, 307 | utm_origin, 308 | landuse_layer, 309 | landuse_type, 310 | fire_layer, 311 | path=None, # unused 312 | name=None, # unused 313 | ) -> None: 314 | self.feedback = feedback 315 | self.sampling_layer = sampling_layer 316 | self.utm_origin = utm_origin 317 | self.landuse_layer = landuse_layer 318 | self.landuse_type = landuse_type 319 | self.fire_layer = fire_layer 320 | 321 | # Init 322 | self.min_z = 0.0 323 | self.max_z = 0.0 324 | 325 | # Calc 326 | self._init_matrix() 327 | 328 | if self.feedback.isCanceled(): 329 | return {} 330 | 331 | self._inject_ghost_centers() 332 | self._init_obsts() 333 | 334 | def _init_obsts(self): 335 | """Get the formatted OBSTs from sampling layer.""" 336 | feedback = self.feedback 337 | feedback.pushInfo("Prepare OBSTs...") 338 | feedback.setProgress(0) 339 | m = self._m 340 | _obsts = list() 341 | 342 | # Init 343 | ncenters = m.shape[0] * m.shape[1] 344 | partial_progress = ncenters // 100 or 1 345 | surf_id_dict = self.landuse_type.surf_id_dict 346 | 347 | # Skip last two rows and last two cols 348 | min_z = self.min_z 349 | for ip, idxs in enumerate(np.ndindex(m.shape[0] - 2, m.shape[1] - 2)): 350 | i, j = idxs 351 | p0 = (m[i + 2, j, :2] + m[i + 1, j + 1, :2]) / 2.0 352 | p1 = (m[i + 1, j + 1, :2] + m[i, j + 2, :2]) / 2.0 353 | z = m[i + 1, j + 1, 2] 354 | lu = m[i + 1, j + 1, 3] 355 | xb = tuple((p0[0], p1[0], p0[1], p1[1], min_z, z)) 356 | try: 357 | surf_id = surf_id_dict[lu] 358 | except ValueError: 359 | self.feedback.reportError(f"Unknown landuse index <{lu}>, setting <0>.") 360 | surf_id = surf_id_dict[0] 361 | _obsts.append( 362 | f"&OBST XB={xb[0]:.2f},{xb[1]:.2f},{xb[2]:.2f},{xb[3]:.2f},{xb[4]:.2f},{xb[5]:.2f} SURF_ID='{surf_id}' /" 363 | ) 364 | if ip % partial_progress == 0: 365 | self.feedback.setProgress(int(ip / ncenters * 100)) 366 | 367 | self._obsts = _obsts 368 | 369 | def get_fds(self) -> str: 370 | """Get the FDS text.""" 371 | self.feedback.pushInfo(f"OBST terrain ready.") 372 | obsts_str = "\n".join(self._obsts) 373 | return f""" 374 | Terrain ({len(self._obsts)} OBSTs) 375 | {obsts_str} 376 | """ 377 | -------------------------------------------------------------------------------- /qgis2fds_algorithm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """qgis2fds""" 4 | 5 | __author__ = "Emanuele Gissi" 6 | __date__ = "2020-05-04" 7 | __copyright__ = "(C) 2020 by Emanuele Gissi" 8 | __revision__ = "$Format:%H$" # replaced with git SHA1 9 | 10 | DEBUG = False 11 | 12 | from qgis.core import ( 13 | QgsProject, 14 | QgsPoint, 15 | QgsCoordinateReferenceSystem, 16 | QgsCoordinateTransform, 17 | QgsProcessingException, 18 | QgsProcessingAlgorithm, 19 | QgsProcessingParameterRasterLayer, 20 | QgsProcessingParameterVectorLayer, 21 | QgsProcessingParameterPoint, 22 | QgsProcessingParameterExtent, 23 | QgsProcessingParameterFile, 24 | QgsProcessingParameterString, 25 | QgsProcessingParameterNumber, 26 | QgsProcessingParameterDefinition, 27 | QgsProcessingParameterFeatureSink, 28 | QgsProcessingParameterBoolean, 29 | QgsRasterFileWriter, 30 | QgsRasterLayer, 31 | QgsRasterPipe, 32 | QgsRasterProjector 33 | ) 34 | 35 | import os, sys 36 | from .types import ( 37 | utils, 38 | FDSCase, 39 | Domain, 40 | OBSTTerrain, 41 | GEOMTerrain, 42 | LanduseType, 43 | Texture, 44 | Wind, 45 | ) 46 | from . import algos 47 | import processing 48 | 49 | DEFAULTS = { 50 | "chid": "terrain", 51 | "fds_path": "./", 52 | "extent": None, 53 | "pixel_size": 10.0, 54 | "origin": None, 55 | "dem_layer": None, 56 | "landuse_layer": None, 57 | "landuse_type_filepath": "", 58 | "fire_layer": None, 59 | "wind_filepath": "", 60 | "tex_layer": None, 61 | "tex_pixel_size": 5.0, 62 | "nmesh": 1, 63 | "cell_size": None, 64 | "export_obst": True, 65 | "debug": False, 66 | } 67 | 68 | 69 | class qgis2fdsAlgorithm(QgsProcessingAlgorithm): 70 | """ 71 | qgis2fds algorithm. 72 | """ 73 | 74 | def initAlgorithm(self, config=None): 75 | """! 76 | Inputs and outputs of the algorithm. 77 | """ 78 | project = QgsProject.instance() 79 | 80 | # Check if project crs has changed 81 | 82 | prev_project_crs_desc, _ = project.readEntry("qgis2fds", "project_crs", None) 83 | is_project_crs_changed = False 84 | if prev_project_crs_desc != project.crs().description(): 85 | is_project_crs_changed = True 86 | 87 | # Define parameter: chid 88 | 89 | defaultValue, _ = project.readEntry("qgis2fds", "chid", DEFAULTS["chid"]) 90 | self.addParameter( 91 | QgsProcessingParameterString( 92 | "chid", 93 | "FDS case identificator (CHID)", 94 | multiLine=False, 95 | defaultValue=defaultValue, 96 | ) 97 | ) 98 | 99 | # Define parameter: fds_path 100 | 101 | defaultValue, _ = project.readEntry( 102 | "qgis2fds", "fds_path", DEFAULTS["fds_path"] 103 | ) 104 | self.addParameter( 105 | QgsProcessingParameterFile( 106 | "fds_path", 107 | "Save in folder", 108 | behavior=QgsProcessingParameterFile.Folder, 109 | fileFilter="All files (*.*)", 110 | defaultValue=defaultValue, 111 | ) 112 | ) 113 | 114 | # Define parameter: extent 115 | 116 | defaultValue, _ = project.readEntry("qgis2fds", "extent", DEFAULTS["extent"]) 117 | self.addParameter( 118 | QgsProcessingParameterExtent( 119 | "extent", 120 | "Domain extent", 121 | defaultValue=defaultValue, 122 | ) 123 | ) 124 | 125 | # Define parameter: pixel_size 126 | 127 | defaultValue, _ = project.readDoubleEntry( 128 | "qgis2fds", "pixel_size", DEFAULTS["pixel_size"] 129 | ) 130 | self.addParameter( 131 | QgsProcessingParameterNumber( 132 | "pixel_size", 133 | "Desired resolution (in meters)", 134 | type=QgsProcessingParameterNumber.Double, 135 | defaultValue=defaultValue, 136 | minValue=0.1, 137 | ) 138 | ) 139 | 140 | # Define parameter: origin [optional] 141 | 142 | if is_project_crs_changed: 143 | defaultValue = DEFAULTS["origin"] 144 | else: 145 | defaultValue, _ = project.readEntry( 146 | "qgis2fds", "origin", DEFAULTS["origin"] 147 | ) 148 | param = QgsProcessingParameterPoint( 149 | "origin", 150 | "Domain origin (if not set, use domain extent centroid)", 151 | optional=True, 152 | defaultValue=defaultValue, 153 | ) 154 | self.addParameter(param) 155 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 156 | 157 | # Define parameter: dem_layer 158 | 159 | defaultValue, _ = project.readEntry( 160 | "qgis2fds", "dem_layer", DEFAULTS["dem_layer"] 161 | ) 162 | if not defaultValue: 163 | try: # first layer name containing "dem" 164 | defaultValue = [ 165 | layer.name() 166 | for layer in QgsProject.instance().mapLayers().values() 167 | if "DEM" in layer.name() or "dem" in layer.name() 168 | ][0] 169 | except IndexError: 170 | pass 171 | self.addParameter( 172 | QgsProcessingParameterRasterLayer( 173 | "dem_layer", 174 | "DEM layer", 175 | defaultValue=defaultValue, 176 | ) 177 | ) 178 | 179 | # Define parameter: landuse_layer [optional] 180 | 181 | defaultValue, _ = project.readEntry( 182 | "qgis2fds", "landuse_layer", DEFAULTS["landuse_layer"] 183 | ) 184 | self.addParameter( 185 | QgsProcessingParameterRasterLayer( 186 | "landuse_layer", 187 | "Landuse layer (if not set, landuse is not exported)", 188 | optional=True, 189 | defaultValue=defaultValue, 190 | ) 191 | ) 192 | 193 | # Define parameter: landuse_type_filepath [optional] 194 | 195 | defaultValue, _ = project.readEntry( 196 | "qgis2fds", "landuse_type_filepath", DEFAULTS["landuse_type_filepath"] 197 | ) 198 | self.addParameter( 199 | QgsProcessingParameterFile( 200 | "landuse_type_filepath", 201 | "Landuse type *.csv file (if not set, landuse is not exported)", 202 | behavior=QgsProcessingParameterFile.File, 203 | fileFilter="CSV files (*.csv)", 204 | optional=True, 205 | defaultValue=defaultValue, 206 | ) 207 | ) 208 | 209 | # Define parameters: fire_layer [optional] 210 | 211 | defaultValue, _ = project.readEntry( 212 | "qgis2fds", "fire_layer", DEFAULTS["fire_layer"] 213 | ) 214 | if not defaultValue: 215 | try: # first layer name containing "fire" 216 | defaultValue = [ 217 | layer.name() 218 | for layer in QgsProject.instance().mapLayers().values() 219 | if "Fire" in layer.name() or "fire" in layer.name() 220 | ][0] 221 | except IndexError: 222 | pass 223 | self.addParameter( 224 | QgsProcessingParameterVectorLayer( 225 | "fire_layer", 226 | "Fire layer", 227 | optional=True, 228 | defaultValue=defaultValue, 229 | ) 230 | ) 231 | 232 | # Define parameters: devc_layer # FIXME implement 233 | # 234 | # defaultValue, _ = project.readEntry("qgis2fds", "devc_layer", None) 235 | # if not defaultValue: 236 | # try: # first layer name containing "devc" 237 | # defaultValue = [ 238 | # layer.name() 239 | # for layer in QgsProject.instance().mapLayers().values() 240 | # if "DEVC" in layer.name() or "devc" in layer.name() 241 | # ][0] 242 | # except IndexError: 243 | # pass 244 | # self.addParameter( 245 | # QgsProcessingParameterVectorLayer( 246 | # "devc_layer", 247 | # "FDS DEVCs layer", 248 | # optional=True, 249 | # defaultValue=defaultValue, 250 | # ) 251 | # ) 252 | 253 | # Define parameters: wind_filepath [optional] 254 | 255 | defaultValue, _ = project.readEntry( 256 | "qgis2fds", "wind_filepath", DEFAULTS["wind_filepath"] 257 | ) 258 | self.addParameter( 259 | QgsProcessingParameterFile( 260 | "wind_filepath", 261 | "Wind *.csv file", 262 | behavior=QgsProcessingParameterFile.File, 263 | fileFilter="CSV files (*.csv)", 264 | optional=True, 265 | defaultValue=defaultValue, 266 | ) 267 | ) 268 | 269 | # Define parameter: tex_layer [optional] 270 | 271 | defaultValue, _ = project.readEntry( 272 | "qgis2fds", "tex_layer", DEFAULTS["tex_layer"] 273 | ) 274 | param = QgsProcessingParameterRasterLayer( 275 | "tex_layer", 276 | "Texture layer (if not set, export current view)", 277 | optional=True, 278 | defaultValue=defaultValue, 279 | ) 280 | self.addParameter(param) 281 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 282 | 283 | # Define parameter: tex_pixel_size [optional] 284 | 285 | defaultValue, _ = project.readDoubleEntry( 286 | "qgis2fds", "tex_pixel_size", DEFAULTS["tex_pixel_size"] 287 | ) 288 | param = QgsProcessingParameterNumber( 289 | "tex_pixel_size", 290 | "Texture layer pixel size (in meters)", 291 | type=QgsProcessingParameterNumber.Double, 292 | defaultValue=defaultValue, 293 | minValue=0.1, 294 | ) 295 | self.addParameter(param) 296 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 297 | 298 | # Define parameter: nmesh 299 | 300 | defaultValue, _ = project.readNumEntry("qgis2fds", "nmesh", DEFAULTS["nmesh"]) 301 | param = QgsProcessingParameterNumber( 302 | "nmesh", 303 | "Max number of FDS MESHes", 304 | type=QgsProcessingParameterNumber.Integer, 305 | defaultValue=defaultValue, 306 | minValue=1, 307 | ) 308 | self.addParameter(param) 309 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 310 | 311 | # Define parameter: cell_size 312 | 313 | defaultValue, _ = project.readDoubleEntry("qgis2fds", "cell_size") 314 | param = QgsProcessingParameterNumber( 315 | "cell_size", 316 | "FDS MESH cell size (in meters; if not set, use desired resolution)", 317 | type=QgsProcessingParameterNumber.Double, 318 | optional=True, 319 | defaultValue=defaultValue or None, # protect 320 | minValue=0.1, 321 | ) 322 | self.addParameter(param) 323 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 324 | 325 | # Define parameter: export_obst 326 | 327 | defaultValue, _ = project.readBoolEntry( 328 | "qgis2fds", "export_obst", DEFAULTS["export_obst"] 329 | ) 330 | param = QgsProcessingParameterBoolean( 331 | "export_obst", 332 | "Export FDS OBSTs", 333 | defaultValue=defaultValue, 334 | ) 335 | self.addParameter(param) 336 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 337 | 338 | # Define parameter: debug 339 | defaultValue, _ = project.readBoolEntry( 340 | "qgis2fds", "debug", DEFAULTS["debug"] 341 | ) 342 | param = QgsProcessingParameterBoolean("debug","debug",defaultValue=defaultValue) 343 | param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) 344 | self.addParameter(param) 345 | 346 | # Output 347 | 348 | # param = QgsProcessingParameterFeatureSink( # DEBUG FIXME 349 | # "utm_dem_layer", # Name 350 | # "Interpolated DEM Layer", # Description 351 | # createByDefault=False, 352 | # defaultValue=None, 353 | # ) 354 | # self.addParameter(param) 355 | 356 | # param = QgsProcessingParameterFeatureSink( # DEBUG FIXME 357 | # "sampling_layer", # Name 358 | # "Sampling Layer", # Description 359 | # type=QgsProcessing.TypeVectorPoint, 360 | # createByDefault=False, 361 | # defaultValue=None, 362 | # ) 363 | # self.addParameter(param) 364 | 365 | def processAlgorithm(self, parameters, context, feedback): 366 | """ 367 | Process algorithm. 368 | """ 369 | 370 | results, outputs, project = {}, {}, QgsProject.instance() 371 | 372 | # Check project crs and save it 373 | 374 | if not project.crs().isValid(): 375 | raise QgsProcessingException( 376 | f"Project CRS <{project.crs().description()}> is not valid, cannot proceed." 377 | ) 378 | project.writeEntry("qgis2fds", "project_crs", project.crs().description()) 379 | 380 | # Get parameter: chid 381 | 382 | chid = self.parameterAsString(parameters, "chid", context) 383 | if not chid: 384 | raise QgsProcessingException(self.invalidSourceError(parameters, "chid")) 385 | project.writeEntry("qgis2fds", "chid", chid) 386 | 387 | # Get parameter: fds_path 388 | 389 | project_path = project.readPath("./") 390 | if not project_path: 391 | raise QgsProcessingException( 392 | "Save the qgis project to disk, cannot proceed." 393 | ) 394 | 395 | fds_path = self.parameterAsFile(parameters, "fds_path", context) 396 | if not fds_path: 397 | raise QgsProcessingException( 398 | self.invalidSourceError(parameters, "fds_path") 399 | ) 400 | project.writeEntry("qgis2fds", "fds_path", fds_path) 401 | fds_path = os.path.join(project_path, fds_path) # make abs 402 | 403 | # Establish os specific parameters directory 404 | if sys.platform.startswith('linux'): 405 | pass 406 | elif sys.platform == 'darwin': 407 | os.environ["PROJ_LIB"]="/Applications/QGIS.app/Contents/Resources/proj" 408 | elif (sys.platform == 'win32') or (sys.platform == 'cygwin'): 409 | pass 410 | 411 | # Get parameter for debug 412 | DEBUG = self.parameterAsBool(parameters, "debug", context) 413 | 414 | # Get parameter: pixel_size 415 | 416 | pixel_size = self.parameterAsDouble(parameters, "pixel_size", context) 417 | if not pixel_size or pixel_size <= 0.0: 418 | raise QgsProcessingException( 419 | self.invalidSourceError(parameters, "pixel_size") 420 | ) 421 | project.writeEntryDouble("qgis2fds", "pixel_size", pixel_size) 422 | 423 | # Get parameter: nmesh 424 | 425 | nmesh = self.parameterAsInt(parameters, "nmesh", context) 426 | if not nmesh or nmesh < 1: 427 | raise QgsProcessingException(self.invalidSourceError(parameters, "nmesh")) 428 | project.writeEntry("qgis2fds", "nmesh", nmesh) 429 | 430 | # Get parameter: cell_size 431 | 432 | cell_size = self.parameterAsDouble(parameters, "cell_size", context) 433 | if not cell_size: 434 | cell_size = pixel_size 435 | project.writeEntry("qgis2fds", "cell_size", "") 436 | elif cell_size <= 0.0: 437 | raise QgsProcessingException( 438 | self.invalidSourceError(parameters, "cell_size") 439 | ) 440 | else: 441 | project.writeEntryDouble("qgis2fds", "cell_size", cell_size) 442 | 443 | # Get parameter: extent (and wgs84_extent) 444 | 445 | extent = self.parameterAsExtent(parameters, "extent", context) 446 | if not extent: 447 | raise QgsProcessingException(self.invalidSourceError(parameters, "extent")) 448 | project.writeEntry("qgis2fds", "extent", parameters["extent"]) # as str 449 | 450 | wgs84_crs = QgsCoordinateReferenceSystem("EPSG:4326") 451 | wgs84_extent = self.parameterAsExtent( 452 | parameters, "extent", context, crs=wgs84_crs 453 | ) 454 | 455 | # Get parameter: origin 456 | 457 | wgs84_origin = QgsPoint(wgs84_extent.center()) 458 | origin = parameters.get("origin") or "" 459 | project.writeEntry("qgis2fds", "origin", origin) # as str 460 | if origin: 461 | if "[" in origin: 462 | crs_txt = origin.split('[')[1].split(']')[0] 463 | origin_crs = QgsCoordinateReferenceSystem(crs_txt) 464 | else: 465 | origin_crs = project.crs() 466 | # prevent a QGIS bug when using parameterAsPoint with crs=wgs84_crs 467 | # the point is exported in project crs 468 | origin = self.parameterAsPoint(parameters, "origin", context) 469 | wgs84_origin = QgsPoint(origin) 470 | project_to_wgs84_tr = QgsCoordinateTransform( 471 | origin_crs, wgs84_crs, project 472 | ) 473 | wgs84_origin.transform(project_to_wgs84_tr) 474 | 475 | # Get applicable UTM crs, then UTM origin and extent 476 | 477 | utm_epsg = utils.lonlat_to_epsg(lon=wgs84_origin.x(), lat=wgs84_origin.y()) 478 | utm_crs = QgsCoordinateReferenceSystem(utm_epsg) 479 | 480 | wgs84_to_utm_tr = QgsCoordinateTransform(wgs84_crs, utm_crs, project) 481 | utm_origin = wgs84_origin.clone() 482 | utm_origin.transform(wgs84_to_utm_tr) 483 | 484 | utm_extent = self.parameterAsExtent(parameters, "extent", context, crs=utm_crs) 485 | 486 | # Get parameters: landuse_layer and landuse_type (optional) 487 | 488 | landuse_layer, landuse_type_filepath = None, None 489 | if "landuse_layer" in parameters and "landuse_type_filepath" in parameters: 490 | landuse_type_filepath = self.parameterAsFile( 491 | parameters, "landuse_type_filepath", context 492 | ) 493 | landuse_layer = self.parameterAsRasterLayer( 494 | parameters, "landuse_layer", context 495 | ) 496 | if landuse_layer and not landuse_layer.crs().isValid(): 497 | raise QgsProcessingException( 498 | f"Landuse layer CRS <{landuse_layer.crs().description()}> is not valid, cannot proceed." 499 | ) 500 | project.writeEntry( 501 | "qgis2fds", "landuse_layer", parameters.get("landuse_layer") 502 | ) # as str 503 | project.writeEntry( 504 | "qgis2fds", "landuse_type_filepath", landuse_type_filepath 505 | ) 506 | 507 | landuse_type = LanduseType( 508 | feedback=feedback, 509 | project_path=project_path, 510 | filepath=landuse_type_filepath, 511 | ) 512 | 513 | # Get parameter: fire_layer (optional) 514 | 515 | fire_layer, utm_fire_layer, utm_b_fire_layer = None, None, None 516 | if "fire_layer" in parameters: 517 | fire_layer = self.parameterAsVectorLayer(parameters, "fire_layer", context) 518 | if fire_layer: 519 | if not fire_layer.crs().isValid(): 520 | raise QgsProcessingException( 521 | f"Fire layer CRS <{fire_layer.crs().description()}> is not valid, cannot proceed." 522 | ) 523 | utm_fire_layer, utm_b_fire_layer = algos.get_utm_fire_layers( 524 | context, 525 | feedback, 526 | fire_layer=fire_layer, 527 | destination_crs=utm_crs, 528 | pixel_size=pixel_size, 529 | ) 530 | project.writeEntry( 531 | "qgis2fds", "fire_layer", parameters.get("fire_layer") 532 | ) # as str 533 | 534 | # Get devc_layer (optional) 535 | # devc_layer = None 536 | # if parameters["devc_layer"]: 537 | # devc_layer = self.parameterAsVectorLayer(parameters, "devc_layer", context) 538 | # if not devc_layer.crs().isValid(): 539 | # raise QgsProcessingException( 540 | # f"DEVCs layer CRS <{devc_layer.crs().description()}> is not valid, cannot proceed." 541 | # ) 542 | # project.writeEntry("qgis2fds", "devc_layer", parameters["devc_layer"]) 543 | 544 | # Get parameter: wind_filepath (optional) 545 | 546 | wind_filepath = self.parameterAsFile(parameters, "wind_filepath", context) 547 | project.writeEntry("qgis2fds", "wind_filepath", wind_filepath) 548 | 549 | wind = Wind( 550 | feedback=feedback, project_path=project_path, filepath=wind_filepath 551 | ) 552 | 553 | # Get parameter: tex_layer (optional) 554 | 555 | tex_layer, texture = None, None 556 | if "tex_layer" in parameters: 557 | tex_layer = self.parameterAsRasterLayer(parameters, "tex_layer", context) 558 | if tex_layer and not tex_layer.crs().isValid(): 559 | raise QgsProcessingException( 560 | f"Texture layer CRS <{tex_layer.crs().description()}> is not valid, cannot proceed." 561 | ) 562 | project.writeEntry("qgis2fds", "tex_layer", parameters.get("tex_layer")) 563 | 564 | # Get parameter: tex_pixel_size 565 | 566 | tex_pixel_size = float( 567 | self.parameterAsDouble(parameters, "tex_pixel_size", context) 568 | ) 569 | if not tex_pixel_size or tex_pixel_size <= 0.0: 570 | raise QgsProcessingException( 571 | self.invalidSourceError(parameters, "tex_pixel_size") 572 | ) 573 | project.writeEntryDouble("qgis2fds", "tex_pixel_size", tex_pixel_size) 574 | 575 | texture = Texture( 576 | feedback=feedback, 577 | path=fds_path, 578 | name=chid, 579 | image_type="png", 580 | pixel_size=tex_pixel_size, 581 | tex_layer=tex_layer, 582 | utm_extent=utm_extent, 583 | utm_crs=utm_crs, 584 | ) 585 | 586 | # Get DEVCs layer # FIXME implement 587 | # utm_devc_layer = None 588 | # if devc_layer: 589 | # pass 590 | 591 | # if feedback.isCanceled(): 592 | # return {} 593 | 594 | # Get parameter: export_obst 595 | 596 | export_obst = self.parameterAsBool(parameters, "export_obst", context) 597 | project.writeEntryBool("qgis2fds", "export_obst", export_obst) 598 | 599 | # Get parameter: dem_layer 600 | 601 | dem_layer = self.parameterAsRasterLayer(parameters, "dem_layer", context) 602 | if not dem_layer: 603 | raise QgsProcessingException( 604 | self.invalidSourceError(parameters, "dem_layer") 605 | ) 606 | if not dem_layer.crs().isValid(): 607 | raise QgsProcessingException( 608 | f"DEM layer CRS <{dem_layer.crs().description()}> is not valid, cannot proceed." 609 | ) 610 | project.writeEntry("qgis2fds", "dem_layer", parameters.get("dem_layer")) 611 | 612 | # Calc the interpolated DEM layer 613 | 614 | outputs["utm_dem_layer"] = algos.clip_and_interpolate_dem( 615 | context, 616 | feedback, 617 | dem_layer=dem_layer, 618 | extent=utm_extent, 619 | extent_crs=utm_crs, 620 | pixel_size=pixel_size, 621 | # output=parameters["utm_dem_layer"], # DEBUG 622 | ) 623 | 624 | if feedback.isCanceled(): 625 | return {} 626 | 627 | # results["utm_dem_layer"] = outputs["utm_dem_layer"]["OUTPUT"] # DEBUG 628 | utm_dem_layer = QgsRasterLayer(outputs["utm_dem_layer"]["OUTPUT"]) 629 | 630 | # Get the sampling grid 631 | outputs["sampling_layer"] = algos.get_sampling_point_grid_layer( 632 | context, 633 | feedback, 634 | utm_dem_layer=utm_dem_layer, 635 | landuse_layer=landuse_layer, 636 | landuse_type=landuse_type, 637 | utm_fire_layer=utm_fire_layer, # utm 638 | utm_b_fire_layer=utm_b_fire_layer, # utm buffered 639 | # output=parameters["sampling_layer"], # DEBUG 640 | ) 641 | 642 | if feedback.isCanceled(): 643 | return {} 644 | 645 | # if DEBUG: 646 | # results["sampling_layer"] = outputs["sampling_layer"]["OUTPUT"] # DEBUG FIXME 647 | sampling_layer = context.getMapLayer(outputs["sampling_layer"]["OUTPUT"]) 648 | 649 | if sampling_layer.featureCount() < 9: 650 | raise QgsProcessingException( 651 | f"[QGIS bug] Too few features in sampling layer, cannot proceed.\n{sampling_layer.featureCount()}" 652 | ) 653 | 654 | # Align utm_extent to the new interpolated dem 655 | utm_extent = algos.get_pixel_aligned_extent( 656 | context, 657 | feedback, 658 | raster_layer=utm_dem_layer, 659 | extent=None, 660 | extent_crs=None, 661 | to_centers=False, 662 | larger=0.0, 663 | ) 664 | 665 | if feedback.isCanceled(): 666 | return {} 667 | 668 | if DEBUG: 669 | for layer_id in context.temporaryLayerStore().mapLayers(): 670 | layer = context.getMapLayer(layer_id) 671 | name = layer.name() 672 | if chid in name: 673 | outname = os.path.join(project_path,"debug_" + name) 674 | else: 675 | outname = os.path.join(project_path,"debug_" + chid + "_" + name) 676 | if type(layer) is QgsRasterLayer: 677 | outname = outname + '.tif' 678 | renderer = layer.renderer() 679 | provider = layer.dataProvider() 680 | pipe = QgsRasterPipe() 681 | projector = QgsRasterProjector() 682 | projector.setCrs(layer.crs(), layer.crs()) 683 | file_writer = QgsRasterFileWriter(outname) 684 | file_writer.Mode(1) 685 | width = layer.width() 686 | height = layer.height() 687 | layer_extent = layer.extent() 688 | layer_crs = layer.crs() 689 | 690 | error = file_writer.writeRaster(pipe, width, height, layer_extent, layer_crs) 691 | else: 692 | outname = outname + '.gpkg' 693 | alg_params = {"INPUT": name, "OUTPUT": outname, 'LAYER_NAME': name} 694 | processing.run("native:savefeatures", alg_params, context=context) 695 | feedback.pushInfo("Saving %s"%(outname)) 696 | 697 | # Prepare terrain, domain, and fds_case 698 | if export_obst: 699 | Terrain = OBSTTerrain 700 | else: 701 | Terrain = GEOMTerrain 702 | terrain = Terrain( 703 | feedback=feedback, 704 | sampling_layer=sampling_layer, 705 | utm_origin=utm_origin, 706 | landuse_layer=landuse_layer, 707 | landuse_type=landuse_type, 708 | fire_layer=fire_layer, 709 | path=fds_path, 710 | name=chid, 711 | ) 712 | 713 | if feedback.isCanceled(): 714 | return {} 715 | 716 | domain = Domain( 717 | feedback=feedback, 718 | utm_crs=utm_crs, 719 | utm_extent=utm_extent, 720 | utm_origin=utm_origin, 721 | wgs84_origin=wgs84_origin, 722 | min_z=terrain.min_z, 723 | max_z=terrain.max_z, 724 | cell_size=cell_size, 725 | nmesh=nmesh, 726 | ) 727 | 728 | fds_case = FDSCase( 729 | feedback=feedback, 730 | path=fds_path, 731 | name=chid, 732 | utm_crs=utm_crs, 733 | wgs84_origin=wgs84_origin, 734 | pixel_size=pixel_size, 735 | dem_layer=dem_layer, 736 | domain=domain, 737 | terrain=terrain, 738 | texture=texture, 739 | wind=wind, 740 | ) 741 | fds_case.save() 742 | 743 | return results 744 | 745 | def name(self): 746 | """! 747 | Returns the algorithm name. 748 | """ 749 | return "Export terrain" 750 | 751 | def displayName(self): 752 | """! 753 | Returns the translated algorithm name. 754 | """ 755 | return self.name() 756 | 757 | def group(self): 758 | """! 759 | Returns the name of the group this algorithm belongs to. 760 | """ 761 | return self.groupId() 762 | 763 | def groupId(self): 764 | """ 765 | Returns the unique ID of the group this algorithm belongs to. 766 | """ 767 | return "" 768 | 769 | def createInstance(self): 770 | return qgis2fdsAlgorithm() 771 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------