Пример #1
0
    def nowarp_import(self, file, map):
        """Import raster file into GRASS"""
        if grass.run_command('r.in.gdal',
                             quiet = True,
                             flags = 'o' + self.gdal_flags,
                             input = file,
                             output = map) != 0:
            grass.fatal(_('r.in.gdal failed'))

        # get a list of channels:
        pattern = map + '*'
        grass.debug("pattern: %s" % ','.join(pattern))
        mapset = grass.gisenv()['MAPSET']
        channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
        grass.debug("channel list: %s" % ','.join(channel_list))

        if len(channel_list) < 2:
            # test for single band data
            self.channel_suffixes = []
        else:
            self.channel_suffixes = channel_list # ???
    
        # add to the list of all suffixes:
        self.suffixes = self.suffixes + channel.suffixes
        self.suffixes.sort()
    
        for suffix in self.channel_suffixes:
            # make patch lists
            suffix = suffix.replace('.', '_')
            # this is a hack to make the patch lists empty
            if self.tiler == 0:
                self.patches = []
            self.patches = self.patches.append(map + suffix)
Пример #2
0
    def warp_import(self, file, map):
        """Wrap raster file using gdalwarp and import wrapped file
        into GRASS"""
        warpfile = self.tmp + 'warped.geotiff'
        tmpmapname = map + '_tmp'

        t_srs = grass.read_command('g.proj',
                                   quiet = True,
                                   flags = 'jf').rstrip('\n')
        if not t_srs:
            grass.fatal(_('g.proj failed'))
        
        grass.debug("gdalwarp -s_srs '%s' -t_srs '%s' -r %s %s %s %s" % \
                        (self.options['srs'], t_srs,
                         self.options['method'], self.options['warpoptions'],
                         file, warpfile))
        grass.verbose("Warping input file '%s'..." % os.path.basename(file))
        if self.options['warpoptions']:
            ps = subprocess.Popen(['gdalwarp',
                                   '-s_srs', '%s' % self.options['srs'],
                                   '-t_srs', '%s' % t_srs,
                                   '-r', self.options['method'],
                                   self.options['warpoptions'],
                                   file, warpfile])
        else:
            ps = subprocess.Popen(['gdalwarp',
                                   '-s_srs', '%s' % self.options['srs'],
                                   '-t_srs', '%s' % t_srs,
                                   '-r', self.options['method'],
                                   file, warpfile])
            
        ps.wait()
        if ps.returncode != 0 or \
                not os.path.exists(warpfile):
            grass.fatal(_('gdalwarp failed'))
    
        # import it into a temporary map
        grass.info(_('Importing raster map...'))
        if grass.run_command('r.in.gdal',
                             quiet = True,
                             flags = self.gdal_flags,
                             input = warpfile,
                             output = tmpmapname) != 0:
            grass.fatal(_('r.in.gdal failed'))
        
        os.remove(warpfile)

        # get list of channels
        pattern = tmpmapname + '*'
        grass.debug('Pattern: %s' % pattern)
        mapset = grass.gisenv()['MAPSET']
        channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)[mapset]
        grass.debug('Channel list: %s' % ','.join(channel_list))
        
        if len(channel_list) < 2: # test for single band data
            self.channel_suffixes = []
        else:
            self.channel_suffixes = channel_list # ???
        
        grass.debug('Channel suffixes: %s' % ','.join(self.channel_suffixes))
        
        # add to the list of all suffixes
        self.suffixes = self.suffixes + self.channel_suffixes
        self.suffixes.sort()
        
        # get last suffix
        if len(self.channel_suffixes) > 0:
            last_suffix = self.channel_suffixes[-1]
        else:
            last_suffix = ''

        # find the alpha layer
        if self.flags['k']:
            alphalayer = tmpmapname + last_suffix
        else:
            alphalayer = tmpmapname + '.alpha'
        
        # test to see if the alpha map exists
        if not grass.find_file(element = 'cell', name = alphalayer)['name']:
            alphalayer = ''
        
        # calculate the new maps:
        for suffix in self.channel_suffixes:
            grass.debug("alpha=%s MAPsfx=%s%s tmpname=%s%s" % \
                            (alphalayer, map, suffix, tmpmapname, suffix))
            if alphalayer:
                # Use alpha channel for nulls: problem: I've seen a map
                # where alpha was 1-255; 1 being transparent. what to do?
                # (Geosci Australia Gold layer, format=tiff)
                if grass.run_command('r.mapcalc',
                                     quiet = True,
                                     expression = "%s%s = if(%s, %s%s, null())" % \
                                         (map, sfx, alphalayer, tmpmapname, sfx)) != 0:
                    grass.fatal(_('r.mapcalc failed'))
            else:
                if grass.run_command('g.copy',
                                     quiet = True,
                                     rast = "%s%s,%s%s" % \
                                         (tmpmapname, suffix, map, suffix)) != 0:
                    grass.fatal(_('g.copy failed'))
        
            # copy the color tables
            if grass.run_command('r.colors',
                                 quiet = True,
                                 map = map + suffix,
                                 rast = tmpmapname + suffix) != 0:
                grass.fatal(_('g.copy failed'))

            # make patch lists
            suffix = suffix.replace('.', '_')
            # this is a hack to make the patch lists empty:
            if self.tiler == 0:
                self.patches = []
            self.patches = self.patches.append(map + suffix)
    
        # if no suffix, processing is simple (e.g. elevation has only 1
        # band)
        if len(channel_list) < 2:
            # run r.mapcalc to crop to region
            if grass.run_command('r.mapcalc',
                                 quiet = True,
                                 expression = "%s = %s" % \
                                     (map, tmpmapname)) != 0:
                grass.fatal(_('r.mapcalc failed'))
            
            if grass.run_command('r.colors',
                                 quiet = True,
                                 map = map,
                                 rast = tmpmapname) != 0:
                grass.fatal(_('r.colors failed'))
    
        # remove the old channels
        if grass.run_command('g.remove',
                             quiet = True,
                             rast = ','.join(channel_list)) != 0:
            grass.fatal(_('g.remove failed'))