Auto load Wikidata items

This commit is contained in:
Edward Betts 2021-06-17 19:14:42 +02:00
parent 0c98ca921c
commit f0cb54bf95
2 changed files with 34 additions and 0 deletions

View file

@ -531,6 +531,7 @@ export default {
var lon = parseFloat(hit.lon).toFixed(5); var lon = parseFloat(hit.lon).toFixed(5);
this.map.setView([lat, lon], 16); this.map.setView([lat, lon], 16);
this.auto_load();
}, },
process_wikidata_items(load_items) { process_wikidata_items(load_items) {
@ -625,6 +626,19 @@ export default {
this.osm_loading = false; this.osm_loading = false;
this.check_for_missing(); this.check_for_missing();
this.hits = [];
});
},
auto_load() {
var count_url = this.api_base_url + "/api/1/count";
var bounds = this.map.getBounds();
var params = { bounds: bounds.toBBoxString() };
axios.get(count_url, { params: params }).then((response) => {
var count = response.data.count;
if (count < 1000) {
this.load_wikidata_items();
}
}); });
}, },
run_search() { run_search() {
@ -744,6 +758,8 @@ export default {
this.detail_qid = this.qid_from_url(); this.detail_qid = this.qid_from_url();
if (this.detail_qid) { if (this.detail_qid) {
this.load_wikidata_items(); this.load_wikidata_items();
} else {
this.auto_load();
} }
}); });

View file

@ -409,6 +409,24 @@ def get_bbox_centroid(bbox):
return lat, lon return lat, lon
@app.route("/api/1/count")
def api_wikidata_items_count():
t0 = time()
bounds = request.args.get("bounds")
db_bbox = make_envelope(bounds)
q = (
model.Item.query.join(model.ItemLocation)
.filter(func.ST_Covers(db_bbox, model.ItemLocation.location))
)
t1 = time() - t0
response = jsonify(success=True, count=q.count(), duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/items") @app.route("/api/1/items")
def api_wikidata_items(): def api_wikidata_items():
bounds = request.args.get("bounds") bounds = request.args.get("bounds")