0

Hey in my Django project i have a list of Algerian cities in my database, i recently added the latitude and longitude fields to the city table, What i want to do is to use wikidata API to feed my database with the coordinates of each city i have in my database which are all algerian cities.
In terms of django and python i can figure it out on my own but i'm new to sparql so i need help in the SPARQL part so how do i achieve that?
This is what i have so far:

import sys
from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """

 #sparql query here

"""


def get_results(endpoint_url, query):
    user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
    sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()


results = get_results(endpoint_url, query)

for result in results["results"]["bindings"]:
    # my logic here

I preferred not to include my try to write the query because i don't think it'll be a good starting point

Mohcen CH
  • 275
  • 6
  • 17
  • 1
    so where is the SPARQL query you tried so far? what does not work with it? – UninformedUser Oct 09 '22 at 03:44
  • @UninformedUser i still don't know what is the query for what is need, I had some tries but i think they won't be a very good starting point, can you help me with it? – Mohcen CH Oct 11 '22 at 21:20
  • `select ?city ?cityLabel ?loc { ?city wdt:P17 wd:Q262 ; wdt:P625 ?loc SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }` – UninformedUser Oct 12 '22 at 05:50
  • or to get long/lat out of the WKT point: `select ?city ?cityLabel ?long ?lat { ?city wdt:P17 wd:Q262 ; p:P625/psv:P625 [ wikibase:geoLatitude ?lat; wikibase:geoLongitude ?long ] . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }` – UninformedUser Oct 12 '22 at 05:51

0 Answers0