leaflet-velocity - cannot read property 'data' of null

故事扮演 提交于 2019-12-24 08:38:51

问题


I am working on the wind animation by using leaflet-velocity.js, which can be found in either npm or github (https://github.com/danwild/leaflet-velocity) from my own atmospheric model output (WRF).

To perform the wind animation, I wrote down my own python code to convert the model output from netCDF format to json format. The code is showing below

import os, sys, json, numpy as np
from glob import glob
from netCDF4 import Dataset, num2date, date2num

#- header templete
header = {  
    'parameterUnit': 'm.s-1',
    'parameterNumber': 2,
    'dx': 1.0,
    'dy': 1.0,
    'parameterNumberName': 'eastward_wind',
    'la1': 90.0,
    'la2': -90.0,
    'parameterCategory': 2,
    'lo1': 0.0,
    'nx': 360,
    'ny': 181,
    'refTime': '2016-04-30T06:00:00.000Z',
    'lo2': 359.0,
    }

lists = glob('TXGLO.surf_wind4json.nc')
ntimes= 8
for fid in lists[:1]:
    nc = Dataset(fid)
    tm = nc.variables['Times'][:]
    dim1, dim2 = nc.variables['XLAT'][0].shape
    nPoints = dim1*dim2
    lat= np.flipud(nc.variables['XLAT'][0]).flatten().tolist()
    lon= np.flipud(nc.variables['XLONG'][0]+360.).flatten().tolist()
    header['nx'] = dim1
    header['ny'] = dim2
    header['la1']= lat[0]
    header['la2']= lat[-1]
    header['lo1']= lon[0]
    header['lo2']= lon[-1]
    numPoints = nPoints
    for num, tim in enumerate(tm[:1]):
        refTime = ''.join(tim).replace('_',' ')
        print(' Processing file : '+fid+' , time : '+str(num)+' '+refTime)
        header['refTime'] = refTime
        with open('cresm_atmos_surf.json','w') as outfile:  
            outfile.write('[')
            #-  U10
            header['parameterNumberName'] = 'eastward_wind'
            u10 = np.flipud(nc.variables['U10'][num]).flatten().tolist()
            json.dump({'data':u10,'header':header}, outfile)
            outfile.write(',')
            #- V10
            header['parameterNumberName'] = 'northward_wind'
            v10 = np.flipud(nc.variables['V10'][num]).flatten().tolist()
            json.dump({'data':v10,'header':header}, outfile)
            outfile.write(']')

The results of json output looks like similar with the demo json file, such as wind-gbr.json (https://github.com/danwild/leaflet-velocity/blob/master/demo/wind-gbr.json)

Once I completed the converter, I refreshed my web page and found out there is an error on reading my json file.

Could someone please help me to figure out what is the error?

Thank You,

my netcdf file : https://www.dropbox.com/s/tmyrrinraetvcxs/TXGLO.surf_wind4json.nc?dl=0

my json file : https://www.dropbox.com/s/huiffld05zmldrs/cresm_atmos_surf.json?dl=0

the demo json file : https://www.dropbox.com/s/17pr3vdkl1v3bq7/wind_gbr.json?dl=0


回答1:


If you step down the error call stack, you will inspect createBuilder function which shows you how it extracts uComp and vComp from your provided JSON data.

You will see that it uses your records header parameterCategory and parameterNumber fields to determine whether the record data should be assigned to uComp or vComp:

switch (record.header.parameterCategory + "," + record.header.parameterNumber) {
  case "1,2":
  case "2,2":
    uComp = record;
    break;
  case "1,3":
  case "2,3":
    vComp = record;
    break;
  default:
    scalar = record;
}

It looks like your data has 2 records with exactly the same values for these 2 fields:

"parameterNumber": 2,
"parameterCategory": 2

Therefore you do not seem to provide vComp type record.

By forcing one of your records with "parameterNumber": 3, leaflet-velocity script no longer throws an error and displays something on the map, although it might not be the appropriate display.



来源:https://stackoverflow.com/questions/46699194/leaflet-velocity-cannot-read-property-data-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!