Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # PURPOSE
  4. # This script processes LANDSAT 7 ETM+ images
  5. # 1 - unzip *.gz files
  6. # 2 - import files in GRASS GIS Location of your choice (r.in.gdal)
  7. # 3 - Grouping of Imagery (i.group)
  8. # 4 - Clustering of selected bands (i.cluster)
  9. # 5 - Unsupervised classification (i.maxlik)
  10.  
  11. # Setup the path to the Landsat 7 Directories
  12. rsdatapath = "/home/hempire/grassdata/L7Dir"
  13.  
  14. import glob
  15. import os
  16. import subprocess
  17. import sys
  18.  
  19. # path to the GRASS GIS launch script
  20. # MS Windows
  21. # grass7bin_win = r'C:\OSGeo4W\bin\grass72svn.bat'
  22. # uncomment when using standalone WinGRASS installer
  23. # grass7bin_win = r'C:\Program Files (x86)\GRASS GIS 7.2.0\grass72.bat'
  24. # Linux
  25. grass7bin_lin = 'grass72'
  26.  
  27. # DATA
  28. # define GRASS DATABASE
  29. # add your path to grassdata (GRASS GIS database) directory
  30. gisdb = os.path.join(os.path.expanduser("~"), "grassdata")
  31. # the following path is the default path on MS Windows
  32. # gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")
  33.  
  34. # specify (existing) location and mapset
  35. location = "Boni"
  36. mapset = "hempire"
  37.  
  38. # SOFTWARE
  39. if sys.platform.startswith('linux'):
  40. # we assume that the GRASS GIS start script is available and in the PATH
  41. # query GRASS 7 itself for its GISBASE
  42. grass7bin = grass7bin_lin
  43. else:
  44. raise OSError('Platform not configured.')
  45.  
  46. # query GRASS 7 itself for its GISBASE
  47. startcmd = [grass7bin, '--config', 'path']
  48.  
  49. p = subprocess.Popen(startcmd, shell=False,
  50. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  51. out, err = p.communicate()
  52. if p.returncode != 0:
  53. print >> sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd
  54. sys.exit(-1)
  55. gisbase = out.strip('\n\r')
  56.  
  57. # Set GISBASE environment variable
  58. os.environ['GISBASE'] = gisbase
  59. # the following not needed with trunk
  60. os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
  61. # add path to GRASS addons
  62. home = os.path.expanduser("~")
  63. os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons', 'scripts')
  64.  
  65. # define GRASS-Python environment
  66. gpydir = os.path.join(gisbase, "etc", "python")
  67. sys.path.append(gpydir)
  68.  
  69. # DATA
  70. # Set GISDBASE environment variable
  71. os.environ['GISDBASE'] = gisdb
  72.  
  73. # import GRASS Python bindings (see also pygrass)
  74. import grass.script.setup as gsetup
  75. import grass.script as grass
  76.  
  77. gsetup.init(gisbase, gisdb, location, mapset)
  78. from grass.pygrass.modules.shortcuts import raster as r
  79.  
  80. # env = os.environ.copy()
  81. # env['GRASS_MESSAGE_FORMAT'] = 'gui'
  82. # Function to get a list of L7 Directories in the rsdatapath
  83. def fn(path):
  84. for top, dirs, files in os.walk(path):
  85. return [os.path.join(top, dir) for dir in dirs]
  86.  
  87. # START PROCESS
  88. # Find the central location of the Landsat file from metadata
  89. metadata = []
  90. fileList = []
  91. L7Dirs = fn(rsdatapath)
  92. for L7Dir in L7Dirs:
  93. # Ungzip all of your Landsat7 images in all your directories
  94. # print "Ungzip Landsat files in\t",L7Dir
  95. # p=os.system("gzip -d -q "+L7Dir+"/*.gz")
  96. # Using pthreads on multi-core machines
  97. # p=os.system("pigz -d "+L7Dir+"/*.gz")
  98. # Wait ten seconds for gzip to create the tif images
  99. # time.sleep(10)
  100. print "Import in GRASS GIS"
  101. for L7f in glob.glob(os.path.join(L7Dir, "*.[tT][iI][fF]")):
  102. f1 = L7f.replace(L7Dir + "/", "")
  103. f2 = f1.replace(".TIF", "")
  104. f3 = f2.replace("_B10", ".1")
  105. f4 = f3.replace("_B20", ".2")
  106. f5 = f4.replace("_B30", ".3")
  107. f6 = f5.replace("_B40", ".4")
  108. f7 = f6.replace("_B50", ".5")
  109. f8 = f7.replace("_B61", ".61")
  110. f9 = f8.replace("_B62", ".62")
  111. f10 = f9.replace("_B70", ".7")
  112. f11 = f10.replace("_B80", ".8")
  113. f12 = f11.replace("L72", "L71")
  114. L7r = f12.replace("_VCID_", "")
  115. print "\t> ", L7r
  116. r.in_gdal(input=L7f, output=L7r, flags="o", overwrite=True)
  117. fileList.append(L7r)
  118.  
  119. # Listing the imported raster files
  120. grass.run_command("g.list", flags="f", type="rast")
  121.  
  122. # creating a color composite by combining three bands in RGB
  123. r.composite(blue="LE07_L1TP_165061_20170113_20170215_01_T1_B2", green="LE07_L1TP_165061_20170113_20170215_01_T1_B3",
  124. red="LE07_L1TP_165061_20170113_20170215_01_T1_B4", output="LE07_L1TP_165061_composite", overwrite=True)
  125.  
  126.  
  127. # Pansharpening of landsat image to 15m resolution
  128. # i.pansharpen with IHS algorithm
  129. # i.pansharpen(blue="LE07_L1TP_165061_20170113_20170215_01_T1_B2", green="LE07_L1TP_165061_20170113_20170215_01_T1_B3",
  130. # red="LE07_L1TP_165061_20170113_20170215_01_T1_B4", pan="LE07_L1TP_165061_20170113_20170215_01_T1_B8",
  131. # output="ihs432", method="ihs", overwrite=True)
  132.  
  133.  
  134. # Grouping raster layers into a group and subgroup as required in clustering and classification
  135. from grass.pygrass.gis import Mapset
  136.  
  137.  
  138. mapset = Mapset("hempire")
  139. grass.run_command("i.group", group="lamu", subgroup="lamusubgrp", input=m.glist("raster", pattern="LE07_*"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement