Skip to content

Commit 2f6ea72

Browse files
committed
Updated graphql queries to produce better geojson - more shorthands and conventions are yet needed.
1 parent a451e55 commit 2f6ea72

File tree

1 file changed

+123
-4
lines changed

1 file changed

+123
-4
lines changed

Diff for: docs/demos/leaflet-graphql/index.html

+123-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,123 @@ <h3>Edit GraphQL Query</h3>
113113

114114
// GraphQL query
115115

116+
116117
const simpleQuery = `
118+
{
119+
locations(limit: 10)
120+
@pattern(of: "?s a coy:Country", from: "s", to: "s")
121+
@prefix(name: "rdfs", iri: "http://www.w3.org/2000/01/rdf-schema#")
122+
@prefix(name: "geo", iri: "http://www.opengis.net/ont/geosparql#")
123+
@prefix(name: "geof", iri: "http://www.opengis.net/def/function/geosparql/")
124+
@prefix(name: "norse", iri: "https://w3id.org/aksw/norse#")
125+
@prefix(name: "coy", iri: "https://schema.coypu.org/global#")
126+
{
127+
type @one @pattern(of: "BIND(?x AS ?xx) BIND('Feature' AS ?y)", from: "x", to: "y")
128+
129+
# Properties container
130+
properties @one @pattern(of: "BIND(?x AS ?y)", from: "x", to: "y") {
131+
132+
# Style
133+
style @one @pattern(of: """
134+
BIND(?x as ?y)
135+
BIND(norse:json.object("fillColor", "red") AS ?json)
136+
""", from: "x", to: "json")
137+
138+
label @one
139+
@pattern(of: """
140+
SELECT ?s ?o {
141+
?s rdfs:label ?o .
142+
FILTER(langMatches(lang(?o), 'en'))
143+
}
144+
""", from: "s", to: "o")
145+
}
146+
147+
# Geometry container
148+
# Note: simplifyDp simplifies polygons on query time
149+
# Reduces amount of data and thus loading time
150+
geometry @one
151+
@pattern(of: """
152+
?s geo:hasGeometry/geo:asWKT ?x .
153+
BIND(STRDT(STR(geof:asGeoJSON(
154+
geof:simplifyDp(?x, 0.2)
155+
)), norse:json) AS ?o)
156+
""", from: "s", to: "o")
157+
}
158+
}
159+
`;
160+
161+
const complexQuery = `
162+
{
163+
locations(limit: 1000)
164+
@pattern(of: "?s a coy:Country", from: "s", to: "s")
165+
@prefix(name: "", iri: "https://schema.coypu.org/global#")
166+
@prefix(name: "rdfs", iri: "http://www.w3.org/2000/01/rdf-schema#")
167+
@prefix(name: "geo", iri: "http://www.opengis.net/ont/geosparql#")
168+
@prefix(name: "geof", iri: "http://www.opengis.net/def/function/geosparql/")
169+
@prefix(name: "norse", iri: "https://w3id.org/aksw/norse#")
170+
@prefix(name: "afn", iri: "http://jena.apache.org/ARQ/function#")
171+
@prefix(name: "coy", iri: "https://schema.coypu.org/global#")
172+
{
173+
type @one @pattern(of: "BIND(?x AS ?xx) BIND('Feature' AS ?y)", from: "x", to: "y")
174+
175+
# Properties container
176+
properties @one @pattern(of: "BIND(?x AS ?y)", from: "x", to: "y") {
177+
178+
# Style
179+
style @one @pattern(of: """
180+
BIND(?x as ?y)
181+
BIND(CONCAT('#', SUBSTR(MD5(STR(?x)), 1, 6)) AS ?color)
182+
BIND(norse:json.object("fillColor", ?color) AS ?json)
183+
""", from: "x", to: "json")
184+
185+
186+
label @one
187+
@pattern(of: """
188+
SELECT ?s ?o {
189+
?s rdfs:label ?o .
190+
FILTER(langMatches(lang(?o), 'en'))
191+
}
192+
""", from: "s", to: "o")
193+
194+
features
195+
@pattern(of: """
196+
SELECT * {
197+
{
198+
?s ?p ?o .
199+
FILTER(?p NOT IN (rdfs:label))
200+
}
201+
# Auto-derive property cardinalities from all data
202+
{ SERVICE <cache:> {
203+
{ SELECT ?p (MAX(?c) AS ?pc) {
204+
SELECT ?x ?p (COUNT(*) AS ?c) {
205+
?x ?p ?z
206+
} GROUP BY ?x ?p
207+
} GROUP BY ?p }
208+
}
209+
}
210+
}
211+
""", from: "s", to: "o")
212+
@index(by: "afn:localname(?p)", oneIf: "?pc = 1")
213+
}
214+
215+
# Geometry Section
216+
geometry @one
217+
@pattern(of: """
218+
?s geo:hasGeometry/geo:asWKT ?x .
219+
BIND(STRDT(STR(geof:asGeoJSON(
220+
geof:simplifyDp(?x, 0.2)
221+
)), norse:json) AS ?o)
222+
""", from: "s", to: "o")
223+
}
224+
}
225+
`;
226+
227+
228+
229+
230+
231+
232+
const simpleQueryOld = `
117233
{
118234
locations(limit: 10)
119235
@pattern(of: "?s a coy:Country", from: "s", to: "s")
@@ -144,7 +260,7 @@ <h3>Edit GraphQL Query</h3>
144260
}
145261
`;
146262

147-
const complexQuery = `
263+
const complexQueryOld = `
148264
{
149265
locations(limit: 1000)
150266
@pattern(of: "?s a coy:Country", from: "s", to: "s")
@@ -242,7 +358,7 @@ <h3>Edit GraphQL Query</h3>
242358
// Add data to the map
243359
async function addDataToMap() {
244360
const query = document.getElementById('graphqlQuery').value;
245-
const locations = await fetchData(query);
361+
var locations = await fetchData(query);
246362

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

254370
locations.forEach(location => {
255-
L.geoJSON(location.geometry, {
371+
L.geoJSON(location, {
256372
onEachFeature: (feature, layer) => {
257-
const popupContent = createPopupContent(location);
373+
const popupContent = createPopupContent(feature.properties);
258374
layer.bindPopup(popupContent);
375+
},
376+
style: (feature) => {
377+
return feature.properties && feature.properties.style;
259378
}
260379
}).addTo(map);
261380
});

0 commit comments

Comments
 (0)