我有一个GeoJSON,里面有大约140个气象站的天气数据。问题是每个站点列出了3次,因为GeoJSON包含来自每个站点的3个不同读数的数据:3小时前的读数,2小时前的读数和1小时前的读数。我从中获得此GeoJson的url按以下顺序列出数据:所有站点的-3h数据,然后是所有站点的-2h数据,最后是-1h数据。图中显示了这一序列,如您所见,根据读数的小时,同一站点列出了3次(功能#0、#142和#283)。还可以看到,properties time格式是2020-02-22T21:00:00,对于-3h、-2h和-1h,分钟和秒始终是00:00,因为读数是在小时完成的。

当我运行脚本时,它只显示来自-3h的数据,可能是因为这是它在序列中获得的第一个日期。
下面是脚本的主要部分,我在这里获取温度:
<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值?谢谢。
发布于 2020-02-27 18:33:10
已通过过滤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',
'text-field': ['get', 'temperatura'],
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 0],
'text-anchor': 'top',
'text-size': 15
},
----
</script>https://stackoverflow.com/questions/60368719
复制相似问题