Reputation: 425
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
Upvotes: 1
Views: 1780
Reputation: 4199
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 "[email protected]" 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}
Upvotes: 2
Reputation: 1817
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.
Upvotes: 4