Skip to content

Commit

Permalink
Added getLayerByName and getFeatureById api function to the map compo…
Browse files Browse the repository at this point in the history
…nent
  • Loading branch information
ka-pr committed Jun 13, 2024
1 parent 758be71 commit 335f80c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
35 changes: 35 additions & 0 deletions boranga/frontend/boranga/src/components/common/component_map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4883,6 +4883,10 @@ export default {
layer.set('editing', false);
}
},
/**
* Queries the tenure layer at point coordinates and pans/zooms to coordinates
* @param {Array} coordinates Point coordinates
*/
highlightPointOnTenureLayer: function (coordinates) {
if (!coordinates) {
return;
Expand All @@ -4898,6 +4902,37 @@ export default {
});
this.centerOnFeature(feature, 12);
},
/**
* Returns a layer by its name
* @param {String} layer_name The 'name' property of the layer. Corresponds to the 'name' property in the layer definition prop
*/
getLayerByName: function (layer_name) {
return this.map
.getLayers()
.getArray()
.find((layer) => {
return layer.get('name') == layer_name;
});
},
/**
* Returns a layer's feature by its id
* @param {Object} layer A layer
* @param {String|Number} feature_id The id of a feature on the layer
*/
getFeatureById: function (layer, feature_id) {
let featureId = Number(feature_id);
if (isNaN(featureId)) {
console.error(`Feature ID ${feature_id} is not a number`);
return;
}
return layer
.getSource()
.getFeatures()
.find((feature) => {
// The features name property is the model instance pk
return feature.getProperties().name == featureId;
});
},
},
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
:spatial-operations-allowed="['__all__']"
:tile-layer-api-url="tileLayerApiUrl"
:query-layer-definition="{
name: 'query_layer',
name: queryLayerName,
title: 'Occurrence Reports',
default: false,
can_edit: true,
Expand Down Expand Up @@ -325,6 +325,7 @@ export default {
coordination_source_list: [],
location_accuracy_list: [],
mapReady: false,
queryLayerName: 'query_layer',
};
},
computed: {
Expand Down Expand Up @@ -590,7 +591,10 @@ export default {
this.$refs.component_map.highlightPointOnTenureLayer(coordinates);
},
highlightIdOnMapLayer: function (id) {
console.log(id);
const map = this.$refs.component_map;
const layer = map.getLayerByName(this.queryLayerName);
const feature = map.getFeatureById(layer, id);
map.centerOnFeature(feature, 12);
},
},
};
Expand Down

0 comments on commit 335f80c

Please sign in to comment.