1

I am trying to get some data from land registry API (http://landregistry.data.gov.uk/app/hpi/qonsole) and it uses SPARQL query. I am new to sparql queries and not sure what to pass in my param.query object and how to pass prefixes required.

 //myCode
 var req = {
        method: 'GET',
        url: 'http://landregistry.data.gov.uk/landregistry/query',
        headers: { 'Content-type' : 'application/x-www-form-urlencoded',
            'Accept' : 'application/sparql-results+json' },
        params: {
            query :"select ?paon ?saon ?street ?town ?county ?postcode ?amount ?date where {?addr lrcommon:postcode "PL6 8RU"} limit 10",
            format: "json"
        }
    };
    console.log(req)
// SparQL Query
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix lrhpi: <http://landregistry.data.gov.uk/def/hpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>

#-- Returns the Price Paid data from the default graph for each transaction record having
#-- an address with the given postcode.
#-- The postcode to query is set in the line - ?address_instance common:postcode "PL6 8RU"^^xsd:string .


SELECT ?paon ?saon ?street ?town ?county ?postcode ?amount ?date
WHERE
{
?transx lrppi:pricePaid ?amount ;
      lrppi:transactionDate ?date ;
      lrppi:propertyAddress ?addr.

?addr lrcommon:postcode "PL6 8RU"^^xsd:string.
?addr lrcommon:postcode ?postcode.

OPTIONAL {?addr lrcommon:county ?county}
OPTIONAL {?addr lrcommon:paon ?paon}
OPTIONAL {?addr lrcommon:saon ?saon}
OPTIONAL {?addr lrcommon:street ?street}
OPTIONAL {?addr lrcommon:town ?town}
}
ORDER BY ?amount
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
kam
  • 425
  • 2
  • 6
  • 24
  • Not familiar with the particular service, but maybe SPARQL standard will help: http://www.w3.org/TR/sparql11-protocol/#query-via-post-urlencoded (prefix declaration may be in the beginning of the query) – Roman Susi Jun 18 '15 at 14:22
  • its service from this url => http://landregistry.data.gov.uk/app/hpi/qonsole – kam Jun 18 '15 at 14:30

2 Answers2

4

You are half way there. You would use Angular's $http service with the request object you developed (assuming you populate the query string properly). $http service returns a promise, which you can resolve using the shortcut methods available (.success and .error). For these you specify a callback function that is called asynchronously when a response is ready. For example:

$http({
    url: 'http://landregistry.data.gov.uk/landregistry/query', 
    headers: { 'Content-type' : 'application/x-www-form-urlencoded',
            'Accept' : 'application/sparql-results+json' },
    method: "GET",
    params: {
            query : "select * where {?s a ?o} limit 10",
            format: "json"
        }
  })

  .success(function(data, status, headers, config) {
    // callback called asynchronously when the response is available 
    $scope.results = data.results.bindings;
   })

  .error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status
  });

Here's a working plunker that executes the above query and displays the result.

chris
  • 1,787
  • 1
  • 13
  • 13
  • Thanks Chris, i am familiar with $http, $q promises success etc, but i am trying to get my head around "select * where {?s a ?o} limit 10" . as i want to get data returned in json format for any given postcode. something like here => http://landregistry.data.gov.uk/app/hpi/qonsole – kam Jun 18 '15 at 14:47
  • Then update pls your question, as it does not currently state it as a problem with SPARQL, rather AngularJs. – Roman Susi Jun 18 '15 at 14:58
  • you're right i think i should change the question wording as it is misunderstood – kam Jun 18 '15 at 15:11
  • Yes, pls update your question. Anyone reading it is inclined to think the issue is Angular-related. If the problem is SPARQL-related then be specific about it. – chris Jun 18 '15 at 15:24
2

I think, you can use this answer: How do I POST urlencoded form data with $http in AngularJS? to form your request, and you supply query in the data:

data: {query: "Put your SPARQL Query here"}

SPARQL query is everything below "// SparQL Query" in your listing, just a multiline text in a JavaScript string. (Creating multiline strings in JavaScript for multiline string in JavaScript). For example, if I put it into text file yourquery.txt, I can fetch results with curl:

curl --data-urlencode "query@yourquery.txt" http://landregistry.data.gov.uk/landregistry/query

and it returns nice json:

{
  "head": {
    "vars": [ "paon" , "saon" , "street" , "town" , "county" , "postcode" , "amount" , "date" ]
  } ,
  "results": {
    "bindings": [
      {
        "paon": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "58" } ,
        "street": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PATTINSON DRIVE" } ,
        "town": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PLYMOUTH" } ,
        "county": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "CITY OF PLYMOUTH" } ,
        "postcode": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PL6 8RU" } ,
        "amount": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "56000" } ,
        "date": { "datatype": "http://www.w3.org/2001/XMLSchema#date" , "type": "typed-literal" , "value": "1999-11-08" }
      } ,
...
}

NB. I am using POST, not GET - this is not essential.

Remark:

?addr lrcommon:postcode "PL6 8RU"^^xsd:string.
?addr lrcommon:postcode ?postcode.

This is not quite good. It's better to use VALUES or BIND:

BIND ("PL6 8RU"^^xsd:string AS ?postcode)
?addr lrcommon:postcode ?postcode.

Because this is what is presumably wanted here. See SPARQL Query language specs for details.

If JavaScript variable's needs to be used in the query, then the value should be escaped and concatenation used:

'BIND ("' + escapedPostcodeVar + '"^^xsd:string AS ?postcode)'

See documentation for details on what and how to escape.

For the reference, working SPARQL query:

var myquery = 'prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n\
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n\
prefix owl: <http://www.w3.org/2002/07/owl#> \n\
prefix xsd: <http://www.w3.org/2001/XMLSchema#> \n\
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/> \n\
prefix lrhpi: <http://landregistry.data.gov.uk/def/hpi/> \n\
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/> \n\
prefix skos: <http://www.w3.org/2004/02/skos/core#> \n\
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/> \n\
 \n\
SELECT ?paon ?saon ?street ?town ?county ?postcode ?amount ?date \n\
WHERE \n\
{ \n\
BIND ("PL6 8RU"^^xsd:string AS ?postcode) \n\
 \n\
?addr lrcommon:postcode ?postcode. \n\
 \n\
?transx lrppi:pricePaid ?amount ; \n\
      lrppi:transactionDate ?date ; \n\
      lrppi:propertyAddress ?addr. \n\
 \n\
OPTIONAL {?addr lrcommon:county ?county} \n\
OPTIONAL {?addr lrcommon:paon ?paon} \n\
OPTIONAL {?addr lrcommon:saon ?saon} \n\
OPTIONAL {?addr lrcommon:street ?street} \n\
OPTIONAL {?addr lrcommon:town ?town} \n\
} \n\
ORDER BY ?amount \n\
LIMIT 10'

Then in $http:

params: {query: myquery}
Community
  • 1
  • 1
Roman Susi
  • 4,135
  • 2
  • 32
  • 47
  • something like in here => landregistry.data.gov.uk/app/hpi/qonsole – kam Jun 18 '15 at 14:58
  • You just insert it as a text instead of Put your SPARQL Query here in my answer. If you want to put variable instead of string literal "PL6 8RU", you will need to use escape and concatenate. Please, clarify, what do you mean by "pass this query". – Roman Susi Jun 18 '15 at 15:05
  • sorry Roman, the questions is misunderstood, i am going to change the wording as i am more trying to understand the 'where' part of query i've posted above as it is throwing errors – kam Jun 18 '15 at 15:15
  • Thanks Roman for explanation but i tried everything as you suggested but still no luck, i either get unexpected syntax or unexpected token error when i tried the query. I know you tried with the curl and it worked...i wonder if you be able to provide me snippet of code with the SPARQL query. something like $http({data:{ query }}) . I have created a fiddle and pasted query in html box for reference. https://jsfiddle.net/n7wr1dbq/ – kam Jun 18 '15 at 17:18
  • Try chris's code but in the javascript code put "var myquery = ..." as in my answer and change params to the one from my answer (params: {query: myquery}) – Roman Susi Jun 18 '15 at 18:36
  • Thanks very much, It works I must have had some issue with either newline or escape... :) – kam Jun 18 '15 at 18:40