From e48e53b1df0fd6e1c1e58e9dc24908e90e76d5c7 Mon Sep 17 00:00:00 2001 From: Hannah Bast Date: Tue, 24 Sep 2024 20:29:52 +0200 Subject: [PATCH] Rebase to the current master --- src/qlever-petrimaps/GeomCache.cpp | 96 ++++++++++++++---------------- src/qlever-petrimaps/GeomCache.h | 2 +- 2 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/qlever-petrimaps/GeomCache.cpp b/src/qlever-petrimaps/GeomCache.cpp index cd3cc9d..bc4117b 100644 --- a/src/qlever-petrimaps/GeomCache.cpp +++ b/src/qlever-petrimaps/GeomCache.cpp @@ -26,75 +26,67 @@ using util::geo::DPoint; using util::geo::FPoint; using util::geo::latLngToWebMerc; -const static std::string QUERY = - "PREFIX geo: " - "SELECT ?geometry WHERE {" - " ?osm_id geo:hasGeometry ?geometry " - "} INTERNAL SORT BY ?geometry"; - -const static std::string COUNT_QUERY = - "PREFIX geo: " - "SELECT (COUNT(?geometry) as ?count) WHERE { " - " ?osm_id geo:hasGeometry ?geometry " - "}"; - +// Different SPAQRL queries to obtain the WKT geometries from an endpoint. +// It depends on the endpoint which query is used, see `getQuery`. +// +// NOTE: It is important that the order of the geometries is deterministic. +// We use `INTERNAL SORT BY` instead of `ORDER BY` because the former is +// more efficient (and the actual order does not matter). const static std::string QUERY_ASWKT = "PREFIX geo: " "SELECT ?geometry WHERE {" - " ?osm_id geo:hasGeometry ?m . ?m geo:asWKT ?geometry " + " ?subject geo:asWKT ?geometry " "} INTERNAL SORT BY ?geometry"; -const static std::string COUNT_QUERY_ASWKT = - "PREFIX geo: " - "SELECT (COUNT(?geometry) AS ?count) WHERE {" - " ?osm_id geo:hasGeometry ?m . ?m geo:asWKT ?geometry " - "}"; - -const static std::string QUERY_WD = +const static std::string QUERY_WDTP625 = "PREFIX wdt: " - "SELECT ?coord WHERE {" - " ?ob wdt:P625 ?coord" - "} INTERNAL SORT BY ?coord"; + "SELECT ?geometry WHERE {" + " ?subject wdt:P625 ?geometry" + "} INTERNAL SORT BY ?geometry"; -const static std::string COUNT_QUERY_WD = +const static std::string QUERY_WDTP625_SERVICE = "PREFIX wdt: " - "SELECT (COUNT(?coord) as ?count) WHERE { " - " ?ob wdt:P625 ?coord" + "SELECT ?geometry WHERE {" + " SERVICE {" + " SELECT ?geometry WHERE {" + " ?subject wdt:P625 ?geometry" + " } INTERNAL SORT BY ?geometry" + " }" "}"; -// Helper function that returns one of the given three query strings based on -// the `backendUrl`. Used for `getQuery` and `getCountQuery` below. // _____________________________________________________________________________ -static const std::string &selectQueryBasedOnUrl(const std::string &backendUrl, - const std::string &query1, - const std::string &query2, - const std::string &query3) { - // Helper lambda that returns true if the backend (part after the final - // slash) starts with the given prefix. - size_t pos = backendUrl.find_last_of('/'); - pos = pos != std::string::npos ? pos + 1 : 0; - auto backendStartsWith = [&pos, &backendUrl](const std::string &prefix) { - return backendUrl.find(prefix, pos) == pos; +const std::string &GeomCache::getQuery(const std::string &backendUrl) const { + // Helper lambda that returns true if the backend name (the part after the + // final slash) starts with the given prefix. + size_t backendPos = backendUrl.find_last_of('/'); + backendPos = backendPos != std::string::npos ? backendPos + 1 : 0; + auto backendStartsWith = [&backendPos, + &backendUrl](const std::string &prefix) { + return backendUrl.find(prefix, backendPos) == backendPos; }; - if (backendStartsWith("osm")) { - return query1; - } else if (backendStartsWith("wikidata") || backendStartsWith("dblp")) { - return query2; + + // Return query depending on the backend name. + if (backendStartsWith("wikidata") || backendStartsWith("dblp-plus")) { + return QUERY_WDTP625; + } else if (backendStartsWith("dblp")) { + return QUERY_WDTP625_SERVICE; } else { - return query3; + return QUERY_ASWKT; } } // _____________________________________________________________________________ -const std::string &GeomCache::getQuery(const std::string &backendUrl) const { - return selectQueryBasedOnUrl(backendUrl, QUERY_ASWKT, QUERY_WD, QUERY); -} - -// _____________________________________________________________________________ -const std::string &GeomCache::getCountQuery( - const std::string &backendUrl) const { - return selectQueryBasedOnUrl(backendUrl, COUNT_QUERY_ASWKT, COUNT_QUERY_WD, - COUNT_QUERY); +std::string GeomCache::getCountQuery(const std::string &backendUrl) const { + // Modify the query from `getQuery` to count the number of geometries. + std::string query = getQuery(backendUrl); + auto pos = query.find("SELECT"); + if (pos == std::string::npos) { + LOG(ERROR) << "Could not find SELECT in query: " << query; + return "SELECT ?count WHERE { VALUES ?count { 0 } }"; + } + query.insert(pos, "SELECT (COUNT(?geometry) AS ?count) WHERE { "); + query.append(" }"); + return query; } // _____________________________________________________________________________ diff --git a/src/qlever-petrimaps/GeomCache.h b/src/qlever-petrimaps/GeomCache.h index bf44fc7..f679753 100644 --- a/src/qlever-petrimaps/GeomCache.h +++ b/src/qlever-petrimaps/GeomCache.h @@ -118,7 +118,7 @@ class GeomCache { // Get the right SPARQL query for the given backend. const std::string& getQuery(const std::string& backendUrl) const; - const std::string& getCountQuery(const std::string& backendUrl) const; + std::string getCountQuery(const std::string& backendUrl) const; std::string requestIndexHash();