00001
00002
00003 """
00004 Created on Mon Nov 15 19:54:08 2010
00005
00006 @author: sat kumar tomer (http://civil.iisc.ernet.in/~satkumar/)
00007
00008 Functions:
00009 utm2deg: Calculate utm co-ordinates from Lat, Lon
00010 deg2utm : Calculate Lat, Lon from UTM
00011 kabini:
00012 berambadi:
00013 """
00014
00015
00016 from pyproj import Proj
00017 import os
00018 from subprocess import call
00019 import sys
00020 import numpy as np
00021
00022 def utm2image(GT,utm):
00023 Xpixel = ((utm[:,0] - GT[0])*GT[5] - (utm[:,1] - GT[3])*GT[2])/(GT[1]*GT[5]-GT[4]*GT[2])
00024 Ypixel = ((utm[:,1] - GT[3])*GT[1] - (utm[:,0] - GT[0])*GT[4])/(GT[1]*GT[5]-GT[4]*GT[2])
00025 return (np.round(Xpixel)).astype('int'),(np.round(Ypixel)).astype('int')
00026
00027 def Geo2Pixel(Xgeo,Ygeo,GT):
00028 a1 = GT[1]
00029 b1 = GT[2]
00030 c1 = Xgeo - GT[0]
00031 a2 = GT[4]
00032 b2 = GT[5]
00033 c2 = Ygeo - GT[3]
00034 Xpixel = (b2*c1-b1*c2)/(a1*b2-a2*b1)
00035 Yline = (a2*c1-a1*c2)/(a2*b1-a1*b2)
00036 return Xpixel, Yline
00037
00038 def Pixel2Geo(Xpixel,Yline,GT):
00039 Xgeo = GT[0] + Xpixel*GT[1] + Yline*GT[2]
00040 Ygeo = GT[3] + Xpixel*GT[4] + Yline*GT[5]
00041 return Xgeo,Ygeo
00042
00043 def SetProjectionBerambadi():
00044 return 'PROJCS["WGS 84 / UTM zone 43N",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"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",75],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32643"]]'
00045
00046 def utm2deg(x,y,utmzone=43):
00047 p = Proj(proj='utm',zone=43,ellps='WGS84')
00048 Lon,Lat = p(x, y, inverse=True)
00049 return Lon,Lat
00050
00051 def deg2utm(Lon,Lat,utmzone=43):
00052 p = Proj(proj='utm',zone=utmzone,ellps='WGS84')
00053 x,y = p(Lon, Lat)
00054 return x,y
00055
00056 def kabini(Ifile,Ofile):
00057
00058 Temp1 = '/home/tomer/MODISdata/temp/temp1.tif'
00059 Temp2 = '/home/tomer/MODISdata/temp/temp2.tif'
00060
00061
00062 cmd= 'gdalwarp -r bilinear -t_srs \'+proj=utm +zone=43 +datum=WGS84\' ' + Ifile + ' ' +Temp1
00063 try:
00064 returncode = call(cmd, shell=True)
00065 if returncode:
00066 print 'Failure with returncode', returncode; sys.exit(1)
00067 except OSError, message:
00068 print 'Execution failed!\n', message; sys.exit(1)
00069
00070
00071 kab='582000 1372000 711000 1270000'
00072 cmd = 'gdal_translate -a_ullr ' + kab + ' -projwin ' + kab + ' ' + Temp1+ ' ' + Temp2
00073 try:
00074 returncode = call(cmd, shell=True)
00075 if returncode:
00076 print 'Failure with returncode', returncode; sys.exit(1)
00077 except OSError, message:
00078 print 'Execution failed!\n', message; sys.exit(1)
00079
00080
00081 cmd = 'gdalwarp -r bilinear -tr 1000 1000 ' + Temp2 + ' ' +Ofile
00082 try:
00083 returncode = call(cmd, shell=True)
00084 if returncode:
00085 print 'Failure with returncode', returncode; sys.exit(1)
00086 except OSError, message:
00087 print 'Execution failed!\n', message; sys.exit(1)
00088
00089
00090 try:
00091 os.remove(Temp)
00092 os.remove(Temp2)
00093 except:
00094 print 'temp file not created'
00095
00096 def berambadi(Ifile,Ofile):
00097
00098 Temp1 = '/home/tomer/MODISdata/temp/temp1.tif'
00099 Temp2 = '/home/tomer/MODISdata/temp/temp2.tif'
00100
00101
00102 cmd= 'gdalwarp -r bilinear -t_srs \'+proj=utm +zone=43 +datum=WGS84\' ' + Ifile + ' ' +Temp1
00103 try:
00104 returncode = call(cmd, shell=True)
00105 if returncode:
00106 print 'Failure with returncode', returncode; sys.exit(1)
00107 except OSError, message:
00108 print 'Execution failed!\n', message; sys.exit(1)
00109
00110
00111 bmd='664000 1309000 685000 1294000'
00112 cmd = 'gdal_translate -a_ullr ' + bmd + ' -projwin ' + bmd + ' ' + Temp1+ ' ' + Temp2
00113 try:
00114 returncode = call(cmd, shell=True)
00115 if returncode:
00116 print 'Failure with returncode', returncode; sys.exit(1)
00117 except OSError, message:
00118 print 'Execution failed!\n', message; sys.exit(1)
00119
00120
00121 cmd = 'gdalwarp -r bilinear -tr 1000 1000 ' + Temp2 + ' ' +Ofile
00122 try:
00123 returncode = call(cmd, shell=True)
00124 if returncode:
00125 print 'Failure with returncode', returncode; sys.exit(1)
00126 except OSError, message:
00127 print 'Execution failed!\n', message; sys.exit(1)
00128
00129
00130 try:
00131 os.remove(Temp1)
00132 os.remove(Temp2)
00133 except:
00134 print 'temp file not created'
00135