From 9d2994b240c98fec6a158eff0bdbc3c878116e16 Mon Sep 17 00:00:00 2001 From: Robin <43668539+robin-natale@users.noreply.github.com> Date: Mon, 2 Mar 2020 17:45:40 +0000 Subject: [PATCH 1/2] Add files via upload --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2258e1271..5f26fca2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -Contributing to Leaflet Environmental Layers +Robin Contributing to Leaflet Environmental Layers ========================== We welcome community contributions to Leaflet Environmental Layers! For local installation, follow the instructions in the [README.md file](./README.md). From 4722cb58a1cca89acb0d51a0a0daa934a05b2d93 Mon Sep 17 00:00:00 2001 From: Robin <43668539+robin-natale@users.noreply.github.com> Date: Mon, 2 Mar 2020 17:53:58 +0000 Subject: [PATCH 2/2] Add files via upload Step 1 completed --- layercode.js | 545 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 545 insertions(+) create mode 100644 layercode.js diff --git a/layercode.js b/layercode.js new file mode 100644 index 000000000..b1f64d0f3 --- /dev/null +++ b/layercode.js @@ -0,0 +1,545 @@ +require('jquery'); +require('leaflet'); + +L.LayerGroup.LayerCode = L.LayerGroup.extend( + + { + options: { + popupOnMouseover: false, + clearOutsideBounds: true, + target: '_self', + }, + + initialize: function(name, options) { + this.layer = name; + options = options || {}; + L.Util.setOptions(this, options); + this._layers = {}; + }, + + onAdd: function(map) { + var info = require('./info.json'); + this._map = map; + switch(this.layer) { + case this.layer: + if (this.layer === 'fractracker' || this.layer === 'skytruth' || + this.layer === 'odorreport' || this.layer === 'mapknitter') { + map.on('moveend', function() { + if(this._map && this._map.getZoom() > info[this.layer].extents.minZoom - 1) { + this.requestData(); + } + }, this); + } + default: + if (this.layer === 'luftdaten' || this.layer === 'openaq' || + this.layer === 'opensense' || this.layer === 'purpleairmarker') { + map.on('moveend', this.requestData, this); + } + this.requestData(); + } + }, + + onRemove: function(map) { + map.off('moveend', this.requestData, this); + if (typeof map.spin === 'function') { + map.spin(false); + } + this.clearLayers(); + map.closePopup(); + // oms.clearMarkers(); + this._layers = {}; + }, + + + requestData: function() { + var self = this; + var info = require('./info.json'); + (function() { + var zoom; + var Layer_URL; + var $ = window.jQuery; + + if (self.layer === 'fractracker') { + Layer_URL = info.fractracker.api_url; ; + } + if (self.layer === 'skytruth') { + zoom = self._map.getZoom(), northeast = self._map.getBounds().getNorthEast(), southwest = self._map.getBounds().getSouthWest(); + Layer_URL = info.skytruth.api_url + '?n=100&l='+(southwest.lat)+','+(southwest.lng)+','+(northeast.lat)+','+(northeast.lng); + } + if (self.layer === 'odorreport') { + zoom = self._map.getZoom(), origin = self._map.getCenter(); + Layer_URL = info.odorreport.api_url; + } + if (self.layer === 'mapknitter') { + zoom = self._map.getZoom(), northeast = self._map.getBounds().getNorthEast(), southwest = self._map.getBounds().getSouthWest(); + Layer_URL = info.mapknitter.api_url + '?minlon='+(southwest.lng)+'&minlat='+(southwest.lat)+'&maxlon='+(northeast.lng)+'&maxlat='+(northeast.lat); + } + if (self.layer === 'luftdaten') { + Layer_URL = 'https://maps.luftdaten.info/data/v2/data.dust.min.json'; + } + if (self.layer === 'openaq') { + Layer_URL = 'https://api.openaq.org/v1/latest?limit=5000'; + } + if (self.layer === 'opensense') { + Layer_URL = 'https://api.opensensemap.org/boxes'; + } + if (self.layer == 'purpleairmarker') { + zoom = self._map.getZoom(), northwest = self._map.getBounds().getNorthWest(), southeast = self._map.getBounds().getSouthEast(); + if (zoom < info.purpleair.layers.purpleairmarker.extents.minZoom) { + return; + } + Layer_URL = info.purpleair.api_url + '?fetchData=true&minimize=true&sensorsActive2=10080&orderby=L&nwlat='+(northwest.lat)+'&selat='+(southeast.lat)+'&nwlng='+(northwest.lng)+'&selng='+(southeast.lng); + } + + + if (self._map && typeof self._map.spin === 'function') { + self._map.spin(true); + } + $.getJSON(Layer_URL, function(data) { + if (self.layer == 'fractracker') + { self.parseData(data.feed.entry); } + if (self.layer == 'openaq') + { self.parseData(data.results); } + else + { self.parseData(data); } + if (self._map && typeof self._map.spin === 'function') { + self._map.spin(false); + } + }); + })(); + }, + + getMarker: function(data) { + if (this.layer == 'fractracker') { + var redDotIcon = new L.icon.fracTrackerIcon(); + var props = ['timestamp', 'name', 'summary', 'website', 'contact', 'email', 'phone', 'streetaddress', 'city', 'state', 'zipcode', 'latitude', 'longitude', 'category']; + var item = {}; + props.forEach(function(element) { + item[element] = data['gsx$' + element]['$t']; + }); + item['updated'] = data.updated.$t; + item['use'] = (data.gsx$useformap.$t.replace(/\s+/g, '').toLowerCase() === 'use'); + item['latitude'] = item['latitude'].replace(/[^\d.-]/g, ''); + item['latitude'] = item['latitude'].replace(/[^\d.-]/g, ''); + var defaultMarker = L.marker([item['latitude'], item['longitude']], { + icon: redDotIcon, + }); + var minimalMarker = L.circleMarker(L.latLng([item['latitude'], item['longitude']]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#e8e800' }); + var content = this.generatePopup(item); + var fracTracker; + fracTracker = this._map && this._map._minimalMode ? minimalMarker : defaultMarker; + return fracTracker.bindPopup(content); + } + + if (this.layer == 'skytruth') { + var redDotIcon =new L.icon.skyTruthIcon(); + var lat = data.lat; + var lng = data.lng; + var title = data.title; + var url = data.link; + var defaultMarker = L.marker([lat, lng], {icon: redDotIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#f00' }); + var content = '' +title + '
' + + '
lat: ' + lat + + '
lon: ' + lng + + '

Data provided by alerts.skytruth.org/'; + var skymarker; + if (!isNaN(lat) && !isNaN(lng) ) { + skymarker = this._map && this._map._minimalMode ? minimalMarker.bindPopup(content) : defaultMarker.bindPopup(content); + } + return skymarker; + } + + if (this.layer == 'odorreport') { + var redDotIcon =new L.icon.odorReportIcon(); + var lat = data.values['bcc29002-c4d3-4c2c-92c7-1c9032c3b0fd'][0].lat; + var lng = data.values['bcc29002-c4d3-4c2c-92c7-1c9032c3b0fd'][0].lon; + var title = data.title; + var url = data.url; + var defaultMarker = L.marker([lat, lng], {icon: redDotIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#ff00ff' }); + var content = title + + '
' + url +'' + + '
lat: ' + lat + + '
lon: ' + lng + + '

Data provided by https://odorlog.ushahidi.io'; + var odormarker; + if (!isNaN(lat) && !isNaN(lng) ) { + odormarker = this._map && this._map._minimalMode ? minimalMarker.bindPopup(content) : defaultMarker.bindPopup(content); + } + // oms.addMarker(odormarker); + return odormarker; + } + + if (this.layer == 'mapknitter') { + var redDotIcon =new L.icon.mapKnitterIcon(); + var lat = data.lat; + var lng = data.lon; + var title = data.name; + var location = data.location; + var author = data.author; + var url = 'https://publiclab.org/profile/' + author; + var map_page = 'https://mapknitter.org/maps/'+ title; + var image_url; + if (data.image_urls.length > 0) { + image_url = data.image_urls[0]; + } + var defaultMarker = L.marker([lat, lng], {icon: redDotIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#ca283b' }); + var content = 'Title : '+ '' + title + '' + + '
Author : ' + '' + author +'' + + '
Location : ' + location + + '
Lat : ' + lat + ' , Lon : ' + lng + + '
'+ + '
For more info on MapKnitter Layer, visit here'; + var mapknitter; + if (!isNaN(lat) && !isNaN(lng) ) { + if (image_url !== undefined) { + mapknitter = this._map && this._map._minimalMode ? minimalMarker.bindPopup(content) : defaultMarker.bindPopup(content); + // oms.addMarker(mapknitter); + } + else { + content = 'Title : '+ '' + title + '' + + '
Author : ' + '' + author +'' + + '
Location : ' + location + + '
Lat : ' + lat + ' , Lon : ' + lng + + '
For more info on MapKnitter Layer, visit here'; + mapknitter = this._map && this._map._minimalMode ? minimalMarker.bindPopup(content) : defaultMarker.bindPopup(content); + // oms.addMarker(mapknitter); + } + } + return mapknitter; + } + + if (this.layer == 'luftdaten') + { + var greenIcon = new L.icon.luftdatenIcon(); + var country = data.location.country; + var lng = data.location.longitude; + var lat = data.location.latitude; + var sensorID = data.sensor.id; + var popupContent = ''; + if (country) { + popupContent += '

Country: ' + country + '

'; + } + if (sensorID) { + popupContent += '

Sensor ID: ' + sensorID + '

'; + } + if (data.sensordatavalues.length > 0) { + for (const i in data.sensordatavalues) { + popupContent += '' + data.sensordatavalues[i].value_type + ': ' + data.sensordatavalues[i].value + '
'; + } + } + + var defaultMarker = L.marker([lat, lng], {icon: greenIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#4edd51' }); + var marker = this._map && this._map._minimalMode ? minimalMarker : defaultMarker; + return marker.bindPopup(popupContent); + } + + if (this.layer == 'openaq') + { + var redDotIcon = new L.icon.openaqIcon(); + var distance = data.distance; + var lat = data.coordinates.latitude; + var lon = data.coordinates.longitude; + var contentData = ''; + var labels = { + pm25: 'PM2.5', + pm10: 'PM10', + o3: 'Ozone', + no2: 'Nitrogen Dioxide', + so2: 'Sulphur Dioxide', + co: 'Carbon Monoxide', + }; + for (var i = 0; i < data.measurements.length; i++) { + contentData+=''+labels[data.measurements[i].parameter]+' : '+data.measurements[i].value+' '+data.measurements[i].unit+'
'; + } + var defaultMarker = L.marker([lat, lon], {icon: redDotIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lon]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#912d25' }); + var content = '

'+data.location+', '+data.country+'


'+ + 'distance: '+''+data.distance+'
'+contentData; + var marker = this._map && this._map._minimalMode ? minimalMarker : defaultMarker; + return marker.bindPopup(content); + } + + if (this.layer == 'opensense') + { + var blackCube = new L.icon.openSenseIcon(); + var lat = data.currentLocation.coordinates[1]; + var lng = data.currentLocation.coordinates[0]; + var loadingText = 'Loading ...'; + var defaultMarker = L.marker([lat, lng], {icon: blackCube, boxId: data._id}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#262626' }); + var marker = this._map && this._map._minimalMode ? minimalMarker : defaultMarker; + return marker.bindPopup(loadingText); + } + + if (this.layer == 'purpleairmarker') + { + var redDotIcon =new L.icon.purpleAirMarkerIcon(); + var lat = data[25]; + var lng = data[26]; + var value = parseFloat(data[16]); + var Label = data[24]; + var temp_f = data[21]; + var humidity = data[20]; + var pressure = data[22]; + var defaultMarker = L.marker([lat, lng], {icon: redDotIcon}); + var minimalMarker = L.circleMarker(L.latLng([lat, lng]), { radius: 5, weight: 1, fillOpacity: 1, color: '#7c7c7c', fillColor: '#7c22b5' }); + var content = 'Label : ' + Label + '

PM2.5 Value : ' + value +'
Lat: ' + lat + '
Lon: ' + lng + '
Temp (F) : '+temp_f+'
Humidity : ' + humidity + '
Pressure : ' + pressure +'

Data provided by
www.purpleair.com'; + var purpleAirMarker; + if (lat!=null && lng!=null) { + purpleAirMarker = this._map && this._map._minimalMode ? minimalMarker.bindPopup(content) : defaultMarker.bindPopup(content); + } + return purpleAirMarker; + } + }, + + generatePopup: function(item) { + if (this.layer == 'fractracker') { + var content = '' + item['name'] + ' '; + if (item['website']) content += '(website' + ')'; + content += '
'; + if (!!item['Descrition']) content += 'Description: ' + item['summary'] + '
'; + if (!!item['contact']) content += 'Contact: ' + item['contact'] + '
'; + var generics = ['phone', 'email', 'street', 'city', 'state', 'zipcode', 'timestamp', 'latitude', 'longitude']; + for (var i = 0; i < generics.length; i++) { + var key = generics[i]; + if (!!item[generics[i]]) { + itemContent = item[generics[i]]; + key = key.charAt(0).toUpperCase() + key.slice(1); + content += key + ': ' + itemContent + '
'; + } + } + content += '
Data last updated ' + item['updated'] + '
'; + content += 'Data provided by http://fractracker.org/'; + return content; + } + }, + + addMarker: function(data) { + if (this.layer == 'fractracker') { + var key = data.gsx$name.$t; + if (!this._layers[key]) { + var marker = this.getMarker(data); + this._layers[key] = marker; + this.addLayer(marker); + } + } + if (this.layer == 'purpleairmarker') { + var marker = this.getMarker(data); + if (marker != null) { + key = data[0]; // ID + if (!this._layers[key]) { + this._layers[key] = marker; + this.addLayer(marker); + } + } + } + else { + var marker = this.getMarker(data); + var key = data.id; + if (!this._layers[key]) { + this._layers[key] = marker; + this.addLayer(marker); + } + } + }, + + addMarker1: function(data, i) { + if (this.layer == 'luftdaten' || this.layer == 'openaq') { + var self = this; + var marker = this.getMarker(data); + var key = i; + if (!this._layers[key]) { + this._layers[key] = marker; + this.addLayer(marker); + } + } + }, + + parseData: function(data) { + if (this.layer == 'fractracker') { + for (i = 1; i < data.length; i++) { + this.addMarker(data[i]); + } + } + if (this.layer == 'skytruth') { + if (!!data.feed) { + for (i = 0; i < data.feed.length; i++) { + this.addMarker(data.feed[i]); + } + if (this.options.clearOutsideBounds && this._map) { + this.clearOutsideBounds(); + } + } + } + if (this.layer == 'odorreport') { + if (data.total_count != 0 ) { + for (i = 0; i < data.total_count; i++) { + this.addMarker(data.results[i]); + } + if (this.options.clearOutsideBounds && this._map) { + this.clearOutsideBounds(); + } + } + } + if (this.layer == 'mapknitter') { + for (i = 0; i < data.length; i++) { + this.addMarker(data[i]); + } + if (this.options.clearOutsideBounds && this._map) { + this.clearOutsideBounds(); + } + } + if (this.layer == 'luftdaten' || this.layer == 'opensense') { + for (var i = 0; i < data.length; i++) { + this.addMarker1(data[i], i); + } + } + if (this.layer == 'openaq') { + if (!!data) { + for (var i = 0; i