Encoding URIs, filter by ranks

This commit is contained in:
Ilya Zverev 2016-02-10 13:37:06 +03:00
parent 8c9e9f19b3
commit c4f41e4ca2
3 changed files with 50 additions and 42 deletions

View file

@ -121,15 +121,17 @@ def query_crossing():
xmax = request.args.get('xmax')
ymin = request.args.get('ymin')
ymax = request.args.get('ymax')
region = request.args.get('region')
region = request.args.get('region').encode('utf-8')
points = request.args.get('points') == '1'
rank = request.args.get('rank') or '4'
cur = g.conn.cursor()
sql = """SELECT id, ST_AsGeoJSON({line}, 7) as geometry, region, processed FROM {table}
WHERE line && ST_MakeBox2D(ST_Point(%s, %s), ST_Point(%s, %s)) and processed = 0 {reg};
WHERE line && ST_MakeBox2D(ST_Point(%s, %s), ST_Point(%s, %s)) and processed = 0 {reg} and rank <= %s;
""".format(table=config.CROSSING_TABLE, reg='and region = %s' if region else '', line='line' if not points else 'ST_Centroid(line)')
params = [xmin, ymin, xmax, ymax]
if region:
params.append(region)
params.append(rank)
cur.execute(sql, tuple(params))
result = []
for rec in cur:
@ -174,7 +176,7 @@ def check_osm_table():
@app.route('/search')
def search():
query = request.args.get('q')
query = request.args.get('q').encode('utf-8')
cur = g.conn.cursor()
cur.execute('select ST_XMin(geom), ST_YMin(geom), ST_XMax(geom), ST_YMax(geom) from borders where name like %s limit 1', ('%{0}%'.format(query),))
if cur.rowcount > 0:
@ -186,7 +188,7 @@ def search():
def split():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
line = request.args.get('line')
cur = g.conn.cursor()
# check that we're splitting a single polygon
@ -224,8 +226,8 @@ def split():
def join_borders():
if config.READONLY:
abort(405)
name = request.args.get('name')
name2 = request.args.get('name2')
name = request.args.get('name').encode('utf-8')
name2 = request.args.get('name2').encode('utf-8')
cur = g.conn.cursor()
cur.execute('update {table} set geom = ST_Union(geom, b2.g), count_k = -1 from (select geom as g from {table} where name = %s) as b2 where name = %s;'.format(table=config.TABLE), (name2, name))
cur.execute('delete from {} where name = %s;'.format(config.TABLE), (name2,))
@ -249,7 +251,7 @@ def copy_from_osm():
if config.READONLY:
abort(405)
osm_id = request.args.get('id')
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('insert into {table} (geom, name, modified, count_k) select o.way as way, {name}, now(), -1 from {osm} o where o.osm_id = %s limit 1;'.format(table=config.TABLE, osm=config.OSM_TABLE, name='%s' if name != '' else '%s || o.name'), (name, osm_id))
g.conn.commit()
@ -259,8 +261,8 @@ def copy_from_osm():
def set_name():
if config.READONLY:
abort(405)
name = request.args.get('name')
new_name = request.args.get('newname')
name = request.args.get('name').encode('utf-8')
new_name = request.args.get('newname').encode('utf-8')
cur = g.conn.cursor()
cur.execute('update {} set name = %s where name = %s;'.format(config.TABLE), (new_name, name))
g.conn.commit()
@ -270,7 +272,7 @@ def set_name():
def delete_border():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('delete from {} where name = %s;'.format(config.TABLE), (name,))
g.conn.commit()
@ -280,7 +282,7 @@ def delete_border():
def disable_border():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('update {} set disabled = true where name = %s;'.format(config.TABLE), (name,))
g.conn.commit()
@ -290,7 +292,7 @@ def disable_border():
def enable_border():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('update {} set disabled = false where name = %s;'.format(config.TABLE), (name,))
g.conn.commit()
@ -298,8 +300,8 @@ def enable_border():
@app.route('/comment', methods=['POST'])
def update_comment():
name = request.form['name']
comment = request.form['comment']
name = request.form['name'].encode('utf-8')
comment = request.form['comment'].encode('utf-8')
cur = g.conn.cursor()
cur.execute('update {} set cmnt = %s where name = %s;'.format(config.TABLE), (comment, name))
g.conn.commit()
@ -307,7 +309,7 @@ def update_comment():
@app.route('/divpreview')
def divide_preview():
like = request.args.get('like')
like = request.args.get('like').encode('utf-8')
query = request.args.get('query')
cur = g.conn.cursor()
cur.execute('select name, ST_AsGeoJSON(ST_Simplify(way, 0.01)) as way from {table}, (select way as pway from {table} where name like %s) r where ST_Contains(r.pway, way) and {query};'.format(table=config.OSM_TABLE, query=query), (like,))
@ -321,10 +323,10 @@ def divide_preview():
def divide():
if config.READONLY:
abort(405)
name = request.args.get('name')
like = request.args.get('like')
name = request.args.get('name').encode('utf-8')
like = request.args.get('like').encode('utf-8')
query = request.args.get('query')
prefix = request.args.get('prefix')
prefix = request.args.get('prefix').encode('utf-8')
if prefix != '':
prefix = '{}_'.format(prefix);
cur = g.conn.cursor()
@ -343,7 +345,7 @@ def divide():
def chop_largest_or_farthest():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('select ST_NumGeometries(geom) from {} where name = %s;'.format(config.TABLE), (name,))
res = cur.fetchone()
@ -367,7 +369,7 @@ def chop_largest_or_farthest():
def draw_hull():
if config.READONLY:
abort(405)
name = request.args.get('name')
name = request.args.get('name').encode('utf-8')
cur = g.conn.cursor()
cur.execute('select ST_NumGeometries(geom) from {} where name = %s;'.format(config.TABLE), (name,))
res = cur.fetchone()
@ -382,7 +384,7 @@ def fix_crossing():
if config.READONLY:
abort(405)
preview = request.args.get('preview') == '1'
region = request.args.get('region')
region = request.args.get('region').encode('utf-8')
if region is None:
return jsonify(status='Please specify a region')
ids = request.args.get('ids')

View file

@ -8,6 +8,7 @@ var IMPORT_ENABLED = false;
var map, borders = {}, bordersLayer, selectedId, editing = false, readonly = false;
var size_good = 5, size_bad = 50;
var maxRank = 1;
var tooSmallLayer = null;
var oldBordersLayer = null;
var routingGroup = null;
@ -49,6 +50,10 @@ function init() {
if( e.keyCode == 13 )
$('#b_search').click();
});
$('#b_comment').keyup(function(e) {
if( e.keyCode == 13 )
$('#b_comment_send').click();
});
checkHasOSM();
filterSelect(true);
}
@ -119,7 +124,8 @@ function updateBorders() {
'xmax': b.getEast(),
'ymin': b.getSouth(),
'ymax': b.getNorth(),
'points': (map.getZoom() < 10 ? 1 : 0)
'points': (map.getZoom() < 10 ? 1 : 0),
'rank': maxRank
},
success: processCrossing,
dataType: 'json'
@ -336,7 +342,7 @@ function doSearch() {
var query = $('#fsearch').val();
if( query.length > 1 ) {
$.ajax(getServer('search'), {
data: { 'q': query },
data: { 'q': encodeURIComponent(query) },
success: zoomToFound
});
}
@ -379,7 +385,7 @@ function importInJOSM(method, data ) {
var url = getServer(method) + '?' + $.param(data);
$.ajax({
url: 'http://127.0.0.1:8111/import',
data: { url: url, new_layer: 'true', format: '.osm' },
data: { url: encodeURIComponent(url), new_layer: 'true', format: '.osm' },
complete: function(t) {
if( t.status != 200 )
window.alert('Please enable remote_control in JOSM');
@ -437,7 +443,7 @@ function bRename() {
return;
$('#rename').css('display', 'none');
$.ajax(getServer('rename'), {
data: { 'name': selectedId, 'newname': $('#b_rename').val() },
data: { 'name': encodeURIComponent(selectedId), 'newname': encodeURIComponent($('#b_rename').val()) },
success: updateBorders
});
}
@ -446,7 +452,7 @@ function bDisable() {
if( !selectedId || !(selectedId in borders) )
return;
$.ajax(getServer(borders[selectedId].disabled ? 'enable' : 'disable'), {
data: { 'name': selectedId },
data: { 'name': encodeURIComponent(selectedId) },
success: updateBorders
});
}
@ -457,7 +463,7 @@ function bDelete() {
if( !window.confirm('Точно удалить регион ' + selectedId + '?') )
return;
$.ajax(getServer('delete'), {
data: { 'name': selectedId },
data: { 'name': encodeURIComponent(selectedId) },
success: updateBorders
});
}
@ -466,7 +472,7 @@ function sendComment( text ) {
if( !selectedId || !(selectedId in borders) )
return;
$.ajax(getServer('comment'), {
data: { 'name': selectedId, 'comment': text },
data: { 'name': encodeURIComponent(selectedId), 'comment': encodeURIComponent(text) },
type: 'POST',
success: updateBorders
});
@ -521,7 +527,7 @@ function bSplitDo() {
wkt += L.Util.formatNum(lls[i].lng, 6) + ' ' + L.Util.formatNum(lls[i].lat, 6);
}
$.ajax(getServer('split'), {
data: { 'name': splitSelected, 'line': 'LINESTRING(' + wkt + ')' },
data: { 'name': encodeURIComponent(splitSelected), 'line': encodeURIComponent('LINESTRING(' + wkt + ')') },
datatype: 'json',
success: function(data) { if( data.status != 'ok' ) alert(data.status); else updateBorders(); }
});
@ -574,7 +580,7 @@ function bJoinSelect(layer) {
function bJoinDo() {
if( joinSelected != null && joinAnother != null ) {
$.ajax(getServer('join'), {
data: { 'name': joinSelected, 'name2': joinAnother },
data: { 'name': encodeURIComponent(joinSelected), 'name2': encodeURIComponent(joinAnother) },
success: updateBorders
});
}
@ -626,7 +632,7 @@ function pPointSelect(id, name1) {
var name = $('#p_name').val();
name = name.replace('*', name1);
$.ajax(getServer('from_osm'), {
data: { 'name': name, 'id': id },
data: { 'name': encodeURIComponent(name), 'id': id },
success: updateBorders
});
bPointCancel();
@ -664,8 +670,8 @@ function bDividePreview() {
$('#d_none').css('display', 'none');
$.ajax(getServer('divpreview'), {
data: {
'like': $('#d_like').val(),
'query': $('#d_where').val()
'like': encodeURIComponent($('#d_like').val()),
'query': encodeURIComponent($('#d_where').val())
},
success: bDivideDrawPreview
});
@ -689,10 +695,10 @@ function bDivideDrawPreview(geojson) {
function bDivideDo() {
$.ajax(getServer('divide'), {
data: {
'name': divSelected,
'prefix': $('#d_prefix').val(),
'like': $('#d_like').val(),
'query': $('#d_where').val()
'name': encodeURIComponent(divSelected),
'prefix': encodeURIComponent($('#d_prefix').val()),
'like': encodeURIComponent($('#d_like').val()),
'query': encodeURIComponent($('#d_where').val())
},
success: updateBorders
});
@ -713,7 +719,7 @@ function bLargest() {
if( !selectedId || !(selectedId in borders) )
return;
$.ajax(getServer('chop1'), {
data: { 'name': selectedId },
data: { 'name': encodeURIComponent(selectedId) },
success: updateBorders
});
}
@ -722,7 +728,7 @@ function bHull() {
if( !selectedId || !(selectedId in borders) )
return;
$.ajax(getServer('hull'), {
data: { 'name': selectedId },
data: { 'name': encodeURIComponent(selectedId) },
success: updateBorders
});
}
@ -884,7 +890,7 @@ function bFixCrossPreview() {
$.ajax(getServer('fixcrossing'), {
data: {
'preview': 1,
'region': crossSelected,
'region': encodeURIComponent(crossSelected),
'ids': Object.keys(selectedCrossings).join(',')
},
success: bFixCrossDrawPreview
@ -907,7 +913,7 @@ function bFixCrossDrawPreview(geojson) {
function bFixCrossDo() {
$.ajax(getServer('fixcrossing'), {
data: {
'region': crossSelected,
'region': encodeURIComponent(crossSelected),
'ids': Object.keys(selectedCrossings).join(',')
},
success: updateBorders

View file

@ -108,7 +108,7 @@
<b>Статус:</b> <span id="b_status"></span><br>
<b>Комментарий:</b><br>
<textarea style="width: 240px; height: 200px;" id="b_comment"></textarea><br>
<button onclick="bComment()">Отправить</button>
<button id="b_comment_send" onclick="bComment()">Отправить</button>
<a href="#" onclick="bClearComment(); return false;" id="b_clear">Очистить</a>
</div>
</div>