[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: \
&#39;NoneType&#39; 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 = \
&quot;tm7&quot;<br><br># Setup the path to the Landsat 7 Directories<br>rsdatapath = \
&quot;~/rawData/L7Dir&quot;<br># set L7 Metadata wildcards<br>wldc_mtl = \
&quot;*_MTL.txt&quot;<br># Visibility distance [Km]<br>vis = 18<br><br># DEM input to \
atmospheric correction<br>inDEM = rsdatapath + \
&quot;/dem_30m/Kenya_SRTM30meters.tif&quot;<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&#39;C:\OSGeo4W\bin\grass72svn.bat&#39;<br># uncomment \
when using standalone WinGRASS installer<br># grass7bin_win = r&#39;C:\Program Files \
(x86)\GRASS GIS 7.2.0\grass72.bat&#39;<br># Linux<br>grass7bin_lin = \
&#39;grass72&#39;<br># Mac OS X<br># grass7bin_mac = \
&#39;/Applications/GRASS/GRASS-7.2.app/&#39;<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(&quot;~&quot;), &quot;rawData&quot;)<br># the \
following path is the default path on MS Windows<br># gisdb = \
os.path.join(os.path.expanduser(&quot;~&quot;), \
&quot;Documents/grassdata&quot;)<br><br># specify (existing) location and \
mapset<br>location = &quot;L7_Kenya&quot;<br>mapset = &quot;hempire&quot;<br><br># \
SOFTWARE<br>if sys.platform.startswith(&#39;linux&#39;):<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(&#39;Platform not configured.&#39;)<br><br># query GRASS 7 itself for its \
GISBASE<br>startcmd = [grass7bin, &#39;--config&#39;, &#39;path&#39;]<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 &gt;&gt; sys.stderr, &quot;ERROR: Cannot find \
GRASS GIS 7 start script (%s)&quot; % startcmd<br>       sys.exit(-1)<br>gisbase = \
out.strip(&#39;\n\r&#39;)<br><br># Set GISBASE environment \
variable<br>os.environ[&#39;GISBASE&#39;] = gisbase<br># the following not needed \
with trunk<br>os.environ[&#39;PATH&#39;] += os.pathsep + os.path.join(gisbase, \
&#39;extrabin&#39;)<br># add path to GRASS addons<br>home = \
os.path.expanduser(&quot;~&quot;)<br>os.environ[&#39;PATH&#39;] += os.pathsep + \
os.path.join(home, &#39;.grass7&#39;, &#39;addons&#39;, &#39;scripts&#39;)<br><br># \
define GRASS-Python environment<br>gpydir = os.path.join(gisbase, &quot;etc&quot;, \
&quot;python&quot;)<br>sys.path.append(gpydir)<br><br># DATA<br># Set GISDBASE \
environment variable<br>os.environ[&#39;GISDBASE&#39;] = 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[&#39;GRASS_MESSAGE_FORMAT&#39;] \
= &#39;gui&#39;<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=&quot;dem&quot;,overwrite=OVR)<br># \
r.mapcalc(expression=&quot;dem=25&quot;,overwrite=OVR)<br># create a visibility \
map<br># r.mapcalc(expression=&quot;vis=18&quot;, 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 &quot;Ungzip Landsat files \
in\t&quot;,L7Dir<br>       # p=os.system(&quot;gzip -d -q \
&quot;+L7Dir+&quot;/*.gz&quot;)<br>       # Using pthreads on multi-core machines<br> \
# p=os.system(&quot;pigz -d &quot;+L7Dir+&quot;/*.gz&quot;)<br>       # Wait ten \
seconds for gzip to create the tif images<br>       # time.sleep(10)<br>       print \
&quot;Import in GRASS GIS&quot;<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