Mohcen CH
Mohcen CH

Reputation: 315

SPARQL to Get the coordinates of any city using wikidata api

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

Upvotes: 0

Views: 257

Answers (0)

Related Questions