Try to write FVCOM mesh as geojson


In [1]:
from shapely.geometry import MultiPolygon, mapping, polygon
import json

In [2]:
#url = 'http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc'
#url = 'http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_FVCOM_OCEAN_MASSBAY_FORECAST.nc'
url = 'http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_WAVE_FORECAST.nc'

use netcdf4 because UGRID takes longer

import pyugrid ug = pyugrid.UGrid.from_ncfile(url) lon = ug.nodes[:,0] lat = ug.nodes[:,1] nv = ug.faces[:]

In [11]:
import netCDF4
nc = netCDF4.Dataset(url)
ncv = nc.variables
lon = ncv['lon'][:]
lat = ncv['lat'][:]
nv = ncv['nv'][:,:].T - 1

In [12]:
#mp = MultiPolygon([polygon.Polygon(zip(lon[element],lat[element])) for element in nv])
mp = MultiPolygon([polygon.Polygon(zip(lon[element],lat[element])) for element in nv[0:5]])

In [ ]:
type(mp)

In [ ]:
mp

In [ ]:
json.dumps(mapping(mp))

In [ ]:
with open('ugrid.json','w') as f:
    json.dump(mapping(mp), f)

In [ ]:


In [ ]: