fetching buildings from triple store
davvalent

davvalent commited on 2023-03-07 01:24:04
Showing 1 changed files, with 54 additions and 14 deletions.

... ...
@@ -10,24 +10,64 @@
10 10
 
11 11
 (async () => {
12 12
 
13
+  let response;
14
+
15
+  /** URLs assignments */
16
+  let tilesUrl = "https://ntnlv.ca/faubourg/tiles/{z}/{x}/{y}.png",
17
+    tripleStoreEndpointUrl = "http://qdmtl.ca/sparql/endpoint.php";
18
+
19
+  /** Fetch values for dev env */
20
+  if (dev()) { // develop
21
+    response = await fetch("./js/config.json"); // config.json is .gitignored
22
+    const dev = await response.json();
23
+    tilesUrl = dev.devUrl;
24
+    tripleStoreEndpointUrl = dev.tripleStoreEndpointUrl;
25
+  }
26
+
27
+  /** SPARQL query */
28
+  const buildingsQuery = "query=" + encodeURIComponent(
29
+    "PREFIX ecrm:<http://erlangen-crm.org/current/>PREFIX geo:<http://www.opengis.net/ont/geosparql#>SELECT ?a ?b WHERE{?a a <http://onto.qdmtl.ca/E24_Building>;ecrm:P53_has_former_or_current_location ?c.?c ecrm:P168_place_is_defined_by ?d.?d geo:asGeoJSON ?b.}"
30
+    );
31
+
13 32
   /**
14
-   * Télécharger données (requête http)
15
-   * Retourner application/geo+json content type
16
-   * @todo voir https://geojson.org/geojson-ld/
17
-   * @todo voir linked places format
33
+   * fetching data from triple store
34
+   * buildings and geographic coordinates
18 35
    */
19
-  let response = await fetch("./buildings.json");
20
-  const geojsonFeatures = await response.json();
21
-
22
-  /** FAML Tiles */
23
-  let tilesUrl = "https://ntnlv.ca/faubourg/tiles/{z}/{x}/{y}.png";
36
+  response = await fetch(tripleStoreEndpointUrl, {
37
+    method: "POST",
38
+    headers: {
39
+      "Accept": "application/sparql-results+json",
40
+      "Content-Type": "application/x-www-form-urlencoded"
41
+    },
42
+    body: buildingsQuery
43
+  });
44
+  const sparqlResults = await response.json();
45
+  if (dev()) { // develop
46
+    console.log(sparqlResults);
47
+  };
24 48
 
25
-  /** Localhost tiles for dev */
26
-  if (location.host === "localhost" || location.host === "127.0.0.1") {
27
-    response = await fetch("./js/config.json");
28
-    tilesUrl = await response.json();
29
-    tilesUrl = tilesUrl.devUrl;
49
+  /**
50
+   * making of geoJSON object
51
+   */
52
+  let featuresArray = [];
53
+  sparqlResults.results.bindings.forEach(feature => {
54
+    featuresArray.push(
55
+      {
56
+        "type": "Feature",
57
+        "properties": {
58
+          "URI": feature.a.value
59
+        },
60
+        "geometry": JSON.parse(feature.b.value)
30 61
   }
62
+    )
63
+  });
64
+  const geojsonFeaturesFromTripleStore = {
65
+    "type": "FeatureCollection",
66
+    "features": featuresArray
67
+  };
68
+  if (dev()) { // develop
69
+    console.log(geojsonFeaturesFromTripleStore);
70
+  };
31 71
 
32 72
   /** 
33 73
    * Base Layer
34 74