question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Split Nearby into a fast query for coordinates + a details query for each pin

See original GitHub issue

Nearby is too slow to use in Paris:

  1. Be in the center of Paris (or use a fake GPS app to pretend)
  2. Open Nearby
  3. Spinning forever, never finishes

I suggest splitting the current big SPARQL requests into:

  • a first simple request that only returns QID,coordinates pairs.
  • for each pin, an asynchronous request that loads the place’s details: name, description, class, existence, etc.

I did some tests (both with 1km radius):

Due to radius stepping, the request that fetches coordinates is often performed several times, making it even more important to fine-tune. Fetching details afterwards means that some pins will disappear when Exists is enabled, but I don’t think it is problematic as it is disabled by default, users who willingly enabled it will understand why the pins disappear.

Latest master.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:18 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolas-raoulcommented, Aug 23, 2021

Sure, here is a query that gives everything, coordinates and details and whether it is a WLM monument:

SELECT
  ?item
  (SAMPLE(?label) AS ?label)
  (SAMPLE(?description) AS ?description)
  (SAMPLE(?class) AS ?class) # TODO Is it really used?
  (SAMPLE(?classLabel) AS ?classLabel)
  (SAMPLE(?pic) AS ?pic)
  (SAMPLE(?destroyed) AS ?destroyed)
  (SAMPLE(?endTime) AS ?endTime)
  (SAMPLE(?wikipediaArticle) AS ?wikipediaArticle)
  (SAMPLE(?commonsArticle) AS ?commonsArticle)
  (SAMPLE(?commonsCategory) AS ?commonsCategory)
  (SAMPLE(?monument) AS ?monument)
WHERE {
  # Around given location
  SERVICE wikibase:around {
    ?item wdt:P625 ?location.
    bd:serviceParam wikibase:center "Point(4.89 52.37)"^^geo:wktLiteral. # Longitude latitude
    bd:serviceParam wikibase:radius "0.1". # Radius in kilometers.
  }

  # Get the label in the preferred language of the user, or any other language if no label is available in that language.
  OPTIONAL {?item rdfs:label ?itemLabelPreferredLanguage. FILTER (lang(?itemLabelPreferredLanguage) = "en")}
  OPTIONAL {?item rdfs:label ?itemLabelAnyLanguage}
  BIND(COALESCE(?itemLabelPreferredLanguage, ?itemLabelAnyLanguage, "?") as ?label)

  # Get the description in the preferred language of the user, or any other language if no description is available in that language.
  OPTIONAL {?item schema:description ?itemDescriptionPreferredLanguage. FILTER (lang(?itemDescriptionPreferredLanguage) = "en")}
  OPTIONAL {?item schema:description ?itemDescriptionAnyLanguage}
  BIND(COALESCE(?itemDescriptionPreferredLanguage, ?itemDescriptionAnyLanguage, "?") as ?description)

  # Get the class label in the preferred language of the user, or any other language if no label is available in that language.
  OPTIONAL {
  ?item p:P31/ps:P31 ?class.
    OPTIONAL {?class rdfs:label ?classLabelPreferredLanguage. FILTER (lang(?classLabelPreferredLanguage) = "en")}
    OPTIONAL {?class rdfs:label ?classLabelAnyLanguage}
    BIND(COALESCE(?classLabelPreferredLanguage, ?classLabelAnyLanguage, "?") as ?classLabel)
  }

  # Get picture
  OPTIONAL {?item wdt:P18 ?pic}

  # Get existence
  OPTIONAL {?item wdt:P576 ?destroyed}
  OPTIONAL {?item wdt:P582 ?endTime}

  # Get Commons category
  OPTIONAL {?item wdt:P373 ?commonsCategory}

  # Get Wikipedia article
  OPTIONAL {
    ?wikipediaArticle schema:about ?item.
    ?wikipediaArticle schema:isPartOf <https://en.wikipedia.org/>. # TODO internationalization
  }

  # Get Commons article
  OPTIONAL {
    ?commonsArticle schema:about ?item.
    ?commonsArticle schema:isPartOf <https://commons.wikimedia.org/>.
  }

  # Wiki Loves Monuments
  OPTIONAL {?item p:P1435 ?monument}
  OPTIONAL {?item p:P2186 ?monument}
  OPTIONAL {?item p:P1459 ?monument}
  OPTIONAL {?item p:P1460 ?monument}
  OPTIONAL {?item p:P1216 ?monument}
  OPTIONAL {?item p:P709 ?monument}
  OPTIONAL {?item p:P718 ?monument}
  OPTIONAL {?item p:P5694 ?monument}
}
GROUP BY ?item

Obviously it times out in crowded places. The example above took 30 seconds even with a tiny radius of 0.1.

1reaction
misaochancommented, Aug 23, 2021

Thanks for helping with the query @nicolas-raoul ! Pinging @ashishkumar468 instead, I don’t think the other username works. 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql - Find nearest latitude/longitude with an SQL query
It calculates the distance based on the latitude/longitude of that row and the target latitude/longitude, and then asks for only rows where the...
Read more >
Nearby Search | Places API - Google Developers
Returns only those places that are open for business at the time the query is sent. Places that do not specify opening hours...
Read more >
Calculating Distance Between 2 Locations of Latitude ...
Calculate or Query Great Circle Distance Between Points of Latitude and Longitude Using The Haversine Formula (PHP, JavaScript, Java, Python, ...
Read more >
Geo-distance query | Elasticsearch Guide [8.5] | Elastic
How to compute the distance. Can either be arc (default), or plane (faster, but inaccurate on long distances and close to the poles)....
Read more >
Searching for a close numeric match on spatial coordinates
The smart and fast solution for this class of problems is an index-backed "nearest neighbor" search. For the record: if you want precise ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found