0

I'm using the following query to receive label and description of all Wikidata-entries with geo-location in a geo-bounding box:

SELECT DISTINCT ?place ?placeLabel ?placeDescription ?location WHERE
{
  SERVICE wikibase:box
  {
    ?place wdt:P625 ?location.
    bd:serviceParam wikibase:cornerWest 'Point({0} {1})'^^geo:wktLiteral.
    bd:serviceParam wikibase:cornerEast 'Point({2} {3})'^^ geo:wktLiteral.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language '{4},en,fr,es'. }
  ?place(wdt:P31/wdt:P279*) wd:Q618123.
}

This works fine, but now I would like to also have the link to the wikipedia article included in the result. I tried to modify the query to:

SELECT DISTINCT ?place ?placeLabel ?placeDescription ?location ?article WHERE

but it doesn't work, articles are not returned. Any idea, how this works?

Edit: after all the useful hints from you guys, here is the version, which works fine now:

SELECT DISTINCT ?place ?placeLabel ?placeDescription ?location ?article WHERE
{
  SERVICE wikibase:box
  {
     ?place wdt:P625 ?location.
     bd:serviceParam wikibase:cornerWest 'Point({0} {1})'^^geo:wktLiteral.
     bd:serviceParam wikibase:cornerEast 'Point({2} {3})'^^ geo:wktLiteral.
  }
  SERVICE wikibase:label
  { 
      bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en,fr,es,{4}'. 
      ?place schema:description ?placeDescription ; rdfs:label ?placeLabel. 
  }
  # all places with a location
  ?place(wdt:P31/wdt:P279*) wd:Q618123.

  # include wikipedia links
  ?article schema:about ?place.
  FILTER REGEX(STR(?article), '.wikipedia.org/wiki/').

  # optional, only places containing string in label
  FILTER CONTAINS(lcase(?placeLabel), '{5}').
}

1 Answer 1

1

The query service does not know that your variable ?article means that you want a Wikipedia article. You have to also ask for it in the actual query. One way to do that is to add these lines in the query:

?article schema:about ?place.
FILTER REGEX(STR(?article), ".wikipedia.org/wiki/") .

The first line queries for all the the sitelinks that the item have. The second one filters out those that go to other projects than Wikipedia, for instance Wikimedia Commons. So here is an example that might work for you:

SELECT DISTINCT ?place ?placeLabel ?placeDescription ?location WHERE
{
  SERVICE wikibase:box
  {
    ?place wdt:P625 ?location.
    bd:serviceParam wikibase:cornerWest 'Point({0} {1})'^^geo:wktLiteral.
    bd:serviceParam wikibase:cornerEast 'Point({2} {3})'^^ geo:wktLiteral.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language '{4},en,fr,es'. }
  ?place(wdt:P31/wdt:P279*) wd:Q618123.
  ?article schema:about ?place.
  FILTER REGEX(STR(?article), ".wikipedia.org/wiki/") .
}
6
  • May I ask one more thing? How would you extend this query, to get only those records, where placeLabel contains a specific string? Like this in SQL: WHERE placeLabel LIKE '%Station%'
    – Tom
    Commented Sep 16, 2019 at 18:20
  • That would be CONTAINS( ?placeLabel , "Station") However, it might, depending on your use case, be better to change the value of your line for geographical objects to stations: ?place wd:P31/wd:P279* wd:Q12819564
    – Ainali
    Commented Sep 16, 2019 at 18:51
  • "Station" is actually a part of a name, not the type of the place. Adding line FILTER CONTAINS(?placeLabel, "Station") is resulting in no records. Where exactly goes that line in the query?
    – Tom
    Commented Sep 16, 2019 at 19:54
  • Oops, sorry, that doesn't seem to work with the label service. Remove the line starting with SERVICE wikibase:label and add these two lines: ?place rdfs:label ?placeLabel . FILTER (CONTAINS(?placeLabel, "Station")) If that gives you too many language hits you can filter it more with something like: FILTER(LANG(?placeLabel) = "en")
    – Ainali
    Commented Sep 16, 2019 at 20:05
  • 1
    @Ainali, "FILTER REGEX(STR(?article), ".wikipedia.org/wiki/")" — another option is ?article schema:isPartOf/wikibase:wikiGroup "wikipedia" . "That doesn't seem to work with the label service" — use manual mode: SERVICE wikibase:label { bd:serviceParam wikibase:language '{4},en,fr,es'. ?place schema:description ?placeDescription ; rdfs:label ?placeLabel }. Commented Sep 16, 2019 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.