Friday, December 26, 2014

How to Create an API Listing Nigerian States or Local Government Areas

How to Create an API for listing Nigerian States and Local Government Area using Linked Data from the Web of Data

.

Subjects: , , , ,

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 ? 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:

  1. Use any server side programming language like PHP, Ruby, Python, Perl, ASP, etc to exploit SPARQL via REST based web service.
  2. 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".
  3. 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" ) 
      
      
      
  4. 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]).
  5. Add the query output format to the "output" parameter (e.g http://dbpedia.org/sparql?query=[encoded sparql here]&output=json).
  6. This URI is usable anywhere on the Web.
  7. Make the HTTP request to the URL and return the response body as a string.
  8. 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 -

http://en.wikipedia.org/wiki/SPARQL -

http://www.w3.org/TR/rdf-sparql-query/ -

http://semanticweb.org/wiki/SPARQL_endpoint -

http://en.wikipedia.org/wiki/Server-side_scripting -

Keywords: linkedopendatang, open data, linked data, nigeria, sparql, states, local government areas, api