Skip to content
Snippets Groups Projects
Commit b93e55d5 authored by Simon Will's avatar Simon Will
Browse files

Create script with which qtypes can be incorporated in Overpass queries

parent a16f2e82
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import argparse
import json
from nlmaps_tools.parse_mrl import MrlGrammar, Symbol
from nlmaps_tools.answer_mrl import (
add_name_tags, canonicalize_nwr_features,
ENV as overpass_ql_env, transform_features
)
def main(nl_file, mrl_file, out_file):
grammar = MrlGrammar()
records = []
with open(nl_file) as nlf, open(mrl_file) as mrlf:
for nl_line, mrl_line in zip(nlf, mrlf):
nl = nl_line.strip()
mrl = mrl_line.strip()
if mrl:
if mrl.startswith('dist'):
continue
try:
parse_result = grammar.parseMrl(mrl, is_escaped=False)
except:
continue
feats = parse_result['features']
feats = transform_features(feats, add_name_tags)
feats = transform_features(feats, canonicalize_nwr_features)
template_name = feats['query_type'] + '.jinja2'
template = overpass_ql_env.get_template(template_name)
ql = template.render(features=feats, Symbol=Symbol)
records.append({
'nl': nl,
'mrl': mrl,
'ql': ql
})
with open(out_file, 'w') as f:
json.dump(records, f)
def parse_args():
parser =argparse.ArgumentParser()
parser.add_argument('nl_file')
parser.add_argument('mrl_file')
parser.add_argument('out_file')
return parser.parse_args()
if __name__ == '__main__':
ARGS = parse_args()
main(**vars(ARGS))
{% import "macros.jinja2" as M %}
{% if features['qtype'][0][0] == 'findkey' %}
[out:csv(::type,::id,"{{ features['qtype'][0][1] }}")];
{% endif %}
{% if features['area'] %}
{{ M.area(features) }}
{{ M.nwr(features['center_nwr'], result_set='.center') }}
......@@ -6,7 +9,8 @@
{{ M.nwr(features['center_nwr'], area=none, result_set='.center') }}
{% endif %}
{{ M.nwr(features['target_nwr'], area='around.center:' + features['maxdist']|dist_lookup) }}
.center out geom;
make separator "name" = "separator" -> .sep;
.sep out;
{% if features['qtype'][0] == Symbol('count') %}
out count;
{% else %}
out geom;
{% endif %}
{% import "macros.jinja2" as M %}
{% if features['qtype'][0][0] == 'findkey' %}
[out:csv(::type,::id,"{{ features['qtype'][0][1] }}")];
{% endif %}
{% if features['area'] %}
{{ M.area(features) }}
{{ M.nwr(features['target_nwr']) }}
{% else %}
{{ M.nwr(features['target_nwr'], area=none) }}
{% endif %}
{% if features['qtype'][0] == Symbol('count') %}
out count;
{% else %}
out geom;
{% endif %}
......@@ -51,9 +51,5 @@ nwr{##}
{% endmacro %}
{% macro area(features, area_var='.a') %}
{% if features['area_id'] %}
area({{ features['area_id'] }}) -> {{ area_var }};
{% else %}
area["name"="{{ features['area']|esc }}"] -> {{ area_var }};
{% endif %}
{{ '{{' }}geocodeArea:{{ features['area']|esc }}{{ '}}' }} -> {{ area_var }};
{% endmacro %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment