### Map generator for "BE Graphes"

The purpose of this repository is to ease the process of generating graph and mapforge file that are consistent (using the same OpenStreetMap data).

### How to start?

To clone this repository:

```bash
# You need the --recursive option (--recurse-submodules with Git >= 2.13),
# you can use https or ssh, it does not matter.
git clone --recursive https://gitea.typename.fr/INSA/be-graphes-map-generator.git

# Then you need to initialize the repository, this may takes some time as it needs
# to fetch a shapefile (~500MB) on a remote server.
cd be-graphes-map-generator
bash init.sh
```

If you do not plan on generating maps with coastline for mapsforge (see below), you can use the `--no-shapefile` option
to avoid the download:
```bash
bash init.sh --no-shapefile
```

### Folders organization

The two main folders are `inputs` and `outputs`:

- `inputs` mainly contains:
    - a script `coastline-fix.sh` to merge land/sea polygon (see below) and PBF files;
    - a directory containing boundinx-box (`bbox`) for specific regions;
    - a file containing highway filters;
    - PBF files downloaded organized in folder `$REGION/$REGION.pbf`.

- `outputs` is the default folder for generated output.

### Generating maps

#### Step 1 - Obtaining a PBF file.

You should create a PBF files containining the region you want:

- Download a complete region from Geofabrik: [http://download.geofabrik.de/](http://download.geofabrik.de/)
- Chop a region previously downloaded:

*Using a bounding box:*

```bash
bin/osmosis --rb inputs/bretagne/bretagne-latest.osm.pbf \ # Specify your input PBF
            --bounding-box bottom=47.160000 left=-3.855000 top=48.330000 right=-1.915000 completeWays=yes \ # Specify the bounding bo
            --write-pbf inputs/bretagne/morbihan.pbf # Specify the output file
```

*Using a polygon:*

```bash
osmosis --rb inputs/bretagne/bretagne-latest.osm.pbf \ # Specify your input PBF 
        --bounding-polygon file=inputs/bbox/morbihan.poly completeWays=yes \ # Specify your bounding polygon
        --write-pbf file=inputs/bretagne/morbihan.pbf # Specify your output file
```

The `inputs/bbox` directory contains bounding-box (`.bbox`) and bounding polygon (`.poly`) for some regions. You can find polygon for a lots of region here: [http://polygons.openstreetmap.fr/index.py](http://polygons.openstreetmap.fr/index.py) (to find the id of the relation, search it on openstreetmap, e.g. [https://www.openstreetmap.org/relation/7447](https://www.openstreetmap.org/relation/7447)).

#### Step 1 Bis - Fixing coastlines and sea.

Skip this step if your region does not contain sea or big inner lakes, or if you do not want to generate mapsforge file.

We need to add sea information to the PBF file to generate a correct mapsforge map. To do so, a script `coastline-fix.sh` is provided. Run it simply:

```bash
./coastline-fix.sh bretagne/morbihan.pbf
```

To apply the fix, you need a polygon file (`morbihan.poly` &mdash; not another name), either in the same directory as the PBF or in the `inputs/bbox` directory (recommended).

This script will generate multiple intermediate files, but only two are importants:

- `inputs/bbox/morbihan.bbox`, actual bounding-box of the PBF file &mdash; This will overwrite the old bounding-box if there was one.
- `inputs/bretagne/morbihan_merge.pbf`, merged file that you should use as input for mapsforge.


#### Step 2 - Binary graph files

```bash
# You can use either "insa2016" or "insa2018" writers, and you should change the map id to your need.
bin/osmosis --rb inputs/bretagne/morbihan.pbf  \
    --used-node \
    --tf reject-relations \
    --tf accept-ways highway=(cat inputs/highway-filter.cmd) natural=coastline \
    --osm2graph file=outputs/morbihan.mapgr writer=insa2018 map-id=0x235
```

#### Step 3 - Mapsforge files

```bash
# You must specify a bounding-box if you used the coastline fix.
# You can use type=ram to speed-up the process for small maps.
bin/osmosis --rb inputs/bretagne/morbihan_merge.pbf \
            --mapfile-writer file=outputs/morbihan.mapfg bbox=$(cat inputs/bbox/morbihan.bbox) type=hd threads=2

```

#### Extra informations:

- Using a polygon to crop your region is preferred if you plan on applying the coastline fix.
- I do not recommend using the merged file as input for `osm2graph` because coastlines are often very long OpenStreetMap ways, and the plugin will not cut them (typically, you could reach Spain when using the merged Morbihan file).
- I do not recommend using `osmosis` bounding-box option with `osm2graph` and/or `mapfile-writer` unless your bounding-box is small and does not contain sea or coastline. It is always preferred to generate an intermediate PBF file containing only your region (using `--write-pbf`).