Skip to content

Commit

Permalink
fix flask handlers and redirects
Browse files Browse the repository at this point in the history
fix #156
  • Loading branch information
TomPohys committed Jan 21, 2020
1 parent 7586985 commit 951bae7
Showing 1 changed file with 69 additions and 18 deletions.
87 changes: 69 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/<int:id>')
@app.route('/<int:id>-<int:code>') # :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('/<id>') # :re:[\d]+(-[\d]+)?
def index2(id):
ref = osr.SpatialReference()
ix = open_dir(INDEX, readonly=True)
url_social = id
Expand Down Expand Up @@ -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('/<id>') # :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('/<int:id>-<string:code>') # :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:
Expand Down Expand Up @@ -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('/<id>.?gml') # :re:[\d]+(-[a-zA-Z]+)><format:re:[\.]+[gml]+
def index4(id, format='gml'):
# Handler for other codes (not crs and trans) for return qml from sqlite
@app.route('/<int:id>-<string:code>.<string:format>') # :re:[\d]+(-[a-zA-Z]+)><format:re:[\.]+[gml]+
def index4(id, code, format):
id = str(id) + '-' + code

code, text = (id+'-0').split('-')[:2]
text = text.replace("/","").replace(".","")
code = code.replace(".","")
Expand All @@ -1152,7 +1166,7 @@ def index4(id, format='gml'):
elif text.startswith("cs"):urn = "urn:ogc:def:cs:EPSG::"+str(code)
elif text.startswith("primem"):urn = "urn:ogc:def:meridian:EPSG::"+str(code)

if urn != "" and format == ".gml":
if urn != "" and format == "gml":
cur.execute('SELECT id,xml FROM gml where urn = ?', (urn,))
gml = cur.fetchall()
for id,xml in gml:
Expand All @@ -1165,8 +1179,17 @@ def index4(id, format='gml'):
return make_response(xml)

# Returns definition of projection as text or xml file
@app.route('/<id>.<format>') # :re:[\d]+(-[\d]+)?><format:re:[\/\.]+[\w]+
def crs_text(id, format):
# Returns variable formats for crs with or without transformations
@app.route('/<int:id>.<string:format>')
@app.route('/<int:id>/<string:format>')
@app.route('/<int:id>-<int:code>.<string:format>') # :re:[\d]+(-[\d]+)?><format:re:[\/\.]+[\w]+
@app.route('/<int:id>-<int:code>/<string:format>')
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 = ""
Expand Down Expand Up @@ -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('/<id>.<format>') # /<id:re:(urn:?_?ogc:?_?def:?_?([\w]+-?[\w]+):?_?[EPSG]+:?:?_?_?(\d+\.?\d+(\.\d)?))><format:re:(\.gml)?>
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<string:id>')
@app.route('/urn<string:id>.<string:format>') # /<id:re:(urn:?_?ogc:?_?def:?_?([\w]+-?[\w]+):?_?[EPSG]+:?:?_?_?(\d+\.?\d+(\.\d)?))><format:re:(\.gml)?>
def index6(id,format=''):
id = "urn"+id

# if i want gml
if format == "gml":
cur.execute('SELECT id,xml FROM gml where urn = ?', (id,))
Expand Down Expand Up @@ -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<id>') # /<id:re:(\w+-\w+-\d+((\.\d+)?(\.?\d+)?)?)><format:re:((\.)?gml)?>
@app.route('/epsg<id>')
@app.route('/iogp<id>')
def index7(id):

# the same logic as for sqlite urn's
@app.route('/<id>.<format>') # /<id:re:(\w+-\w+-\d+((\.\d+)?(\.?\d+)?)?)><format:re:((\.)?gml)?>
def index7(id,format):
#name_map={}
name_map={
'cr':"Change request",
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 951bae7

Please sign in to comment.