Public
Edited
Oct 29, 2022
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof big_cities_item = Inputs.table(big_city_media_properties)
Insert cell
Insert cell
Insert cell
viewof heritage_sites_item = Inputs.table(heritage_site_properties)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function query_wikidata(query){
let results_json = fetch(`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`, {headers: {accept: "application/sparql-results+json"}}).then(response => response.json())
return results_json
}
Insert cell
Insert cell
function parse_values(results_json) {
return results_json.results.bindings
}
Insert cell
### Extract property identifiers and labels
Insert cell
function extract_property_labels(property_values) {
let property_labels = property_values.map(function(d) {
let output = {
Label: d.property.value.replace("http://www.wikidata.org/prop/direct/",""),
Description: d.name.value
};
return output;
})
return property_labels
}
Insert cell
Insert cell
sparql_query = `SELECT ?property ?name WHERE {
?property wikibase:propertyType wikibase:CommonsMedia;
rdfs:label ?name.
FILTER((LANG(?name)) = "en")
}`
Insert cell
property_json = query_wikidata(sparql_query)
Insert cell
property_values = parse_values(property_json)
Insert cell
Insert cell
Insert cell
big_city_sparql_query = `SELECT DISTINCT ?property ?name WHERE {
{
SELECT ?property ?name WHERE {
_:b53 wikibase:propertyType wikibase:CommonsMedia;
rdfs:label ?name;
wikibase:directClaim ?property.
FILTER((LANG(?name)) = "en")
}
}
_:b54 wdt:P31 wd:Q1549591;
?property _:b55.
}`
Insert cell
big_city_media_property_json = query_wikidata(big_city_sparql_query)
Insert cell
big_city_media_property_values = parse_values(big_city_media_property_json)
Insert cell
big_city_media_properties = extract_property_labels(big_city_media_property_values)
Insert cell
Insert cell
heritage_site_sparql_query = `SELECT DISTINCT ?property ?name WHERE {
{
SELECT ?property ?name WHERE {
_:b53 wikibase:propertyType wikibase:CommonsMedia;
rdfs:label ?name;
wikibase:directClaim ?property.
FILTER((LANG(?name)) = "en")
}
}
_:b54 wdt:P31 wd:Q358;
?property _:b55.
}`
Insert cell
heritage_site_json =query_wikidata(heritage_site_sparql_query)
Insert cell
heritage_site_query_values = parse_values(heritage_site_json)
Insert cell
heritage_site_properties = extract_property_labels(heritage_site_query_values)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more