Extract data from USGS National Map WCS Service


In [1]:
from owslib.wcs import WebCoverageService
import numpy as np
import numpy.ma as ma
#endpoint='http://gis.sam.usace.army.mil/server/services/JALBTCX/NCMP_BareEarth_1m/ImageServer/WCSServer?request=GetCapabilities&service=WCS'
#endpoint='http://gis.sam.usace.army.mil/server/services/JALBTCX/NCMP_2005_MA_BareEarth_1m/ImageServer/WCSServer?request=GetCapabilities&service=WCS'
endpoint='http://raster.nationalmap.gov/arcgis/services/LandCover/USGS_EROS_LandCover_NLCD/MapServer/WCSServer?request=GetCapabilities&service=WCS'

In [2]:
wcs = WebCoverageService(endpoint,version='1.0.0',timeout=60)

In [3]:
for k,v in wcs.contents.iteritems():
    print v.title


Impervious_Surface_2001_AK_11
Land_Cover_2001_CONUS_10
Impervious_Surface_2001_CONUS_13
Impervious_Surface_2001_HI_12
Canopy_2001_AK_15
Impervious_Surface_2001_PR_14
Canopy_2001_CONUS_17
Canopy_2001_HI_16
Land_Cover_1992_19
Canopy_2001_PR_18
Land_Cover_2011_CONUS_1
Canopy_2011_CONUS_3
Impervious_Surface_2011_CONUS_2
Land_Cover_2006_CONUS_5
conus_2011_canopy_new.img_4
Land_Cover_2001_AK_7
Impervious_Surface_2006_CONUS_6
Land_Cover_2001_PR_9
Land_Cover_2001_HI_8

In [4]:
wcs.contents


Out[4]:
{'1': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04c890>,
 '10': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cc90>,
 '11': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cd10>,
 '12': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cd50>,
 '13': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cd90>,
 '14': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cdd0>,
 '15': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04ce10>,
 '16': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04ce50>,
 '17': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04ce90>,
 '18': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cf10>,
 '19': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cf50>,
 '2': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04c8d0>,
 '3': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04c990>,
 '4': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04c9d0>,
 '5': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04ca50>,
 '6': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cad0>,
 '7': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cb10>,
 '8': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cb90>,
 '9': <owslib.coverage.wcs100.ContentMetadata at 0x7ffb4c04cc10>}

In [5]:
lidar = wcs['1']
print lidar.title
print lidar.boundingBoxWGS84
print lidar.timelimits
print lidar.supportedFormats


Land_Cover_2011_CONUS_1
(-130.23284428732586, 21.74226357397646, -63.671993647433084, 52.87727272194017)
[]
[]

In [6]:
# try Plum Island Sound Region
bbox = (-70.3,41.1,-69.9,41.4)
output = wcs.getCoverage(identifier="1",bbox=bbox,crs='EPSG:4326',format='GeoTIFF',
                         resx=0.0001, resy=0.0001)

In [7]:
f=open('test.tif','wb')
f.write(output.read())
f.close()

In [8]:
from osgeo import gdal
gdal.UseExceptions()

In [9]:
ds = gdal.Open('test.tif')

In [10]:
band = ds.GetRasterBand(1)
elevation = band.ReadAsArray()
nrows, ncols = elevation.shape

# I'm making the assumption that the image isn't rotated/skewed/etc. 
# This is not the correct method in general, but let's ignore that for now
# If dxdy or dydx aren't 0, then this will be incorrect
x0, dx, dxdy, y0, dydx, dy = ds.GetGeoTransform()

if dxdy == 0.0:
    x1 = x0 + dx * ncols
    y1 = y0 + dy * nrows

In [11]:
import cartopy.crs as ccrs
from cartopy.io.img_tiles import MapQuestOpenAerial, MapQuestOSM, OSM

In [12]:
print x0,x1,y1,y0


-70.3 -69.9 41.1 41.4

In [13]:
elevation=ma.masked_equal(elevation,0)

In [14]:
plt.figure(figsize=(8,10))
ax = plt.axes(projection=ccrs.PlateCarree())
tiler = MapQuestOpenAerial()
ax.add_image(tiler, 14)
plt.imshow(elevation, cmap='jet', extent=[x0, x1, y1, y0],
           transform=ccrs.PlateCarree(),alpha=0.6,zorder=2);
ax.gridlines(draw_labels=True,zorder=3);