Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active July 31, 2020 10:09
Show Gist options
  • Save hubgit/5488213 to your computer and use it in GitHub Desktop.
Save hubgit/5488213 to your computer and use it in GitHub Desktop.
Fetch Land Registry transaction data using SPARQL
<?php
$curl = curl_init('http://landregistry.data.gov.uk/landregistry/query');
curl_setopt_array($curl, array(
CURLOPT_VERBOSE => true,
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_HTTPHEADER => array('Accept: text/plain'),
));
$limit = 100000;
$total = 860350; // SELECT COUNT(*) WHERE { ?x a <http://landregistry.data.gov.uk/def/ppi/TransactionRecord> }
for ($i = 0; $i < $total; $i += $limit) {
$query = sprintf('
SELECT ?postcode ?amount ?date ?status
WHERE {
?transx a <http://landregistry.data.gov.uk/def/ppi/TransactionRecord> .
?transx <http://landregistry.data.gov.uk/def/ppi/pricePaid> ?amount .
?transx <http://landregistry.data.gov.uk/def/ppi/transactionDate> ?date .
?transx <http://landregistry.data.gov.uk/def/ppi/propertyAddress> ?addr.
?transx <http://landregistry.data.gov.uk/def/ppi/recordStatus> ?status.
?addr <http://landregistry.data.gov.uk/def/common/postcode> ?postcode .
}
LIMIT %d OFFSET %d', $limit, $i);
$params = array(
'query' => $query,
'output' => 'csv',
);
$file = gzopen(sprintf('data/%s.csv.gz', $i), 'w');
curl_setopt_array($curl, array(
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_FILE => $file,
));
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code !== 200) {
exit("Error: $code\n" . print_r($result, true));
}
gzclose($file);
}
@connorwilloughby
Copy link

You are a legend, thank you for sharing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment