[prev in list] [next in list] [prev in thread] [next in thread]
List: grass-user
Subject: [GRASS-user] Landsat Processing in PyGrass
From: Joseph Kariuki <joehene () gmail ! com>
Date: 2017-04-12 12:07:42
Message-ID: CAGGeTwXFFQ1DH5T8MTuDQjiD8yj=7mTC8XAL+z1UsmGR=EzSoQ () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi,
I am new to pygrass, I got the landsat processing script from osgeo pygrass
wiki page. I am stuck here at looping through landsat directories that I
have already created through unzipping lansat .gz archives. Kindly assist,
code is a shown below, the error being raised is
* for L7Dir in L7Dirs:TypeError: 'NoneType' object is not iterable*
#!/usr/bin/env python
# PURPOSE
# This script processes LANDSAT 7 ETM+ images
# 1 - unzip *.gz files
# 2 - import files in GRASS GIS Location of your choice (r.in.gdal)
# 3 - DN to Top of Atmosphere reflectance (i.landsat.toar)
# 4 - TOA reflectance to Surface reflectance (i.atcorr)
# 5 - NDVI (i.vi), Albedo (i.albedo), Emissivity (i.emissivit
# USER HAS TO SET THOSE
# QUIET REPORTS
QIET = True
# OVERWRITE EXISTING FILES
OVR = False
# Define Landsat 7 sensor for i.landsat.toar
LSENSOR = "tm7"
# Setup the path to the Landsat 7 Directories
rsdatapath = "~/rawData/L7Dir"
# set L7 Metadata wildcards
wldc_mtl = "*_MTL.txt"
# Visibility distance [Km]
vis = 18
# DEM input to atmospheric correction
inDEM = rsdatapath + "/dem_30m/Kenya_SRTM30meters.tif"
import glob
import os
import subprocess
import sys
# path to the GRASS GIS launch script
# MS Windows
# grass7bin_win = r'C:\OSGeo4W\bin\grass72svn.bat'
# uncomment when using standalone WinGRASS installer
# grass7bin_win = r'C:\Program Files (x86)\GRASS GIS 7.2.0\grass72.bat'
# Linux
grass7bin_lin = 'grass72'
# Mac OS X
# grass7bin_mac = '/Applications/GRASS/GRASS-7.2.app/'
# DATA
# define GRASS DATABASE
# add your path to grassdata (GRASS GIS database) directory
gisdb = os.path.join(os.path.expanduser("~"), "rawData")
# the following path is the default path on MS Windows
# gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")
# specify (existing) location and mapset
location = "L7_Kenya"
mapset = "hempire"
# SOFTWARE
if sys.platform.startswith('linux'):
# we assume that the GRASS GIS start script is available and in the PATH
# query GRASS 7 itself for its GISBASE
grass7bin = grass7bin_lin
else:
raise OSError('Platform not configured.')
# query GRASS 7 itself for its GISBASE
startcmd = [grass7bin, '--config', 'path']
p = subprocess.Popen(startcmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print >> sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)"
% startcmd
sys.exit(-1)
gisbase = out.strip('\n\r')
# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase
# the following not needed with trunk
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
# add path to GRASS addons
home = os.path.expanduser("~")
os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons',
'scripts')
# define GRASS-Python environment
gpydir = os.path.join(gisbase, "etc", "python")
sys.path.append(gpydir)
# DATA
# Set GISDBASE environment variable
os.environ['GISDBASE'] = gisdb
# import GRASS Python bindings (see also pygrass)
import grass.script.setup as gsetup
gsetup.init(gisbase, gisdb, location, mapset)
from grass.pygrass.modules.shortcuts import raster as r
# Needed for floor()
# env = os.environ.copy()
# env['GRASS_MESSAGE_FORMAT'] = 'gui'
# Function to get a list of L7 Directories in the rsdatapath
def fn(path):
for top, dirs, files in os.walk(path):
return [os.path.join(top, dir) for dir in dirs]
# START PROCESS
# PART 0: PRE-PROCESSING STUFF ###
# import DEM for atmospheric correction
# r.in.gdal(input=inDEM,output="dem",overwrite=OVR)
# r.mapcalc(expression="dem=25",overwrite=OVR)
# create a visibility map
# r.mapcalc(expression="vis=18", overwrite=OVR)
# Find the central location of the Landsat file from metadata
metadata = []
fileList = []
L7Dirs = fn(rsdatapath)
for L7Dir in L7Dirs:
# Ungzip all of your Landsat7 images in all your directories
# print "Ungzip Landsat files in\t",L7Dir
# p=os.system("gzip -d -q "+L7Dir+"/*.gz")
# Using pthreads on multi-core machines
# p=os.system("pigz -d "+L7Dir+"/*.gz")
# Wait ten seconds for gzip to create the tif images
# time.sleep(10)
print "Import in GRASS GIS"
Kind Regards,
*Joseph Kariuki*
*Geospatial Engineer** | **GIS / Web Developer*
[Attachment #5 (text/html)]
<div dir="ltr"><div>Hi,<br><br></div>I am new to pygrass, I got the landsat \
processing script from osgeo pygrass wiki page. I am stuck here at looping through \
landsat directories that I have already created through unzipping lansat .gz \
archives. Kindly assist, code is a shown below, the error being raised is \
<i><b><span style="color:rgb(255,0,0)"> for L7Dir in L7Dirs:<br>TypeError: \
'NoneType' object is not iterable</span></b></i><br><br>#!/usr/bin/env \
python<br><br># PURPOSE<br># This script processes LANDSAT 7 ETM+ images<br># 1 - \
unzip *.gz files<br># 2 - import files in GRASS GIS Location of your choice \
(r.in.gdal)<br># 3 - DN to Top of Atmosphere reflectance (i.landsat.toar)<br># 4 - \
TOA reflectance to Surface reflectance (i.atcorr)<br># 5 - NDVI (<a \
href="http://i.vi">i.vi</a>), Albedo (i.albedo), Emissivity (i.emissivit<br><br># \
USER HAS TO SET THOSE<br># QUIET REPORTS<br>QIET = True<br># OVERWRITE EXISTING \
FILES<br>OVR = False<br># Define Landsat 7 sensor for i.landsat.toar<br>LSENSOR = \
"tm7"<br><br># Setup the path to the Landsat 7 Directories<br>rsdatapath = \
"~/rawData/L7Dir"<br># set L7 Metadata wildcards<br>wldc_mtl = \
"*_MTL.txt"<br># Visibility distance [Km]<br>vis = 18<br><br># DEM input to \
atmospheric correction<br>inDEM = rsdatapath + \
"/dem_30m/Kenya_SRTM30meters.tif"<br><br>import glob<br>import os<br>import \
subprocess<br>import sys<br><br># path to the GRASS GIS launch script<br># MS \
Windows<br># grass7bin_win = r'C:\OSGeo4W\bin\grass72svn.bat'<br># uncomment \
when using standalone WinGRASS installer<br># grass7bin_win = r'C:\Program Files \
(x86)\GRASS GIS 7.2.0\grass72.bat'<br># Linux<br>grass7bin_lin = \
'grass72'<br># Mac OS X<br># grass7bin_mac = \
'/Applications/GRASS/GRASS-7.2.app/'<br><br># DATA<br># define GRASS \
DATABASE<br># add your path to grassdata (GRASS GIS database) directory<br>gisdb = \
os.path.join(os.path.expanduser("~"), "rawData")<br># the \
following path is the default path on MS Windows<br># gisdb = \
os.path.join(os.path.expanduser("~"), \
"Documents/grassdata")<br><br># specify (existing) location and \
mapset<br>location = "L7_Kenya"<br>mapset = "hempire"<br><br># \
SOFTWARE<br>if sys.platform.startswith('linux'):<br> # we assume that \
the GRASS GIS start script is available and in the PATH<br> # query GRASS 7 \
itself for its GISBASE<br> grass7bin = grass7bin_lin<br>else:<br> raise \
OSError('Platform not configured.')<br><br># query GRASS 7 itself for its \
GISBASE<br>startcmd = [grass7bin, '--config', 'path']<br><br>p = \
subprocess.Popen(startcmd, shell=False,<br> \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br>out, err = p.communicate()<br>if \
p.returncode != 0:<br> print >> sys.stderr, "ERROR: Cannot find \
GRASS GIS 7 start script (%s)" % startcmd<br> sys.exit(-1)<br>gisbase = \
out.strip('\n\r')<br><br># Set GISBASE environment \
variable<br>os.environ['GISBASE'] = gisbase<br># the following not needed \
with trunk<br>os.environ['PATH'] += os.pathsep + os.path.join(gisbase, \
'extrabin')<br># add path to GRASS addons<br>home = \
os.path.expanduser("~")<br>os.environ['PATH'] += os.pathsep + \
os.path.join(home, '.grass7', 'addons', 'scripts')<br><br># \
define GRASS-Python environment<br>gpydir = os.path.join(gisbase, "etc", \
"python")<br>sys.path.append(gpydir)<br><br># DATA<br># Set GISDBASE \
environment variable<br>os.environ['GISDBASE'] = gisdb<br><br># import GRASS \
Python bindings (see also pygrass)<br>import grass.script.setup as \
gsetup<br><br>gsetup.init(gisbase, gisdb, location, mapset)<br>from \
grass.pygrass.modules.shortcuts import raster as r<br><br><br># Needed for \
floor()<br><br><br># env = os.environ.copy()<br># env['GRASS_MESSAGE_FORMAT'] \
= 'gui'<br># Function to get a list of L7 Directories in the \
rsdatapath<br>def fn(path):<br> for top, dirs, files in os.walk(path):<br> \
return [os.path.join(top, dir) for dir in dirs]<br><br><br># START PROCESS<br># PART \
0: PRE-PROCESSING STUFF ###<br># import DEM for atmospheric correction<br># \
r.in.gdal(input=inDEM,output="dem",overwrite=OVR)<br># \
r.mapcalc(expression="dem=25",overwrite=OVR)<br># create a visibility \
map<br># r.mapcalc(expression="vis=18", overwrite=OVR)<br># Find the \
central location of the Landsat file from metadata<br>metadata = []<br>fileList = \
[]<br>L7Dirs = fn(rsdatapath)<br>for L7Dir in L7Dirs:<br> # Ungzip all of your \
Landsat7 images in all your directories<br> # print "Ungzip Landsat files \
in\t",L7Dir<br> # p=os.system("gzip -d -q \
"+L7Dir+"/*.gz")<br> # Using pthreads on multi-core machines<br> \
# p=os.system("pigz -d "+L7Dir+"/*.gz")<br> # Wait ten \
seconds for gzip to create the tif images<br> # time.sleep(10)<br> print \
"Import in GRASS GIS"<br><br clear="all"><div><div><div><div \
class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div \
dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><br><br>Kind \
Regards,<div><font size="4" face="comic sans ms, sans-serif"><b><span \
style="color:rgb(0,0,0)"><span style="font-family:arial,helvetica,sans-serif">Joseph \
Kariuki</span></span><br></b></font></div><div><br></div><div><span \
style="color:rgb(111,168,220)"><u><b>Geospatial Engineer</b></u></span><span \
style="color:rgb(153,153,153)"><b> | </b></span><u><span \
style="color:rgb(106,168,79)"><b>GIS / Web Developer</b></span></u><b> \
</b></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
</div></div></div>
[Attachment #6 (text/plain)]
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic