From 5462e731295ce4693e5f7f9ac87ef7dac19e04eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Wed, 4 Sep 2024 10:36:41 +0200 Subject: [PATCH] Allow unlocated features --- dist/Leaflet.GeoSSE.min.js | 2 +- src/Leaflet.GeoSSE.src.js | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/dist/Leaflet.GeoSSE.min.js b/dist/Leaflet.GeoSSE.min.js index 7ed1215..0802e5f 100644 --- a/dist/Leaflet.GeoSSE.min.js +++ b/dist/Leaflet.GeoSSE.min.js @@ -1 +1 @@ -"use strict";const GeoSSE=L.GeoJSON.extend({connectToEventStream:function(){function addFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to add "+"a feature, so it can be updated or removed.",feature)}self.addData(feature)}function updateOrAddFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to add "+"a feature, so it can be updated or removed.",feature)}const layer=getLayer(feature);if(!layer)return self.addData(feature);const{geometry:geometry}=layer.feature;const{type:type}=feature.geometry;if(geometry.type!==type)return console.warn("Feature type mismatch. Existing feature type is ${geometry.type} "+`and new feature type is ${type}.`,feature);const{coordinates:[lng,lat]}=feature.geometry;layer.setLatLng({lat:lat,lng:lng});layer.feature=feature;self.resetStyle(layer);const{onEachFeature:onEachFeature}=self.options;if(onEachFeature)onEachFeature(feature,layer)}function finderId({feature:{id:id}}){return id===this.id}function finderIdField({feature:{properties:properties}}){const{featureIdField:featureIdField}=self.options;return properties[featureIdField]===this.properties[featureIdField]}function getLayer(feature){const finder=self.options.featureIdField?finderIdField:finderId;return self.getLayers().find(finder,feature)}function removeFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to "+"delete a feature.",feature)}const layer=getLayer(feature);if(layer){self.removeLayer(layer)}}function add(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return updateOrAddFeature(geojson)}geojson.features.forEach(updateOrAddFeature)}function create(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return addFeature(geojson)}geojson.features.forEach(addFeature)}function update(event){const{options:{featureIdField:featureIdField}}=self;const geojson=JSON.parse(event.data);const{properties:{[featureIdField]:id}}=geojson;for(let layer of self.getLayers())if(layer.feature.properties[featureIdField]===id){self.removeLayer(layer);self.addData(geojson)}}function remove(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return removeFeature(geojson)}geojson.features.forEach(removeFeature)}const self=this;if(typeof this.options.streamUrl==="undefined"){throw Error("Undefined event streamUrl.")}const source=new EventSource(this.options.streamUrl);source.addEventListener("add",add,false);source.addEventListener("remove",remove,false);source.addEventListener("create",create,false);source.addEventListener("update",update,false);source.addEventListener("delete",remove,false);source.onopen=function(event){};source.onmessage=function(event){};source.onerror=function(event){if(source.readyState===2){self.connectToEventStream()}};this.eventSource=source},disconnect:function(){this.eventSource.close()},setStreamUrl:function(newStreamUrl){this.options.streamUrl=newStreamUrl},setFeatureIdField:function(fieldName){this.options.featureIdField=fieldName},switchStream:function(newStream,featureIdField,emptyLayer=false){if(emptyLayer){this.clearLayers()}this.setStreamUrl(newStream);if(featureIdField!==undefined){this.setFeatureIdField(featureIdField)}this.disconnect();this.connectToEventStream()}});L.geoSSE=function(data,options){return new GeoSSE(data,options)}; \ No newline at end of file +"use strict";const GeoSSE=L.GeoJSON.extend({connectToEventStream:function(){function addFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to add "+"a feature, so it can be updated or removed.",feature)}self.addData(feature)}function updateOrAddFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to add "+"a feature, so it can be updated or removed.",feature)}const layer=getLayer(feature);if(!layer)return self.addData(feature);const{geometry:geometry}=layer.feature;if(geometry===null)return console.warn("Feature is unlocated.",feature);const{coordinates:[lng,lat],type:type}=feature.geometry;if(geometry.type!==type)console.warn(`Feature type mismatch: existing feature type is ${geometry.type} `+`and new feature type is ${type}.`,feature);layer.setLatLng({lat:lat,lng:lng});layer.feature=feature;self.resetStyle(layer);const{onEachFeature:onEachFeature}=self.options;if(onEachFeature)onEachFeature(feature,layer)}function finderId({feature:{id:id}}){return id===this.id}function finderIdField({feature:{properties:properties}}){const{featureIdField:featureIdField}=self.options;return properties[featureIdField]===this.properties[featureIdField]}function getLayer(feature){const finder=self.options.featureIdField?finderIdField:finderId;return self.getLayers().find(finder,feature)}function removeFeature(feature){if(!(self.options.featureIdField||feature.id)){return console.warn("`featureIdField` option or Feature `id` field are required to "+"delete a feature.",feature)}const layer=getLayer(feature);if(layer){self.removeLayer(layer)}}function add(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return updateOrAddFeature(geojson)}geojson.features.forEach(updateOrAddFeature)}function create(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return addFeature(geojson)}geojson.features.forEach(addFeature)}function update(event){const{options:{featureIdField:featureIdField}}=self;const geojson=JSON.parse(event.data);const{properties:{[featureIdField]:id}}=geojson;for(let layer of self.getLayers())if(layer.feature.properties[featureIdField]===id){self.removeLayer(layer);self.addData(geojson)}}function remove(event){const geojson=JSON.parse(event.data);if(geojson.type==="Feature"){return removeFeature(geojson)}geojson.features.forEach(removeFeature)}const self=this;if(typeof this.options.streamUrl==="undefined"){throw Error("Undefined event streamUrl.")}const source=new EventSource(this.options.streamUrl);source.addEventListener("add",add,false);source.addEventListener("remove",remove,false);source.addEventListener("create",create,false);source.addEventListener("update",update,false);source.addEventListener("delete",remove,false);source.onopen=function(event){};source.onmessage=function(event){};source.onerror=function(event){if(source.readyState===2){self.connectToEventStream()}};this.eventSource=source},disconnect:function(){const{eventSource:eventSource}=this;if(!eventSource)return;eventSource.close();this.eventSource=null},setStreamUrl:function(newStreamUrl){this.options.streamUrl=newStreamUrl},setFeatureIdField:function(fieldName){this.options.featureIdField=fieldName},switchStream:function(newStream,featureIdField,emptyLayer=false){if(emptyLayer){this.clearLayers()}this.setStreamUrl(newStream);if(featureIdField!==undefined){this.setFeatureIdField(featureIdField)}this.disconnect();this.connectToEventStream()}});L.geoSSE=function(data,options){return new GeoSSE(data,options)}; \ No newline at end of file diff --git a/src/Leaflet.GeoSSE.src.js b/src/Leaflet.GeoSSE.src.js index 0750ead..1a5b5a6 100644 --- a/src/Leaflet.GeoSSE.src.js +++ b/src/Leaflet.GeoSSE.src.js @@ -37,10 +37,15 @@ const GeoSSE = L.GeoJSON.extend({ // Layer not found, add it if (!layer) return self.addData(feature); - // Check for feature type mismatch const {geometry} = layer.feature; - const {type} = feature.geometry; + // Check if the feature is unlocated + if(geometry === null) + return console.warn("Feature is unlocated.", feature); + + const {coordinates: [lng, lat], type} = feature.geometry; + + // Check for feature type mismatch if(geometry.type !== type) console.warn( `Feature type mismatch: existing feature type is ${geometry.type} ` + @@ -48,7 +53,6 @@ const GeoSSE = L.GeoJSON.extend({ ); // Update the position of the Marker - const {coordinates: [lng, lat]} = feature.geometry; layer.setLatLng({lat, lng}); // Re-init the layer