Making progress. I have answered two of my own questions:
strtry = """ "OX17 2JP", "B2 5EE", "G12 9ER", "AB1 0AG", "SY25 6PY" """
query = """
PREFIX pcd: <http://statistics.data.gov.uk/def/postcode/unit#>
PREFIX geog: <http://statistics.data.gov.uk/def/hierarchy/best-fit#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
#PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX stge: <http://statistics.data.gov.uk/def/statistical-geography#>
SELECT ?display ?ward ?la ?pcon ?reg ?dintr ?dterm
WHERE
{
?s rdf:type <http://statistics.data.gov.uk/def/postcode/unit> .
?s pcd:postcode1space ?display ;
geog:ward ?wardcode ;
geog:europeanelectoralregion ?regcode ;
geog:localauthoritydistrict ?lacode ;
geog:parliamentaryconstituency ?pconcode .
OPTIONAL {?s <http://statistics.data.gov.uk/def/postcode/unit/dateofintroduction> ?dintr ;
<http://statistics.data.gov.uk/def/postcode/unit/dateoftermination> ?dterm .}
?wardcode stge:officialname ?ward .
?lacode stge:officialname ?la .
?pconcode stge:officialname ?pcon .
?regcode stge:officialname ?reg .
filter(?display in ($strtry))
}
LIMIT 1000
"""
b = py"""
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://statistics.data.gov.uk/sparql")
sparql.setReturnFormat(JSON)
sparql.setQuery($(query))
try:
ret = sparql.queryAndConvert()
except Exception as e:
print(e)
"""
ret = py"ret"
values = DataFrame(display=String[], ward=String[], la=String[], pcon=String[], reg=String[], dintr=Union{Date,Missing}[], dterm=Union{Date,Missing}[])
for (i,d) in enumerate(ret["results"]["bindings"])
push!(values, ["","","","","",missing,missing])
values[i, :display] = d["display"]["value"]
values[i, :ward] = d["ward"]["value"]
values[i, :la] = d["la"]["value"]
values[i, :pcon] = d["pcon"]["value"]
values[i, :reg] = d["reg"]["value"]
if get!(d, "dintr", "missing") !== "missing"
values[i, :dintr] = Date(last(d["dintr"]["value"],7), "yyyy-mm")
end
if get!(d, "dterm", "missing") !== "missing"
values[i, :dterm] = Date(last(d["dterm"]["value"],7), "yyyy-mm")
end
end
println(values)
Which produces exactly what I want:
5×7 DataFrame
Row │ display ward la pcon reg dintr dterm
│ String String String String String Date? Date?
─────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ B2 5EE Ladywood Birmingham Birmingham, Ladywood West Midlands missing missing
2 │ OX17 2JP Middleton Cheney West Northamptonshire South Northamptonshire East Midlands missing missing
3 │ AB1 0AG Lower Deeside Aberdeen City Aberdeen South Scotland 1990-12-01 1992-07-01
4 │ G12 9ER Partick East/Kelvindale Glasgow City Glasgow North Scotland missing missing
5 │ SY25 6PY Llangeitho Ceredigion Ceredigion Wales missing missing
I am wondering if I could use HTTP.jl to construct this query in native Julia. I’ve tried a number of ways but always unsuccessfully. For example,
query = """QUERY = PREFIX pcd: <http://statistics.data.gov.uk/def/postcode/unit#> PREFIX geog: <http://statistics.data.gov.uk/def/hierarchy/best-fit#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX stge: <http://statistics.data.gov.uk/def/statistical-geography#> SELECT ?display ?ward ?la ?pcon ?reg ?dintr ?dterm WHERE { ?s rdf:type <http://statistics.data.gov.uk/def/postcode/unit> . ?s pcd:postcode1space ?display ; geog:ward ?wardcode ; geog:europeanelectoralregion ?regcode ; geog:localauthoritydistrict ?lacode ; geog:parliamentaryconstituency ?pconcode . OPTIONAL {?s <http://statistics.data.gov.uk/def/postcode/unit/dateofintroduction> ?dintr ; <http://statistics.data.gov.uk/def/postcode/unit/dateoftermination> ?dterm .} ?wardcode stge:officialname ?ward . ?lacode stge:officialname ?la . ?pconcode stge:officialname ?pcon . ?regcode stge:officialname ?reg . filter(?display in ("OX17 2JP")) } LIMIT 1000"""
url = raw"http://statistics.data.gov.uk/sparql.json"
response = HTTP.post(url, query = "$query")
(essentially the same as the python formulation) gives:
ERROR: LoadError: HTTP.Exceptions.StatusError(400, "POST", "/sparql.json?QUERY = PREFIX pcd: <http://statistics.data.gov.uk/def/postcode/unit#> PREFIX geog: <http://statistics.data.gov.uk/def/hierarchy/best-fit#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX stge: <http://statistics.data.gov.uk/def/statistical-geography#> SELECT ?display ?ward ?la ?pcon ?reg ?dintr ?dterm WHERE { ?s rdf:type <http://statistics.data.gov.uk/def/postcode/unit> . ?s pcd:postcode1space ?display ; geog:ward ?wardcode ; geog:europeanelectoralregion ?regcode ; geog:localauthoritydistrict ?lacode ; geog:parliamentaryconstituency ?pconcode . OPTIONAL {?s <http://statistics.data.gov.uk/def/postcode/unit/dateofintroduction> ?dintr ; <http://statistics.data.gov.uk/def/postcode/unit/dateoftermination> ?dterm .} ?wardcode stge:officialname ?ward . ?lacode stge:officialname ?la . ?pconcode stge:officialname ?pcon . ?regcode stge:officialname ?reg . filter(?display in (\"OX17 2JP\")) } LIMIT 1000", HTTP.Messages.Response:
"""
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 273
Date: Fri, 20 Oct 2023 18:09:57 GMT
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>400 Bad Request</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Bad Request</h1>
<h2>Your client has issued a malformed or illegal request.</h2>
<h2></h2>
</body></html>
""")
What do I need to do different?