Commit 9fe38177 authored by Christine Plumejeaud's avatar Christine Plumejeaud
Browse files

request for flows with 3 params : list of ports, direction and degree

Documentation is also inside the readme
parent cd2d4a01
......@@ -12,8 +12,7 @@
- [1.2.4.1. /details/departures?lon=lat=radius=](#1241-detailsdepartureslonlatradius)
- [1.2.4.2. /agg/departures?lon=lat=radius=](#1242-aggdepartureslonlatradius)
- [1.2.4.3. /agg/destinations?lon=lat=radius=](#1243-aggdestinationslonlatradius)
- [1.2.4.4. /travels?link_to_port="UHGS_id du port"&both-to=true](#1244-travelslinktoport%22uhgsid-du-port%22both-totrue)
- [1.2.4.5. /travels?link_to_port="UHGS_id du port"&degree=0&both-to=true](#1245-travelslinktoport%22uhgsid-du-port%22degree0both-totrue)
- [1.2.5. /flows?](#125-flows)
......@@ -130,6 +129,8 @@ Exemple :
### 1.2.3. /pointcalls?
-- TOUT en json ou csv
- http://data.portic.fr/api/pointcalls/?format=csv
- http://data.portic.fr/api/pointcalls/?format=json
### 1.2.4. /travels?
-- TOUT sans duplicats par défaut en json ou csv
......@@ -180,26 +181,63 @@ Les calculs spatiaux sont déportés dans le SGBD Postgres pour bénéficier des
```
#### 1.2.4.4. /travels?link_to_port="UHGS_id du port"&both-to=true
### 1.2.5. /flows?
TODO not done yet
Return the flows as specified in API : a list of travels linked to the specified ports, either by entering in
(direction In), either by existing from (direction Out), either by having sailing around (direction In-out),
or for any direction (default, or specify direction No).
- ports : ports identifiers (UHGS_id) for which travels are filtered, comma separated(,)
- direction : In | Out | In-out | **No**
- degree : **0**, 1 or more
(when 0 : all flows going through out the list of cited ports, with also the list of previous and next pointcalls)
Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL),
but with a filter by default : only source_entry = from and both-from, to avoid duplicates
- params : **all** | tableau des noms longs des attributs de l'API à renvoyer
params=pointcall,pointcall_uhgs_id for instance
- both_to : true | **false**
Examples :
- http://data.portic.fr:80/api/flows/?format=csv&both_to=false : VERY LONG, AVOID. Use travels instead
- http://data.portic.fr:80/api/flows/?format=csv&both_to=true&shortenfields=true&ports=A0180923
Incomings into Boulogne sur Mer (A0152606) and Bordeaux (A0180923)
- http://data.portic.fr:80/api/flows/?format=json&ports=A0180923,A0152606&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
Exits Boulogne sur Mer (A0152606) and Bordeaux (A0180923)
- http://data.portic.fr:80/api/flows/?format=json&ports=A0180923,A0152606&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
Sailing around Bordeaux (A0180923)
- http://data.portic.fr/api/flows/?format=json&ports=A0180923&direction=In-out&params=id,departure,destination&shortenfields=true
```JSON
[{"t01":"0008663N- 05","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0009557N- 01","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0010656N- 01","t04":"Bordeaux","t19":"Bordeaux"}]
```
All passing by Bordeaux (A0180923) and Boulogne sur Mer (A0152606)
- http://data.portic.fr:80/api/flows/?format=json&ports=A0180923,A0152606&direction=No&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
All incomings into Boulogne sur Mer (A0152606) without previous steps
- http://data.portic.fr:80/api/flows/?format=json&ports=A0152606&degree=0&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
All outgoings from Bordeaux (A0180923) and Boulogne sur Mer (A0152606) with the next step
- http://data.portic.fr:80/api/flows/?format=json&ports=A0180923,A0152606&degree=1&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
Donnerait les trajets passant par le port en question, et toutes les escales.
sans doublons par défaut
avec doublons si both-to=true
All ships having sailing around Bordeaux (A0180923) with the two next steps
- http://data.portic.fr:80/api/flows/?format=json&ports=A0180923&degree=2&direction=In-out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
- link_to_port : **null** | identifiant du port UHGS_id dans la base de données sur lequel on filtre les données si précisé
paramètres :
- ports : identifiants des ports (UHGS_id) dans la base de données sur lesquels on filtre les données, séparés par des virgules (,)
- direction : In | Out | In-out | **No**
- degree : **0**, 1 ou plus (si 0 : tous les flux passant par le port précisé, avec la liste des escales précédentes et suivantes)
Tous les flux passant par ces ports,
- au degré spécifié (0 : toutes les escales sur les trajets passant par ces ports, 1 seulement les ports précédents et/ou suivants sur les trajets, 2 : jusqu'à 2 escales avant ou après les ports, etc.),
- et dans la direction spécifiée (In : les arrivées et les escales précédentes, Out : les départs et les escales suivantes, In-out : uniquement les trajectoires ayant navigué en aller-retour autour des mêmes ports, No : toutes directions confondues)
#### 1.2.4.5. /travels?link_to_port="UHGS_id du port"&degree=0&both-to=true
TODO not done yet
Donnerait les trajets entrants ou partants du port en question,
sans doublons, seulement les escales directement précédentes ou suivantes
sans doublons par défaut
avec doublons si both-to=true
- link_to_port : **null** | identifiant du port UHGS_id dans la base de données sur lequel on filtre les données si précisé
......@@ -298,7 +298,7 @@ def getDeparturesDetails():
lat = float(request.args.get("lat"))
lon = float(request.args.get("lon"))
radius = int(request.args.get("radius"))
except ValueError:
except :
#We go on, silenciously, by selecting all data
pass
......@@ -336,7 +336,7 @@ def getDeparturesAgg():
lat = float(request.args.get("lat"))
lon = float(request.args.get("lon"))
radius = int(request.args.get("radius"))
except ValueError:
except :
#We go on, silenciously, by selecting all data
pass
#df2 = calcul_isInside(lat, lon, radius).departure.value_counts().reset_index()
......@@ -378,7 +378,7 @@ def getDestinationsAgg():
lat = float(request.args.get("lat"))
lon = float(request.args.get("lon"))
radius = int(request.args.get("radius"))
except ValueError:
except :
#We go on, silenciously, by selecting all data
pass
......@@ -402,7 +402,151 @@ def getDestinationsAgg():
return formatOutput(df3, 'travels')
@app.route('/api/flows/')
def getFlows():
"""
Return the flows as specified in API : a list of travels linked to the specified ports, either by entering in
(direction In), either by existing from (direction Out), either by having sailing around (direction In-out),
or for any direction (default, or specify direction No).
- ports : ports identifiers (UHGS_id) for which travels are filtered, comma separated(,)
- direction : In | Out | In-out | **No**
- degree : **0**, 1 or more
(when 0 : all flows going through out the list of cited ports, with also the list of previous and next pointcalls)
Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL),
but with a filter by default : only source_entry = from and both-from, to avoid duplicates
- params : **all** | tableau des noms longs des attributs de l'API à renvoyer
params=pointcall,pointcall_uhgs_id for instance
- both_to : true | **false**
http://localhost:80/api/flows/?format=csv&both_to=false
http://localhost:80/api/flows/?format=csv&both_to=true&shortenfields=true
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost/api/flows/?format=json&ports=A0180923&direction=In-out&params=id,departure,destination&shortenfields=true
[{"t01":"0008663N- 05","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0009557N- 01","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0010656N- 01","t04":"Bordeaux","t19":"Bordeaux"}]
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=No&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost:80/api/flows/?format=json&ports=A0152606&degree=0&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&degree=1&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost:80/api/flows/?format=json&ports=A0180923&degree=2&direction=In-out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
"""
import pandas as pd
#filename = os.path.join(APP_DATA, 'travels_API_11mai2020.csv')
#dfcsv = pd.read_csv(filename, sep = ';')
attributes = '*'
filter_clause = 'true'
## Filter the result according requested params
params = request.args.get("params")
if (params is not None and len(params)>0) :
#print('selecting some columns')
fields = readFieldnames('travels')
keepparams = str(params).split(',')
#https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike
labels = []
for k in keepparams:
if k in fields['name'].tolist() :
labels.append(k)
attributes = ",".join(labels)
query = 'select '+attributes+' from navigoviz.built_travels'
# Filter to remove duplicates (by default) - if both_to is given, then do not filter and travels will contain duplicates
getduplicates = request.args.get("both_to")
if getduplicates is None or getduplicates != 'true' :
#Filter travels
#print('filtering duplicates out of travels')
#dfcsv = dfcsv[dfcsv['source_entry']!='both-to']
filter_clause = " source_entry <> 'both-to' "
query = 'select '+attributes+' from navigoviz.built_travels '+filter_clause
## Filter the result according requested ports
filter_clauseIN = filter_clause
filter_clauseOUT = filter_clause
filter_clauseINOUT = filter_clause
ports = request.args.get("ports")
if (ports is not None and len(ports)>0) :
port_list = str(ports).split(',')
port_list = "','".join(port_list)
print(port_list)
filter_clauseIN = filter_clauseIN+""" and destination_uhgs_id in ('%s') """% (port_list)
filter_clauseOUT = filter_clauseOUT+""" and departure_uhgs_id in ('%s')"""% (port_list)
filter_clauseINOUT = filter_clauseINOUT+"""
and (destination_uhgs_id in ('%s') OR departure_uhgs_id in ('%s'))"""% (port_list,port_list)
## Filter the result according the requested degree
degree = 0
filter_degree_in = "travel_rank >= k.subject_order"
filter_degree_out = "travel_rank <= k.subject_order"
try:
degree = int(request.args.get("degree"))
except :
#We go on, silenciously, by selecting all data
pass
if (degree > 0) :
filter_degree_in = filter_degree_in +"-"+ str(degree-1)
filter_degree_out = filter_degree_out +"+"+ str(degree-1)
## Filter the result according requested direction
direction = request.args.get("direction")
if (direction is not None and len(direction)>0 and direction != 'No') :
if (direction == 'In') :
query = """select distinct """+attributes+"""
from navigoviz.built_travels,
( select ship_id as subject, travel_rank as subject_order
from navigoviz.built_travels
where """+filter_clauseIN+"""
and distance_dep_dest > 0
) as k
where ship_id = k.subject and """+filter_clause+"""
and travel_rank <= k.subject_order and """+filter_degree_in+"""
"""
if (direction == 'Out') :
query = """select distinct """+attributes+"""
from navigoviz.built_travels,
( select ship_id as subject, travel_rank as subject_order
from navigoviz.built_travels
where """+filter_clauseOUT+"""
and distance_dep_dest > 0
) as k
where ship_id = k.subject and """+filter_clause+"""
and travel_rank >= k.subject_order and """+filter_degree_out+"""
"""
if (direction == 'In-out') :
query = """select distinct """+attributes+"""
from navigoviz.built_travels,
( select ship_id as subject, travel_rank as subject_order
from navigoviz.built_travels
where """+filter_clauseINOUT+"""
and distance_dep_dest = 0
and (departure_action like 'Sailing around' or departure_action like 'In-out' )
) as k
where ship_id = k.subject and """+filter_clause+"""
and """+filter_degree_in+""" and """+filter_degree_out+"""
"""
else :
# Take all, whatever the direction, but restrict according the degree
query = """select distinct """+attributes+"""
from navigoviz.built_travels,
( select ship_id as subject, travel_rank as subject_order
from navigoviz.built_travels
where """+filter_clauseINOUT+"""
) as k
where ship_id = k.subject and """+filter_clause+"""
and """+filter_degree_in+""" and """+filter_degree_out
print(query)
dfcsv = retrieveDataFromPostgres(query)
return formatOutput(dfcsv, 'travels')
if __name__ == '__main__':
app.run(debug=True,port=port,threaded=True)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment