Last active
July 31, 2020 10:09
-
-
Save hubgit/5488213 to your computer and use it in GitHub Desktop.
Fetch Land Registry transaction data using SPARQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are a legend, thank you for sharing this.