Commit a2041f5c authored by Christine Plumejeaud's avatar Christine Plumejeaud
Browse files

Extension of the API to deliver listing of port features.

parent c900fb3b
......@@ -13,6 +13,7 @@
- [1.2.4.2. /agg/departures?lon=lat=radius=](#1242-aggdepartureslonlatradius)
- [1.2.4.3. /agg/destinations?lon=lat=radius=](#1243-aggdestinationslonlatradius)
- [1.2.5. /flows?](#125-flows)
- [1.2.6. /ports?](#126-ports)
......@@ -238,7 +239,19 @@ Return the flows as specified in API : a list of travels linked to the specifie
- 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.6. /ports?
Export list of ports_points (in 900013 projection or what is specified by user) in json format,
with all required attributes for visualisations (selection of parameters is not possible for the moment)
List of attributes :
ogc_fid, uhgs_id, total, toponym, belonging_states, status, geonameid, admiralty, province, shiparea , point
User can get a description of the attributes by using /api/fieldnames?api=ports
Default srid is 900913
You get another by specifying a srid param
Will be extracted from postgres, schema ports, table port_points (see navigocorpus/ETL)
Tested alone in a browser:
- http://data.portic.fr/api/ports?srid=4326
- http://data.portic.fr/api/ports?
and by using explorex.portic.fr application (code alphaportic for visualisation) as client : it works
......@@ -37,7 +37,7 @@ CORS(app)
#port = app.config['PORT']
port = '80'
postgresport = '5433'
database = 'portic_v4'
def retrieveDataFromPostgres(query) :
"""
......@@ -47,7 +47,7 @@ def retrieveDataFromPostgres(query) :
import pandas.io.sql as psql
import pandas as pd
#connection = pg.connect("host='134.158.33.179' port='5433' dbname='portic_v3' user='api_user' password='portic'")
connection = pg.connect("""host='localhost' port='%s' dbname='portic_v3' user='api_user' password='portic'"""% postgresport)
connection = pg.connect("""host='localhost' port='%s' dbname='%s' user='api_user' password='portic'"""% (postgresport, database))
df = pd.read_sql_query(query,con=connection)
connection.close()
......@@ -116,7 +116,7 @@ def formatOutput(dfcsv, api='travels'):
else:
return formatJSON(dfcsv)
def readFieldnames(api) :
def readFieldnames(api, schema='navigoviz') :
"""
Internal method
We read the information schema to be sure to be conform to real living database
......@@ -133,6 +133,9 @@ def readFieldnames(api) :
table_name = 'built_travels'
if api == 'pointcalls' :
table_name = 'pointcall'
if api == 'ports' :
table_name = 'port_points'
schema = 'ports'
#API;name;shortname;type;description
query = """SELECT case when c.table_name= 'built_travels' then 'travels' else 'pointcalls' end as API,
......@@ -142,7 +145,7 @@ def readFieldnames(api) :
FROM information_schema.columns c
left outer join pg_catalog.pg_description pgd on (pgd.objsubid=c.ordinal_position )
left outer join pg_catalog.pg_statio_all_tables st on (pgd.objoid=st.relid and c.table_schema=st.schemaname and c.table_name=st.relname)
where c.table_name in ('%s') and c.table_schema = 'navigoviz' and pgd.objoid = st.relid;"""% (table_name)
where c.table_name in ('%s') and c.table_schema = '%s' and pgd.objoid = st.relid;"""% (table_name, schema)
#print(query)
metadata = retrieveDataFromPostgres(query)
......@@ -169,6 +172,7 @@ def getFieldnames():
http://127.0.0.1/api/fieldnames/?format=csv&shortenfields=true&api=pointcalls
"""
# Filter to keep desired API
api = request.args.get("api")
df = readFieldnames(api)
......@@ -548,5 +552,51 @@ def getFlows():
return formatOutput(dfcsv, 'travels')
@app.route('/api/ports/')
def getPorts():
"""
export list of ports_points (in 900013 projection or what is specified by user) in json format,
with all required attributes for visualisations (selection of parameters is not possible for the moment)
List of attributes :
ogc_fid, uhgs_id, total, toponym, belonging_states, status, geonameid, admiralty, province, shiparea , point
User can get a description of the attributes by using /api/fieldnames?api=ports
Default srid is 900913
You get another by specifying a srid param
Will be extracted from postgres, schema ports, table port_points (see navigocorpus/ETL)
Tested alone : http://localhost/api/ports?srid=4326 ou http://localhost/api/ports?
and by using explorex.portic.fr application (code alphaportic for visualisation)
Test OK on 06 June 2020
"""
# select the srid given by user for geometry transformation
srid = request.args.get("srid")
#print (srid)
if srid is None :
srid = '900913'
else :
#we chek if it is valid by looking in the postgres spatial_ref_sys table : 4326, 3857 for instance
query = """select distinct srid from public.spatial_ref_sys srs"""
srids = retrieveDataFromPostgres(query)
if int(srid) not in srids['srid'].tolist() :
srid = '900913'
print (srid)
query = """SELECT ogc_fid, uhgs_id, total, toponyme as toponym, belonging_states, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom, %s)) as point
FROM ports.port_points p,
(select pointcall_uhgs_id, count( *) as total
from navigoviz.pointcall gg group by pointcall_uhgs_id) as k
where p.toponyme is not null and p.uhgs_id = k.pointcall_uhgs_id""" %(srid)
data = retrieveDataFromPostgres(query)
return formatOutput(data, 'ports')
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