2015-11-15 07:56:36 +00:00
# ogn-python
2015-10-24 21:13:21 +00:00
2017-09-16 07:19:21 +00:00
[](https://travis-ci.org/glidernet/ogn-python)
[](https://coveralls.io/r/glidernet/ogn-python)
2015-11-15 08:26:09 +00:00
2016-03-18 21:56:59 +00:00
A database backend for the [Open Glider Network ](http://wiki.glidernet.org/ ).
The ogn-python module saves all received beacons into a database with [SQLAlchemy ](http://www.sqlalchemy.org/ ).
It connects to the OGN aprs servers with [python-ogn-client ](https://github.com/glidernet/python-ogn-client ).
2019-01-06 19:37:20 +00:00
It requires [PostgreSQL ](http://www.postgresql.org/ ) and [PostGIS ](http://www.postgis.net/ ).
For best performance you should use [TimescaleDB ](https://www.timescale.com ), which is based on PostgreSQL.
2016-01-12 18:31:08 +00:00
[Examples ](https://github.com/glidernet/ogn-python/wiki/Examples )
2016-03-18 21:54:22 +00:00
## Installation and Setup
2016-01-12 18:31:08 +00:00
1. Checkout the repository
2019-01-24 18:44:19 +00:00
```
git clone https://github.com/glidernet/ogn-python.git
```
2016-01-12 18:31:08 +00:00
2. Install python requirements
2015-12-05 09:04:26 +00:00
```
pip install -r requirements.txt
```
2015-11-15 08:26:09 +00:00
2019-01-24 18:44:19 +00:00
3. Install [PostgreSQL ](http://www.postgresql.org/ ) with [PostGIS ](http://www.postgis.net/ ) Extension.
Create a database (use "ogn" as default, otherwise you have to modify the configuration, see below)
2016-06-16 08:58:03 +00:00
2019-01-24 18:44:19 +00:00
4. Optional: Install redis for asynchronous tasks (like takeoff/landing-detection)
2015-12-05 09:04:26 +00:00
```
apt-get install redis-server
```
2015-11-15 08:26:09 +00:00
2016-06-16 08:58:03 +00:00
5. Create database
2015-12-05 09:04:26 +00:00
```
./manage.py db.init
```
2015-11-15 08:26:09 +00:00
2019-01-06 19:37:20 +00:00
6. Optional: Prepare tables for TimescaleDB
```
./manage.py db.init_timescaledb
```
2019-01-24 18:44:19 +00:00
7. Optional: Import world border dataset (needed if you want to know the country a receiver belongs to, etc.)
Get the [World Borders Dataset ](http://thematicmapping.org/downloads/world_borders.php ) and unpack it.
Then import it into your database (we use "ogn" as database name).
```
shp2pgsql -s 4326 TM_WORLD_BORDERS-0.3.shp world_borders_temp | psql -d ogn
psql -d ogn -c "INSERT INTO countries SELECT * FROM world_borders_temp;"
psql -d ogn -c "DROP TABLE world_borders_temp;"
```
8. Optional: Import world elevation data (needed for AGL calculation)
For Europe we can get the DEM as GeoTIFF files from the
[European Environment Agency ](https://land.copernicus.eu/imagery-in-situ/eu-dem/eu-dem-v1.1 ).
Because the spatial reference system (SRID) of these files is 3035 and we want 4326 we have to convert them:
```
gdalwarp -s_srs "EPSG:3035" -t_srs "EPSG:4326" source.tif target.tif
```
Then we can import the GeoTIFF into the elevation table:
```
raster2pgsql -c -C -I -M -t 100x100 elevation_data.tif public.elevation | psql -d ogn
```
2016-09-23 18:13:23 +00:00
There is also a [Vagrant ](https://www.vagrantup.com/ ) environment for the development of ogn-python.
You can create and start this virtual machine with `vagrant up` and login with `vagrant ssh` .
The code of ogn-python will be available in the shared folder `/vagrant` .
2016-03-18 21:54:22 +00:00
## Usage
2016-01-12 18:31:08 +00:00
### Running the aprs client and task server
To schedule tasks like takeoff/landing-detection (`logbook.compute`),
[Celery ](http://www.celeryproject.org/ ) with [Redis ](http://www.redis.io/ ) is used.
The following scripts run in the foreground and should be deamonized
2015-11-15 08:26:09 +00:00
(eg. use [supervisord ](http://supervisord.org/ )).
2016-01-29 04:33:59 +00:00
- Start the aprs client
2016-01-12 18:31:08 +00:00
```
./manage.py gateway.run
```
2016-01-29 04:33:59 +00:00
- Start a task server (make sure redis is up and running)
2015-11-15 08:26:09 +00:00
2016-01-12 18:31:08 +00:00
```
celery -A ogn.collect worker -l info
```
2016-01-29 04:33:59 +00:00
- Start the task scheduler (make sure a task server is up and running)
```
celery -A ogn.collect beat -l info
```
2016-01-29 01:34:12 +00:00
To load a custom configuration, create a file `myconfig.py` (see [config/default.py ](config/default.py ))
and set the environment variable `OGN_CONFIG_MODULE` accordingly.
```
2016-02-04 22:15:54 +00:00
touch myconfig.py
export OGN_CONFIG_MODULE="myconfig"
2016-01-29 01:34:12 +00:00
./manage.py gateway.run
```
2016-01-12 18:31:08 +00:00
### manage.py - CLI options
2015-11-15 08:26:09 +00:00
```
2017-06-23 06:16:45 +00:00
usage: manage [< namespace > .]< command > [< args > ]
2015-11-15 08:26:09 +00:00
positional arguments:
command the command to run
optional arguments:
-h, --help show this help message and exit
available commands:
2017-06-23 06:16:45 +00:00
[bulkimport]
2018-11-28 06:37:35 +00:00
create_flights2d Create complete flight traces from logfile tables.
create_gaps2d Create 'gaps' from logfile tables.
file_export Export separate logfile tables to csv files. They can be used for fast bulk import with sql COPY command.
file_import Import APRS logfiles into separate logfile tables.
2019-01-04 14:06:11 +00:00
transfer Transfer beacons from separate logfile tables to beacon table.
2018-11-28 06:37:35 +00:00
update Update beacons (add foreign keys, compute distance, bearing, ags, etc.) in separate logfile tables.
2017-06-23 06:16:45 +00:00
2015-11-15 08:26:09 +00:00
[db]
2016-01-12 18:31:08 +00:00
drop Drop all tables.
2016-04-28 14:39:02 +00:00
import_airports Import airports from a ".cup" file
2015-12-09 02:41:58 +00:00
import_ddb Import registered devices from the DDB.
2018-11-28 06:37:35 +00:00
import_file Import registered devices from a local file.
import_flarmnet Import registered devices from a local file.
2015-11-15 08:26:09 +00:00
init Initialize the database.
2019-01-04 14:06:11 +00:00
init_timescaledb Initialize TimescaleDB features.
2018-11-28 06:37:35 +00:00
update_country_codes Update country codes of all receivers.
2016-01-31 01:25:21 +00:00
upgrade Upgrade database to the latest version.
2017-06-23 06:16:45 +00:00
2019-01-01 19:11:41 +00:00
[flights]
flights2d Compute flights.
2015-11-15 08:26:09 +00:00
[gateway]
run Run the aprs client.
2017-06-23 06:16:45 +00:00
2019-01-31 20:26:20 +00:00
[export]
cup Export receiver waypoints as '.cup'.
igc Export igc file for < address > at < date > .
2017-06-23 06:16:45 +00:00
2015-11-15 08:26:09 +00:00
[logbook]
2016-07-14 19:15:58 +00:00
compute_logbook Compute logbook.
compute_takeoff_landingCompute takeoffs and landings.
2016-04-28 14:39:02 +00:00
show Show a logbook for < airport_name > .
2017-06-23 06:16:45 +00:00
2017-12-16 21:18:04 +00:00
[stats]
2019-01-05 10:25:33 +00:00
create Create DeviceStats, ReceiverStats and RelationStats.
2019-01-06 20:03:29 +00:00
create_ognrange Create stats for Melissa's ognrange.
2019-01-05 10:10:10 +00:00
update_devices Update devices with data from stats.
update_receivers Update receivers with data from stats.
2015-11-15 08:26:09 +00:00
```
2015-12-09 02:41:58 +00:00
Only the command `logbook.compute` requires a running task server (celery) at the moment.
2015-11-15 08:26:09 +00:00
2016-01-29 04:33:59 +00:00
### Available tasks
2015-11-15 08:26:09 +00:00
2017-12-16 21:18:04 +00:00
- `ogn.collect.database.import_ddb` - Import registered devices from the DDB.
- `ogn.collect.database.import_file` - Import registered devices from a local file.
- `ogn.collect.database.update_country_code` - Update country code in receivers table if None.
- `ogn.collect.database.update_devices` - Add/update entries in devices table and update foreign keys in aircraft beacons.
- `ogn.collect.database.update_receivers` - Add/update_receivers entries in receiver table and update receivers foreign keys and distance in aircraft beacons and update foreign keys in receiver beacons.
- `ogn.collect.logbook.update_logbook` - Add/update logbook entries.
- `ogn.collect.logbook.update_max_altitude` - Add max altitudes in logbook when flight is complete (takeoff and landing).
- `ogn.collect.stats.update_device_stats` - Add/update entries in device stats table.
- `ogn.collect.stats.update_receiver_stats` - Add/update entries in receiver stats table.
- `ogn.collect.takeoff_landing.update_takeoff_landing` - Compute takeoffs and landings.
2016-02-04 22:05:32 +00:00
If the task server is up and running, tasks could be started manually.
```
python3
>>>from ogn.collect.database import import_ddb
>>>import_ddb.delay()
```
2015-11-15 08:26:09 +00:00
2016-01-12 18:31:08 +00:00
## License
Licensed under the [AGPLv3 ](LICENSE ).