我有一个GeoJSON,其中包含来自约140个气象站的气象数据。问题在于每个站点被列出3次,因为GeoJSON包含每个站点3个不同读数的数据:3小时前的读数,2小时前的读数和1小时前的读数。我从中获得GeoJson的URL列出了具有以下顺序的数据:所有工作站的-3h
数据,然后是所有工作站的-2h
数据,最后是-1h
数据。该序列如图所示,并且您可以看到,根据读数的小时数,同一电台被列出3次(功能#0,#142和#283)。还可以看出properties time
格式为2020-02-22T21:00:00
,其中00:00
,-3h
和-2h
的分钟和秒始终为-1h
,因为读数是在小时进行的。
[当我运行脚本时,它只是显示-3h
中的数据,可能是因为它是序列中的第一个日期。结果可以在这里看到:https://meteolitoral.com/weather_map.php
这是脚本的主要部分,在这里获得温度:
<script>
//Get current date-time withh same format GeoJSON uses
var curdt = new Date().toJSON().substring(0,19);
map.on('load', function() {
map.addSource('points', {
'type': 'geojson',
'data':
'https://api.ipma.pt/open-data/observation/meteorology/stations/obs-surface.geojson',
});
map.addLayer({
'id': 'Temp (ºC)',
'type': 'symbol',
'source': 'points',
// filter data by properties time
"filter": [
"<=",
[ "get", "time" ], curdt
],
'layout': {
"visibility": "none",
'text-field': ['get', 'temperatura'],
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 0],
'text-anchor': 'top',
"text-size": 15
});
---
</script>
我想过滤(忽略)较旧的数据,而仅获取-1h
天气数据。例如,如果current date-time
为2020-02-22T23:18:35
,我想获取所有具有properties->time: 2020-02-22T22:00:00
的电台的GeoJSON数据。
[我对JS的了解很少,我试图改编一个过滤器选项,它适用于time <= current time
,但如何使它适用于time = (current time - 1h)
?
有没有办法只过滤GeoJSON的-1h
数据并显示这些值,而不是现在显示的-3h
值?谢谢。
通过过滤propreties time
以仅显示-1h
数据而不是-3h
数据解决的问题
<script>
//subtract one hour from current time
var subtracthour = new Date();
subtracthour.setHours(subtracthour.getHours()-1);
// Can also subtrat extra minuts if needed
//subtracthour.setMinutes(subtracthour.getMinutes()-15);
// Convert to ISO Format used in this GeoJSON (2020-02-28T15:31:25)
var dt = new Date(subtracthour).toJSON().substring(0,19);
// Apply the filter to get only "-1h" data
map.addLayer({
'id': 'Temp (ºC)',
'type': 'symbol',
'source': 'points',
"filter": [
">=",
[ "get", "time" ], dt
],
'layout': {
"visibility": "none",
// get the title name from the source's "title" property
'text-field': ['get', 'temperatura'],
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 0],
'text-anchor': 'top',
"text-size": 15
},
----
</script>