Skip to content

Commit

Permalink
Updated graphql queries to produce better geojson - more shorthands a…
Browse files Browse the repository at this point in the history
…nd conventions are yet needed.
  • Loading branch information
Aklakan committed Aug 22, 2024
1 parent a451e55 commit 2f6ea72
Showing 1 changed file with 123 additions and 4 deletions.
127 changes: 123 additions & 4 deletions docs/demos/leaflet-graphql/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,123 @@ <h3>Edit GraphQL Query</h3>

// GraphQL query


const simpleQuery = `
{
locations(limit: 10)
@pattern(of: "?s a coy:Country", from: "s", to: "s")
@prefix(name: "rdfs", iri: "http://www.w3.org/2000/01/rdf-schema#")
@prefix(name: "geo", iri: "http://www.opengis.net/ont/geosparql#")
@prefix(name: "geof", iri: "http://www.opengis.net/def/function/geosparql/")
@prefix(name: "norse", iri: "https://w3id.org/aksw/norse#")
@prefix(name: "coy", iri: "https://schema.coypu.org/global#")
{
type @one @pattern(of: "BIND(?x AS ?xx) BIND('Feature' AS ?y)", from: "x", to: "y")
# Properties container
properties @one @pattern(of: "BIND(?x AS ?y)", from: "x", to: "y") {
# Style
style @one @pattern(of: """
BIND(?x as ?y)
BIND(norse:json.object("fillColor", "red") AS ?json)
""", from: "x", to: "json")
label @one
@pattern(of: """
SELECT ?s ?o {
?s rdfs:label ?o .
FILTER(langMatches(lang(?o), 'en'))
}
""", from: "s", to: "o")
}
# Geometry container
# Note: simplifyDp simplifies polygons on query time
# Reduces amount of data and thus loading time
geometry @one
@pattern(of: """
?s geo:hasGeometry/geo:asWKT ?x .
BIND(STRDT(STR(geof:asGeoJSON(
geof:simplifyDp(?x, 0.2)
)), norse:json) AS ?o)
""", from: "s", to: "o")
}
}
`;

const complexQuery = `
{
locations(limit: 1000)
@pattern(of: "?s a coy:Country", from: "s", to: "s")
@prefix(name: "", iri: "https://schema.coypu.org/global#")
@prefix(name: "rdfs", iri: "http://www.w3.org/2000/01/rdf-schema#")
@prefix(name: "geo", iri: "http://www.opengis.net/ont/geosparql#")
@prefix(name: "geof", iri: "http://www.opengis.net/def/function/geosparql/")
@prefix(name: "norse", iri: "https://w3id.org/aksw/norse#")
@prefix(name: "afn", iri: "http://jena.apache.org/ARQ/function#")
@prefix(name: "coy", iri: "https://schema.coypu.org/global#")
{
type @one @pattern(of: "BIND(?x AS ?xx) BIND('Feature' AS ?y)", from: "x", to: "y")
# Properties container
properties @one @pattern(of: "BIND(?x AS ?y)", from: "x", to: "y") {
# Style
style @one @pattern(of: """
BIND(?x as ?y)
BIND(CONCAT('#', SUBSTR(MD5(STR(?x)), 1, 6)) AS ?color)
BIND(norse:json.object("fillColor", ?color) AS ?json)
""", from: "x", to: "json")
label @one
@pattern(of: """
SELECT ?s ?o {
?s rdfs:label ?o .
FILTER(langMatches(lang(?o), 'en'))
}
""", from: "s", to: "o")
features
@pattern(of: """
SELECT * {
{
?s ?p ?o .
FILTER(?p NOT IN (rdfs:label))
}
# Auto-derive property cardinalities from all data
{ SERVICE <cache:> {
{ SELECT ?p (MAX(?c) AS ?pc) {
SELECT ?x ?p (COUNT(*) AS ?c) {
?x ?p ?z
} GROUP BY ?x ?p
} GROUP BY ?p }
}
}
}
""", from: "s", to: "o")
@index(by: "afn:localname(?p)", oneIf: "?pc = 1")
}
# Geometry Section
geometry @one
@pattern(of: """
?s geo:hasGeometry/geo:asWKT ?x .
BIND(STRDT(STR(geof:asGeoJSON(
geof:simplifyDp(?x, 0.2)
)), norse:json) AS ?o)
""", from: "s", to: "o")
}
}
`;






const simpleQueryOld = `
{
locations(limit: 10)
@pattern(of: "?s a coy:Country", from: "s", to: "s")
Expand Down Expand Up @@ -144,7 +260,7 @@ <h3>Edit GraphQL Query</h3>
}
`;

const complexQuery = `
const complexQueryOld = `
{
locations(limit: 1000)
@pattern(of: "?s a coy:Country", from: "s", to: "s")
Expand Down Expand Up @@ -242,7 +358,7 @@ <h3>Edit GraphQL Query</h3>
// Add data to the map
async function addDataToMap() {
const query = document.getElementById('graphqlQuery').value;
const locations = await fetchData(query);
var locations = await fetchData(query);

// Clear existing map layers
map.eachLayer((layer) => {
Expand All @@ -252,10 +368,13 @@ <h3>Edit GraphQL Query</h3>
});

locations.forEach(location => {
L.geoJSON(location.geometry, {
L.geoJSON(location, {
onEachFeature: (feature, layer) => {
const popupContent = createPopupContent(location);
const popupContent = createPopupContent(feature.properties);
layer.bindPopup(popupContent);
},
style: (feature) => {
return feature.properties && feature.properties.style;
}
}).addTo(map);
});
Expand Down

0 comments on commit 2f6ea72

Please sign in to comment.