Skip to content

Commit

Permalink
Merge pull request #18 from forefireAPI/geojson
Browse files Browse the repository at this point in the history
add geojsom dump mode fix #10
  • Loading branch information
antonio-leblanc authored Oct 27, 2022
2 parents d057f65 + d824921 commit a2b475c
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 93 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ __pycache__

# compilation files
*.dblite
*.o
*.o

# editors
.vscode
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ Access the [demo simulator here](http://forefire.univ-corse.fr/sim/dev/)
![demo](./doc/sim-forefire.jpg)


It has been designed and run on Unix systems, three modules can be built with the source code.
It has been designed and runs on Unix systems. Three modules can be built with the source code.

The main binaries are

The main binaries are
- An interpreter (executable)
- A dynamic library (shared, with C/C++/Java and Fortran bindings)
- A shared library (with C/C++/Java and Fortran bindings)

## 1. Requirements

Expand All @@ -41,9 +40,9 @@ apt install cmake -y
```

To install
- The C++ compiler (that may come pre-built with your linux distribution)
- [NetCDF Library](https://www.unidata.ucar.edu/software/netcdf/) and [NetCDF-C++ legacy](https://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp), for compatibilities issues
- [cmake](https://cmake.org/), is used to build the executable and the python library
- The C++ compiler
- [NetCDF Library](https://www.unidata.ucar.edu/software/netcdf/) and [NetCDF-C++ legacy](https://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp)
- [Cmake](https://cmake.org/) build tool

## 2. Build

Expand All @@ -58,7 +57,14 @@ To make the program [executable from eveywhere](https://unix.stackexchange.com/q
```
export PATH=$PATH:`pwd`/bin
```
If you want to change it permanently add `export PATH=$PATH:</path/to/file>` to your ~/.bashrc file
If you want to change it permanently, paste
```
export PATH=$PATH:</path/to/file>
```
at the end of your `~/.bashrc` file. The file can be edited with
```
nano ~/.bashrc
```


### 2.2 Scons and Other build systems
Expand Down Expand Up @@ -87,25 +93,41 @@ More info on build systems can be found on [this issue](https://github.com/foref

## 3. Running an example

An example for the region of aullene in south France is provided. The example contains 3 files
- fuels.ff
- aullene.ff
- landscape.nc:

Run the example with

```
cd firefront/examples/aullene/
../../bin/forefire -i aullene.ff
```
The simulation result will be outputed in JSON format

### Converting output JSON to GeoJSON

### 4. Running with python

Use the script `ff2geojson.py` with the .json file as argument.
Installing requirements
```
python3 py3_tools/ff2geojson.py examples/aullene/1-2009-07-24T15-01-00Z.json
cd py3_tools
pip install -r requirements.txt
```
The JSON will be converted to GeoJSON (EPSG 4326) of geometry type MultiPoint and saved in the same directory.

### Running an example in a chosen location
You can use the script `coord_to_ff.py` to run the simulation in a default location

```
python coord_to_ff.py
```

For running in a chosen location, the script accepts latitude and longitude in epsg:4326 projection as inputs. It reprojects the coordinates into epsg:32632 projection, used in aullene's landscape.
```
python coord_to_ff.py --lat 41.6 --lon 9.1
```

Use the script `coord_to_ff.py` using `--lon` and `--lat` flags to pass coordinates. An example simulation will be outputted in GeoJSON in `/examples/aullene`.
The GeoJSON of geometry type Polygon will be saved in the `/examples/aullene` folder

## 4. Building python Lib
The `/swig` folder contains and `Sconstruct` file for python bindings.
Expand Down
3 changes: 2 additions & 1 deletion examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.json
*.geojson
*.geojson
*.ffgeojson
14 changes: 14 additions & 0 deletions examples/aullene/aullene2.ff
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
setParameter[dumpMode=geojson]
setParameter[caseDirectory=.]
setParameter[ForeFireDataDirectory=.]
setParameter[projection=EPSG:32632]
setParameter[fuelsTableFile=./fuels.ff]
setParameter[propagationModel=Rothermel]
setParameter[year=2022]
setParameter[month=10]
setParameter[day=27]
loadData[landscape.nc;2009-07-24T11:37:39Z]
startFire[loc=(516666.85406561225,4605385.548179354,0);t=0]
step[dt=12000s]
print[./*count*-*ISOdate*.ffgeojson]
print[]
11 changes: 6 additions & 5 deletions py3_tools/coord_to_ff.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import argparse

from ff2geojson import *
from ForeFirepy3 import *
from forefirepy.ForeFire import *

def main():
ap = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
ap.add_argument(
"--lat",
default=41.6,
help="latitude of fire start"
)
ap.add_argument(
"--lon",
default=9.2,
help="longitude of fire start"
)
args = vars(ap.parse_args())
Expand All @@ -19,7 +20,7 @@ def main():

dir_path = os.path.dirname(os.path.realpath(__file__))
output_path = dir_path + '/../examples/aullene/'
filename = f'{lon}_{lat}.ff'
filename = f'aullene2.ff'
complete_path = output_path + filename

[x, y] = reproject([lon, lat], inEpsg='epsg:4326', outEpsg='epsg:32632')
Expand All @@ -28,9 +29,9 @@ def main():
ff.configBasicFf(lon=x, lat=y)
ff.saveFf(complete_path)

os.system(f'cd {output_path}; ../../bin/forefire -i {filename}')
os.system(f'cd {output_path}; forefire -i {filename}')

ffjson2geojson(output_path + '0-2009-07-24T14-57-39Z.json')
ff.convert_to_geojson(output_path + '0-2009-07-24T14-57-39Z.ffgeojson')


if __name__ == '__main__':
Expand Down
67 changes: 0 additions & 67 deletions py3_tools/ff2geojson.py

This file was deleted.

11 changes: 9 additions & 2 deletions py3_tools/ForeFirepy3.py → py3_tools/forefirepy/ForeFire.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import os
from datetime import date
from .ff2geojson import *

class Forefire:

version = '1.1.20'

def __init__(self):
self.ff = '''setParameter[dumpMode=json]
self.ff = '''setParameter[dumpMode=geojson]
setParameter[caseDirectory=.]
setParameter[ForeFireDataDirectory=.]
'''
Expand Down Expand Up @@ -51,7 +54,7 @@ def step(self, dt):
self.ff += f'step[dt={dt}s]\n'

def printOutput(self):
self.ff += 'print[./*count*-*ISOdate*.json]\nprint[]'
self.ff += 'print[./*count*-*ISOdate*.ffgeojson]\nprint[]'

def saveFf(self, path):
with open(path, 'w', encoding='utf-8') as f:
Expand All @@ -66,3 +69,7 @@ def configBasicFf(self, lon, lat, t=12000):
self.startFire(lon, lat)
self.step(t)
self.printOutput()

def convert_to_geojson(self, path):
ffjson2geojson(path)
return True
Empty file.
39 changes: 39 additions & 0 deletions py3_tools/forefirepy/ff2geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
import json
from pyproj import Transformer

def load_json(file_path):
with open(file_path, 'r') as f:
data = json.load(f)
return data

def reproject(xy, inEpsg, outEpsg='epsg:4326'):
[x1, y1] = xy
transformer = Transformer.from_crs(inEpsg, outEpsg, always_xy=True)
x2, y2 = transformer.transform(x1, y1)
return [x2,y2]

def ffjson2geojson(filepath):
ff_geojson = load_json(filepath)

#reproject
inEpsg = ff_geojson['projection'].lower()
for feature in ff_geojson["features"]:
reproj = [reproject(xy, inEpsg) for xy in feature['geometry']['coordinates'][0]]
reproj.append(reproj[0])
feature['geometry']['coordinates'][0] = reproj
ff_geojson['projection'] = 'epsg:4326'

#save
fileextension = filepath.split('.')[-1]
savePath = filepath.replace(f'.{fileextension}', '.geojson')
with open(savePath, 'w', encoding='utf-8') as f:
json.dump(ff_geojson, f, ensure_ascii=False, indent=4)


if __name__ == '__main__':
try:
filepath = sys.argv[1]
ffjson2geojson(filepath)
except Exception as e:
print(e)
Loading

0 comments on commit a2b475c

Please sign in to comment.