Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按属性时间值筛选GeoJSON_Javascript_Geojson_Mapbox Gl Js - Fatal编程技术网

Javascript 按属性时间值筛选GeoJSON

Javascript 按属性时间值筛选GeoJSON,javascript,geojson,mapbox-gl-js,Javascript,Geojson,Mapbox Gl Js,我有一个GeoJSON,里面有大约140个气象站的天气数据。问题是每个站点被列出3次,因为GeoJSON包含来自每个站点3个不同读数的数据:3小时前读数、2小时前读数和1小时前读数。 我从中获取此GeoJson的url按以下顺序列出数据:-3h所有站点的数据,然后是-2h所有站点的数据,最后是-1h数据。这一序列如图所示,如您所见,根据读数的小时数,同一电台被列出3次(特征#0、#142和#283)。还可以看出,属性时间格式为2020-02-22T21:00:00,其中-3h、-2h和-1h的分

我有一个GeoJSON,里面有大约140个气象站的天气数据。问题是每个站点被列出3次,因为GeoJSON包含来自每个站点3个不同读数的数据:3小时前读数、2小时前读数和1小时前读数。 我从中获取此GeoJson的url按以下顺序列出数据:
-3h
所有站点的数据,然后是
-2h
所有站点的数据,最后是
-1h
数据。这一序列如图所示,如您所见,根据读数的小时数,同一电台被列出3次(特征#0、#142和#283)。还可以看出,
属性时间
格式为
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>

//使用GeoJSON使用的相同格式获取当前日期时间
var curdt=new Date().toJSON().substring(0,19);
map.on('load',function()){
map.addSource('点'{
'type':'geojson',
“数据”:
'https://api.ipma.pt/open-data/observation/meteorology/stations/obs-surface.geojson',
});
map.addLayer({
“id”:“温度(摄氏度)”,
“类型”:“符号”,
'来源':'点',
//按时间属性筛选数据
“过滤器”:[

“通过过滤
使时间
仅显示
-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>

//从当前时间减去一小时
var减去小时=新日期();
subtract hour.setHours(subtract hour.getHours()-1);
//如果需要的话,还可以对额外的分钟进行细分
//subtract hour.setMinutes(subtract hour.getMinutes()-15);
//转换为本GeoJSON中使用的ISO格式(2020-02-28T15:31:25)
var dt=新日期(减去小时).toJSON()子字符串(0,19);
//应用过滤器仅获取“-1h”数据
map.addLayer({
“id”:“温度(摄氏度)”,
“类型”:“符号”,
'来源':'点',
“过滤器”:[
'>=',
['get','time',dt
],
“布局”:{
“可见性”:“无”,
“文本字段”:[“获取”、“温度”],
“文本字体”:[“Open Sans Semibold”,“Arial Unicode MS Bold”],
“文本偏移量”:[0,0],
“文本锚定”:“顶部”,
“文本大小”:15
},
----

您能包含一条实际GeoJSON数据的记录吗?答案将取决于实际包含的时间数据的格式。@Steve Bennett感谢您的回复。我编辑了我的评论,提供了更多关于GeoJSON的详细信息,但同时我设法解决了问题。不知道这是否是“最干净”的方式,但效果很好。