def __init__( self, data=[], src_region=None, xinc=None, yinc=None, name='waffles_sm', src_srs='epsg:4326', dst_srs=None, extend=0, node='pixel', ogr_format='ESRI Shapefile', recursive=False, verbose=False ): """generate spatial-metadata""" self.data = data self.xinc = utils.float_or(xinc) self.yinc = utils.float_or(yinc) self.src_srs = utils.str_or(src_srs, 'epsg:4326') self.dst_srs = utils.str_or(dst_srs, 'epsg:4326') self.extend = extend self.node = node self.recursive = recursive self.region = src_region self.d_region = self.dist_region() self.name = name self.ogr_format = ogr_format self.verbose = verbose self._init_data() self._init_vector()
def from_list(self, region_list): """import a region from a region list Args: region_list (list): [xmin, xmax, ymin, ymax[, zmin, zmax[, wmin, wmax]]] Returns: region-object: self """ if len(region_list) >= 4: self.xmin = utils.float_or(region_list[0]) self.xmax = utils.float_or(region_list[1]) self.ymin = utils.float_or(region_list[2]) self.ymax = utils.float_or(region_list[3]) if len(region_list) >= 6: self.zmin = utils.float_or(region_list[4]) self.zmax = utils.float_or(region_list[5]) if len(region_list) >= 8: self.wmin = utils.float_or(region_list[6]) self.wmax = utils.float_or(region_list[7]) if self.wkt is None: if self.valid_p(check_xy = True): self.wkt = self.export_as_wkt() else: self.wkt = None return(self)
def __init__(self, x=None, y=None, z=None, w=1, src_srs='epsg:4326', z_units='m', z_datum='msl'): self.x = float_or(x) self.y = float_or(y) self.z = float_or(z) self.w = float_or(w, 1) self.src_srs = str_or(src_srs, 'epsg:4326') self.z_units = z_units self.z_datum = z_datum
def yield_xyz(self, entry): """process ngs monuments""" src_data = 'ngs_tmp.json' src_csv = 'ngs_tmp.csv' if f_utils.Fetch(entry[0], callback=self.callback, verbose=self.verbose).fetch_file(src_data) == 0: with open(src_data, 'r') as json_file: r = json.load(json_file) if len(r) > 0: for row in r: z = utils.float_or(row[self.datum]) if z is not None: xyz = xyzfun.XYZPoint(src_srs='epsg:4326').from_list( [float(row['lon']), float(row['lat']), z]) if self.dst_srs is not None: xyz.warp(dst_srs=self.dst_srs) yield (xyz) else: utils.echo_error_msg( 'failed to fetch remote file, {}...'.format(src_data)) utils.remove_glob('{}*'.format(src_data))
def ogr_wkts(src_ds): """return the wkt(s) of the ogr dataset""" these_regions = [] src_s = src_ds.split(':') if os.path.exists(src_s[0]): poly = ogr.Open(src_s[0]) if poly is not None: p_layer = poly.GetLayer(0) for pf in p_layer: pgeom = pf.GetGeometryRef() pwkt = pgeom.ExportToWkt() r = Region().from_string(pwkt) if len(src_s) > 1: src_r = src_s[1].split('/') if len(src_r) > 0: r.zmin = utils.float_or(src_r[0]) if len(src_r) > 1: r.zmax = utils.float_or(src_r[1]) if len(src_r) > 2: r.wmin = utils.float_or(src_r[2]) if len(src_r) > 3: r.wmax = utils.float_or(src_r[3]) these_regions.append(r) poly = None return(these_regions)
def from_list(self, xyz_list, x_pos=0, y_pos=1, z_pos=2, w_pos=3): """load xyz data from a list Args: xyz_list (list): a list of xyz data [x,y,z,...] Returns: xyz: self """ if len(xyz_list) > x_pos: self.x = float_or(xyz_list[x_pos]) if len(xyz_list) > y_pos: self.y = float_or(xyz_list[y_pos]) if len(xyz_list) > z_pos: self.z = float_or(xyz_list[z_pos]) if len(xyz_list) > w_pos: self.w = float_or(xyz_list[w_pos]) return (self)
def __init__(self, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, wmin=None, wmax=None, src_srs='epsg:4326', wkt=None): self.xmin = utils.float_or(xmin) self.xmax = utils.float_or(xmax) self.ymin = utils.float_or(ymin) self.ymax = utils.float_or(ymax) self.zmin = utils.float_or(zmin) self.zmax = utils.float_or(zmax) self.wmin = utils.float_or(wmin) self.wmax = utils.float_or(wmax) self.src_srs = utils.str_or(src_srs, 'epsg:4326') self.wkt = wkt
def from_string(self, region_str): """import a region from a region string Args: region_str (str): <-R>xmin/xmax/ymin/ymax/zmin/zmax Returns: region-object: self """ if region_str[:2] == '-R': region_str = region_str[2:] str_list = region_str.strip().split('/') if len(str_list) >= 4: r_list = [utils.float_or(x) for x in str_list] self.from_list(r_list) elif region_str.split()[0] == "POLYGON" or region_str.split()[0] == "MULTIPOLYGON": self.wkt = region_str self.from_list(ogr.CreateGeometryFromWkt(region_str).GetEnvelope()) return(self)
outgrd = None gt_data = None lt_data = None eq_data = None overwrite = False verbose = False mk_ndata = False ndata = None bin_mask = False applygrd = None i = 1 while i < len(sys.argv): arg = sys.argv[i] if arg == '-gt': gt_data = utils.float_or(sys.argv[i + 1]) i = i + 1 elif arg == '-lt': lt_data = utils.float_or(sys.argv[i + 1]) i = i + 1 elif arg == '-eq': eq_data = utils.float_or(sys.argv[i + 1]) i = i + 1 elif arg == '-nodata': mk_ndata = True ndata = sys.argv[i + 1] i = i + 1 elif arg == '-apply': bin_mask = True applygrd = sys.argv[i + 1] i = i + 1
def parse_fn(self): """parse the datalist entry line""" if self.fn is None: return (self) if os.path.exists(self.fn): self.guess_data_format() self.metadata['name'] = self.fn.split('.')[0] return (self.fn) this_entry = re.findall(r'[^"\s]\S*|".+?"', self.fn.rstrip()) try: entry = [utils.str_or(x) if n == 0 else utils.int_or(x) if n < 2 else utils.float_or(x) if n < 3 else utils.str_or(x) \ for n, x in enumerate(this_entry)] except Exception as e: utils.echo_error_msg('could not parse entry {}'.format(self.fn)) return (self) ## ============================================== ## data format ## guess format based on fn if not specified and ## parse format for dataset specific opts. ## ============================================== if len(entry) < 2: for key in self.data_types.keys(): se = entry[0].split('.') see = se[-1] if len(se) > 1 else entry[0].split(":")[0] if see in self.data_types[key]['fmts']: entry.append(int(key)) break if len(entry) < 2: utils.echo_error_msg('could not parse entry {}'.format( self.fn)) return (self) else: opts = this_entry[1].split(':') if len(opts) > 1: self.ds_args = utils.args2dict(list(opts[1:]), {}) this_entry[1] = opts[0] else: self.ds_args = {} ## ============================================== ## weight ## inherit weight of parent ## ============================================== if len(entry) < 3: entry.append(1) elif entry[2] is None: entry[2] = 1 if self.parent is not None: if self.weight is not None: self.weight *= entry[2] else: if self.weight is not None: self.weight = entry[2] ## ============================================== ## title ## ============================================== if len(entry) < 4: entry.append(self.metadata['title']) else: self.metadata['title'] = entry[3] ## ============================================== ## source ## ============================================== if len(entry) < 5: entry.append(self.metadata['source']) else: self.metadata['source'] = entry[4] ## ============================================== ## date ## ============================================== if len(entry) < 6: entry.append(self.metadata['date']) else: self.metadata['date'] = entry[5] ## ============================================== ## data type ## ============================================== if len(entry) < 7: entry.append(self.metadata['data_type']) else: self.metadata['data_type'] = entry[6] ## ============================================== ## resolution ## ============================================== if len(entry) < 8: entry.append(self.metadata['resolution']) else: self.metadata['resolution'] = entry[7] ## ============================================== ## hdatum ## ============================================== if len(entry) < 9: entry.append(self.metadata['hdatum']) else: self.metadata['hdatum'] = entry[8] ## ============================================== ## vdatum ## ============================================== if len(entry) < 10: entry.append(self.metadata['vdatum']) else: self.metadata['vdatum'] = entry[9] ## ============================================== ## url ## ============================================== if len(entry) < 11: entry.append(self.metadata['url']) else: self.metadata['url'] = entry[10] ## ============================================== ## file-name ## ============================================== if self.parent is None or entry[1] == -11: self.fn = entry[0] else: self.fn = os.path.join(os.path.dirname(self.parent.fn), entry[0]) self.data_format = entry[1] self.guess_data_format() return (self)
def regions_cli(argv = sys.argv): """run regions from command-line See `regions_usage` for full cli options. """ src_srs = None dst_srs = None i_regions = [] these_regions = [] want_verbose = True echo = False echo_fn = False bv = None te = False ## ============================================== ## parse command line arguments. ## ============================================== i = 1 while i < len(argv): arg = argv[i] if arg == '--region' or arg == '-R': i_regions.append(str(argv[i + 1])) i = i + 1 elif arg[:2] == '-R': i_regions.append(str(arg[2:])) elif arg == '-s_srs' or arg == '--s_srs' or arg == '-P': src_srs = argv[i + 1] i = i + 1 elif arg == '-t_srs' or arg == '--t_srs' or arg == '-W': dst_srs = argv[i + 1] i = i + 1 elif arg == '-b' or arg == '-B' or arg == '--buffer': bv = utils.float_or(argv[i+1]) i = i + 1 elif arg == '-e' or arg == '--echo' or arg == '-ee': echo = True elif arg == '-te': echo = True te = True elif arg == '-n' or arg == '--name': echo_fn = True elif arg == '--quiet' or arg == '-q': want_verbose = False 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]), cudem.__version__)) sys.exit(1) elif arg[0] == '-': print(regions_usage) sys.exit(0) else: dls.append(arg) i = i + 1 for i_region in i_regions: tmp_region = Region().from_string(i_region) if tmp_region.valid_p(): these_regions.append(tmp_region) else: tmp_region = ogr_wkts(i_region) for i in tmp_region: if i.valid_p(): these_regions.append(i) if len(these_regions) == 0: print(regions_usage) utils.echo_error_msg('you must specify at least one region') sys.exit(-1) else: if want_verbose: utils.echo_msg('parsed {} region(s)'.format(len(these_regions))) for rn, this_region in enumerate(these_regions): if bv is not None: this_region.buffer(x_bv=bv, y_bv=bv) if echo: if te: print(this_region.format('te')) else: print(this_region.format('gmt')) elif echo_fn: print(this_region.format('fn')) else: this_region.export_as_ogr('test.shp')