How to Create an API for listing Nigerian States and Local Government Area using Linked Data from the Web of Data
.Subjects:
API
,
Linked Data
,
Nigeria
,
Local Government Areas of Nigeria
,
States of Nigeria
Situation Analysis:
Mfoniso Ossom (https://www.facebook.com/mfoniso?fref=nf) posed the question on Silicon Africa Facebook group (https://www.facebook.com/groups/siliconafrica/), "Is there a public API that provides a listing of Nigerian states, Cities, LGA e.t.c?". Investigation has shown there was none as the time of this post.Challenge:
How do I create an API out of a document that comprised of data representing States or Local Government Areas of Nigeria? We know that Wikipedia documents hosts these data so we can use DBPedia data as well as any other additional data source on the web.Solution:
- Use any server side programming language like PHP, Ruby, Python, Perl, ASP, etc to exploit SPARQL via REST based web service.
- Determine which SPARQL endpoint, a REST web service URI, you want to access e.g DBpedia (http://dbpedia.org/sparql) or a local virtuoso instance (typically http://localhost:8890/sparql). SPARQL endpoints provides a REST API to exploit RDF documents. The URL we'll send will start with the SPARQL endpoint and then pass it some query string parameters, "query" and "output".
- Construct the SPARQL query to list the data
## SPARQL Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?s1 as ?stateURI str(?s4) as ?stateName str(?s5) as ?stateDesc WHERE { ?s1 a <http://dbpedia.org/ontology/Place> . ?s1 <http://dbpedia.org/property/type> ?s3 . ?s1 rdfs:label ?s4 . ?s1 rdfs:comment ?s5 . FILTER ( ?s3 in ( <http://dbpedia.org/resource/States_of_Nigeria> ) ) FILTER langMatches( lang(?s4), "en" ) FILTER langMatches( lang(?s5), "en" )
- URL Encode the SPARQL query and add it to the "query" parameter in the HTTP URL (e.g http://dbpedia.org/sparql?query=[encoded sparql here]).
- Add the query output format to the "output" parameter (e.g http://dbpedia.org/sparql?query=[encoded sparql here]&output=json).
- This URI is usable anywhere on the Web.
- Make the HTTP request to the URL and return the response body as a string.
- Using the right content header, you can write to standard output.
Download:
- Source codes for the Local Government Areas API https://github.com/emekaokoye/NG-LGA-API - here.
- Source codes for the States API https://github.com/emekaokoye/NG-States-API - here.
Live Demo:
- Local Government Areas API http://linkedopendatang.com/entity/localgov/getlg.php?f=xml - here.
- States API http://linkedopendatang.com/entity/state/getstate.php?f=json - here.
- Note: Options for query parameter 'f' are json,csv,xml,ttl,rdf.
References
http://en.wikipedia.org/wiki/DBpedia - DBpedia
http://en.wikipedia.org/wiki/SPARQL - SPARQL
http://www.w3.org/TR/rdf-sparql-query/ - SPARQL Query Language for RDF
http://semanticweb.org/wiki/SPARQL_endpoint - SPARQL Endpoint
http://en.wikipedia.org/wiki/Server-side_scripting - Server Side Scripting
Keywords: linkedopendatang, open data, linked data, nigeria, sparql, states, local government areas, api
Emeka, this cool. It seems that some states are missing in the data returned. We have just 21 states. Is there some reliable data source you recommend?
ReplyDeleteThis comment has been removed by the author.
ReplyDelete