Esempio n. 1
0
def entry2py(dl_e, w=1):
    """convert a datalist entry to python

    Args:
      dl_e (str): a datalist entry line
      w (float): inherited weight

    Returns:
      list: a datalist entry [fn, fmt, wt, ...]
    """

    this_entry = dl_e.rstrip().split()
    try:
        entry = [x if n == 0 else int(x) if n < 2 else float(x) if n < 3 else x for n, x in enumerate(this_entry)]
    except Exception as e:
        utils.echo_error_msg('could not parse entry {}'.format(dl_e))
        return(None)
    if len(entry) < 2:
        for key in _dl_dl_h.keys():
            se = entry[0].split('.')
            if len(se) == 1:
                see = entry[0].split(':')[0]
            else:
                see = se[-1]
            if see in _dl_dl_h[key]['fmts']:
                entry.append(int(key))
    if len(entry) < 3:
        entry.append(w)
    return(entry)
Esempio n. 2
0
def gmt_inc2inc(inc_str):
    """convert a GMT-style `inc_str` (6s) to geographic units

    c/s - arc-seconds
    m - arc-minutes

    Args:
      inc_str (str): GMT style increment string

    Returns:
      float: increment value.
    """

    if inc_str is None or inc_str.lower() == 'none': return (None)
    units = inc_str[-1]
    if units == 'c': inc = float(inc_str[:-1]) / 3600.
    elif units == 's': inc = float(inc_str[:-1]) / 3600.
    elif units == 'm': inc = float(inc_str[:-1]) / 360.
    else:
        try:
            inc = float(inc_str)
        except ValueError as e:
            utils.echo_error_msg('could not parse increment {}, {}'.format(
                inc_str, e))
            return (None)
    return (inc)
Esempio n. 3
0
def inf_entry(src_entry, overwrite=False, epsg=None):
    """Read .inf file and extract minmax info.
    the .inf file can either be an MBSystem style inf file or the 
    result of `gmt gmtinfo file.xyz -C`, which is a 6 column line 
    with minmax info, etc.

    Args:
      src_entry (list): the source datalist entry list
      overwrite (bool): overwrite the inf file
      epsg (int): EPSG code

    Returns:
      list: the region [xmin, xmax, ymin, ymax] of the inf file.
    """
    
    ei = {'name': src_entry, 'numpts': 0,
          'minmax': [0,0,0,0,0,0],
          'wkt': regions.region2wkt([0,0,0,0,0,0])}
    if entry_exists_p(src_entry[0]) or src_entry[1] == -4:
        path_i = src_entry[0] + '.inf'
        if not os.path.exists(path_i) or overwrite:
            ei = _dl_dl_h[src_entry[1]]['inf'](src_entry, epsg)
        else:
            ei = inf_parse(path_i)
        if regions.region_is_zeros(ei['minmax']):
            ei = inf_entry(src_entry, overwrite=True, epsg=epsg)
        if not regions.region_valid_p(ei['minmax']):
            utils.echo_error_msg('invalid inf file {}'.format(path_i))
            return({})
    return(ei)
Esempio n. 4
0
def inf_parse(src_inf):
    """parse an inf file (mbsystem or gmt)

    Try to parse the infos file, could be native waffles infos, 
    gmt-style infos, of mbsystem .inf file.

    Args:
      src_inf (str): path to sourc infos file

    Returns:
      list: the region [xmin, xmax, ymin, ymax, zmin, zmax] of the info dict
    """
    
    xyzi = {'name': src_inf, 'numpts': 0, 'minmax': [0,0,0,0,0,0], 'wkt': regions.region2wkt([0,0,0,0,0,0])}
    try:
        with open(src_inf) as iob:
            xyzi = json.load(iob)
    except:
        try:
            xyzi = gmtfun.gmt_inf_parse(src_inf)
        except:
            try:
                xyzi = mbsfun.mb_inf_parse(src_inf)
            except:
                utils.echo_error_msg('could not parse inf file {}'.format(src_inf))
    return(xyzi)
Esempio n. 5
0
def xyz_parse_line(xyz_line, xyz_c=_xyz_config):
    """parse an xyz line-string, using _xyz_config

    Args:
      xyz_line (str): a string representing delimited data.
      xyz_c (dict): xyz config dictionary

    Returns:
      list: xyz data; [x, y, z]
    """

    try:
        this_line = xyz_line.strip()
    except AttributeError as e:
        utils.echo_error_msg('input is list, should be xyz-line: {}'.format(e))
        return (None)
    except Exception as e:
        utils.echo_error_msg(e)
        return (None)
    if xyz_c['delim'] is None:
        xyz_c['delim'] = xyz_line_delim(this_line)
    this_xyz = this_line.split(xyz_c['delim'])
    try:
        o_xyz = [
            float(this_xyz[xyz_c['xpos']]) + float(xyz_c['x-off']),
            float(this_xyz[xyz_c['ypos']]),
            float(this_xyz[xyz_c['zpos']]) * float(xyz_c['z-scale'])
        ]
    except IndexError as e:
        if xyz_c['verbose']: utils.echo_error_msg(e)
        return (None)
    except Exception as e:
        if xyz_c['verbose']: utils.echo_error_msg(e)
        return (None)
    return (o_xyz)
Esempio n. 6
0
def datalist_major(dls, major='.mjr.datalist', region=None):
    """set the major datalist

    Args:
      dls (list): datalist entries, minimally: ['datafile.xyz']
      region (list): a region list [xmin, xmax, ymin, ymax]    

    Returns:
      str: the major datalist filename
    """

    with open(major, 'w') as md:        
        for dl in dls:
            for entry in datalist2py(dl, region):
                md.write('{}\n'.format(' '.join([str(e) for e in entry])))
    if os.stat(major).st_size == 0:
        utils.remove_glob(major)
        utils.echo_error_msg('bad datalist/entry, {}'.format(dls))
        return(None)
    #utils.echo_msg('processed datalist')
    return(major)
Esempio n. 7
0
def datalist2py(dl, region=None):
    """convert a datalist to python data

    Args:
      dl (str): a datalist path/entry
      region (list): a region list [xmin, xmax, ymin, ymax]    

    Returns:
      list: list of datalist entries.
    """
    
    this_entry = entry2py(dl)
    this_dir = os.path.dirname(this_entry[0])
    if this_entry[1] == -1:
        if os.path.exists(this_entry[0]):
            with open(this_entry[0], 'r') as op:
                for this_line in op:
                    if this_line[0] != '#' and this_line[0] != '\n' and this_line[0].rstrip() != '':
                        yield([os.path.join(this_dir, x) if n == 0 else x for n,x in enumerate(entry2py(this_line.rstrip()))])
        else:
            utils.echo_error_msg('could not open datalist/entry {}'.format(this_entry[0]))
    else:
        yield(this_entry)
Esempio n. 8
0
def str2region(region_str):
    """attempt to convert a string into a region

    Arguments:
      region(str): a region string

    Returns:
      region(list/wkt): a region list or a polygon wkt
    """
    
    try:
        this_region = [float(x) for x in region_str.split('/')]
    except ValueError:
        this_region = gdal_ogr_multi_poly(region_str)
    except Exception as e:
        utils.echo_error_msg('failed to parse region(s), {}'.format(e))
        this_region = None

    if this_region is None:
        if region_wkt_p(regions_str):
            this_region = region_str
            
    return(this_region)
Esempio n. 9
0
        elif arg == '-overwrite' or arg == '--overwrite':
            overwrite = True
        elif arg == '-verbose' or arg == '--verbose':
            verbose = True
        elif arg[0] == '-':
            sys.stderr.write(_usage)
            sys.exit(1)
        elif output is None:
            output = arg
        else:
            sys.stderr.write(_usage)
            sys.exit(1)

        i = i + 1

    if output is None:
        sys.stderr.write(_usage)
        utils.echo_error_msg('you must enter an output file name')
        sys.exit(0)

    if extent == None: extent = '1'

    #Run the program given the user input
    if cpgrd is not None:
        createNullCopy(cpgrd, output, nodata, d_format, verbose, overwrite)
    else:
        createGrid(output, extent, cellsize, nodata, d_format, verbose,
                   overwrite)

### END
Esempio n. 10
0
    chunk_value = 1000
    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if arg == '-C' or arg == '-c' or arg == '-chunk' or arg == '--chunk':
            try:
                chunk_value = int(sys.argv[i + 1])
            except: pass
            i = i + 1
        elif arg == '-help' or arg == '--help' or arg == '-h':
            sys.stderr.write(_usage)
            sys.exit(-1)
        elif arg == '-version' or arg == '--version':
            sys.stdout.write('{}\n'.format(_version))
            sys.exit(-1)
        elif src_fn is None: src_fn = arg
        else:
            sys.stderr.write(_usage)
            sys.exit(-1)
        i = i + 1

    if src_fn is None:
        utils.echo_error_msg('you must enter an input file')
        sys.stderr.write(_usage)
        sys.exit(1)

    if not os.path.exists(src_fn):
        utils.echo_error_msg('{} is not valid'.format(src_fn))
    else: gdalfun.gdal_chunks(src_fn, n_chunk = chunk_value)
### End
Esempio n. 11
0
    while i < len(sys.argv):
        arg = sys.argv[i]
        if arg == '-s' or arg == '-split' or arg == '--split':
            split_value = float(sys.argv[i + 1])
            i = i + 1
        elif arg == '-help' or arg == '--help' or arg == '-h':
            sys.stderr.write(_usage)
            sys.exit(1)
        elif arg == '-version' or arg == '--version':
            sys.stderr.write('gdal_split.py v.{}'.format(_version))
            sys.exit(1)
        elif elev is None:
            elev = arg
        else:
            sys.stderr.write(_usage)
            sys.exit(1)

        i = i + 1

    if elev is None:
        sys.stderr.write(_usage)
        utils.echo_error_msg('you must enter an input file')
        sys.exit(1)

    if not os.path.exists(elev):
        utils.echo_error_msg("{} is not a valid file".format(elev))
    else:
        gdalfun.gdal_split(elev, split_value)

### End
Esempio n. 12
0
            i = i + 1
        elif arg == '-p' or arg == '--op':
            d_form = 'xy' + sys.argv[i + 1]
            i = i + 1
        elif i_ds is None:
            i_ds = arg
        elif t_ds is None:
            t_ds = arg
        else:
            sys.stderr.write(_usage)
            sys.exit(1)
        i = i + 1

    if i_ds is None or not os.path.exists(i_ds):
        sys.stderr.write(_usage)
        utils.echo_error_msg('you must enter a valid input raster file')
        sys.exit(1)

    if t_ds is None or not os.path.exists(t_ds):
        sys.stderr.write(_usage)
        utils.echo_error_msg('you must enter a valid input raster file')
        sys.exit(1)

    if d_ds is None:
        d_ds = i_ds.split('.')[0] + '_trans.' + i_ds.split('.')[-1]

    ds = gdal.Open(i_ds)
    ds_config = gdalfun.gdal_gather_infos(ds)
    ds_region = regions.gt2region(ds_config)
    ds_inc = ds_config['geoT'][1]
Esempio n. 13
0
def datalists_cli(argv = sys.argv):
    """run datalists from command-line

    See `datalists_cli_usage` for full cli options.
    """
    
    status = 0
    dls = []
    i_region = None
    i_inc = 0.000277777
    o_bn = None
    epsg = None
    these_regions = []
    want_verbose = False
    want_inf = False
    want_region = False
    want_sm = False
    want_glob = False
    want_list = False
    want_archive = False
    want_mask = False
    want_weights = False
    z_region = None
    w_region = None
    dl_fmt = None

    ## ==============================================
    ## parse command line arguments.
    ## ==============================================
    i = 1
    while i < len(argv):
        arg = argv[i]
        if arg == '--region' or arg == '-R':
            i_region = str(argv[i + 1])
            i = i + 1
        elif arg[:2] == '-R':
            i_region = str(arg[2:])
        elif arg == '--output-name' or arg == '-O':
            o_bn = str(argv[i + 1])
            i = i + 1
        elif arg[:2] == '-O':
            o_bn = str(arg[2:])
        elif arg == '--increment' or arg == '-E':
            try:
                i_inc = float(argv[i + 1])
            except:
                sys.stederr.write('error, -E should be a float value\n')
                sys.exit(1)
            i = i + 1
        elif arg[:2] == '-E':
            try:
                i_inc = float(arg[2:])
            except:
                sys.stederr.write('error, -E should be a float value\n')
                sys.exit(1)
        elif arg == '--epsg' or arg == '-P':
            epsg = argv[i + 1]
            i = i + 1
        elif arg[:2] == '-P':
            epsg = arg[2:]
        elif arg == '--format' or arg == '-F':
            dl_fmt = int(argv[i + 1])
            i = i + 1
        elif arg[:2] == '-F':
            dl_fmt = int(arg[2:])
        elif arg == '--glob' or arg == '-g':
            want_glob = True
        elif arg == '--spatial-md' or arg == '-s':
            want_sm = True
        elif arg == '--list' or arg == '-l':
            want_list = True
        elif arg == '--archive' or arg == '-a':
            want_archive = True
        elif arg == '--info-file' or arg == '-i':
            want_inf = True
        elif arg == '--region-info' or arg == '-r':
            want_region = True
        elif arg == '--weights' or arg == '-w':
            want_weights = True
        elif arg == '--help' or arg == '-h':
            print(datalists_usage)
            sys.exit(1)
        elif arg == '--version' or arg == '-v':
            print('{}, version {}'.format(os.path.basename(sys.argv[0]), datalists_version))
            sys.exit(1)
        elif arg == '--verbose' or arg == '-V':
            want_verbose = True
        elif arg[0] == '-':
            print(datalists_usage)
            sys.exit(0)
        else:
            dls.append(arg)

        i = i + 1

    if want_glob:
        if dl_fmt is None:
            dl_fmts = list(_dl_dl_h.keys())
            dl_fmts.remove(-1)
        else:
            dl_fmts = [dl_fmt]
        # for f in _known_datalist_fmts[-1]:
        #     globs = glob.glob('*.{}'.format(f))
        #     [sys.stdout.write('{}\n'.format(' '.join([x, str(-1), '1']))) for x in globs]
        for key in dl_fmts:
            for f in _dl_dl_h[key]['fmts']:
                globs = glob.glob('*.{}'.format(f))
                [sys.stdout.write('{}\n'.format(' '.join([x, str(key), '1']))) for x in globs]
        sys.exit(0)
        
    if len(dls) == 0:
        print(datalists_usage)
        sys.exit(1)
        
    ## ==============================================
    ## process input region(s) and loop
    ## ==============================================
    region_is_geom_p = False
    if i_region is not None:
        this_region = regions.str2region(i_region)
        these_regions = [this_region]
        # try:
        #     these_regions = [[float(x) for x in i_region.split('/')]]
        #     region_is_geom_p = False
        # except ValueError:
        #     these_regions = regions.gdal_ogr_polys(i_region)
        #     region_is_geom_p = True
        # except Exception as e:
        #     utils.echo_error_msg('failed to parse region(s), {}'.format(e))            
    else: these_regions = [None]
    
    if len(these_regions) == 0:
        utils.echo_error_msg('failed to parse region(s), {}'.format(i_region))

    for rn, this_region in enumerate(these_regions):
        dl = datalist_major(dls, region = this_region)
        dlp_hooks = []

        if regions.region_valid_p(this_region):
            dlp_hooks.append(lambda e: intersect_p(this_region, e, epsg))
        elif regions.region_wkt_p(this_region):
            dlp_hooks.append(lambda e: intersect_wkt_p(this_region, e, epsg))
        if z_region is not None:
            dlp_hooks.append(lambda e: regions.z_region_pass(inf_entry(e)['minmax'], upper_limit=z_region[1], lower_limit=z_region[0]))
        if w_region is not None:
            dlp_hooks.append(lambda e: regions.z_pass(e[2], upper_limit=w_region[1], lower_limit=w_region[0]))
            
        #for dl in dls:
        if want_inf:
            #datalist_inf_entry([dl_m, -1, 1], epsg = epsg, overwrite = True)
            datalist_inf_entry(dl, epsg=epsg, overwrite=True)
        elif want_region:
            #print(datalist_inf(dl_m, inf_file = False, epsg = epsg, overwrite = False))
            print(datalist_inf(dl, inf_file=False, epsg=epsg, overwrite=False))
        elif want_list:
            for this_entry in datalist(dl, wt=1, pass_h=dlp_hooks, verbose=want_verbose):
                print(' '.join([','.join(x) if i == 3 else os.path.abspath(str(x)) if i == 0 else str(x) for i,x in enumerate(this_entry[:-1])]))
        else:
            datalist_dump_xyz(dl, wt=1 if want_weights else None, pass_h=dlp_hooks, region=this_region, epsg=epsg, verbose=want_verbose)