William R
William R

Reputation: 37

How to find entities which are cities with Wikidata API?

I try to find a city based on a part of its name (typically https://en.wikipedia.org/wiki/Lavo%C3%BBte-sur-Loire, a French city)

The only way I found is to first request for "Lavoute" (without accent) through the following request.

https://fr.wikipedia.org/w/api.php?action=opensearch&generator=links&format=xml&search=lavoute&prop=pageprops&gpllimit=50&ppprop=wikibase_item&format=json

It returns 5 links and then use this information to request through all the results having something like this at the end with the good value (having property P281 means it is a city)

https://www.wikidata.org/w/api.php?action=wbgetentities&titles=Lavo%C3%BBte-sur-Loire&sites=enwiki|frwiki&format=json

Is there an easier way to do?

Upvotes: 1

Views: 1342

Answers (1)

Coder
Coder

Reputation: 519

As you said that you want to find a city based on a part of its name, here is the solution for your question:

SELECT ?location ?locationLabel WHERE {
 ?location wdt:P17 wd:Q142.
 ?location wdt:P31 ?settlement .
 ?settlement wdt:P279 wd:Q3266850 .
 ?location rdfs:label ?de_label .
 FILTER (lang(?de_label) = "en") .
 FILTER (regex((?de_label), "Lavoûte")).

 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
 }

Refer to this link, you might also get some ideas regarding your question : https://opendata.stackexchange.com/questions/11675/most-efficient-way-to-get-the-wikidata-entity-for-a-city-given-as-a-string

Upvotes: 5

Related Questions