AMBHAS
extract_gis_data.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003 Created on Mon Feb 27 19:18:08 2012
00004 
00005 @author: Sat Kumar Tomer
00006 @website: www.ambhas.com
00007 @email: satkumartomer@gmail.com
00008 """
00009 
00010 # import required libraries
00011 from osgeo.gdalconst import *
00012 import gdal, xlrd,xlwt
00013 import numpy as np
00014 from ambhas.gis import utm2image
00015 
00016 def extract_gis(xls_in, xls_out, ds, ds_short_name):
00017     """
00018     it reads the gis file defined in the ds
00019     then extract the data at coordinates defined in each sheet of the xls_in file
00020     and then write the data in the xls_out file
00021     the header of the data in the xls_out are written as defined in the 
00022     ds_short_name
00023     
00024     xls_in: the name of the input xls file containing the co-ordinates of the plot
00025     xls_out: the xls file in which the output will be written
00026     ds: the data source file name in the gis format, these files must be in the 
00027         tiff format
00028     ds_short_name:  the name that will appear as header in the output xls file
00029     """
00030     
00031     if type(ds) is not list:
00032         raise TypeError('input ds should be of list type')
00033     
00034     book_out = xlwt.Workbook()
00035 
00036     final_data = np.empty((66,2,len(ds)))
00037     for k in range(len(ds)):
00038         dataset = gdal.Open(ds[k], GA_ReadOnly)
00039         data = dataset.GetRasterBand(1).ReadAsArray() 
00040         GT = dataset.GetGeoTransform()
00041         
00042         book = xlrd.open_workbook(xls_in)
00043         
00044         for i in xrange(66):
00045             sheet = book.sheet_by_name(str(i+1))
00046             xy = np.empty((sheet.nrows-1,2))
00047             for j in range(xy.shape[0]):
00048                 xy[j,0] = sheet.cell_value(j+1,0)    
00049                 xy[j,1] = sheet.cell_value(j+1,1)
00050                     
00051             x,y = utm2image(GT,xy)
00052                 
00053             extracted_data = data[y,x]
00054             final_data[i,0,k] = np.median(extracted_data)
00055             final_data[i,1,k] = np.std(extracted_data)
00056     
00057     dataset = None
00058     
00059     sheet_median = book_out.add_sheet('median')   
00060     sheet_std = book_out.add_sheet('std')
00061     
00062     sheet_median.write(0,0,'Plot no.')
00063     sheet_std.write(0,0,'Plot no.')
00064     
00065     for i in range(final_data.shape[0]):
00066         sheet_median.write(i+1,0,i+1)
00067         sheet_std.write(i+1,0,i+1)
00068     
00069     for i in range(len(ds)):
00070         sheet_median.write(0,i+1,ds_short_name[i])
00071         sheet_std.write(0,i+1,ds_short_name[i])
00072     
00073     for i in range(final_data.shape[0]):
00074         for j in range(final_data.shape[2]):
00075             sheet_median.write(i+1, j+1, final_data[i,0,j])
00076             sheet_std.write(i+1, j+1, final_data[i,1,j])
00077     book_out.save(xls_out)
 All Classes Namespaces Files Functions Variables