from arcgis.gis import GIS from arcgis.features import FeatureLayerCollection import requests import urllib.request from zipfile import ZipFile import xarray as xr import matplotlib import matplotlib.pyplot as plt import metpy.calc as mpcalc from metpy.units import units from shapely import geometry import numpy as np import fiona #Load data #urllib.request.urlretrieve('rap.t00z.awp130pgrbf00.grib2', 'a.grib2') # Open data file & read data ds = xr.open_dataset('rap.t23z.awp252pgrbf00.grib2', engine='cfgrib') lon, lat = ds.longitude.values-360., ds.latitude.values t2m = mpcalc.smooth_gaussian((ds.variables['t2m'].values*units('K')).to('degF').magnitude, 4) # Plot fig = plt.figure(figsize=(12,10)) ax = fig.add_subplot(111) cs = ax.contourf(lon, lat, t2m, levels=[10,20,30,32,36]) ## EDIT CONTOUR LEVELS HERE #plt.close() #plt.show() # Write contours to shapefile lvl_lookup = dict(zip(cs.collections, cs.levels)) PolyList=[] for col in cs.collections: z = lvl_lookup[col] # the value of this level for contour_path in col.get_paths(): # create the polygon for this level for ncp,cp in enumerate(contour_path.to_polygons()): lons = cp[:,0] lats = cp[:,1] new_shape = geometry.Polygon([(i[0], i[1]) for i in zip(lons,lats)]) if ncp == 0: poly = new_shape # first shape else: poly = poly.difference(new_shape) # Remove the holes PolyList.append({'poly':poly,'props':{'z': z}}) schema = {'geometry': 'Polygon','properties': {'z': 'float'}} crs = {'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat'} with fiona.collection('rap_temp.shp', 'w', driver='ESRI Shapefile', crs=crs, schema=schema) as output: for p in PolyList: output.write({'properties': p['props'], 'geometry': geometry.mapping(p['poly'])}) # create a ZipFile object rap_temp = ZipFile('rap_temp2.zip', 'w') # Add multiple files to the zip rap_temp.write('rap_temp.cpg') rap_temp.write('rap_temp.dbf') rap_temp.write('rap_temp.prj') rap_temp.write('rap_temp.shp') rap_temp.write('rap_temp.shx') #Close the zip file rap_temp.close() # ARC GIS #Login user = '****' password = '****' gis = GIS('https://rvhlcps.maps.arcgis.com', user, password) un = gis.properties.user.username print('Logged in as: {}'.format(un)) #overwrite layer with new data itemid = '41b06ab534504b388c17b89c04bc0eef' dataitem = gis.content.get(itemid) print('1') flayercol = FeatureLayerCollection.fromitem(dataitem) print('2') flayercol.manager.overwrite('rap_temp2.zip') print('Layer Updated')