Skip to content

Commit

Permalink
Added SpacePrez map search watchers, fixed geometry filter switch in …
Browse files Browse the repository at this point in the history
…query
  • Loading branch information
jamiefeiss committed Aug 29, 2023
1 parent bf68c70 commit fc05e5d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 26 deletions.
76 changes: 71 additions & 5 deletions src/components/search/SpacePrezSearchMap.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import { ref, computed, inject, onMounted, watch } from "vue";
import { DataFactory } from "n3";
import { mapConfigKey, type MapConfig, type ProfileHeader } from "@/types";
import { apiBaseUrlConfigKey, mapConfigKey, type MapConfig, type ProfileHeader } from "@/types";
import { useUiStore } from "@/stores/ui";
import { useApiRequest, useConcurrentApiRequests, useSparqlRequest } from "@/composables/api";
import { useRdfStore } from "@/composables/rdfStore";
Expand All @@ -22,6 +22,16 @@ type Option = {
iri: string;
};
type SparqlBinding = {
[key: string]: {
type: string;
datatype?: string;
value: string;
"xml:lang"?: string;
}
};
const apiBaseUrl = inject(apiBaseUrlConfigKey) as string;
const mapConfig = inject(mapConfigKey) as MapConfig;
const ui = useUiStore();
Expand All @@ -48,8 +58,15 @@ const spatialSelectionType = ref<AreaTypes>(AreaTypes.Nearby);
const showQuery = ref(false);
const limit = ref(10);
const radius = ref(5);
const results = ref([]);
const results = ref<{
uri: string;
link: string;
wkt: string;
fcLabel: string;
label: string;
}[]>([]);
const datasetCollapse = ref<{[key: string]: boolean}>({});
const searchMap = ref<typeof MapClient | null>(null);
const allDatasetsCollapsed = computed(() => {
return Object.values(datasetCollapse.value).every(isCollapsed => isCollapsed);
Expand Down Expand Up @@ -227,9 +244,58 @@ async function getDatasets() {
}
async function doSearch() {
if (shape.value.coords.length > 0) {
const searchData = await searchSparqlGetRequest(`${apiBaseUrl}/sparql`, query.value);
if (searchData && !searchError.value) {
results.value = (searchData.results.bindings as SparqlBinding[]).map(result => {
return {
uri: result.f_uri.value,
link: `/object?uri=${encodeURIComponent(result.f_uri.value)}`,
wkt: result.wkt.value,
fcLabel: result.fc_label ? result.fc_label.value : "",
label: result.f_label ? result.f_label.value : ""
}
});
}
}
}
watch(selectedDatasets, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
}, { deep: true });
watch(selectedFeatureCollections, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
}, { deep: true });
watch(limit, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
});
watch(radius, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
});
watch(shape, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
}, { deep: true });
watch(spatialSelectionType, async (newValue, oldValue) => {
if (LIVE_SEARCH) {
await doSearch();
}
});
onMounted(async () => {
await getDatasets();
Expand Down Expand Up @@ -310,7 +376,7 @@ onMounted(async () => {
</div>
<div class="search-map">
<MapClient
ref="searchMapRef"
ref="searchMap"
:geo-w-k-t="results"
:drawing-modes="['MARKER', 'POLYGON', 'RECTANGLE']"
@selectionUpdated="handleMapSelectionChange"
Expand All @@ -331,7 +397,7 @@ onMounted(async () => {
<tbody>
<template v-for="result in results">
<tr>
<td><a :href="result.link">{{ result.label }}</a></td>
<td><a :href="result.link">{{ result.label || result.uri }}</a></td>
<td>{{ result.fcLabel }}</td>
</tr>
</template>
Expand Down
46 changes: 25 additions & 21 deletions src/sparqlQueries/spatialSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,45 @@ export function spatialSearchQuery(
shape = `POLYGON ((${coords.map(coord => `${coord[0]} ${coord[1]}`).join(', ')}))`;
}

let spatialFilter = "";

switch(areaType) {
case AreaTypes.Contains:
spatialFilter = `FILTER (geof:sfContains("${shape}"^^geo:wktLiteral, ?wkt))`;
break;
case AreaTypes.Within:
spatialFilter = `FILTER (geof:sfWithin("${shape}"^^geo:wktLiteral, ?wkt))`;
break;
case AreaTypes.Nearby:
spatialFilter = `FILTER (spatialF:nearby("${shape}"^^geo:wktLiteral, ?wkt, ${radius}, unit:kilometre))`;
break;
case AreaTypes.Overlaps:
spatialFilter = `FILTER (geof:sfOverlaps("${shape}"^^geo:wktLiteral, ?wkt))`;
break;
default:
spatialFilter = '';
}

return `PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX spatialF: <http://jena.apache.org/function/spatial#>
PREFIX unit: <http://www.opengis.net/def/uom/OGC/1.0/>
SELECT ?f ?wkt ?fc ?fc_label ?label ?desc ?type ?type_label
SELECT ?f_uri ?wkt ?fc ?fc_label ?f_label
WHERE {
VALUES ?fc {${featureCollections.map(fc => `<${fc}>`).join(' ')}}
?fc rdfs:member ?f .
?f a ?type ;
geo:hasGeometry/geo:asWKT ?wkt .
?fc rdfs:member ?f_uri .
?f_uri geo:hasGeometry/geo:asWKT ?wkt .
OPTIONAL {
?f <${config.props.fLabel}> ?label .
?f_uri <${config.props.fLabel}> ?f_label .
}
OPTIONAL {
?fc <${config.props.fcLabel}> ?fc_label .
}
OPTIONAL {
?f sdo:description ?desc . # config for desc predicate?
}
OPTIONAL {
?type rdfs:label ?type_label .
}
${() => {switch(areaType) {
case AreaTypes.Contains: return `FILTER (geof:sfContains("${shape}"^^geo:wktLiteral, ?wkt))`;
case AreaTypes.Within: return `FILTER (geof:sfWithin("${shape}"^^geo:wktLiteral, ?wkt))`;
case AreaTypes.Nearby: return `FILTER (spatialF:nearby("${shape}"^^geo:wktLiteral, ?wkt, ${radius}, unit:kilometre))`;
case AreaTypes.Overlaps: return `FILTER (geof:sfOverlaps("${shape}"^^geo:wktLiteral, ?wkt))`;
default:
return '';
}}}
${spatialFilter}
}
LIMIT ${limit}`;
};

0 comments on commit fc05e5d

Please sign in to comment.