/** * rechercher les bornes de recharge, * afficher des cercles colorés selon la puissance max de la station * lister les bornes trouvées dans la page * @type {boolean} */ let showHighPower = true const overrideQuery = true const initialZoom = 12 const osmMention = '© OpenStreetMap contributors' let unknown_color = '#c0b1b1' // color for unknown power output of the station // serveurs de tuiles: https://wiki.openstreetmap.org/wiki/Tile_servers // https://stamen-tiles.a.ssl.fastly.net/toner/{z}/{x}/{y}.png // https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png // https://tile.openstreetmap.org/{z}/{x}/{y}.png // 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png' const tileServer = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png' // Créer la carte centrée sur Rouen var map = L.map('map').setView([49.4438, 1.0993], initialZoom) // 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];' + // 'area(id:3600075628)->.searchArea;' + // 'node[amenity=charging_station](area.searchArea);' + // 'out body geom;' L.tileLayer(tileServer, { maxZoom: 19, attribution: osmMention, }).addTo(map) let filteredMarkers = L.layerGroup().addTo(map) function buildOverpassApiUrl (map, overpassQuery) { var baseUrl = 'https://overpass-api.de/api/interpreter' var bounds = map.getBounds().getSouth() + ',' + map.getBounds().getWest() + ',' + map.getBounds().getNorth() + ',' + map.getBounds().getEast() var resultUrl, query = '' if (overrideQuery) { query = `?data=[out:json][timeout:15];( node[amenity=charging_station](${bounds}); );out body geom;` } else { var nodeQuery = 'node[' + overpassQuery + '](' + bounds + ');' var wayQuery = 'way[' + overpassQuery + '](' + bounds + ');' var relationQuery = 'relation[' + overpassQuery + '](' + bounds + ');' query = '?data=[out:json][timeout:15];(' + nodeQuery + wayQuery + relationQuery + ');out body geom;' } resultUrl = baseUrl + query return resultUrl } const tags_to_display_in_popup = [ 'name', 'capacity', 'date_start', 'charging_station:output', 'socket:type_2', 'socket:type2:output', 'socket:typee', 'socket:typee:output', 'socket:type2_combo', 'socket:type2_combo:output', 'socket:chademo', 'operator', 'ref:EU:EVSE', 'network', 'opening_hours', 'contact', 'phone', 'contact:phone', 'website', 'contact:website', 'ref', 'fee', 'payment', 'payment:contactless', 'authentication:app', 'authentication:debit_card', ] function supprimerMarqueurs (map) { map.eachLayer((layer) => { if (layer instanceof L.Marker) { layer.remove() } }) } const colors = [ '#36423d', '#4e8a8d', '#2999b3', '#1782dd', '#2900ff', '#8000ff', ] function guessOutputPowerFromFeature (feature) { let outputPower = 0 if (feature.properties && feature.properties.tags) { /** * fouiller dans les tags les valeurs explicites de puissance déclarée. * Deviner aussi les puissances non déclarées: * - type 2 présent, max 43kW * - type Chademo présent, max 63kW * https://forum.openstreetmap.fr/t/bornes-de-recharges-et-puissance-chargeurs-quel-est-votre-avis/27828 * */ let found_type_2 = false let found_type_chademo = false for (var tag in feature.properties.tags) { if (tag.indexOf('type2') !== -1) { // console.log('tag type2', tag) found_type_2 = true power = 43 } if (tag.indexOf('chademo') !== -1) { found_type_chademo = true console.log('tag chademo', tag) power = 63 } let value = feature.properties.tags[tag] if (value && tag.toLowerCase().indexOf('output') !== -1) { console.log('tag contient output', tag, value) value = '' + value if (value.replace) { value = value.replace(' ') value = value.replace('kW', '') } var power = parseInt(value) // deviner les types de prises présents if (power) { console.log('power', power) console.log('outputPower', outputPower) } if (power > outputPower) { outputPower = power console.log('power', power) } } } } feature.properties.outputPower = outputPower return outputPower } function getColor (feature) { let outputPower = guessOutputPowerFromFeature(feature) feature.properties.tags.has_output_of_irve_specified = outputPower if (outputPower) { var index = Math.min(Math.floor(outputPower / 10), colors.length - 1) console.log('outputPower', outputPower) // console.log('colors[index]', colors[index]) return colors[index] } // autrement, sans puissance max trouvée, on met la couleur des indéfinis return unknown_color } let coef_reduction_bars = 0.8 function calculerPourcentage (partie, total, reduc) { if (total === 0) { return 'Division par zéro impossible' } let coef_reduction = 1 if (reduc) { coef_reduction = coef_reduction_bars } return ((partie / total) * 100 * coef_reduction).toFixed(1) } function displayStatsFromGeoJson (resultAsGeojson) { let count = resultAsGeojson.features.length let count_station_output = 0 let count_ref_eu = 0 let output_more_than_300 = 0 let output_more_than_200 = 0 let output_more_than_100 = 0 let output_more_than_50 = 0 let count_station_outputoutput_between_1_and_50 = 0 let count_output_unknown = 0 let count_estimated_type2combo = 0 let count_found_type2combo = 0 let count_found_type2 = 0 resultAsGeojson.features.map(feature => { let found_type2_combo = false // trouver si les tags présentent un type combo let found_type2 = false // trouver si les tags présentent un type 2 let keys_of_object = Object.keys(feature.properties.tags) keys_of_object.map(tagKey => { // console.log('tagKey', tagKey) if (tagKey.indexOf('type2_combo') !== -1) { found_type2_combo = true console.log('tagkey trouvé combo', tagKey) } if (tagKey.indexOf('type2') !== -1) { found_type2 = true } }) let outputPower = guessOutputPowerFromFeature(feature) if (found_type2_combo) { count_found_type2combo++ } if (found_type2) { count_found_type2++ } if (outputPower == 0) { count_output_unknown++ } if (outputPower >= 200 && !found_type2_combo) { /** * si on trouve une puissance supérieure à 200kW on peut partir du principe que la station dispose d'une prise type_2_combo à minima */ count_estimated_type2combo++ } if (outputPower > 0 && outputPower < 50) { count_station_outputoutput_between_1_and_50++ } if (outputPower >= 50 && outputPower < 100) { output_more_than_50++ } else if (outputPower >= 100 && outputPower < 200) { output_more_than_100++ } else if (outputPower >= 200 && outputPower < 300) { output_more_than_200++ } else if (outputPower >= 300) { feature.properties.puissance_haute = true output_more_than_300++ } if (feature.properties.tags['charging_station:output']) { count_station_output++ } if (feature.properties.tags['ref:EU:EVSE']) { count_ref_eu++ } }) let bar_powers = `
${count_output_unknown}
${count_station_outputoutput_between_1_and_50}
${output_more_than_50}
${output_more_than_100}
${output_more_than_200}
${output_more_than_300}
` let stats_content = `
Statistiques des ${count} stations trouvées:
${count_station_output} (${calculerPourcentage(count_station_output, count)}%) ont une info de puissance max délivrée charging_station:output.
${count_ref_eu} (${calculerPourcentage(count_ref_eu, count)}%) ont une référence européenne ref:EU:EVSE.
${count_output_unknown} (${calculerPourcentage(count_output_unknown, count)}%) ont une puissance max inconnue *output*.
${output_more_than_300} (${calculerPourcentage(output_more_than_300, count)}%) ont une puissance max supérieure à 300 kW *output*.
${output_more_than_200} (${calculerPourcentage(output_more_than_200, count)}%) ont une puissance max supérieure à 200 kW *output*.
${output_more_than_100} (${calculerPourcentage(output_more_than_100, count)}%) ont une puissance max supérieure à 100 kW *output*.
${output_more_than_50} (${calculerPourcentage(output_more_than_50, count)}%) ont une puissance max supérieure à 50 kW *output*.
${count_found_type2combo} (${calculerPourcentage(count_found_type2combo, count)}%) ont un prise combo définie *type2_combo*.
${count_estimated_type2combo} (${calculerPourcentage(count_estimated_type2combo, count)}%) ont une prise combo présumée à partir de la puissance max trouvée mais non spécifiée *type2_combo*.
${count_found_type2} (${calculerPourcentage(count_found_type2, count)}%) ont un prise type2 définie *type2*.
` $('#found_charging_stations').html(stats_content) $('#bars_power').html(bar_powers) } let geojsondata; function displayPointsFromApi (points) { geojsondata = osmtogeojson(points) console.log('resultAsGeojson', geojsondata) displayStatsFromGeoJson(geojsondata) var resultLayer = L.geoJson(geojsondata, { style: function (feature) { return { color: '#f00' } }, /** * enlever les polygones, ne garder que les points * @param feature * @param layer * @returns {boolean} */ filter: function (feature, layer) { var isPolygon = (feature.geometry) && (feature.geometry.type !== undefined) && (feature.geometry.type === 'Polygon') if (isPolygon) { feature.geometry.type = 'Point' var polygonCenter = L.latLngBounds(feature.geometry.coordinates[0]).getCenter() feature.geometry.coordinates = [polygonCenter.lat, polygonCenter.lng] } return true }, onzoomend: function (event) { console.log('event', event) }, onEachFeature: function (feature, layer) { let popupContent = '' // ne montrer que certains champs dans la popup tags_to_display_in_popup.forEach(function (key) { if (tags_to_display_in_popup.indexOf(key)) { let value = feature.properties.tags[key] if (value) { if (value.indexOf('http') !== -1) { value = '' + value + '' } popupContent = popupContent + '
' + key + ' :' + value + '' } } }) // popupContent = popupContent + '' layer.bindPopup(popupContent) let outPowerGuessed = guessOutputPowerFromFeature(feature) let color = getColor(feature) let displayOutPowerGuessed = '? kW' if (outPowerGuessed) { displayOutPowerGuessed = outPowerGuessed + ' kW max' } if (!popupContent) { popupContent = ` Aucune information renseignée, ajoutez la dans OpenStreetMap!` } let html = `✏️JOSM ${displayOutPowerGuessed}${popupContent}` let radius = 20 if (outPowerGuessed > 300) { radius = 200 } else if (outPowerGuessed > 200) { radius = 250 } else if (outPowerGuessed > 100) { radius = 150 } else if (outPowerGuessed > 50) { radius = 100 } else if (outPowerGuessed > 20) { radius = 50 } else if (outPowerGuessed > 7) { radius = 20 } let circle = L.circle(layer._latlng, { color: color, fillColor: color, fillOpacity: 0.8, radius: radius }).addTo(map) circle.bindPopup(html) circle.on({ mouseover: function () { this.openPopup() }, mouseout: function () { setTimeout(() => this.closePopup(), 3000) }, click: function () { this.openPopup() }, }) }, }) } function makeCssClassFromTags (tags) { console.log('tags', tags) let tagKeys = Object.keys(tags) console.log('tagKeys', tagKeys) if (!tags) { return '' } let listOfClasses = [] tagKeys.forEach((element) => { listOfClasses.push('tag-' + element + '_' + tags[element].replace(':', '--').replace(' ', '-')) }) return listOfClasses.join(' ') } function getIconFromTags (tags) { let iconFileName = '' // let iconFileName = 'icon_restaurant.png'; if (tags['man_made']) { iconFileName = 'fountain.png' } return iconFileName } // $('#toggleMinPower_50').on('click', toggleMinPower(50)) // $('#toggleMinPower_100').on('click', toggleMinPower(100)) // document.getElementById('toggleMinPower_300').addEventListener('click', toggleMinPower(showHighPower)) $('#query-button').on('click', function () { supprimerMarqueurs(map) loadOverpassQuery() }) function toggleMinPower (showHighPower) { console.log('toggle', showHighPower) showHighPower = !showHighPower; addFilteredMarkers(showHighPower); this.textContent = showHighPower ? "Montrer puissance haute" : "Montrer puissance normale"; } function addFilteredMarkers (showHighPower) { filteredMarkers.clearLayers() // Supprimer les marqueurs existants console.log('addFilteredMarkers: clear des marqueurs fait') let counter = 0 geojsondata.features.forEach(function (feature) { if (feature.properties.puissance_haute === showHighPower) { counter++ var marker = L.marker(feature.geometry.coordinates).bindPopup(feature.properties.puissance_haute ? 'Puissance haute' : 'Puissance normale') filteredMarkers.addLayer(marker) } }) console.log('addFilteredMarkers: ', counter) } let isLoading = false function loadOverpassQuery () { // ne pas charger si on recherche déjà if (!isLoading) { isLoading = true $('#spinning_icon').fadeIn() var queryTextfieldValue = $('#query-textfield').val() var overpassApiUrl = buildOverpassApiUrl(map, queryTextfieldValue) $.get(overpassApiUrl, function (geoDataPointsFromApi) { displayPointsFromApi(geoDataPointsFromApi) $('#spinning_icon').fadeOut() $('#message-loading').fadeOut() isLoading = false }) // end of the getting from overpass API } } $('#spinning_icon').hide() $('#message-loading').hide() loadOverpassQuery()