Question

Sorry but I just can't get my head around SPARQL! Could someone help me out with writing a query to get whether or not a property was a new build from the land registry API please?

Here is the query I've got so far:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
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/>

SELECT ?paon ?saon ?street ?town ?county ?postcode ?propertytype ?amount ?date
    WHERE {
        ?transx lrppi:pricePaid ?amount .
        ?transx lrppi:transactionDate ?date .
        ?transx lrppi:propertyAddress ?addr .
        ?transx lrppi:propertyType ?propertytype .
        ?addr lrcommon:postcode "AB1 1AB" ^^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
Was it helpful?

Solution

First of all let's rewrite your query so it's much easier to read:

PREFIX lrppi: <http://landregistry.data.gov.uk/def/ppi/>
PREFIX lrcommon: <http://landregistry.data.gov.uk/def/common/>

SELECT ?paon ?saon ?street ?town ?county ?postcode ?propertytype ?amount ?date
  WHERE {
    ?transx lrppi:pricePaid ?amount ;
            lrppi:transactionDate ?date ;
            lrppi:propertyAddress ?addr ;
            lrppi:propertyType ?propertytype .
    ?addr lrcommon:postcode "AB1 1AB" ;
          lrcommon:postcode ?postcode .
    OPTIONAL {
        ?addr lrcommon:county ?county ;
              lrcommon:paon ?paon ;
              lrcommon:saon ?saon ;
              lrcommon:street ?street ;
              lrcommon:town ?town .
    }
  } ORDER BY ?amount

You can use a ; to still use the subject and ask for a new object. While . ends your query. Further you can also combine OPTIONAL and you don't need ^^xsd:string for a string.

If you don't get any results something in your query is wrong, probably the postcode. Start by removing parts of the query to see if you get any results and than build it up again. That's the easiest way to do SPARQL in my opinion. If you need more information, check out the EUCLID-project webinar 2. That provides an introduction to SPARQL and will make it much easier to understand.

I couldn't find the SPARQL endpoint, so that's why I couldn't test your query unfortunately... Maybe you can provide a SPARQL endpoint for more help?


EDIT: After finding the SPARQL endpoint thanks to @RobV, I tested out my query. After all ^^xsd:string was required for the land registry SPARQL endpoint, but that's no biggie, we didn't even need it. Also the set postcode didn't exist in the dataset so that's why you wouldn't get any results. Anyway this is the query to get all the new build houses for all postcodes:

PREFIX lrppi: <http://landregistry.data.gov.uk/def/ppi/>
PREFIX lrcommon: <http://landregistry.data.gov.uk/def/common/>

SELECT ?paon ?saon ?street ?town ?county ?postcode ?propertytype ?amount ?date
  WHERE {
    ?transx lrppi:pricePaid ?amount ;
            lrppi:transactionDate ?date ;
            lrppi:propertyAddress ?addr ;
            lrppi:propertyType ?propertytype ;
            lrppi:newBuild true .
    OPTIONAL {
        ?addr lrcommon:county ?county ;
              lrcommon:postcode ?postcode ;
              lrcommon:paon ?paon ;
              lrcommon:saon ?saon ;
              lrcommon:street ?street ;
              lrcommon:town ?town .
    }
  } LIMIT 100

I limited it to 100 rows because of the performance of the land registry SPARQL endpoint. You could also make some elements mandatory instead of optional for a full resultset or do a SELECT DISTINCT to filter out all the duplicates. But that's just nitpicking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top