コード例 #1
0
ファイル: g.extension.py プロジェクト: imincik/pkg-grass
def list_available_extensions():
    mlist = list()

    # try to download XML metadata file first
    url = "http://grass.osgeo.org/addons/grass%s/modules.xml" % grass.version()['version'].split('.')[0]
    grass.debug("url=%s" % url, 1)
    try:
        f = urlopen(url)
        try:
            tree = etree.fromstring(f.read())
        except:
            grass.warning(_("Unable to parse '%s'. Trying to scan SVN (may take some time)...") % url)
            list_available_extensions_svn()
            return
        
        for mnode in tree.findall('task'):
            name = mnode.get('name')
            if flags['c'] or flags['g']:
                desc, keyw = get_optional_params(mnode)
            
            if flags['g']:
                print 'name=' + name
                print 'description=' + desc
                print 'keywords=' + keyw
            elif flags['c']:
                print name + ' - ' + desc
            else:
                print name
    except HTTPError:
        return list_available_extensions_svn()
    
    return mlist
コード例 #2
0
ファイル: g.extension.py プロジェクト: imincik/pkg-grass
def list_wxgui_extensions(print_module = True):
    mlist = list()
    grass.debug('Fetching list of wxGUI extensions from GRASS-Addons SVN (be patient)...')
    pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
    grass.verbose(_("Checking for '%s' modules...") % 'gui/wxpython')
    
    url = '%s/%s' % (options['svnurl'], 'gui/wxpython')
    grass.debug("url = %s" % url, debug = 2)
    f = urlopen(url)
    if not f:
        grass.warning(_("Unable to fetch '%s'") % url)
        return
        
    for line in f.readlines():
        # list extensions
        sline = pattern.search(line)
        if not sline:
            continue
        name = sline.group(2).rstrip('/')
        if name not in ('..', 'Makefile'):
            if print_module:
                print name
            mlist.append(name)
    
    return mlist
コード例 #3
0
ファイル: gdalwarp.py プロジェクト: AsherBond/MondocosmOS
    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)
コード例 #4
0
ファイル: g.extension.py プロジェクト: imincik/pkg-grass
def install_extension_win():
    ### TODO: do not use hardcoded url - http://wingrass.fsv.cvut.cz/grassXX/addonsX.X.X
    version = grass.version()['version'].split('.')
    grass.message(_("Downloading precompiled GRASS Addons <%s>...") % options['extension'])
    url = "http://wingrass.fsv.cvut.cz/grass%s%s/addons" % (version[0], version[1])
    if version[0] == '6' and version[1] == '4':
        url += '/grass-%s.%s.%s' % (version[0], version[1], version[2])
    grass.debug("url=%s" % url, 1)
    
    try:
        f = urlopen(url + '/' + options['extension'] + '.zip')
        
        # create addons dir if not exists
        if not os.path.exists(options['prefix']):
            os.mkdir(options['prefix'])
        
        # download data
        fo = tempfile.TemporaryFile()
        fo.write(f.read())
        zfobj = zipfile.ZipFile(fo)
        for name in zfobj.namelist():
            if name.endswith('/'):
                d = os.path.join(options['prefix'], name)
                if not os.path.exists(d):
                    os.mkdir(d)
            else:
                outfile = open(os.path.join(options['prefix'], name), 'wb')
                outfile.write(zfobj.read(name))
                outfile.close()
        
        fo.close()
    except HTTPError:
        grass.fatal(_("GRASS Addons <%s> not found") % options['extension'])
    
    return 0
コード例 #5
0
def list_available_modules(url, mlist = None):
    # try to download XML metadata file first
    url = url + "modules.xml"
    grass.debug("url=%s" % url, 1)
    try:
        f = urlopen(url, proxies=PROXIES)
        try:
            tree = etree.fromstring(f.read())
        except:
            grass.warning(_("Unable to parse '%s'. Trying to scan SVN repository (may take some time)...") % url)
            list_available_extensions_svn()
            return

        for mnode in tree.findall('task'):
            name = mnode.get('name').strip()
            if mlist and name not in mlist:
                continue
            if flags['c'] or flags['g']:
                desc, keyw = get_optional_params(mnode)

            if flags['g']:
                print 'name=' + name
                print 'description=' + desc
                print 'keywords=' + keyw
            elif flags['c']:
                if mlist:
                    print '*',
                print name + ' - ' + desc
            else:
                print name
    except HTTPError:
        list_available_extensions_svn()
コード例 #6
0
def list_available_extensions_svn():
    grass.message(_('Fetching list of extensions from GRASS-Addons SVN repository (be patient)...'))
    pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)

    if flags['c']:
        grass.warning(_("Flag 'c' ignored, metadata file not available"))
    if flags['g']:
        grass.warning(_("Flag 'g' ignored, metadata file not available"))

    prefix = ['d', 'db', 'g', 'i', 'm', 'ps',
              'p', 'r', 'r3', 's', 'v']
    for d in prefix:
        modclass = expand_module_class_name(d)
        grass.verbose(_("Checking for '%s' modules...") % modclass)

        url = '%s/%s' % (options['svnurl'], modclass)
        grass.debug("url = %s" % url, debug = 2)
        try:
            f = urlopen(url, proxies=PROXIES)
        except HTTPError:
            grass.debug(_("Unable to fetch '%s'") % url, debug = 1)
            continue

        for line in f.readlines():
            # list extensions
            sline = pattern.search(line)
            if not sline:
                continue
            name = sline.group(2).rstrip('/')
            if name.split('.', 1)[0] == d:
                print name
コード例 #7
0
def install_extension_win(name):
    ### do not use hardcoded url - http://wingrass.fsv.cvut.cz/grassXX/addonsX.X.X
    grass.message(_("Downloading precompiled GRASS Addons <%s>...") % options['extension'])
    url = "http://wingrass.fsv.cvut.cz/grass%(major)s%(minor)s/addons/grass-%(major)s.%(minor)s.%(patch)s/" % \
        { 'major' : version[0], 'minor' : version[1], 'patch' : version[2]}
    
    grass.debug("url=%s" % url, 1)

    try:
        f = urlopen(url + '/' + name + '.zip', proxies=PROXIES)

        # create addons dir if not exists
        if not os.path.exists(options['prefix']):
            os.mkdir(options['prefix'])

        # download data
        fo = tempfile.TemporaryFile()
        fo.write(f.read())
        zfobj = zipfile.ZipFile(fo)
        for name in zfobj.namelist():
            if name.endswith('/'):
                d = os.path.join(options['prefix'], name)
                if not os.path.exists(d):
                    os.mkdir(d)
            else:
                outfile = open(os.path.join(options['prefix'], name), 'wb')
                outfile.write(zfobj.read(name))
                outfile.close()

        fo.close()
    except HTTPError:
        grass.fatal(_("GRASS Addons <%s> not found") % name)

    return 0
コード例 #8
0
def list_available_extensions():
    mlist = list()

    # try to download XML metadata file first
    url = "http://grass.osgeo.org/addons/grass%s/modules.xml" % grass.version()['version'].split('.')[0]
    grass.debug("url=%s" % url, 1)
    try:
        f = urlopen(url)
        try:
            tree = etree.fromstring(f.read())
        except:
            grass.warning(_("Unable to parse '%s'. Trying to scan SVN (may take some time)...") % url)
            list_available_extensions_svn()
            return
        
        for mnode in tree.findall('task'):
            name = mnode.get('name')
            if flags['c'] or flags['g']:
                desc, keyw = get_optional_params(mnode)
            
            if flags['g']:
                print 'name=' + name
                print 'description=' + desc
                print 'keywords=' + keyw
            elif flags['c']:
                print name + ' - ' + desc
            else:
                print name
    except HTTPError:
        return list_available_extensions_svn()
    
    return mlist
コード例 #9
0
def install_extension_win():
    ### TODO: do not use hardcoded url - http://wingrass.fsv.cvut.cz/grassXX/addonsX.X.X
    version = grass.version()['version'].split('.')
    grass.message(_("Downloading precompiled GRASS Addons <%s>...") % options['extension'])
    url = "http://wingrass.fsv.cvut.cz/grass%s%s/addons" % (version[0], version[1])
    if version[0] == '6' and version[1] == '4':
        url += '/grass-%s.%s.%s' % (version[0], version[1], version[2])
    grass.debug("url=%s" % url, 1)
    
    try:
        f = urlopen(url + '/' + options['extension'] + '.zip')
        
        # create addons dir if not exists
        if not os.path.exists(options['prefix']):
            os.mkdir(options['prefix'])
        
        # download data
        fo = tempfile.TemporaryFile()
        fo.write(f.read())
        zfobj = zipfile.ZipFile(fo)
        for name in zfobj.namelist():
            if name.endswith('/'):
                d = os.path.join(options['prefix'], name)
                if not os.path.exists(d):
                    os.mkdir(d)
            else:
                outfile = open(os.path.join(options['prefix'], name), 'wb')
                outfile.write(zfobj.read(name))
                outfile.close()
        
        fo.close()
    except HTTPError:
        grass.fatal(_("GRASS Addons <%s> not found") % options['extension'])
    
    return 0
コード例 #10
0
def list_wxgui_extensions(print_module = True):
    mlist = list()
    grass.debug('Fetching list of wxGUI extensions from GRASS-Addons SVN (be patient)...')
    pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
    grass.verbose(_("Checking for '%s' modules...") % 'gui/wxpython')
    
    url = '%s/%s' % (options['svnurl'], 'gui/wxpython')
    grass.debug("url = %s" % url, debug = 2)
    f = urlopen(url)
    if not f:
        grass.warning(_("Unable to fetch '%s'") % url)
        return
        
    for line in f.readlines():
        # list extensions
        sline = pattern.search(line)
        if not sline:
            continue
        name = sline.group(2).rstrip('/')
        if name not in ('..', 'Makefile'):
            if print_module:
                print name
            mlist.append(name)
    
    return mlist
コード例 #11
0
def render(cmd, mapfile):
    env = os.environ.copy()
    if mapfile:
        env['GRASS_RENDER_FILE'] = mapfile
    try:
        grass.run_command(cmd[0], env=env, **cmd[1])
    except Exception as e:
        grass.debug(1, "Unable to render: {}".format(e))
コード例 #12
0
ファイル: g.extension.py プロジェクト: AsherBond/MondocosmOS
def get_module_desc(url, script = True):
    grass.debug('url=%s' % url)
    f = urllib.urlopen(url)
    if script:
        ret = get_module_script(f)
    else:
        ret = get_module_main(f)
        
    return ret
コード例 #13
0
def render(cmd, mapfile):
    env = os.environ.copy()

    if mapfile:
        env["GRASS_RENDER_FILE"] = mapfile
    try:
        grass.run_command(cmd[0], env=env, **cmd[1])
    except CalledModuleError as e:
        grass.debug("Unable to render: {0}".format(e), 1)
コード例 #14
0
def create_dir(path):
    if os.path.isdir(path):
        return

    try:
        os.makedirs(path)
    except OSError as e:
        grass.fatal(_("Unable to create '%s': %s") % (path, e))

    grass.debug("'%s' created" % path)
コード例 #15
0
    def emit(self, record):
        """ Write the log message.

        :param record: record to emit
        """
        if record.levelno >= logging.ERROR:
            fatal(record.msg)
        elif record.levelno >= logging.WARNING:
            warning(record.msg)
        elif record.levelno >= logging.INFO:
            info(record.msg)
        elif record.levelno >= logging.DEBUG:
            debug(record.msg)
コード例 #16
0
def oifcalc(sdev, corr, k1, k2, k3):
    grass.debug(_("Calculating OIF for combination: %s, %s, %s" % (k1, k2, k3)), 1)
    # calculate SUM of Stddeviations:
    ssdev = [sdev[k1], sdev[k2], sdev[k3]]
    numer = sum(ssdev)

    # calculate SUM of absolute(Correlation values):
    scorr = [corr[k1, k2], corr[k1, k3], corr[k2, k3]]
    denom = sum(map(abs, scorr))

    # Calculate OIF index:
    #     Divide (SUM of Stddeviations) and (SUM of Correlation)
    return numer / denom
コード例 #17
0
ファイル: i.oif.py プロジェクト: rashadkm/grass_cmake
def oifcalc(sdev, corr, k1, k2, k3):
    grass.debug(_("Calculating OIF for combination: %s, %s, %s" % (k1, k2,
                                                                   k3)), 1)
    # calculate SUM of Stddeviations:
    ssdev = [sdev[k1], sdev[k2], sdev[k3]]
    numer = sum(ssdev)

    # calculate SUM of absolute(Correlation values):
    scorr = [corr[k1, k2], corr[k1, k3], corr[k2, k3]]
    denom = sum(map(abs, scorr))

    # Calculate OIF index:
    #     Divide (SUM of Stddeviations) and (SUM of Correlation)
    return numer / denom
コード例 #18
0
ファイル: d.wms.py プロジェクト: rashadkm/grass_cmake
def main():
    options['region'] = GetRegion()

    if 'GRASS' in options['driver']:
        grass.debug("Using GRASS driver")
        from wms_drv import WMSDrv
        wms = WMSDrv()
    elif 'GDAL' in options['driver']:
        grass.debug("Using GDAL WMS driver")
        from wms_gdal_drv import WMSGdalDrv
        wms = WMSGdalDrv()

    temp_map = wms.GetMap(options, flags)
    os.rename(temp_map, os.environ["GRASS_RENDER_FILE"])

    return 0
コード例 #19
0
ファイル: d.wms.py プロジェクト: GRASS-GIS/grass-ci
def main():
    options['region'] = GetRegion()

    if 'GRASS' in options['driver']:
        grass.debug("Using GRASS driver")
        from wms_drv import WMSDrv
        wms = WMSDrv()
    elif 'GDAL' in options['driver']:
        grass.debug("Using GDAL WMS driver")
        from wms_gdal_drv import WMSGdalDrv
        wms = WMSGdalDrv()

    temp_map = wms.GetMap(options, flags)
    shutil.move(temp_map, os.environ["GRASS_RENDER_FILE"])

    return 0
コード例 #20
0
ファイル: d.wms.py プロジェクト: caomw/grass
def main():
    options["region"] = GetRegion()

    if "GRASS" in options["driver"]:
        grass.debug("Using GRASS driver")
        from wms_drv import WMSDrv

        wms = WMSDrv()
    elif "GDAL" in options["driver"]:
        grass.debug("Using GDAL WMS driver")
        from wms_gdal_drv import WMSGdalDrv

        wms = WMSGdalDrv()

    temp_map = wms.GetMap(options, flags)
    os.rename(temp_map, os.environ["GRASS_PNGFILE"])

    return 0
コード例 #21
0
def main():
    options["region"] = GetRegion()

    if "GRASS" in options["driver"]:
        grass.debug("Using GRASS driver")
        from wms_drv import WMSDrv

        wms = WMSDrv()
    elif "GDAL" in options["driver"]:
        grass.debug("Using GDAL WMS driver")
        from wms_gdal_drv import WMSGdalDrv

        wms = WMSGdalDrv()

    temp_map = wms.GetMap(options, flags)
    shutil.move(temp_map, os.environ["GRASS_RENDER_FILE"])

    return 0
コード例 #22
0
ファイル: g.extension.py プロジェクト: AsherBond/MondocosmOS
def list_available_modules():
    mlist = list()
    grass.message(_('Fetching list of modules from GRASS-Addons SVN (be patient)...'))
    pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
    i = 0
    prefix = ['d', 'db', 'g', 'i', 'm', 'ps',
              'p', 'r', 'r3', 's', 'v']
    nprefix = len(prefix)
    for d in prefix:
        if flags['g']:
            grass.percent(i, nprefix, 1)
            i += 1
        
        modclass = expand_module_class_name(d)
        grass.verbose(_("Checking for '%s' modules...") % modclass)
        
        url = '%s/%s' % (options['svnurl'], modclass)
        grass.debug("url = %s" % url, debug = 2)
        f = urllib.urlopen(url)
        if not f:
            grass.warning(_("Unable to fetch '%s'") % url)
            continue
        
        for line in f.readlines():
            # list modules
            sline = pattern.search(line)
            if not sline:
                continue
            name = sline.group(2).rstrip('/')
            if name.split('.', 1)[0] == d:
                print_module_desc(name, url)
                mlist.append(name)
    
    mlist += list_wxgui_extensions()
    
    if flags['g']:
        grass.percent(1, 1, 1)
    
    return mlist
コード例 #23
0
    
    try:
        shutil.copyfile(dist_file,addons_file)
    except OSError, e:
        grass.fatal(_("Unable to create '%s': %s") % (addons_file, e))
    
def create_dir(path):
    if os.path.isdir(path):
        return

    try:
        os.makedirs(path)
    except OSError, e:
        grass.fatal(_("Unable to create '%s': %s") % (path, e))
    
    grass.debug("'%s' created" % path)
    
def check_dirs():
    create_dir(os.path.join(options['prefix'], 'bin'))
    create_dir(os.path.join(options['prefix'], 'docs', 'html'))
    check_style_files('grass_logo.png')
    check_style_files('grassdocs.css')
    # create_dir(os.path.join(options['prefix'], 'etc'))
    create_dir(os.path.join(options['prefix'], 'docs', 'man', 'man1'))
    create_dir(os.path.join(options['prefix'], 'man', 'man1'))
    create_dir(os.path.join(options['prefix'], 'scripts'))

def main():
    # check dependecies
    if sys.platform != "win32":
        check_progs()
コード例 #24
0
ファイル: gdalwarp.py プロジェクト: AsherBond/MondocosmOS
    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'))
コード例 #25
0
ファイル: v.unpack.py プロジェクト: GRASS-GIS/grass-ci
def main():
    infile = options['input']

    # create temporary directory
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    # check if the input file exists
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    # copy the files to tmp dir
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    # set the output name
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name

    # grass env
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])

    new_dir = os.path.join(mset_dir, 'vector', map_name)

    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Vector map <%s> already exists") % map_name)
    elif overwrite == '1' and gfile['file']:
        grass.warning(_("Vector map <%s> already exists and will be overwritten") % map_name)
        grass.run_command('g.remove', flags='f', quiet=True, type='vector',
                          name=map_name)
        shutil.rmtree(new_dir, True)

    # extract data
    tar.extractall()
    if os.path.exists(os.path.join(data_name, 'coor')):
        pass
    elif os.path.exists(os.path.join(data_name, 'cell')):
        grass.fatal(_("This GRASS GIS pack file contains raster data. Use "
                      "r.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

    skip_projection_check = False
    if not os.path.exists(os.path.join(tmp_dir, 'PROJ_INFO')):
        if os.path.exists(loc_proj):
            grass.fatal(
                _("PROJ_INFO file is missing, unpack vector map in XY (unprojected) location."))
        skip_projection_check = True  # XY location

    if not skip_projection_check:
        diff_result_1 = diff_result_2 = None
        if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir, 'PROJ_INFO'),
                                                  filename_b=loc_proj, proj=True):
            diff_result_1 = diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
                                       loc_proj)

        if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir, 'PROJ_UNITS'),
                                                  filename_b=loc_proj_units,
                                                  units=True):
            diff_result_2 = diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
                                       loc_proj_units)

        if diff_result_1 or diff_result_2:
            if flags['o']:
                grass.warning(_("Projection information does not match. Proceeding..."))
            else:
                if diff_result_1:
                    grass.warning(_("Difference between PROJ_INFO file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
                grass.fatal(_("Projection of dataset does not appear to match current location."
                              " In case of no significant differences in the projection definitions,"
                              " use the -o flag to ignore them and use"
                              " current location definition."))

    # new db
    fromdb = os.path.join(tmp_dir, 'db.sqlite')
    # copy file
    shutil.copytree(data_name, new_dir)
    # exist fromdb
    if os.path.exists(fromdb):
        # the db connection in the output mapset
        dbconn = grassdb.db_connection(force=True)
        todb = dbconn['database']
        # return all tables
        list_fromtable = grass.read_command('db.tables', driver='sqlite',
                                            database=fromdb).splitlines()

        # return the list of old connection for extract layer number and key
        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
        dbnlist = dbln.readlines()
        dbln.close()
        # check if dbf or sqlite directory exists
        if dbconn['driver'] == 'dbf' and not os.path.exists(os.path.join(mset_dir, 'dbf')):
            os.mkdir(os.path.join(mset_dir, 'dbf'))
        elif dbconn['driver'] == 'sqlite' and not os.path.exists(os.path.join(mset_dir, 'sqlite')):
            os.mkdir(os.path.join(mset_dir, 'sqlite'))
        # for each old connection
        for t in dbnlist:
            # it split the line of each connection, to found layer number and key
            if len(t.split('|')) != 1:
                values = t.split('|')
            else:
                values = t.split(' ')

            from_table = values[1]
            layer = values[0].split('/')[0]
            # we need to take care about the table name in case of several layer
            if options["output"]:
                if len(dbnlist) > 1:
                    to_table = "%s_%s" % (map_name, layer)
                else:
                    to_table = map_name
            else:
                to_table = from_table

            grass.verbose(_("Coping table <%s> as table <%s>") % (from_table,
                                                                  to_table))

            # copy the table in the default database
            try:
                grass.run_command('db.copy', to_driver=dbconn['driver'],
                                  to_database=todb, to_table=to_table,
                                  from_driver='sqlite',
                                  from_database=fromdb,
                                  from_table=from_table)
            except CalledModuleError:
                grass.fatal(_("Unable to copy table <%s> as table <%s>") % (from_table, to_table))

            grass.verbose(_("Connect table <%s> to vector map <%s> at layer <%s>") %
                           (to_table, map_name, layer))

            # and connect the new tables with the right layer
            try:
                grass.run_command('v.db.connect', flags='o', quiet=True,
                                  driver=dbconn['driver'], database=todb,
                                  map=map_name, key=values[2],
                                  layer=layer, table=to_table)
            except CalledModuleError:
                grass.fatal(_("Unable to connect table <%s> to vector map <%s>") %
                             (to_table, map_name))

    grass.message(_("Vector map <%s> successfully unpacked") % map_name)
コード例 #26
0
ファイル: v.in.lines.py プロジェクト: landam/grass-stable-ppa
def main():
    global tmp

    fs = separator(options['separator'])
    threeD = flags['z']

    prog = 'v.in.lines'

    if threeD:
        do3D = 'z'
    else:
        do3D = ''

    tmp = grass.tempfile()

    # set up input file
    if options['input'] == '-':
        infile = None
        inf = sys.stdin
    else:
        infile = options['input']
        if not os.path.exists(infile):
            grass.fatal(_("Unable to read input file <%s>") % infile)
        grass.debug("input file=[%s]" % infile)

    if not infile:
        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
        outf = file(tmp, 'w')
        for line in inf:
            if len(line.lstrip()) == 0 or line[0] == '#':
                continue
            outf.write(line.replace(fs, ' '))

        outf.close()
        runfile = tmp
    else:
        # read from a real file
        if fs == ' ':
            runfile = infile
        else:
            inf = file(infile)
            outf = file(tmp, 'w')

            for line in inf:
                if len(line.lstrip()) == 0 or line[0] == '#':
                    continue
                outf.write(line.replace(fs, ' '))

            inf.close()
            outf.close()
            runfile = tmp

    # check that there are at least two columns (three if -z is given)
    inf = file(runfile)
    for line in inf:
        if len(line.lstrip()) == 0 or line[0] == '#':
            continue
        numcols = len(line.split())
        break
    inf.close()
    if (do3D and numcols < 3) or (not do3D and numcols < 2):
        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))

    grass.run_command('v.in.mapgen',
                      flags='f' + do3D,
                      input=runfile,
                      output=options['output'])
コード例 #27
0
def main():
    coords = options["coordinates"]
    input = options["input"]
    output = options["output"]
    fs = options["separator"]
    proj_in = options["proj_in"]
    proj_out = options["proj_out"]
    ll_in = flags["i"]
    ll_out = flags["o"]
    decimal = flags["d"]
    copy_input = flags["e"]
    include_header = flags["c"]

    # check for cs2cs
    if not gcore.find_program("cs2cs"):
        gcore.fatal(
            _("cs2cs program not found, install PROJ first: \
            https://proj.org"))

    # parse field separator
    # FIXME: input_x,y needs to split on multiple whitespace between them
    if fs == ",":
        ifs = ofs = ","
    else:
        try:
            ifs, ofs = fs.split(",")
        except ValueError:
            ifs = ofs = fs

    ifs = separator(ifs)
    ofs = separator(ofs)

    # set up projection params
    s = gcore.read_command("g.proj", flags="j")
    kv = parse_key_val(s)
    if "XY location" in kv["+proj"] and (ll_in or ll_out):
        gcore.fatal(_("Unable to project to or from a XY location"))

    in_proj = None

    if ll_in:
        in_proj = "+proj=longlat +datum=WGS84"
        gcore.verbose(
            "Assuming LL WGS84 as input, current projection as output ")

    if ll_out:
        in_proj = gcore.read_command("g.proj", flags="jf")

    if proj_in:
        if "+" in proj_in:
            in_proj = proj_in
        else:
            gcore.fatal(_("Invalid PROJ.4 input specification"))

    if not in_proj:
        gcore.verbose("Assuming current location as input")
        in_proj = gcore.read_command("g.proj", flags="jf")

    in_proj = in_proj.strip()
    gcore.verbose("Input parameters: '%s'" % in_proj)

    out_proj = None

    if ll_out:
        out_proj = "+proj=longlat +datum=WGS84"
        gcore.verbose(
            "Assuming current projection as input, LL WGS84 as output ")

    if ll_in:
        out_proj = gcore.read_command("g.proj", flags="jf")

    if proj_out:
        if "+" in proj_out:
            out_proj = proj_out
        else:
            gcore.fatal(_("Invalid PROJ.4 output specification"))

    if not out_proj:
        gcore.fatal(_("Missing output projection parameters "))
    out_proj = out_proj.strip()
    gcore.verbose("Output parameters: '%s'" % out_proj)

    # set up input file
    if coords:
        x, y = coords.split(",")
        tmpfile = gcore.tempfile()
        fd = open(tmpfile, "w")
        fd.write("%s%s%s\n" % (x, ifs, y))
        fd.close()
        inf = open(tmpfile)
    else:
        if input == "-":
            infile = None
            inf = sys.stdin
        else:
            infile = input
            if not os.path.exists(infile):
                gcore.fatal(_("Unable to read input data"))
            inf = open(infile)
            gcore.debug("input file=[%s]" % infile)

    # set up output file
    if not output:
        outfile = None
        outf = sys.stdout
    else:
        outfile = output
        outf = open(outfile, "w")
        gcore.debug("output file=[%s]" % outfile)

    # set up output style
    if not decimal:
        outfmt = ["-w5"]
    else:
        outfmt = ["-f", "%.8f"]
    if not copy_input:
        copyinp = []
    else:
        copyinp = ["-E"]

    # do the conversion
    # Convert cs2cs DMS format to GRASS DMS format:
    #   cs2cs | sed -e 's/d/:/g' -e "s/'/:/g"  -e 's/"//g'

    cmd = ["cs2cs"
           ] + copyinp + outfmt + in_proj.split() + ["+to"] + out_proj.split()

    p = gcore.Popen(cmd, stdin=gcore.PIPE, stdout=gcore.PIPE)

    tr = TrThread(ifs, inf, p.stdin)
    tr.start()

    if not copy_input:
        if include_header:
            outf.write("x%sy%sz\n" % (ofs, ofs))
        for line in p.stdout:
            try:
                xy, z = decode(line).split(" ", 1)
                x, y = xy.split("\t")
            except ValueError:
                gcore.fatal(line)

            outf.write("%s%s%s%s%s\n" %
                       (x.strip(), ofs, y.strip(), ofs, z.strip()))
    else:
        if include_header:
            outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs))
        for line in p.stdout:
            inXYZ, x, rest = decode(line).split("\t")
            inX, inY = inXYZ.split(" ")[:2]
            y, z = rest.split(" ", 1)
            outf.write("%s%s%s%s%s%s%s%s%s\n" % (
                inX.strip(),
                ofs,
                inY.strip(),
                ofs,
                x.strip(),
                ofs,
                y.strip(),
                ofs,
                z.strip(),
            ))

    p.wait()

    if p.returncode != 0:
        gcore.warning(
            _("Projection transform probably failed, please investigate"))
コード例 #28
0
def main():
    out = options["output"]
    wfs_url = options["url"]

    request_base = "REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0"
    wfs_url += request_base

    if options["name"]:
        wfs_url += "&TYPENAME=" + options["name"]

    if options["srs"]:
        wfs_url += "&SRS=" + options["srs"]

    if options["maximum_features"]:
        wfs_url += "&MAXFEATURES=" + options["maximum_features"]
        if int(options["maximum_features"]) < 1:
            # GTC Invalid WFS maximum features parameter
            grass.fatal(_("Invalid maximum number of features"))

    if options["start_index"]:
        wfs_url += "&STARTINDEX=" + options["start_index"]
        if int(options["start_index"]) < 1:
            # GTC Invalid WFS start index parameter
            grass.fatal(_('Features begin with index "1"'))

    if flags["r"]:
        bbox = grass.read_command("g.region", flags="w").split("=")[1]
        wfs_url += "&BBOX=" + bbox

    if flags["l"]:
        wfs_url = options[
            "url"] + "REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0"

    tmp = grass.tempfile()
    tmpxml = tmp + ".xml"

    grass.debug(wfs_url)

    # Set user and password if given
    if options["username"] and options["password"]:
        grass.message(_("Setting username and password..."))
        if os.path.isfile(options["username"]):
            with open(options["username"]) as f:
                filecontent = f.read()
                user = filecontent.strip()
        elif options["username"] in os.environ:
            user = os.environ[options["username"]]
        else:
            user = options["username"]
        if os.path.isfile(options["password"]):
            with open(options["password"]) as f:
                filecontent = f.read()
                pw = filecontent.strip()
        elif options["password"] in os.environ:
            pw = os.environ[options["password"]]
        else:
            pw = options["password"]

        passmgr = HTTPPasswordMgrWithDefaultRealm()
        passmgr.add_password(None, wfs_url, user, pw)
        authhandler = HTTPBasicAuthHandler(passmgr)
        opener = build_opener(authhandler)
        install_opener(opener)

    # GTC Downloading WFS features
    grass.message(_("Retrieving data..."))
    try:
        inf = urlopen(wfs_url)
    except HTTPError as e:
        # GTC WFS request HTTP failure
        grass.fatal(
            _("The server couldn't fulfill the request.\nError code: %s") %
            e.code)
    except URLError as e:
        # GTC WFS request network failure
        grass.fatal(_("Failed to reach the server.\nReason: %s") % e.reason)

    outf = open(tmpxml, "wb")
    while True:
        s = inf.read()
        if not s:
            break
        outf.write(s)
    inf.close()
    outf.close()

    if flags["l"]:
        import shutil

        if os.path.exists("wms_capabilities.xml"):
            grass.fatal(
                _('A file called "wms_capabilities.xml" already exists here'))
        # os.move() might fail if the temp file is on another volume, so we copy instead
        shutil.copy(tmpxml, "wms_capabilities.xml")
        try_remove(tmpxml)
        sys.exit(0)

    grass.message(_("Importing data..."))
    try:
        grass.run_command("v.in.ogr", flags="o", input=tmpxml, output=out)
        grass.message(_("Vector map <%s> imported from WFS.") % out)
    except:
        grass.message(_("WFS import failed"))
    finally:
        try_remove(tmpxml)
コード例 #29
0
ファイル: toolboxes.py プロジェクト: felipebetancur/grass-ci
def _debug(level, message):
    """Show debug message"""
    # this has interface as originally used GUI Debug but uses grass.script
    gcore.debug(message, level)
コード例 #30
0
def main():
    input = options["input"]
    output = options["output"]
    fs = options["fs"]
    proj_in = options["proj_input"]
    proj_out = options["proj_output"]
    ll_in = flags["i"]
    ll_out = flags["o"]
    decimal = flags["d"]
    copy_input = flags["e"]
    include_header = flags["c"]

    #### check for cs2cs
    if not grass.find_program("cs2cs"):
        grass.fatal(_("cs2cs program not found, install PROJ.4 first: http://proj.maptools.org"))

        #### check for overenthusiasm
    if proj_in and ll_in:
        grass.fatal(_("Choose only one input parameter method"))

    if proj_out and ll_out:
        grass.fatal(_("Choose only one output parameter method"))

    if ll_in and ll_out:
        grass.fatal(_("Choise only one auto-projection parameter method"))

    if output and not grass.overwrite() and os.path.exists(output):
        grass.fatal(_("Output file already exists"))

        #### parse field separator
        # FIXME: input_x,y needs to split on multiple whitespace between them
    if fs == ",":
        ifs = ofs = ","
    else:
        try:
            ifs, ofs = fs.split(",")
        except ValueError:
            ifs = ofs = fs

    ifs = ifs.lower()
    ofs = ofs.lower()

    if ifs in ("space", "tab"):
        ifs = " "
    elif ifs == "comma":
        ifs = ","
    else:
        if len(ifs) > 1:
            grass.warning(_("Invalid field separator, using '%s'") % ifs[0])
        try:
            ifs = ifs[0]
        except IndexError:
            grass.fatal(_("Invalid field separator '%s'") % ifs)

    if ofs.lower() == "space":
        ofs = " "
    elif ofs.lower() == "tab":
        ofs = "\t"
    elif ofs.lower() == "comma":
        ofs = ","
    else:
        if len(ofs) > 1:
            grass.warning(_("Invalid field separator, using '%s'") % ofs[0])
        try:
            ofs = ofs[0]
        except IndexError:
            grass.fatal(_("Invalid field separator '%s'") % ifs)

    #### set up projection params
    s = grass.read_command("g.proj", flags="j")
    kv = grass.parse_key_val(s)
    if "XY location" in kv["+proj"] and (ll_in or ll_out):
        grass.fatal(_("Unable to project to or from a XY location"))

    in_proj = None

    if ll_in:
        in_proj = "+proj=longlat +datum=WGS84"
        grass.verbose("Assuming LL WGS84 as input, current projection as output ")

    if ll_out:
        in_proj = grass.read_command("g.proj", flags="jf")

    if proj_in:
        in_proj = proj_in

    if not in_proj:
        grass.verbose("Assuming current location as input")
        in_proj = grass.read_command("g.proj", flags="jf")

    in_proj = in_proj.strip()
    grass.verbose("Input parameters: '%s'" % in_proj)

    out_proj = None

    if ll_out:
        out_proj = "+proj=longlat +datum=WGS84"
        grass.verbose("Assuming current projection as input, LL WGS84 as output ")

    if ll_in:
        out_proj = grass.read_command("g.proj", flags="jf")

    if proj_out:
        out_proj = proj_out

    if not out_proj:
        grass.fatal(_("Missing output projection parameters "))
    out_proj = out_proj.strip()
    grass.verbose("Output parameters: '%s'" % out_proj)

    #### set up input file
    if input == "-":
        infile = None
        inf = sys.stdin
    else:
        infile = input
        if not os.path.exists(infile):
            grass.fatal(_("Unable to read input data"))
        inf = file(infile)
        grass.debug("input file=[%s]" % infile)

        #### set up output file
    if not output:
        outfile = None
        outf = sys.stdout
    else:
        outfile = output
        outf = open(outfile, "w")
        grass.debug("output file=[%s]" % outfile)

        #### set up output style
    if not decimal:
        outfmt = ["-w5"]
    else:
        outfmt = ["-f", "%.8f"]
    if not copy_input:
        copyinp = []
    else:
        copyinp = ["-E"]

        #### do the conversion
        # Convert cs2cs DMS format to GRASS DMS format:
        #   cs2cs | sed -e 's/d/:/g' -e "s/'/:/g"  -e 's/"//g'

    cmd = ["cs2cs"] + copyinp + outfmt + in_proj.split() + ["+to"] + out_proj.split()
    p = grass.Popen(cmd, stdin=grass.PIPE, stdout=grass.PIPE)

    tr = TrThread(ifs, inf, p.stdin)
    tr.start()

    if not copy_input:
        if include_header:
            outf.write("x%sy%sz\n" % (ofs, ofs))
        for line in p.stdout:
            xy, z = line.split(" ", 1)
            x, y = xy.split("\t")
            outf.write("%s%s%s%s%s\n" % (x.strip(), ofs, y.strip(), ofs, z.strip()))
    else:
        if include_header:
            outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs))
        for line in p.stdout:
            inXYZ, x, rest = line.split("\t")
            inX, inY = inXYZ.split(" ")[:2]
            y, z = rest.split(" ", 1)
            outf.write(
                "%s%s%s%s%s%s%s%s%s\n" % (inX.strip(), ofs, inY.strip(), ofs, x.strip(), ofs, y.strip(), ofs, z.strip())
            )

    p.wait()

    if p.returncode != 0:
        grass.warning(_("Projection transform probably failed, please investigate"))
コード例 #31
0
def main():
    infile = options["input"]

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug("tmp_dir = {tmpdir}".format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_("File {name} not found.".format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv["GISDBASE"], gisenv["LOCATION_NAME"],
                            gisenv["MAPSET"])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode="r")
    try:
        data_names = [
            tarinfo.name for tarinfo in tar.getmembers()
            if "/" not in tarinfo.name
        ]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags["p"]:
        # print proj info and exit
        try:
            for fname in ["PROJ_INFO", "PROJ_UNITS"]:
                f = tar.extractfile("{}/{}".format(data_names[0], fname))
                sys.stdout.write(f.read().decode())
        except KeyError:
            grass.fatal(
                _("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    if options["output"]:
        map_name = options["output"]
    else:
        map_name = data_names[0].split("@")[0]

    gfile = grass.find_file(name=map_name, element="cell", mapset=".")
    if gfile["file"]:
        if os.environ.get("GRASS_OVERWRITE", "0") != "1":
            grass.fatal(
                _("Raster map <{name}> already exists".format(name=map_name)))
        else:
            grass.warning(
                _("Raster map <{name}> already exists and will be overwritten".
                  format(name=map_name)))

    # extract data
    tar.extractall()
    tar.close()
    os.chdir(data_names[0])

    if os.path.exists("cell"):
        pass
    elif os.path.exists("coor"):
        grass.fatal(
            _("This GRASS GIS pack file contains vector data. Use "
              "v.unpack to unpack <{name}>".format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags["o"]:
        grass.warning(
            _("Overriding projection check (using current location's projection)."
              ))

    else:

        diff_result_1 = diff_result_2 = None

        proj_info_file_1 = "PROJ_INFO"
        proj_info_file_2 = os.path.join(mset_dir, "..", "PERMANENT",
                                        "PROJ_INFO")

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(
                    _("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."
                      ))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(
                    filename_a=proj_info_file_1,
                    filename_b=proj_info_file_2,
                    proj=True):
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

            proj_units_file_1 = "PROJ_UNITS"
            proj_units_file_2 = os.path.join(mset_dir, "..", "PERMANENT",
                                             "PROJ_UNITS")

            if not grass.compare_key_value_text_files(
                    filename_a=proj_units_file_1,
                    filename_b=proj_units_file_2,
                    units=True):
                diff_result_2 = diff_files(proj_units_file_1,
                                           proj_units_file_2)

            if diff_result_1 or diff_result_2:

                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff="".join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff="".join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    maps = []
    vrt_file = None
    for index, data in enumerate(data_names):

        if index > 0:
            map_name = data

        for element in [
                "cats", "cell", "cellhd", "cell_misc", "colr", "fcell", "hist"
        ]:
            src_path = os.path.join(tmp_dir, data, element)
            if not os.path.exists(src_path):
                continue

            path = os.path.join(mset_dir, element)
            if not os.path.exists(path):
                os.mkdir(path)

            if element == "cell_misc":
                if index > 0:
                    maps.append(
                        "{map_name}@{mapset}".format(
                            map_name=map_name,
                            mapset=gisenv["MAPSET"],
                        ), )

                path = os.path.join(
                    mset_dir,
                    element,
                    map_name,
                )
                if index == 0:
                    vrt_file = os.path.join(path, "vrt")

                if os.path.exists(path):
                    shutil.rmtree(path)
                shutil.copytree(src_path, path)
            else:
                shutil.copyfile(
                    src_path,
                    os.path.join(mset_dir, element, map_name),
                )

    # Update vrt file
    if maps:
        if vrt_file and os.path.exists(vrt_file):
            files = "\n".join(maps)
            with open(vrt_file, "w") as f:
                f.write(files)

    grass.message(_("Raster map <{name}> unpacked".format(name=map_name)))
コード例 #32
0
ファイル: r.tileset.py プロジェクト: rkrug/grass-ci
def main():
    # Take into account those extra pixels we'll be a addin'
    max_cols = int(options['maxcols']) - int(options['overlap'])
    max_rows = int(options['maxrows']) - int(options['overlap'])

    if max_cols == 0:
        gcore.fatal(_("It is not possibile to set 'maxcols=%s' and "
                      "'overlap=%s'. Please set maxcols>overlap" %
                      (options['maxcols'], options['overlap'])))
    elif max_rows == 0:
        gcore.fatal(_("It is not possibile to set 'maxrows=%s' and "
                      "'overlap=%s'. Please set maxrows>overlap" %
                      (options['maxrows'], options['overlap'])))
    # destination projection
    if not options['destproj']:
        dest_proj = gcore.read_command('g.proj',
                                       quiet=True,
                                       flags='jf').rstrip('\n')
        if not dest_proj:
            gcore.fatal(_('g.proj failed'))
    else:
        dest_proj = options['destproj']
    gcore.debug("Getting destination projection -> '%s'" % dest_proj)

    # projection scale
    if not options['destscale']:
        ret = gcore.parse_command('g.proj',
                                  quiet=True,
                                  flags='j')
        if not ret:
            gcore.fatal(_('g.proj failed'))

        if '+to_meter' in ret:
            dest_scale = ret['+to_meter'].strip()
        else:
            gcore.warning(
                _("Scale (%s) not found, assuming '1'") %
                '+to_meter')
            dest_scale = '1'
    else:
        dest_scale = options['destscale']
    gcore.debug('Getting destination projection scale -> %s' % dest_scale)

    # set up the projections
    srs_source = {'proj': options['sourceproj'],
                  'scale': float(options['sourcescale'])}
    srs_dest = {'proj': dest_proj, 'scale': float(dest_scale)}

    if options['region']:
        gcore.run_command('g.region',
                          quiet=True,
                          region=options['region'])
    dest_bbox = gcore.region()
    gcore.debug('Getting destination region')

    # output field separator
    fs = separator(options['separator'])

    # project the destination region into the source:
    gcore.verbose('Projecting destination region into source...')
    dest_bbox_points = bboxToPoints(dest_bbox)

    dest_bbox_source_points, errors_dest = projectPoints(dest_bbox_points,
                                                         source=srs_dest,
                                                         dest=srs_source)

    if len(dest_bbox_source_points) == 0:
        gcore.fatal(_("There are no tiles available. Probably the output "
                      "projection system it is not compatible with the "
                      "projection of the current location"))

    source_bbox = pointsToBbox(dest_bbox_source_points)

    gcore.verbose('Projecting source bounding box into destination...')

    source_bbox_points = bboxToPoints(source_bbox)

    source_bbox_dest_points, errors_source = projectPoints(source_bbox_points,
                                                           source=srs_source,
                                                           dest=srs_dest)

    x_metric = 1 / dest_bbox['ewres']
    y_metric = 1 / dest_bbox['nsres']

    gcore.verbose('Computing length of sides of source bounding box...')

    source_bbox_dest_lengths = sideLengths(source_bbox_dest_points,
                                           x_metric, y_metric)

    # Find the skewedness of the two directions.
    # Define it to be greater than one
    # In the direction (x or y) in which the world is least skewed (ie north south in lat long)
    # Divide the world into strips. These strips are as big as possible contrained by max_
    # In the other direction do the same thing.
    # Theres some recomputation of the size of the world that's got to come in
    # here somewhere.

    # For now, however, we are going to go ahead and request more data than is necessary.
    # For small regions far from the critical areas of projections this makes very little difference
    # in the amount of data gotten.
    # We can make this efficient for big regions or regions near critical
    # points later.

    bigger = []
    bigger.append(max(source_bbox_dest_lengths['x']))
    bigger.append(max(source_bbox_dest_lengths['y']))
    maxdim = (max_cols, max_rows)

    # Compute the number and size of tiles to use in each direction
    # I'm making fairly even sized tiles
    # They differer from each other in height and width only by one cell
    # I'm going to make the numbers all simpler and add this extra cell to
    # every tile.

    gcore.message(_('Computing tiling...'))
    tiles = [-1, -1]
    tile_base_size = [-1, -1]
    tiles_extra_1 = [-1, -1]
    tile_size = [-1, -1]
    tileset_size = [-1, -1]
    tile_size_overlap = [-1, -1]
    for i in range(len(bigger)):
        # make these into integers.
        # round up
        bigger[i] = int(bigger[i] + 1)
        tiles[i] = int((bigger[i] / maxdim[i]) + 1)
        tile_size[i] = tile_base_size[i] = int(bigger[i] / tiles[i])
        tiles_extra_1[i] = int(bigger[i] % tiles[i])
        # This is adding the extra pixel (remainder) to all of the tiles:
        if tiles_extra_1[i] > 0:
            tile_size[i] = tile_base_size[i] + 1
        tileset_size[i] = int(tile_size[i] * tiles[i])
        # Add overlap to tiles (doesn't effect tileset_size
        tile_size_overlap[i] = tile_size[i] + int(options['overlap'])

    gcore.verbose("There will be %d by %d tiles each %d by %d cells" %
                  (tiles[0], tiles[1], tile_size[0], tile_size[1]))

    ximax = tiles[0]
    yimax = tiles[1]

    min_x = source_bbox['w']
    min_y = source_bbox['s']
    max_x = source_bbox['e']
    max_y = source_bbox['n']
    span_x = (max_x - min_x)
    span_y = (max_y - min_y)

    xi = 0
    tile_bbox = {'w': -1, 's': -1, 'e': -1, 'n': -1}

    if errors_dest > 0:
        gcore.warning(_("During computation %i tiles could not be created" %
                        errors_dest))

    while xi < ximax:
        tile_bbox['w'] = float(
            min_x) + (float(xi) * float(tile_size[0]) / float(tileset_size[0])) * float(span_x)
        tile_bbox['e'] = float(min_x) + (float(xi + 1) * float(tile_size_overlap[0]
                                                               ) / float(tileset_size[0])) * float(span_x)
        yi = 0
        while yi < yimax:
            tile_bbox['s'] = float(
                min_y) + (float(yi) * float(tile_size[1]) / float(tileset_size[1])) * float(span_y)
            tile_bbox['n'] = float(min_y) + (
                float(yi + 1) * float(tile_size_overlap[1]) /
                float(tileset_size[1])) * float(span_y)
            tile_bbox_points = bboxToPoints(tile_bbox)
            tile_dest_bbox_points, errors = projectPoints(tile_bbox_points,
                                                          source=srs_source,
                                                          dest=srs_dest)
            tile_dest_bbox = pointsToBbox(tile_dest_bbox_points)
            if bboxesIntersect(tile_dest_bbox, dest_bbox):
                if flags['w']:
                    print("bbox=%s,%s,%s,%s&width=%s&height=%s" %
                          (tile_bbox['w'], tile_bbox['s'], tile_bbox['e'],
                           tile_bbox['n'], tile_size_overlap[0],
                           tile_size_overlap[1]))
                elif flags['g']:
                    print("w=%s;s=%s;e=%s;n=%s;cols=%s;rows=%s" %
                          (tile_bbox['w'], tile_bbox['s'], tile_bbox['e'],
                           tile_bbox['n'], tile_size_overlap[0],
                           tile_size_overlap[1]))
                else:
                    print("%s%s%s%s%s%s%s%s%s%s%s" %
                          (tile_bbox['w'], fs, tile_bbox['s'], fs,
                           tile_bbox['e'], fs, tile_bbox['n'], fs,
                           tile_size_overlap[0], fs, tile_size_overlap[1]))
            yi += 1
        xi += 1
コード例 #33
0
def main():
    global tmp

    fs = separator(options["separator"])
    threeD = flags["z"]

    if threeD:
        do3D = "z"
    else:
        do3D = ""

    tmp = grass.tempfile()

    # set up input file
    if options["input"] == "-":
        infile = None
        inf = sys.stdin
    else:
        infile = options["input"]
        if not os.path.exists(infile):
            grass.fatal(_("Unable to read input file <%s>") % infile)
        grass.debug("input file=[%s]" % infile)

    if not infile:
        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
        outf = open(tmp, "w")
        for line in inf:
            if len(line.lstrip()) == 0 or line[0] == "#":
                continue
            outf.write(line.replace(fs, " "))

        outf.close()
        runfile = tmp
    else:
        # read from a real file
        if fs == " ":
            runfile = infile
        else:
            inf = open(infile)
            outf = open(tmp, "w")

            for line in inf:
                if len(line.lstrip()) == 0 or line[0] == "#":
                    continue
                outf.write(line.replace(fs, " "))

            inf.close()
            outf.close()
            runfile = tmp

    # check that there are at least two columns (three if -z is given)
    inf = open(runfile)
    for line in inf:
        if len(line.lstrip()) == 0 or line[0] == "#":
            continue
        numcols = len(line.split())
        break
    inf.close()
    if (do3D and numcols < 3) or (not do3D and numcols < 2):
        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))

    grass.run_command(
        "v.in.mapgen", flags="f" + do3D, input=runfile, output=options["output"]
    )
コード例 #34
0
def main():
    infile = options['input']
    compression_off = flags['c']
    mapset = None
    if '@' in infile:
        infile, mapset = infile.split('@')

    if options['output']:
        outfile_path, outfile_base = os.path.split(os.path.abspath(options['output']))
    else:
        outfile_path, outfile_base = os.path.split(os.path.abspath(infile + ".pack"))
    
    outfile = os.path.join(outfile_path, outfile_base)
    
    global tmp
    tmp = grass.tempdir()
    tmp_dir = os.path.join(tmp, infile)
    os.mkdir(tmp_dir)
    grass.debug('tmp_dir = %s' % tmp_dir)
    
    gfile = grass.find_file(name = infile, element = 'cell', mapset = mapset)
    if not gfile['name']:
        grass.fatal(_("Raster map <%s> not found") % infile)
    
    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <output>: <%s> exists.") % outfile)
    
    grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
    basedir = os.path.sep.join(os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
    olddir  = os.getcwd()
    
    # copy elements
    for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
        path = os.path.join(basedir, element, infile)
        if os.path.exists(path):
            grass.debug('copying %s' % path)
            shutil.copyfile(path,
                            os.path.join(tmp_dir, element))
            
    if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
        shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
                        os.path.join(tmp_dir, 'cell_misc'))
        
    if not os.listdir(tmp_dir):
        grass.fatal(_("No raster map components found"))
                    
    # copy projection info
    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS', 'EPSG']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
    
    # pack it all up
    os.chdir(tmp)
    if compression_off:
        tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:')
    else:
        tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:gz')
    tar.add(infile, recursive = True)
    tar.close()
    try:
        shutil.move(outfile_base, outfile)
    except shutil.Error as e:
        grass.fatal(e)
        
    os.chdir(olddir)
    
    grass.verbose(_("Raster map saved to '%s'" % outfile))
コード例 #35
0
def main():
    # Take into account those extra pixels we'll be a addin'
    max_cols = int(options['maxcols']) - int(options['overlap'])
    max_rows = int(options['maxrows']) - int(options['overlap'])

    if max_cols == 0:
        gcore.fatal(
            _("It is not possible to set 'maxcols=%s' and "
              "'overlap=%s'. Please set maxcols>overlap" %
              (options['maxcols'], options['overlap'])))
    elif max_rows == 0:
        gcore.fatal(
            _("It is not possible to set 'maxrows=%s' and "
              "'overlap=%s'. Please set maxrows>overlap" %
              (options['maxrows'], options['overlap'])))
    # destination projection
    if not options['destproj']:
        dest_proj = gcore.read_command('g.proj', quiet=True,
                                       flags='jf').rstrip('\n')
        if not dest_proj:
            gcore.fatal(_('g.proj failed'))
    else:
        dest_proj = options['destproj']
    gcore.debug("Getting destination projection -> '%s'" % dest_proj)

    # projection scale
    if not options['destscale']:
        ret = gcore.parse_command('g.proj', quiet=True, flags='j')
        if not ret:
            gcore.fatal(_('g.proj failed'))

        if '+to_meter' in ret:
            dest_scale = ret['+to_meter'].strip()
        else:
            gcore.warning(
                _("Scale (%s) not found, assuming '1'") % '+to_meter')
            dest_scale = '1'
    else:
        dest_scale = options['destscale']
    gcore.debug('Getting destination projection scale -> %s' % dest_scale)

    # set up the projections
    srs_source = {
        'proj': options['sourceproj'],
        'scale': float(options['sourcescale'])
    }
    srs_dest = {'proj': dest_proj, 'scale': float(dest_scale)}

    if options['region']:
        gcore.run_command('g.region', quiet=True, region=options['region'])
    dest_bbox = gcore.region()
    gcore.debug('Getting destination region')

    # output field separator
    fs = separator(options['separator'])

    # project the destination region into the source:
    gcore.verbose('Projecting destination region into source...')
    dest_bbox_points = bboxToPoints(dest_bbox)

    dest_bbox_source_points, errors_dest = projectPoints(dest_bbox_points,
                                                         source=srs_dest,
                                                         dest=srs_source)

    if len(dest_bbox_source_points) == 0:
        gcore.fatal(
            _("There are no tiles available. Probably the output "
              "projection system it is not compatible with the "
              "projection of the current location"))

    source_bbox = pointsToBbox(dest_bbox_source_points)

    gcore.verbose('Projecting source bounding box into destination...')

    source_bbox_points = bboxToPoints(source_bbox)

    source_bbox_dest_points, errors_source = projectPoints(source_bbox_points,
                                                           source=srs_source,
                                                           dest=srs_dest)

    x_metric = 1 / dest_bbox['ewres']
    y_metric = 1 / dest_bbox['nsres']

    gcore.verbose('Computing length of sides of source bounding box...')

    source_bbox_dest_lengths = sideLengths(source_bbox_dest_points, x_metric,
                                           y_metric)

    # Find the skewedness of the two directions.
    # Define it to be greater than one
    # In the direction (x or y) in which the world is least skewed (ie north south in lat long)
    # Divide the world into strips. These strips are as big as possible contrained by max_
    # In the other direction do the same thing.
    # There's some recomputation of the size of the world that's got to come in
    # here somewhere.

    # For now, however, we are going to go ahead and request more data than is necessary.
    # For small regions far from the critical areas of projections this makes very little difference
    # in the amount of data gotten.
    # We can make this efficient for big regions or regions near critical
    # points later.

    bigger = []
    bigger.append(max(source_bbox_dest_lengths['x']))
    bigger.append(max(source_bbox_dest_lengths['y']))
    maxdim = (max_cols, max_rows)

    # Compute the number and size of tiles to use in each direction
    # I'm making fairly even sized tiles
    # They differer from each other in height and width only by one cell
    # I'm going to make the numbers all simpler and add this extra cell to
    # every tile.

    gcore.message(_('Computing tiling...'))
    tiles = [-1, -1]
    tile_base_size = [-1, -1]
    tiles_extra_1 = [-1, -1]
    tile_size = [-1, -1]
    tileset_size = [-1, -1]
    tile_size_overlap = [-1, -1]
    for i in range(len(bigger)):
        # make these into integers.
        # round up
        bigger[i] = int(bigger[i] + 1)
        tiles[i] = int((bigger[i] / maxdim[i]) + 1)
        tile_size[i] = tile_base_size[i] = int(bigger[i] / tiles[i])
        tiles_extra_1[i] = int(bigger[i] % tiles[i])
        # This is adding the extra pixel (remainder) to all of the tiles:
        if tiles_extra_1[i] > 0:
            tile_size[i] = tile_base_size[i] + 1
        tileset_size[i] = int(tile_size[i] * tiles[i])
        # Add overlap to tiles (doesn't effect tileset_size
        tile_size_overlap[i] = tile_size[i] + int(options['overlap'])

    gcore.verbose("There will be %d by %d tiles each %d by %d cells" %
                  (tiles[0], tiles[1], tile_size[0], tile_size[1]))

    ximax = tiles[0]
    yimax = tiles[1]

    min_x = source_bbox['w']
    min_y = source_bbox['s']
    max_x = source_bbox['e']
    max_y = source_bbox['n']
    span_x = (max_x - min_x)
    span_y = (max_y - min_y)

    xi = 0
    tile_bbox = {'w': -1, 's': -1, 'e': -1, 'n': -1}

    if errors_dest > 0:
        gcore.warning(
            _("During computation %i tiles could not be created" %
              errors_dest))

    while xi < ximax:
        tile_bbox['w'] = float(min_x) + (float(xi) * float(
            tile_size[0]) / float(tileset_size[0])) * float(span_x)
        tile_bbox['e'] = float(min_x) + (float(xi + 1) * float(
            tile_size_overlap[0]) / float(tileset_size[0])) * float(span_x)
        yi = 0
        while yi < yimax:
            tile_bbox['s'] = float(min_y) + (float(yi) * float(
                tile_size[1]) / float(tileset_size[1])) * float(span_y)
            tile_bbox['n'] = float(min_y) + (float(yi + 1) * float(
                tile_size_overlap[1]) / float(tileset_size[1])) * float(span_y)
            tile_bbox_points = bboxToPoints(tile_bbox)
            tile_dest_bbox_points, errors = projectPoints(tile_bbox_points,
                                                          source=srs_source,
                                                          dest=srs_dest)
            tile_dest_bbox = pointsToBbox(tile_dest_bbox_points)
            if bboxesIntersect(tile_dest_bbox, dest_bbox):
                if flags['w']:
                    print("bbox=%s,%s,%s,%s&width=%s&height=%s" %
                          (tile_bbox['w'], tile_bbox['s'], tile_bbox['e'],
                           tile_bbox['n'], tile_size_overlap[0],
                           tile_size_overlap[1]))
                elif flags['g']:
                    print("w=%s;s=%s;e=%s;n=%s;cols=%s;rows=%s" %
                          (tile_bbox['w'], tile_bbox['s'], tile_bbox['e'],
                           tile_bbox['n'], tile_size_overlap[0],
                           tile_size_overlap[1]))
                else:
                    print("%s%s%s%s%s%s%s%s%s%s%s" %
                          (tile_bbox['w'], fs, tile_bbox['s'], fs,
                           tile_bbox['e'], fs, tile_bbox['n'], fs,
                           tile_size_overlap[0], fs, tile_size_overlap[1]))
            yi += 1
        xi += 1
コード例 #36
0
ファイル: m.proj.py プロジェクト: rashadkm/grass_cmake
def main():
    coords = options['coordinates']
    input = options['input']
    output = options['output']
    fs = options['separator']
    proj_in = options['proj_in']
    proj_out = options['proj_out']
    ll_in = flags['i']
    ll_out = flags['o']
    decimal = flags['d']
    copy_input = flags['e']
    include_header = flags['c']

    #### check for cs2cs
    if not grass.find_program('cs2cs'):
	grass.fatal(_("cs2cs program not found, install PROJ.4 first: http://proj.maptools.org"))

    #### check for overenthusiasm
    if proj_in and ll_in:
	grass.fatal(_("Choose only one input parameter method"))

    if proj_out and ll_out:
	grass.fatal(_("Choose only one output parameter method")) 

    if ll_in and ll_out:
	grass.fatal(_("Choise only one auto-projection parameter method"))

    if output and not grass.overwrite() and os.path.exists(output):
	grass.fatal(_("Output file already exists")) 

    if not coords and not input:
        grass.fatal(_("One of <coordinates> and <input> must be given"))
    if coords and input:
        grass.fatal(_("Options <coordinates> and <input> are mutually exclusive"))

    #### parse field separator
    # FIXME: input_x,y needs to split on multiple whitespace between them
    if fs == ',':
        ifs = ofs = ','
    else:
	try:
	    ifs, ofs = fs.split(',')
	except ValueError:
	    ifs = ofs = fs

    ifs = separator(ifs)
    ofs = separator(ofs)

    #### set up projection params
    s = grass.read_command("g.proj", flags='j')
    kv = parse_key_val(s)
    if "XY location" in kv['+proj'] and (ll_in or ll_out):
	grass.fatal(_("Unable to project to or from a XY location")) 

    in_proj = None

    if ll_in:
	in_proj = "+proj=longlat +datum=WGS84"
	grass.verbose("Assuming LL WGS84 as input, current projection as output ")

    if ll_out:
	in_proj = grass.read_command('g.proj', flags = 'jf')

    if proj_in:
	in_proj = proj_in

    if not in_proj:
	grass.verbose("Assuming current location as input")
        in_proj = grass.read_command('g.proj', flags = 'jf')
    
    in_proj = in_proj.strip()
    grass.verbose("Input parameters: '%s'" % in_proj)

    out_proj = None

    if ll_out:
	out_proj = "+proj=longlat +datum=WGS84"
	grass.verbose("Assuming current projection as input, LL WGS84 as output ")

    if ll_in:
	out_proj = grass.read_command('g.proj', flags = 'jf')

    if proj_out:
	out_proj = proj_out

    if not out_proj:
	grass.fatal(_("Missing output projection parameters "))
    out_proj = out_proj.strip()
    grass.verbose("Output parameters: '%s'" % out_proj)

    #### set up input file
    if coords:
        x, y = coords.split(',')
        tmpfile = grass.tempfile()
        fd = open(tmpfile, "w")
        fd.write("%s%s%s\n" % (x, ifs, y))
        fd.close()
        inf = file(tmpfile)
    else:
        if input == '-':
            infile = None
            inf = sys.stdin
        else:
            infile = input
            if not os.path.exists(infile):
                grass.fatal(_("Unable to read input data"))
            inf = file(infile)
            grass.debug("input file=[%s]" % infile)
    
    #### set up output file
    if not output:
	outfile = None
	outf = sys.stdout
    else:
	outfile = output
	outf = open(outfile, 'w')
	grass.debug("output file=[%s]" % outfile) 

    #### set up output style
    if not decimal:
	outfmt = ["-w5"]
    else:
	outfmt = ["-f", "%.8f"]
    if not copy_input:
	copyinp = []
    else:
	copyinp = ["-E"]

    #### do the conversion
    # Convert cs2cs DMS format to GRASS DMS format:
    #   cs2cs | sed -e 's/d/:/g' -e "s/'/:/g"  -e 's/"//g'

    cmd = ['cs2cs'] + copyinp + outfmt + in_proj.split() + ['+to'] + out_proj.split()
    p = grass.Popen(cmd, stdin = grass.PIPE, stdout = grass.PIPE)

    tr = TrThread(ifs, inf, p.stdin)
    tr.start()

    if not copy_input:
	if include_header:
	    outf.write("x%sy%sz\n" % (ofs, ofs))
	for line in p.stdout:
            try:
                xy, z = line.split(' ', 1)
                x, y = xy.split('\t')
            except ValueError:
                grass.fatal(line)
            
	    outf.write('%s%s%s%s%s\n' % \
                       (x.strip(), ofs, y.strip(), ofs, z.strip()))
    else:
	if include_header:
	    outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs))
	for line in p.stdout:
            inXYZ, x, rest = line.split('\t')
            inX, inY = inXYZ.split(' ')[:2]
	    y, z = rest.split(' ', 1)
	    outf.write('%s%s%s%s%s%s%s%s%s\n' % \
                       (inX.strip(), ofs, inY.strip(), ofs, x.strip(), \
		        ofs, y.strip(), ofs, z.strip()))

    p.wait()

    if p.returncode != 0:
	grass.warning(_("Projection transform probably failed, please investigate"))
コード例 #37
0
ファイル: v.in.wfs.py プロジェクト: caomw/grass
def main():
    out = options['output']
    wfs_url = options['url']

    request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
    wfs_url += request_base

    if options['name']:
        wfs_url += '&TYPENAME=' + options['name']

    if options['srs']:
        wfs_url += '&SRS=' + options['srs']

    if options['maximum_features']:
        wfs_url += '&MAXFEATURES=' + options['maximum_features']
        if int(options['maximum_features']) < 1:
            grass.fatal('Invalid maximum number of features')

    if options['start_index']:
        wfs_url += '&STARTINDEX=' + options['start_index']
        if int(options['start_index']) < 1:
            grass.fatal('Features begin with index "1"')

    if flags['r']:
        bbox = grass.read_command("g.region", flags = 'w').split('=')[1]
        wfs_url += '&BBOX=' + bbox

    if flags['l']:
        wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'

    tmp = grass.tempfile()
    tmpxml = tmp + '.xml'

    grass.debug(wfs_url)

    grass.message(_("Retrieving data..."))
    inf = urllib.urlopen(wfs_url)
    outf = file(tmpxml, 'wb')
    while True:
	s = inf.read()
	if not s:
	    break
	outf.write(s)
    inf.close()
    outf.close()

    if flags['l']:
        import shutil
        if os.path.exists('wms_capabilities.xml'):
            grass.fatal('A file called "wms_capabilities.xml" already exists here')
        # os.move() might fail if the temp file is on another volume, so we copy instead
        shutil.copy(tmpxml, 'wms_capabilities.xml')
        try_remove(tmpxml)
        sys.exit(0)


    grass.message(_("Importing data..."))
    ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
    try_remove(tmpxml)
    
    if ret == 0:
        grass.message(_("Vector points map <%s> imported from WFS.") % out)
    else:
        grass.message(_("WFS import failed"))
コード例 #38
0
ファイル: toolboxes.py プロジェクト: landam/grass-stable-ppa
def _debug(level, message):
    """Show debug message"""
    # this has interface as originally used GUI Debug but uses grass.script
    gcore.debug(message, level)
コード例 #39
0
ファイル: r3.in.xyz.py プロジェクト: rkrug/grass-ci
def main():
    infile = options['input']
    output = options['output']
    method = options['method']
    dtype = options['type']
    fs = options['separator']
    x = options['x']
    y = options['y']
    z = options['z']
    value_column = options['value_column']
    vrange = options['vrange']
    vscale = options['vscale']
    percent = options['percent']
    pth = options['pth']
    trim = options['trim']
    workers = int(options['workers'])
    scan_only = flags['s']
    shell_style = flags['g']
    ignore_broken = flags['i']

    if workers is 1 and "WORKERS" in os.environ:
        workers = int(os.environ["WORKERS"])

    if not os.path.exists(infile):
        grass.fatal(_("Unable to read input file <%s>") % infile)

    addl_opts = {}
    if pth:
        addl_opts['pth'] = '%s' % pth
    if trim:
        addl_opts['trim'] = '%s' % trim
    if value_column:
        addl_opts['value_column'] = '%s' % value_column
    if vrange:
        addl_opts['vrange'] = '%s' % vrange
    if vscale:
        addl_opts['vscale'] = '%s' % vscale
    if ignore_broken:
        addl_opts['flags'] = 'i'

    if scan_only or shell_style:
        if shell_style:
            doShell = 'g'
        else:
            doShell = ''
        grass.run_command('r.in.xyz', flags='s' + doShell, input=infile,
                          output='dummy', sep=fs, x=x, y=y, z=z,
                          **addl_opts)
        sys.exit()

    if dtype == 'float':
        data_type = 'FCELL'
    else:
        data_type = 'DCELL'

    region = grass.region(region3d=True)

    if region['nsres'] != region['nsres3'] or region['ewres'] != region['ewres3']:
        grass.run_command('g.region', flags='3p')
        grass.fatal(_("The 2D and 3D region settings are different. Can not continue."))

    grass.verbose(_("Region bottom=%.15g  top=%.15g  vertical_cell_res=%.15g  (%d depths)")
                  % (region['b'], region['t'], region['tbres'], region['depths']))

    grass.verbose(_("Creating slices ..."))

    # to avoid a point which falls exactly on a top bound from being
    # considered twice, we shrink the
    # For the top slice we keep it though, as someone scanning the bounds
    # may have set the bounds exactly to the data extent (a bad idea, but
    # it happens..)
    eps = 1.0e-15

    # if there are thousands of depths hopefully this array doesn't get too
    # large and so we don't have to worry much about storing/looping through
    # all the finished process infos.
    proc = {}
    pout = {}

    depths = list(range(1, 1 + region['depths']))

    for i in depths:
        tmp_layer_name = 'tmp.r3xyz.%d.%s' % (os.getpid(), '%05d' % i)

        zrange_min = region['b'] + (region['tbres'] * (i - 1))

        if i < region['depths']:
            zrange_max = region['b'] + (region['tbres'] * i) - eps
        else:
            zrange_max = region['b'] + (region['tbres'] * i)

        # spawn depth layer import job in the background
        #grass.debug("slice %d, <%s>  %% %d" % (band, image[band], band % workers))
        grass.message(_("Processing horizontal slice %d of %d [%.15g,%.15g) ...")
                      % (i, region['depths'], zrange_min, zrange_max))

        proc[i] = grass.start_command('r.in.xyz', input=infile, output=tmp_layer_name,
                                      sep=fs, method=method, x=x, y=y, z=z,
                                      percent=percent, type=data_type,
                                      zrange='%.15g,%.15g' % (zrange_min, zrange_max),
                                      **addl_opts)

        grass.debug("i=%d, %%=%d  (workers=%d)" % (i, i % workers, workers))
        # print sys.getsizeof(proc)  # sizeof(proc array)  [not so big]

        if i % workers is 0:
            # wait for the ones launched so far to finish
            for p_i in depths[:i]:
                pout[p_i] = proc[p_i].communicate()[0]
                if proc[p_i].wait() is not 0:
                    grass.fatal(_("Trouble importing data. Aborting."))

    # wait for jSobs to finish, collect any stray output
    for i in depths:
        pout[i] = proc[i].communicate()[0]
        if proc[i].wait() is not 0:
            grass.fatal(_("Trouble importing data. Aborting."))

    del proc

    grass.verbose(_("Assembling 3D cube ..."))

    # input order: lower most strata first
    slices = grass.read_command('g.list', type='raster', sep=',',
                                pattern='tmp.r3xyz.%d.*' % os.getpid()).rstrip(os.linesep)
    grass.debug(slices)

    try:
        grass.run_command('r.to.rast3', input=slices, output=output)
    except CalledModuleError:
        grass.message(_("Done. 3D raster map <%s> created.") % output)
コード例 #40
0
def main():

    export_shifted = flags['s']

    currmapset = grass.gisenv()['MAPSET']

    #### check for v.in.ply, v.out.ply
    if not grass.find_program("v.in.ply", '--help'):
        grass.fatal(
            _("The GRASS addon v.in.ply was not found, please install it first.\n"
              ))
    if not grass.find_program("v.out.ply", '--help'):
        grass.fatal(
            _("The GRASS addon v.out.ply was not found, please install it first.\n"
              ))

    # import input PLY file
    infile = options['input']
    if not os.path.exists(infile):
        grass.fatal(_("Unable to read input file <%s>") % infile)
    grass.debug("input file=[%s]" % infile)

    if not infile[-4:].lower() == '.ply':
        grass.fatal(_("Input file must end with .ply or .PLY"))

    gcpfile = infile[:-4] + ".txt"
    srcdir, ply = os.path.split(infile)
    ply = ply[:-4]

    if not os.path.exists(gcpfile):
        gcpfile = infile[:-4] + ".TXT"
        if not os.path.exists(gcpfile):
            grass.fatal(
                _("Input file with GCPs must be <%s> or <%s>") % ply + ".txt",
                ply + ".TXT")

    if options['output'] is not None and options['output'] != '':
        ply = options['output']

    clist = list()
    plydesc = grass.read_command('v.in.ply',
                                 flags='p',
                                 input=infile,
                                 output=ply)

    # remember column names for vertices
    currname = ''
    currprop = ''
    for l in plydesc.splitlines():
        f = l.split(':')
        if f[0] == 'element name':
            # new element
            currname = f[1].strip()
            currprop = ''
        if f[0] == 'property name':
            currprop = f[1].strip()
            if currname == 'vertex' and currprop not in ['x', 'y', 'z']:
                clist.append(currprop)

    columns = ','.join(clist)

    grass.run_command('v.in.ply', flags='b', input=infile, output=ply)

    # import vector exists?
    found = grass.find_file(ply, element='vector', mapset=currmapset)

    if found['name'] != ply:
        grass.fatal(_('PLY import failed!'))

    # detach table
    table = gvector.vector_layer_db(map=ply, layer=1)['table']
    grass.run_command('v.db.connect', map=ply, layer=1, flags='d')

    # print RMS
    rmsfile = os.path.join(srcdir, ply + "_rms.csv")
    grass.run_command('v.rectify',
                      input=ply,
                      output=ply + '_georef',
                      points=gcpfile,
                      flags='3bor',
                      separator=';',
                      rmsfile=rmsfile)

    # georectify
    ply_georef = ply + '_georef'
    grass.run_command('v.rectify',
                      input=ply,
                      output=ply_georef,
                      points=gcpfile,
                      flags='3bo')

    # output vector exists?
    found = grass.find_file(ply_georef, element='vector', mapset=currmapset)

    if found['name'] != ply_georef:
        grass.run_command('v.db.connect',
                          map=ply,
                          layer=1,
                          table=table,
                          key='cat')
        grass.fatal('PLY import failed!')

    grass.run_command('v.db.connect',
                      map=ply_georef,
                      layer=1,
                      table=table,
                      key='cat')

    output = os.path.join(srcdir, ply_georef + '.ply')
    grass.run_command('v.out.ply',
                      input=ply_georef,
                      output=output,
                      columns=columns)

    grass.run_command('v.db.connect', map=ply_georef, layer=1, flags='d')

    if export_shifted:
        vinfo = gvector.vector_info(map=ply_georef)
        north_center = (float(vinfo['north']) + float(vinfo['south'])) / -2.0
        east_center = (float(vinfo['east']) + float(vinfo['west'])) / -2.0
        height_center = (float(vinfo['top']) + float(vinfo['bottom'])) / -2.0

        ply_shifted = ply_georef + '_shifted'
        grass.run_command('v.transform',
                          input=ply_georef,
                          layer=-1,
                          output=ply_shifted,
                          xshift=east_center,
                          yshift=north_center,
                          zshift=height_center,
                          xscale=1.0,
                          yscale=1.0,
                          zscale=1.0,
                          zrot=0.0,
                          flags='b')

        # output vector exists?
        found = grass.find_file(ply_shifted,
                                element='vector',
                                mapset=currmapset)

        if found['name'] != ply_shifted:
            grass.run_command('v.db.connect',
                              map=ply,
                              layer=1,
                              table=table,
                              key='cat')
            grass.fatal('PLY import failed!')

        grass.run_command('v.db.connect',
                          map=ply_shifted,
                          layer=1,
                          table=table,
                          key='cat')

        output = os.path.join(srcdir, ply_shifted + '.ply')
        grass.run_command('v.out.ply',
                          input=ply_shifted,
                          output=output,
                          columns=columns)

        grass.run_command('v.db.connect', map=ply_shifted, layer=1, flags='d')

    grass.run_command('v.db.connect', map=ply, layer=1, table=table, key='cat')

    grass.message(
        _("Done: Pointcloud '%s' has been successfully imported, georeferenced, and exported"
          ) % ply)
コード例 #41
0
def main():

    export_shifted = flags["s"]

    currmapset = grass.gisenv()["MAPSET"]

    #### check for v.in.ply, v.out.ply
    if not grass.find_program("v.in.ply", "--help"):
        grass.fatal(
            _("The GRASS addon v.in.ply was not found, please install it first.\n"
              ))
    if not grass.find_program("v.out.ply", "--help"):
        grass.fatal(
            _("The GRASS addon v.out.ply was not found, please install it first.\n"
              ))

    # import input PLY file
    infile = options["input"]
    if not os.path.exists(infile):
        grass.fatal(_("Unable to read input file <%s>") % infile)
    grass.debug("input file=[%s]" % infile)

    if not infile[-4:].lower() == ".ply":
        grass.fatal(_("Input file must end with .ply or .PLY"))

    gcpfile = infile[:-4] + ".txt"
    srcdir, ply = os.path.split(infile)
    ply = ply[:-4]

    if not os.path.exists(gcpfile):
        gcpfile = infile[:-4] + ".TXT"
        if not os.path.exists(gcpfile):
            grass.fatal(
                _("Input file with GCPs must be <%s> or <%s>") % ply + ".txt",
                ply + ".TXT",
            )

    if options["output"] is not None and options["output"] != "":
        ply = options["output"]

    clist = list()
    plydesc = grass.read_command("v.in.ply",
                                 flags="p",
                                 input=infile,
                                 output=ply)

    # remember column names for vertices
    currname = ""
    currprop = ""
    for l in plydesc.splitlines():
        f = l.split(":")
        if f[0] == "element name":
            # new element
            currname = f[1].strip()
            currprop = ""
        if f[0] == "property name":
            currprop = f[1].strip()
            if currname == "vertex" and currprop not in ["x", "y", "z"]:
                clist.append(currprop)

    columns = ",".join(clist)

    grass.run_command("v.in.ply", flags="b", input=infile, output=ply)

    # import vector exists?
    found = grass.find_file(ply, element="vector", mapset=currmapset)

    if found["name"] != ply:
        grass.fatal(_("PLY import failed!"))

    # detach table
    table = gvector.vector_layer_db(map=ply, layer=1)["table"]
    grass.run_command("v.db.connect", map=ply, layer=1, flags="d")

    # print RMS
    rmsfile = os.path.join(srcdir, ply + "_rms.csv")
    grass.run_command(
        "v.rectify",
        input=ply,
        output=ply + "_georef",
        points=gcpfile,
        flags="3bor",
        separator=";",
        rmsfile=rmsfile,
    )

    # georectify
    ply_georef = ply + "_georef"
    grass.run_command("v.rectify",
                      input=ply,
                      output=ply_georef,
                      points=gcpfile,
                      flags="3bo")

    # output vector exists?
    found = grass.find_file(ply_georef, element="vector", mapset=currmapset)

    if found["name"] != ply_georef:
        grass.run_command("v.db.connect",
                          map=ply,
                          layer=1,
                          table=table,
                          key="cat")
        grass.fatal("PLY import failed!")

    grass.run_command("v.db.connect",
                      map=ply_georef,
                      layer=1,
                      table=table,
                      key="cat")

    output = os.path.join(srcdir, ply_georef + ".ply")
    grass.run_command("v.out.ply",
                      input=ply_georef,
                      output=output,
                      columns=columns)

    grass.run_command("v.db.connect", map=ply_georef, layer=1, flags="d")

    if export_shifted:
        vinfo = gvector.vector_info(map=ply_georef)
        north_center = (float(vinfo["north"]) + float(vinfo["south"])) / -2.0
        east_center = (float(vinfo["east"]) + float(vinfo["west"])) / -2.0
        height_center = (float(vinfo["top"]) + float(vinfo["bottom"])) / -2.0

        ply_shifted = ply_georef + "_shifted"
        grass.run_command(
            "v.transform",
            input=ply_georef,
            layer=-1,
            output=ply_shifted,
            xshift=east_center,
            yshift=north_center,
            zshift=height_center,
            xscale=1.0,
            yscale=1.0,
            zscale=1.0,
            zrot=0.0,
            flags="b",
        )

        # output vector exists?
        found = grass.find_file(ply_shifted,
                                element="vector",
                                mapset=currmapset)

        if found["name"] != ply_shifted:
            grass.run_command("v.db.connect",
                              map=ply,
                              layer=1,
                              table=table,
                              key="cat")
            grass.fatal("PLY import failed!")

        grass.run_command("v.db.connect",
                          map=ply_shifted,
                          layer=1,
                          table=table,
                          key="cat")

        output = os.path.join(srcdir, ply_shifted + ".ply")
        grass.run_command("v.out.ply",
                          input=ply_shifted,
                          output=output,
                          columns=columns)

        grass.run_command("v.db.connect", map=ply_shifted, layer=1, flags="d")

    grass.run_command("v.db.connect", map=ply, layer=1, table=table, key="cat")

    grass.message(
        _("Done: Pointcloud '%s' has been successfully imported, georeferenced, and exported"
          ) % ply)
コード例 #42
0
ファイル: v.in.lines.py プロジェクト: caomw/grass
def main():
    global tmp

    fs = separator(options['separator'])
    threeD = flags['z']

    prog = 'v.in.lines'

    if threeD:
        do3D = 'z'
    else:
        do3D = ''


    tmp = grass.tempfile()


    #### set up input file
    if options['input'] == '-':
        infile = None
        inf = sys.stdin
    else:
        infile = options['input']
        if not os.path.exists(infile):
            grass.fatal(_("Unable to read input file <%s>") % infile)
        grass.debug("input file=[%s]" % infile)


    if not infile:
        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
        outf = file(tmp, 'w')
        for line in inf:
            if len(line.lstrip()) == 0 or line[0] == '#':
                continue
            outf.write(line.replace(fs, ' '))

        outf.close()
        runfile = tmp
    else:
        # read from a real file
        if fs == ' ':
            runfile = infile
        else:
            inf = file(infile)
            outf = file(tmp, 'w')

            for line in inf:
                if len(line.lstrip()) == 0 or line[0] == '#':
                    continue
                outf.write(line.replace(fs, ' '))

            inf.close()
            outf.close()
            runfile = tmp


    ##### check that there are at least two columns (three if -z is given)
    inf = file(runfile)
    for line in inf:
        if len(line.lstrip()) == 0 or line[0] == '#':
            continue
        numcols = len(line.split())
        break
    inf.close()
    if (do3D and numcols < 3) or (not do3D and numcols < 2):
        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))


    grass.run_command('v.in.mapgen', flags = 'f' + do3D,
                      input = runfile, output = options['output'])
コード例 #43
0
ファイル: r.unpack.py プロジェクト: rashadkm/grass_cmake
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_('File {name} not found.'.format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name.split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(
                _('Raster map <{name}> already exists'.format(name=map_name)))
        else:
            grass.warning(
                _('Raster map <{name}> already exists and will be overwritten'.
                  format(name=map_name)))

    # extract data
    tar.extractall()
    os.chdir(data_name)

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(
            _('This GRASS GIS pack file contains vector data. Use '
              'v.unpack to unpack <{name}>'.format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags['o']:
        grass.warning(
            _("Overriding projection check (using current location's projection)."
              ))

    else:

        diff_result_1 = diff_result_2 = None

        proj_info_file_1 = 'PROJ_INFO'
        proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT',
                                        'PROJ_INFO')

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(
                    _("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."
                      ))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(
                    filename_a=proj_info_file_1,
                    filename_b=proj_info_file_2,
                    proj=True):
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

            proj_units_file_1 = 'PROJ_UNITS'
            proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT',
                                             'PROJ_UNITS')

            if not grass.compare_key_value_text_files(
                    filename_a=proj_units_file_1,
                    filename_b=proj_units_file_2,
                    units=True):
                diff_result_2 = diff_files(proj_units_file_1,
                                           proj_units_file_2)

            if diff_result_1 or diff_result_2:

                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    # install in $MAPSET
    for element in [
            'cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist'
    ]:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))

    grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
コード例 #44
0
ファイル: v.in.wfs.py プロジェクト: starseeker/archival
def main():
    out = options['output']
    wfs_url = options['url']

    request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
    wfs_url += request_base

    if options['name']:
        wfs_url += '&TYPENAME=' + options['name']

    if options['srs']:
        wfs_url += '&SRS=' + options['srs']

    if options['maximum_features']:
        wfs_url += '&MAXFEATURES=' + options['maximum_features']
        if int(options['maximum_features']) < 1:
            grass.fatal('Invalid maximum number of features')

    if options['start_index']:
        wfs_url += '&STARTINDEX=' + options['start_index']
        if int(options['start_index']) < 1:
            grass.fatal('Features begin with index "1"')

    if flags['r']:
        bbox = grass.read_command("g.region", flags='w').split('=')[1]
        wfs_url += '&BBOX=' + bbox

    if flags['l']:
        wfs_url = options[
            'url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'

    tmp = grass.tempfile()
    tmpxml = tmp + '.xml'

    grass.debug(wfs_url)

    grass.message(_("Retrieving data..."))
    inf = urllib.urlopen(wfs_url)
    outf = file(tmpxml, 'wb')
    while True:
        s = inf.read()
        if not s:
            break
        outf.write(s)
    inf.close()
    outf.close()

    if flags['l']:
        import shutil
        if os.path.exists('wms_capabilities.xml'):
            grass.fatal(
                'A file called "wms_capabilities.xml" already exists here')
        # os.move() might fail if the temp file is on another volume, so we copy instead
        shutil.copy(tmpxml, 'wms_capabilities.xml')
        try_remove(tmpxml)
        sys.exit(0)

    grass.message(_("Importing data..."))
    ret = grass.run_command('v.in.ogr', flags='o', dsn=tmpxml, out=out)
    try_remove(tmpxml)

    if ret == 0:
        grass.message(_("Vector points map <%s> imported from WFS.") % out)
    else:
        grass.message(_("WFS import failed"))
コード例 #45
0
ファイル: g.extension.py プロジェクト: imincik/pkg-grass
    
    try:
        shutil.copyfile(dist_file,addons_file)
    except OSError, e:
        grass.fatal(_("Unable to create '%s': %s") % (addons_file, e))
    
def create_dir(path):
    if os.path.isdir(path):
        return

    try:
        os.makedirs(path)
    except OSError, e:
        grass.fatal(_("Unable to create '%s': %s") % (path, e))
    
    grass.debug("'%s' created" % path)
    
def check_dirs():
    create_dir(os.path.join(options['prefix'], 'bin'))
    create_dir(os.path.join(options['prefix'], 'docs', 'html'))
    check_style_files('grass_logo.png')
    check_style_files('grassdocs.css')
    # create_dir(os.path.join(options['prefix'], 'etc'))
    create_dir(os.path.join(options['prefix'], 'docs', 'man', 'man1'))
    create_dir(os.path.join(options['prefix'], 'man', 'man1'))
    create_dir(os.path.join(options['prefix'], 'scripts'))

def main():
    # check dependecies
    if sys.platform != "win32":
        check_progs()
コード例 #46
0
def main():
    coords = options['coordinates']
    input = options['input']
    output = options['output']
    fs = options['separator']
    proj_in = options['proj_in']
    proj_out = options['proj_out']
    ll_in = flags['i']
    ll_out = flags['o']
    decimal = flags['d']
    copy_input = flags['e']
    include_header = flags['c']

    # check for cs2cs
    if not gcore.find_program('cs2cs'):
        gcore.fatal(
            _("cs2cs program not found, install PROJ.4 first: \
            http://proj.maptools.org"))

    # check for overenthusiasm
    if proj_in and ll_in:
        gcore.fatal(_("Choose only one input parameter method"))

    if proj_out and ll_out:
        gcore.fatal(_("Choose only one output parameter method"))

    if ll_in and ll_out:
        gcore.fatal(_("Choose only one auto-projection parameter method"))

    if output and not gcore.overwrite() and os.path.exists(output):
        gcore.fatal(_("Output file already exists"))

    if not coords and not input:
        gcore.fatal(_("One of <coordinates> and <input> must be given"))
    if coords and input:
        gcore.fatal(
            _("Options <coordinates> and <input> are mutually exclusive"))

    # parse field separator
    # FIXME: input_x,y needs to split on multiple whitespace between them
    if fs == ',':
        ifs = ofs = ','
    else:
        try:
            ifs, ofs = fs.split(',')
        except ValueError:
            ifs = ofs = fs

    ifs = separator(ifs)
    ofs = separator(ofs)

    # set up projection params
    s = gcore.read_command("g.proj", flags='j')
    kv = parse_key_val(s)
    if "XY location" in kv['+proj'] and (ll_in or ll_out):
        gcore.fatal(_("Unable to project to or from a XY location"))

    in_proj = None

    if ll_in:
        in_proj = "+proj=longlat +datum=WGS84"
        gcore.verbose(
            "Assuming LL WGS84 as input, current projection as output ")

    if ll_out:
        in_proj = gcore.read_command('g.proj', flags='jf')

    if proj_in:
        if '+' in proj_in:
            in_proj = proj_in
        else:
            gcore.fatal(_("Invalid PROJ.4 input specification"))

    if not in_proj:
        gcore.verbose("Assuming current location as input")
        in_proj = gcore.read_command('g.proj', flags='jf')

    in_proj = in_proj.strip()
    gcore.verbose("Input parameters: '%s'" % in_proj)

    out_proj = None

    if ll_out:
        out_proj = "+proj=longlat +datum=WGS84"
        gcore.verbose(
            "Assuming current projection as input, LL WGS84 as output ")

    if ll_in:
        out_proj = gcore.read_command('g.proj', flags='jf')

    if proj_out:
        if '+' in proj_out:
            out_proj = proj_out
        else:
            gcore.fatal(_("Invalid PROJ.4 output specification"))

    if not out_proj:
        gcore.fatal(_("Missing output projection parameters "))
    out_proj = out_proj.strip()
    gcore.verbose("Output parameters: '%s'" % out_proj)

    # set up input file
    if coords:
        x, y = coords.split(',')
        tmpfile = gcore.tempfile()
        fd = open(tmpfile, "w")
        fd.write("%s%s%s\n" % (x, ifs, y))
        fd.close()
        inf = file(tmpfile)
    else:
        if input == '-':
            infile = None
            inf = sys.stdin
        else:
            infile = input
            if not os.path.exists(infile):
                gcore.fatal(_("Unable to read input data"))
            inf = file(infile)
            gcore.debug("input file=[%s]" % infile)

    # set up output file
    if not output:
        outfile = None
        outf = sys.stdout
    else:
        outfile = output
        outf = open(outfile, 'w')
        gcore.debug("output file=[%s]" % outfile)

    # set up output style
    if not decimal:
        outfmt = ["-w5"]
    else:
        outfmt = ["-f", "%.8f"]
    if not copy_input:
        copyinp = []
    else:
        copyinp = ["-E"]

    # do the conversion
    # Convert cs2cs DMS format to GRASS DMS format:
    #   cs2cs | sed -e 's/d/:/g' -e "s/'/:/g"  -e 's/"//g'

    cmd = ['cs2cs'] + copyinp + outfmt + \
        in_proj.split() + ['+to'] + out_proj.split()

    p = gcore.Popen(cmd, stdin=gcore.PIPE, stdout=gcore.PIPE)

    tr = TrThread(ifs, inf, p.stdin)
    tr.start()

    if not copy_input:
        if include_header:
            outf.write("x%sy%sz\n" % (ofs, ofs))
        for line in p.stdout:
            try:
                xy, z = line.split(' ', 1)
                x, y = xy.split('\t')
            except ValueError:
                gcore.fatal(line)

            outf.write('%s%s%s%s%s\n' %
                       (x.strip(), ofs, y.strip(), ofs, z.strip()))
    else:
        if include_header:
            outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs))
        for line in p.stdout:
            inXYZ, x, rest = line.split('\t')
            inX, inY = inXYZ.split(' ')[:2]
            y, z = rest.split(' ', 1)
            outf.write('%s%s%s%s%s%s%s%s%s\n' %
                       (inX.strip(), ofs, inY.strip(), ofs, x.strip(), ofs,
                        y.strip(), ofs, z.strip()))

    p.wait()

    if p.returncode != 0:
        gcore.warning(
            _("Projection transform probably failed, please investigate"))
コード例 #47
0
ファイル: gdalwarp.py プロジェクト: AsherBond/MondocosmOS
    def run(self):
        # import tiles
        tiler = 0
        for input in self.options['input'].split(','):
            tmptilename = self.options['output'] + '_tile_' + str(tiler)
            if not os.path.exists(input):
                grass.warning(_("Missing input '%s'") % input)
                continue
            grass.info(_("Importing tile '%s'...") % os.path.basename(input))
            if self.flags['p']:
                self.nowarp_import(input, tmptilename)
            else:
                self.warp_import(input, tmptilename)
            
            self.maplist.append(tmptilename)
            tiler += 1
        
        if tiler < 1:
            grass.message(_("Nothing imported"))
            return 0
        
        # if there's more than one tile patch them together, otherwise
        # just rename that tile.
        if tiler == 1:
            if len(self.channel_suffixes) > 0:
                # multi-band
                for suffix in self.channel_suffixes:
                    # rename tile 0 to be the output
                    ffile = self.options['output'] + '_tile_0' + suffix
                    tfile = self.options['output'] + suffix
                    if grass.run_command('g.rename',
                                         quiet = True,
                                         rast = ffile + ',' + tfile) != 0:
                        grass.fatal(_('g.rename failed'))
            else: # single-band, single-tile
                ffile = self.options['output'] + '_tile_0' # + sfx ?
                tfile = self.options['output'] # + sfx ?
                if grass.run_command('g.rename',
                                     quiet = True,
                                     rast = ffile + ',' + tfile) != 0:
                    grass.fatal(_('g.rename failed'))
        else:
            # patch together each channel
            grass.debug('suffixes: %s' % ','.join(suffixes))
            if len(suffixes) > 0:
                # multi-band data
                for suffix in suffixes:
                    suffix = suffix.replace('.', '_')
                    # patch these together (using nulls only)
                    grass.message(_("Patching '%s' channel...") % suffix)
                    if grass.run_command('r.patch',
                                         quiet = True,
                                         input = patches, # ???
                                         output = self.options['output'] + suffix) != 0:
                        grass.fatal(_('r.patch failed'))
                        
                    # remove the temporary patches we made
                    if grass.run_command('g.remove',
                                         quiet = True,
                                         rast = patches) != 0:
                        grass.fatal(_('g.remove failed'))
            else:
                # single-band data
                grass.info(_("Patching tiles (this may take some time)..."))
                grass.debug("patch list = %s" % ','.join(maplist))

                # HACK: for 8bit PNG, GIF all the different tiles can have
                #   different color tables so patches output end up all freaky.
                #	r.mapcalc r#,g#,b# manual patching + r.composite?
                #	or d.out.file + r.in.png + r.region?
                # *** there is a GDAL utility to do this: gdal_merge.py
                #       http://www.gdal.org/gdal_merge.html
                for color in ('r', 'g', 'b'):
                    maplist_color = []
                    for map in maplist:
                        outmap = map + '_' + color
                        maplist_color.append(outmap)
                        if grass.run_command('r.mapcalc',
                                             quiet = True,
                                             expression = '%s = %s#%s' % (outmap, color, map)) != 0:
                            grass.fatal(_('r.mapcalc failed'))
                        if grass.run_command('r.colors',
                                             quiet = True,
                                             map = outmap,
                                             color = 'grey255') != 0:
                            grass.fatal(_('r.colors failed'))
                        if grass.run_command('r.null',
                                             quiet = True,
                                             map = outmap,
                                             setnull = 255) != 0:
                            grass.fatal(_('r.null failed'))
                
                        if grass.run_command('r.patch',
                                             input = ','.join(maplist_color),
                                             output = outmap + '_all') != 0:
                            grass.fatal(_('r.patch failed'))
                
                if grass.run_command('r.composite',
                                     quiet = True,
                                     red = map + '_r_all',
                                     green = map + '_g_all',
                                     blue = map + '_b_all',
                                     output = self.options['output']) != 0:
                    grass.fatal(_('r.composite failed'))

                if grass.run_command('g.mremove',
                                     quiet = True,
                                     flags = 'f',
                                     rast = map + '*') != 0:
                    grass.fatal(_('g.remove failed'))

        # there's already a no suffix, can't make colors
        # can only go up from here ;)
        colors = 0
        for suffix in self.suffixes:
            if suffix in ('.red', '.green', '.blue'):
                colors += 1
        
        # make a composite image if asked for and colors exist
        if colors == 3 and self.flags['c']:
            grass.message(_("Building color image <%s>...") % self.options['output'])
            if grass.run_command('r.composite',
                                 quiet = True,
                                 red = self.options['output'] + '.red',
                                 green = self.options['output'] + '.green',
                                 blue = self.options['output'] + '.blue',
                                 output = self.options['output']) != 0:
                grass.fatal(_('r.composite failed'))
        
        return 0
コード例 #48
0
def main():
    infile = options['input']

    # create temporary directory
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    # check if the input file exists
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    # copy the files to tmp dir
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags['p']:
        # print proj info and exit
        try:
            for fname in ['PROJ_INFO', 'PROJ_UNITS']:
                f = tar.extractfile(fname)
                sys.stdout.write(f.read())
        except KeyError:
            grass.fatal(
                _("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    # set the output name
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name

    # grass env
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])

    new_dir = os.path.join(mset_dir, 'vector', map_name)

    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Vector map <%s> already exists") % map_name)
    elif overwrite == '1' and gfile['file']:
        grass.warning(
            _("Vector map <%s> already exists and will be overwritten") %
            map_name)
        grass.run_command('g.remove',
                          flags='f',
                          quiet=True,
                          type='vector',
                          name=map_name)
        shutil.rmtree(new_dir, True)

    # extract data
    tar.extractall()
    tar.close()
    if os.path.exists(os.path.join(data_name, 'coor')):
        pass
    elif os.path.exists(os.path.join(data_name, 'cell')):
        grass.fatal(
            _("This GRASS GIS pack file contains raster data. Use "
              "r.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

    skip_projection_check = False
    if not os.path.exists(os.path.join(tmp_dir, 'PROJ_INFO')):
        if os.path.exists(loc_proj):
            grass.fatal(
                _("PROJ_INFO file is missing, unpack vector map in XY (unprojected) location."
                  ))
        skip_projection_check = True  # XY location

    if not skip_projection_check:
        diff_result_1 = diff_result_2 = None
        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_INFO'),
                                                  filename_b=loc_proj,
                                                  proj=True):
            diff_result_1 = diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
                                       loc_proj)

        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_UNITS'),
                                                  filename_b=loc_proj_units,
                                                  units=True):
            diff_result_2 = diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
                                       loc_proj_units)

        if diff_result_1 or diff_result_2:
            if flags['o']:
                grass.warning(
                    _("Projection information does not match. Proceeding..."))
            else:
                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    # new db
    fromdb = os.path.join(tmp_dir, 'db.sqlite')
    # copy file
    shutil.copytree(data_name, new_dir)
    # exist fromdb
    if os.path.exists(fromdb):
        # the db connection in the output mapset
        dbconn = grassdb.db_connection(force=True)
        todb = dbconn['database']
        # return all tables
        list_fromtable = grass.read_command('db.tables',
                                            driver='sqlite',
                                            database=fromdb).splitlines()

        # return the list of old connection for extract layer number and key
        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
        dbnlist = dbln.readlines()
        dbln.close()
        # check if dbf or sqlite directory exists
        if dbconn['driver'] == 'dbf' and not os.path.exists(
                os.path.join(mset_dir, 'dbf')):
            os.mkdir(os.path.join(mset_dir, 'dbf'))
        elif dbconn['driver'] == 'sqlite' and not os.path.exists(
                os.path.join(mset_dir, 'sqlite')):
            os.mkdir(os.path.join(mset_dir, 'sqlite'))
        # for each old connection
        for t in dbnlist:
            # it split the line of each connection, to found layer number and key
            if len(t.split('|')) != 1:
                values = t.split('|')
            else:
                values = t.split(' ')

            from_table = values[1]
            layer = values[0].split('/')[0]
            # we need to take care about the table name in case of several layer
            if options["output"]:
                if len(dbnlist) > 1:
                    to_table = "%s_%s" % (map_name, layer)
                else:
                    to_table = map_name
            else:
                to_table = from_table

            grass.verbose(
                _("Coping table <%s> as table <%s>") % (from_table, to_table))

            # copy the table in the default database
            try:
                grass.run_command('db.copy',
                                  to_driver=dbconn['driver'],
                                  to_database=todb,
                                  to_table=to_table,
                                  from_driver='sqlite',
                                  from_database=fromdb,
                                  from_table=from_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to copy table <%s> as table <%s>") %
                    (from_table, to_table))

            grass.verbose(
                _("Connect table <%s> to vector map <%s> at layer <%s>") %
                (to_table, map_name, layer))

            # and connect the new tables with the right layer
            try:
                grass.run_command('v.db.connect',
                                  flags='o',
                                  quiet=True,
                                  driver=dbconn['driver'],
                                  database=todb,
                                  map=map_name,
                                  key=values[2],
                                  layer=layer,
                                  table=to_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to connect table <%s> to vector map <%s>") %
                    (to_table, map_name))

    grass.message(_("Vector map <%s> successfully unpacked") % map_name)
コード例 #49
0
def main():
    infile = options["input"]
    output = options["output"]
    method = options["method"]
    dtype = options["type"]
    fs = options["separator"]
    x = options["x"]
    y = options["y"]
    z = options["z"]
    value_column = options["value_column"]
    vrange = options["vrange"]
    vscale = options["vscale"]
    percent = options["percent"]
    pth = options["pth"]
    trim = options["trim"]
    workers = int(options["workers"])
    scan_only = flags["s"]
    shell_style = flags["g"]
    ignore_broken = flags["i"]

    if workers == 1 and "WORKERS" in os.environ:
        workers = int(os.environ["WORKERS"])

    if not os.path.exists(infile):
        grass.fatal(_("Unable to read input file <%s>") % infile)

    addl_opts = {}
    if pth:
        addl_opts["pth"] = "%s" % pth
    if trim:
        addl_opts["trim"] = "%s" % trim
    if value_column:
        addl_opts["value_column"] = "%s" % value_column
    if vrange:
        addl_opts["vrange"] = "%s" % vrange
    if vscale:
        addl_opts["vscale"] = "%s" % vscale
    if ignore_broken:
        addl_opts["flags"] = "i"

    if scan_only or shell_style:
        if shell_style:
            doShell = "g"
        else:
            doShell = ""
        grass.run_command(
            "r.in.xyz",
            flags="s" + doShell,
            input=infile,
            output="dummy",
            sep=fs,
            x=x,
            y=y,
            z=z,
            **addl_opts,
        )
        sys.exit()

    if dtype == "float":
        data_type = "FCELL"
    else:
        data_type = "DCELL"

    region = grass.region(region3d=True)

    if region["nsres"] != region["nsres3"] or region["ewres"] != region[
            "ewres3"]:
        grass.run_command("g.region", flags="3p")
        grass.fatal(
            _("The 2D and 3D region settings are different. Can not continue.")
        )

    grass.verbose(
        _("Region bottom=%.15g  top=%.15g  vertical_cell_res=%.15g  (%d depths)"
          ) % (region["b"], region["t"], region["tbres"], region["depths"]))

    grass.verbose(_("Creating slices ..."))

    # to avoid a point which falls exactly on a top bound from being
    # considered twice, we shrink the
    # For the top slice we keep it though, as someone scanning the bounds
    # may have set the bounds exactly to the data extent (a bad idea, but
    # it happens..)
    eps = 1.0e-15

    # if there are thousands of depths hopefully this array doesn't get too
    # large and so we don't have to worry much about storing/looping through
    # all the finished process infos.
    proc = {}
    pout = {}

    depths = list(range(1, 1 + region["depths"]))

    for i in depths:
        tmp_layer_name = "tmp.r3xyz.%d.%s" % (os.getpid(), "%05d" % i)

        zrange_min = region["b"] + (region["tbres"] * (i - 1))

        if i < region["depths"]:
            zrange_max = region["b"] + (region["tbres"] * i) - eps
        else:
            zrange_max = region["b"] + (region["tbres"] * i)

        # spawn depth layer import job in the background
        # grass.debug("slice %d, <%s>  %% %d" % (band, image[band], band % workers))
        grass.message(
            _("Processing horizontal slice %d of %d [%.15g,%.15g) ...") %
            (i, region["depths"], zrange_min, zrange_max))

        proc[i] = grass.start_command(
            "r.in.xyz",
            input=infile,
            output=tmp_layer_name,
            sep=fs,
            method=method,
            x=x,
            y=y,
            z=z,
            percent=percent,
            type=data_type,
            zrange="%.15g,%.15g" % (zrange_min, zrange_max),
            **addl_opts,
        )

        grass.debug("i=%d, %%=%d  (workers=%d)" % (i, i % workers, workers))
        # print sys.getsizeof(proc)  # sizeof(proc array)  [not so big]

        if i % workers == 0:
            # wait for the ones launched so far to finish
            for p_i in depths[:i]:
                pout[p_i] = proc[p_i].communicate()[0]
                if proc[p_i].wait() != 0:
                    grass.fatal(_("Trouble importing data. Aborting."))

    # wait for jSobs to finish, collect any stray output
    for i in depths:
        pout[i] = proc[i].communicate()[0]
        if proc[i].wait() != 0:
            grass.fatal(_("Trouble importing data. Aborting."))

    del proc

    grass.verbose(_("Assembling 3D cube ..."))

    # input order: lower most strata first
    slices = grass.read_command("g.list",
                                type="raster",
                                sep=",",
                                pattern="tmp.r3xyz.%d.*" % os.getpid()).rstrip(
                                    os.linesep)
    grass.debug(slices)

    try:
        grass.run_command("r.to.rast3", input=slices, output=output)
    except CalledModuleError:
        grass.message(_("Done. 3D raster map <%s> created.") % output)
コード例 #50
0
ファイル: v.in.lines.py プロジェクト: rkrug/grass-ci
def main():
    global tmp

    fs = separator(options["separator"])
    threeD = flags["z"]

    prog = "v.in.lines"

    if threeD:
        do3D = "z"
    else:
        do3D = ""

    tmp = grass.tempfile()

    # set up input file
    if options["input"] == "-":
        infile = None
        inf = sys.stdin
    else:
        infile = options["input"]
        if not os.path.exists(infile):
            grass.fatal(_("Unable to read input file <%s>") % infile)
        grass.debug("input file=[%s]" % infile)

    if not infile:
        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
        outf = file(tmp, "w")
        for line in inf:
            if len(line.lstrip()) == 0 or line[0] == "#":
                continue
            outf.write(line.replace(fs, " "))

        outf.close()
        runfile = tmp
    else:
        # read from a real file
        if fs == " ":
            runfile = infile
        else:
            inf = file(infile)
            outf = file(tmp, "w")

            for line in inf:
                if len(line.lstrip()) == 0 or line[0] == "#":
                    continue
                outf.write(line.replace(fs, " "))

            inf.close()
            outf.close()
            runfile = tmp

    # check that there are at least two columns (three if -z is given)
    inf = file(runfile)
    for line in inf:
        if len(line.lstrip()) == 0 or line[0] == "#":
            continue
        numcols = len(line.split())
        break
    inf.close()
    if (do3D and numcols < 3) or (not do3D and numcols < 2):
        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))

    grass.run_command("v.in.mapgen", flags="f" + do3D, input=runfile, output=options["output"])
コード例 #51
0
ファイル: r.unpack.py プロジェクト: rashadkm/grass_cmake
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_('File {name} not found.'.format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name.split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(_('Raster map <{name}> already exists'.format(name=map_name)))
        else:
            grass.warning(_('Raster map <{name}> already exists and will be overwritten'.format(name=map_name)))

    # extract data
    tar.extractall()
    os.chdir(data_name)

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(_('This GRASS GIS pack file contains vector data. Use '
                      'v.unpack to unpack <{name}>'.format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags['o']:
        grass.warning(_("Overriding projection check (using current location's projection)."))
        
    else:
        
        diff_result_1 = diff_result_2 = None
        
        proj_info_file_1 = 'PROJ_INFO'
        proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(_("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
                                                      filename_b=proj_info_file_2,
                                                      proj=True):                                                      
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)
        
            proj_units_file_1 = 'PROJ_UNITS'
            proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
        
            if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
                                                      filename_b=proj_units_file_2,
                                                      units=True):                                                      
                diff_result_2 = diff_files(proj_units_file_1, proj_units_file_2)
        
            if diff_result_1 or diff_result_2:
                
                if diff_result_1:
                    grass.warning(_("Difference between PROJ_INFO file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
                grass.fatal(_("Projection of dataset does not appear to match current location."
                              " In case of no significant differences in the projection definitions,"
                              " use the -o flag to ignore them and use"
                              " current location definition."))

    # install in $MAPSET
    for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))

    grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
コード例 #52
0
def main():
    infile = options['input']
    compression_off = flags['c']
    mapset = None
    if '@' in infile:
        infile, mapset = infile.split('@')

    if options['output']:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(options['output']))
    else:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(infile + ".pack"))

    outfile = os.path.join(outfile_path, outfile_base)

    global tmp
    tmp = grass.tempdir()
    tmp_dir = os.path.join(tmp, infile)
    os.mkdir(tmp_dir)
    grass.debug('tmp_dir = %s' % tmp_dir)

    gfile = grass.find_file(name=infile, element='cell', mapset=mapset)
    if not gfile['name']:
        grass.fatal(_("Raster map <%s> not found") % infile)

    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") %
                outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <output>: <%s> exists.") % outfile)

    grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
    basedir = os.path.sep.join(
        os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
    olddir = os.getcwd()

    # copy elements
    info = grass.parse_command('r.info', flags='e', map=infile)
    vrt_files = {}
    if info['maptype'] == 'virtual':
        map_file = grass.find_file(
            name=infile,
            element='cell_misc',
        )
        if map_file['file']:
            vrt = os.path.join(map_file['file'], 'vrt')
            if os.path.exists(vrt):
                with open(vrt, 'r') as f:
                    for r in f.readlines():
                        map, mapset = r.split('@')
                        map_basedir = os.path.sep.join(
                            os.path.normpath(map_file['file'], ).split(
                                os.path.sep)[:-2], )
                        vrt_files[map] = map_basedir

    for element in [
            'cats',
            'cell',
            'cellhd',
            'cell_misc',
            'colr',
            'fcell',
            'hist',
    ]:
        path = os.path.join(basedir, element, infile)
        if os.path.exists(path):
            grass.debug('copying %s' % path)
            if os.path.isfile(path):
                shutil.copyfile(
                    path,
                    os.path.join(tmp_dir, element),
                )
            else:
                shutil.copytree(
                    path,
                    os.path.join(tmp_dir, element),
                )

        # Copy vrt files
        if vrt_files:
            for f in vrt_files.keys():
                f_tmp_dir = os.path.join(tmp, f)
                if not os.path.exists(f_tmp_dir):
                    os.mkdir(f_tmp_dir)
                path = os.path.join(vrt_files[f], element, f)
                if os.path.exists(path):
                    grass.debug("copying vrt file {}".format(path))
                    if os.path.isfile(path):
                        shutil.copyfile(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )
                    else:
                        shutil.copytree(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )

    if not os.listdir(tmp_dir):
        grass.fatal(_("No raster map components found"))

    # copy projection info
    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS', 'EPSG']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))

    # pack it all up
    os.chdir(tmp)
    if compression_off:
        tar = tarfile.TarFile.open(name=outfile_base, mode='w:')
    else:
        tar = tarfile.TarFile.open(name=outfile_base, mode='w:gz')
    tar.add(infile, recursive=True)
    if vrt_files:
        for f in vrt_files.keys():
            tar.add(f, recursive=True)

    tar.close()
    try:
        shutil.move(outfile_base, outfile)
    except shutil.Error as e:
        grass.fatal(e)

    os.chdir(olddir)

    grass.verbose(_("Raster map saved to '%s'" % outfile))
コード例 #53
0
ファイル: r.unpack.py プロジェクト: AsherBond/MondocosmOS
def main():
    infile = options['input']
    
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)
    
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found" % infile))
    
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name = input_base, mode = 'r:gz')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))
    
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name
    
    gfile = grass.find_file(name = map_name, element = 'cell',
                            mapset = '.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Raster map <%s> already exists") % map_name)
    
    # extract data
    tar.extractall()
    os.chdir(data_name)
    
    # check projection compatibility in a rather crappy way
    if not filecmp.cmp('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')):
        if flags['o']:
            grass.warning(_("Projection information does not match. Proceeding..."))
        else:
            grass.fatal(_("Projection information does not match. Aborting."))
    
    # install in $MAPSET
    for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
    
    grass.verbose(_("Raster map saved to <%s>") % map_name)