In [1]:
#Team Big Coders- Elke and Joy


#import modules
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly, GDT_Float32
import numpy as np


# open file and print info about the file
# the "../" refers to the parent directory of my working directory


!wget https://www.dropbox.com/s/zb7nrla6fqi1mq4/LC81980242014260-SC20150123044700.tar.gz?dl=1 -O LC81980242014260-SC20150123044700.tar.gz

 
import tarfile
tar = tarfile.open("LC81980242014260-SC20150123044700.tar.gz", "r:gz")
tar.extractall(path="data")
tar.close()

filename4 = './data/LC81980242014260LGN00_sr_band4.tif'
filename5 = './data/LC81980242014260LGN00_sr_band5.tif'
dataSource4 = gdal.Open(filename4, GA_ReadOnly)
dataSource5 = gdal.Open(filename5, GA_ReadOnly)


--2017-01-24 16:22:24--  https://www.dropbox.com/s/zb7nrla6fqi1mq4/LC81980242014260-SC20150123044700.tar.gz?dl=1
Resolving www.dropbox.com (www.dropbox.com)... 108.160.172.206
Connecting to www.dropbox.com (www.dropbox.com)|108.160.172.206|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.dropboxusercontent.com/content_link/l0GtrtBTDbIFw5kyv42ZcZ4H3esSTsF0Uim3MmZboK6eJbnGJD3ecvHgGYRJyjz3/file?dl=1 [following]
--2017-01-24 16:22:24--  https://dl.dropboxusercontent.com/content_link/l0GtrtBTDbIFw5kyv42ZcZ4H3esSTsF0Uim3MmZboK6eJbnGJD3ecvHgGYRJyjz3/file?dl=1
Resolving dl.dropboxusercontent.com (dl.dropboxusercontent.com)... 162.125.65.6
Connecting to dl.dropboxusercontent.com (dl.dropboxusercontent.com)|162.125.65.6|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 420944962 (401M) [application/octet-stream]
Saving to: ‘LC81980242014260-SC20150123044700.tar.gz’

LC81980242014260-SC 100%[===================>] 401.44M  32.7MB/s    in 13s     

2017-01-24 16:22:40 (29.8 MB/s) - ‘LC81980242014260-SC20150123044700.tar.gz’ saved [420944962/420944962]


In [2]:
print "\nInformation about " + filename4 + filename5 
print "Driver4: ", dataSource4.GetDriver().ShortName,"/", \
      dataSource4.GetDriver().LongName
print "Driver4: ", dataSource5.GetDriver().ShortName,"/", \
      dataSource5.GetDriver().LongName
    
print "Size4 is ",dataSource4.RasterXSize,"x",dataSource5.RasterYSize, \
      'x',dataSource4.RasterCount
print "Size5 is ",dataSource5.RasterXSize,"x",dataSource5.RasterYSize, \
      'x',dataSource5.RasterCount


Information about ./data/LC81980242014260LGN00_sr_band4.tif./data/LC81980242014260LGN00_sr_band5.tif
Driver4:  GTiff / GeoTIFF
Driver4:  GTiff / GeoTIFF
Size4 is  7791 x 7911 x 1
Size5 is  7791 x 7911 x 1

In [3]:
print '\nProjection is: ', dataSource4.GetProjection()
print '\nProjection is: ', dataSource5.GetProjection()


Projection is:  PROJCS["WGS 84 / UTM zone 31N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32631"]]

Projection is:  PROJCS["WGS 84 / UTM zone 31N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32631"]]

In [3]:


In [3]:


In [4]:
print "\nInformation about the location of the image and the pixel size:"
geotransform4 = dataSource4.GetGeoTransform()
geotransform5 = dataSource5.GetGeoTransform()
print geotransform4
print geotransform5


Information about the location of the image and the pixel size:
(529185.0, 30.0, 0.0, 5847015.0, 0.0, -30.0)
(529185.0, 30.0, 0.0, 5847015.0, 0.0, -30.0)

In [5]:
if not geotransform4 is None:
    print 'Origin = (',geotransform4[0], ',',geotransform4[3],')'
    print 'Pixel Size = (',geotransform4[1], ',',geotransform4[5],')'


Origin = ( 529185.0 , 5847015.0 )
Pixel Size = ( 30.0 , -30.0 )

In [6]:
if not geotransform5 is None:
    print 'Origin = (',geotransform5[0], ',',geotransform5[3],')'
    print 'Pixel Size = (',geotransform5[1], ',',geotransform5[5],')'


Origin = ( 529185.0 , 5847015.0 )
Pixel Size = ( 30.0 , -30.0 )

In [7]:
# Read data into an array
band4Arr = dataSource4.ReadAsArray(0,0,dataSource4.RasterXSize, dataSource4.RasterYSize)
band5Arr = dataSource5.ReadAsArray(0,0,dataSource5.RasterXSize, dataSource5.RasterYSize)
print type(band4Arr)
print type(band5Arr)


<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

In [8]:
# set the data type
band4Arr=band4Arr.astype(np.float32)
band5Arr=band5Arr.astype(np.float32)

In [9]:
# Derive the NDVI
mask = np.greater(band4Arr+band5Arr,0)

# set np.errstate to avoid warning of invalid values (i.e. NaN values) in the divide 
with np.errstate(invalid='ignore') and np.errstate(divide='ignore'):
    ndwi = np.choose(mask,(-99,(band4Arr-band5Arr)/(band4Arr+band5Arr)))
    
print "NDWI min and max values", ndwi.min(), ndwi.max()
# Check the real minimum value
print ndwi[ndwi>-99].min()


NDWI min and max values -99.0 21.3333
-2.36481

In [10]:
# Write the result to disk
driver = gdal.GetDriverByName('GTiff')
outDataSet=driver.Create('./data/ndwi.tif', dataSource4.RasterXSize, dataSource4.RasterYSize, 1, GDT_Float32)
outBand = outDataSet.GetRasterBand(1)
outBand.WriteArray(ndwi,0,0)
outBand.SetNoDataValue(-99)


Out[10]:
0

In [10]:


In [10]:


In [11]:
# set the projection and extent information of the dataset
outDataSet.SetProjection(dataSource4.GetProjection())
outDataSet.SetGeoTransform(dataSource4.GetGeoTransform())

# Finally let's save it... or like in the OGR example flush it
outDataSet.FlushCache()

In [12]:
!gdalinfo ./data/ndwi.tif


Driver: GTiff/GeoTIFF
Files: ./data/ndwi.tif
Size is 7791, 7911
Coordinate System is:
PROJCS["WGS 84 / UTM zone 31N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",3],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",EAST],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","32631"]]
Origin = (529185.000000000000000,5847015.000000000000000)
Pixel Size = (30.000000000000000,-30.000000000000000)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  (  529185.000, 5847015.000) (  3d25'57.39"E, 52d46'19.83"N)
Lower Left  (  529185.000, 5609685.000) (  3d24'45.80"E, 50d38'17.88"N)
Upper Right (  762915.000, 5847015.000) (  6d53'32.41"E, 52d42'32.58"N)
Lower Right (  762915.000, 5609685.000) (  6d42'50.08"E, 50d34'47.26"N)
Center      (  646050.000, 5728350.000) (  5d 6'46.27"E, 51d41'13.53"N)
Band 1 Block=7791x1 Type=Float32, ColorInterp=Gray
  NoData Value=-99

In [13]:
# via the Shell
!gdalwarp -t_srs "EPSG:4326" ./data/ndwi.tif ./data/ndwi_ll.tif


Creating output file that is 9790P x 6168L.
Processing input file ./data/ndwi.tif.
Using internal nodata values (e.g. -99) for image ./data/ndwi.tif.
Copying nodata values from source ./data/ndwi.tif to destination ./data/ndwi_ll.tif.
0...10...20...30...40...50...60...70...80...90...100 - done.

In [14]:
# Let's check what the result is
!gdalinfo ./data/ndwi_ll.tif


Driver: GTiff/GeoTIFF
Files: ./data/ndwi_ll.tif
Size is 9790, 6168
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (3.412720989343651,52.772174160489868)
Pixel Size = (0.000355418665319,-0.000355418665319)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  (   3.4127210,  52.7721742) (  3d24'45.80"E, 52d46'19.83"N)
Lower Left  (   3.4127210,  50.5799518) (  3d24'45.80"E, 50d34'47.83"N)
Upper Right (   6.8922697,  52.7721742) (  6d53'32.17"E, 52d46'19.83"N)
Lower Right (   6.8922697,  50.5799518) (  6d53'32.17"E, 50d34'47.83"N)
Center      (   5.1524954,  51.6760630) (  5d 9' 8.98"E, 51d40'33.83"N)
Band 1 Block=9790x1 Type=Float32, ColorInterp=Gray
  NoData Value=-99

In [15]:
# Notebook magic to select the plotting method
# Change to inline to plot within this notebook
#%matplotlib inline 
%matplotlib inline
from osgeo import gdal
import matplotlib.pyplot as plt

In [16]:
# Open image
dsll = gdal.Open("./data/ndwi_ll.tif")

# Read raster data
ndvi = dsll.ReadAsArray(0, 0, dsll.RasterXSize, dsll.RasterYSize)

# Now plot the raster data using gist_earth palette
plt.imshow(ndwi, interpolation='nearest', vmin=0, cmap=plt.cm.gist_earth)
plt.show()

dsll = None



In [16]: