From 951bae7bf7795ba2e74fe572a0b41ce7b05c5884 Mon Sep 17 00:00:00 2001 From: Tom Pohanka Date: Tue, 21 Jan 2020 12:50:02 +0100 Subject: [PATCH] fix flask handlers and redirects fix #156 --- app.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/app.py b/app.py index 84949d3..27bee19 100755 --- a/app.py +++ b/app.py @@ -520,9 +520,16 @@ def index(): return render_template('results.html', selected_kind_index=selected_kind_index, num_deprecated=num_deprecated, show_alt_search=show_alt_search, kind_low=kind_low, num_kind=num_kind, short_code=short_code, title=title, query=query, deprecated=deprecated, num_results=num_results, elapsed=elapsed, facets_list=facets_list, url_facet_statquery=url_facet_statquery, result=result, pagenum=int(pagenum),paging=paging, version=VERSION) +# links for coordinate system and for transformations, e.g. https://epsg.io/5514 and https://epsg.io/5514-1623 +@app.route('/') +@app.route('/-') # :re:[\d]+(-[\d]+)? +def index2(id,code=''): + # reconstruct full id with or without "code" + if code != '': + id = str(id) + '-' + str(code) + else: + id = str(id) -@app.route('/') # :re:[\d]+(-[\d]+)? -def index2(id): ref = osr.SpatialReference() ix = open_dir(INDEX, readonly=True) url_social = id @@ -973,8 +980,12 @@ def index2(id): return render_template('detail.html',greenwich_longitude=greenwich_longitude, url_social=url_social, url_static_map=url_static_map, ogpxml_highlight=ogpxml_highlight, xml_highlight=xml_highlight, area_trans_item=area_trans_item, ogpxml=ogpxml, bbox_coords=bbox_coords, more_gcrs_result=more_gcrs_result, deprecated_available=deprecated_available, url_kind=url_kind, type_epsg=type_epsg, name=name, projcrs_by_gcrs=projcrs_by_gcrs, kind=kind, alt_title=alt_title, area_item=area_item, code_short=code_short, item=item, trans=trans, default_trans=default_trans, num_results=num_results, url_method=url_method, title=title, url_format=url_format, export_html=export_html, url_area_trans=url_area_trans, url_area=url_area, center=center, g_coords=g_coords, trans_lat=trans_lat, trans_lon=trans_lon, wkt=wkt, facets_list=facets_list,url_concatop=url_concatop, nadgrid=nadgrid, detail=detail,export=export, error_code=error_code, version=VERSION) -@app.route('/') # :re:[\d]+(-[a-zA-Z]+) -def index3(id): +# links for NOT crs or trans. e.g. https://epsg.io/9315-datum; https://epsg.io/8901-primem +# https://github.com/maptiler/epsg.io#types-of-urls - other codes with suffix +@app.route('/-') # :re:[\d]+(-[a-zA-Z]+) +def index3(id, code): + # reconstruct full id + id = str(id) + '-' + code url_social = id ix = open_dir(INDEX, readonly=True) with ix.searcher(closereader=False) as searcher: @@ -1135,8 +1146,11 @@ def index3(id): return render_template('detail.html', url_area=url_area, greenwich_longitude=greenwich_longitude, url_social=url_social, url_static_map=url_static_map, url_concatop=url_concatop, ogpxml_highlight=ogpxml_highlight, area_trans_item=area_trans_item, error_code=error_code, ogpxml=ogpxml, bbox_coords=bbox_coords,more_gcrs_result=more_gcrs_result, deprecated_available=deprecated_available, url_kind=url_kind, type_epsg=type_epsg, name=name, projcrs_by_gcrs=projcrs_by_gcrs, alt_title=alt_title, kind=kind, code_short=code_short,item=item, detail=detail, facets_list=facets_list, nadgrid=nadgrid, trans_lat=trans_lat, trans_lon=trans_lon, trans=trans, url_format=url_format, default_trans=default_trans, center=center,g_coords=g_coords,version=VERSION) # XXX this doesn't match - delete? - handled in crs_text -@app.route('/.?gml') # :re:[\d]+(-[a-zA-Z]+)>-.') # :re:[\d]+(-[a-zA-Z]+)>.') # :re:[\d]+(-[\d]+)?>.') +@app.route('//') +@app.route('/-.') # :re:[\d]+(-[\d]+)?>-/') +def crs_text(id, format='', code=''): + if code != '': + id = str(id) + '-' + str(code) + else: + id = str(id) + ix = open_dir(INDEX, readonly=True) result = [] export = "" @@ -1527,9 +1550,14 @@ def error500(error): try_url = "" return render_template('error.html', error=error, try_url=try_url, version=VERSION) -#handling for urn from sqlite -@app.route('/.') # / -def index6(id,format): +#handling for urn from sqlite, with or without format (only gml) +# https://epsg.io/urn:ogc:def:crs:EPSG::5514 +# all in sqlite "urn" +@app.route('/urn') +@app.route('/urn.') # / +def index6(id,format=''): + id = "urn"+id + # if i want gml if format == "gml": cur.execute('SELECT id,xml FROM gml where urn = ?', (id,)) @@ -1616,11 +1644,17 @@ def index6(id,format): #return render_template('error.html',error=error, try_url=try_url) else: - redirect('/%s' % url) + return redirect('/%s' % url) + +# the same logic as for sqlite urn's(all in sqlite "id") +# https://epsg.io/ogp-crs-7414 +# https://epsg.io/epsg-crs-7411 +# https://epsg.io/iogp-crs-5514 +@app.route('/ogp') # / +@app.route('/epsg') +@app.route('/iogp') +def index7(id): -# the same logic as for sqlite urn's -@app.route('/.') # / -def index7(id,format): #name_map={} name_map={ 'cr':"Change request", @@ -1641,6 +1675,22 @@ def index7(id,format): 'deprecation':"Deprecation", 'area':"Area" } + #if in the link is format (only gml), is after dot + try: + id, format = id.split('.') + except: + format = '' + + # find which prefix was catch by handler. If ogp or epsg or iogp (id field in sqlite). + for prefix in ['ogp', 'epsg', 'iogp']: + cur.execute('SELECT count(id) FROM gml where id = ?', (prefix + id,)) + #(1,) + count = cur.fetchone()[0] + #after find match with id and database record, create id and continue (not big number of iogp) + if count: + id = prefix + id + continue + if format =="gml": cur.execute('SELECT id,xml FROM gml where id = ?', (id,)) gml = cur.fetchall() @@ -1655,7 +1705,8 @@ def index7(id,format): try_url= "" return render_template('error.html',error=error, try_url=try_url, version=VERSION) else: - id_reformated = id.replace("ogp-","").replace("epsg-","") + # iogp must be replace before ogp + id_reformated = id.replace("iogp-","").replace("ogp-","").replace("epsg-","") subject,code = id_reformated.split("-") url = code+"-"+subject @@ -1709,7 +1760,7 @@ def index7(id,format): return render_template('detail.html',url_static_map=url_static_map, url_social=url_social, url_concatop=url_concatop, ogpxml_highlight=ogpxml_highlight, area_trans_item=area_trans_item, error_code=error_code, ogpxml=ogpxml, bbox_coords=bbox_coords, more_gcrs_result=more_gcrs_result, deprecated_available=deprecated_available, url_kind=url_kind, type_epsg=type_epsg, name=name, projcrs_by_gcrs=projcrs_by_gcrs, alt_title=alt_title, kind=kind, code_short=code_short, item=item, detail=detail, nadgrid=nadgrid, trans_lat=trans_lat, trans_lon=trans_lon, trans=trans, url_format=url_format, default_trans=default_trans, center=center,g_coords=g_coords, version=VERSION) else: - redirect ('/%s' %url) + return redirect ('/%s' %url) @app.route('/map') def map():