def main(): _opts = _init_env() import file_unzip with file_unzip.file_unzip(_opts.temp) as _zip: import geo_raster_c as ge print 'loading', _opts.input _bnds = [] _f_in = _zip.unzip(_opts.input) if _f_in.endswith('hdf'): _img = ge.geo_raster.open(_f_in) for _b in _opts.bands: _bnds.append(_img.get_subdataset(_b).get_band()) else: _img = ge.geo_raster.open(_f_in) for _b in _opts.bands: _bnds.append(_img.get_band(int(_b))) if len(_bnds) not in [1, 3]: raise Exception('Incorrect band numbers %s' % len(_bnds)) _bnd = _bnds[0] _opt = [] if _opts.compress: if _opts.output.endswith('.tif'): _opt.append('compress=lzw') if _opts.output.endswith('.img'): _opt.append('COMPRESS=YES') _img = ge.geo_raster.create(_opts.output, [len(_bnds), _bnd.height, _bnd.width], _bnd.geo_transform, _bnd.proj, ge.pixel_type(), opts=_opt) _line = 1024 for i in xrange(len(_bnds)): print ' + band', _opts.bands[i], 'sr' if _opts.convert_sr else 'dn' _bbb = _img.get_band(i + 1) _fun = convert_band_sr if _opts.convert_sr else convert_band import progress_percentage _ppp = progress_percentage.progress_percentage(_bnd.height) for _row in xrange(0, _bnd.height, _line): _ppp.next(_line) _bbb.write(_fun(_bnds[i], _row, _line, _bnd), 0, _row) _ppp.done() _img = None
def task(self, oid, dtype=None): import file_unzip import config print 'request data cache: %s, %s' % (oid, dtype) with file_unzip.file_unzip(config.get_at('general', 'tmp_path')) as _zip: _f = self.mag.load(oid, dtype.lower() if dtype else dtype, _zip) self.output_file(_f)
def insert_band(self, bnd, username, note=''): import file_unzip import config with file_unzip.file_unzip(config.get_at('general', 'tmp_path')) as _zip: _f = _zip.generate_file('', '.tif') bnd.save(_f) return self.insert_file(_f, 'band', 'tif', username, note)
def download_modis_product(host, dtype, code, fd_out, fa): import datetime, os _f_out = os.path.join(fd_out, code + '_' + datetime.datetime.now().strftime('%y%m%d') + '.txt') _url = '/' + dtype + '/' + code # download_list_ftp(host, _url, _f_out) import file_unzip with file_unzip.file_unzip() as _zip: _cs = None if not fa else _load_list(_zip.unzip(fa)) _url = 'http://%s/%s/%s' % (host, dtype, code) download_list_http(_url, _f_out, _cs)
def main(): _opts = _init_env() _f_inp = _opts.input _d_out = _opts.output _f_clr = _opts.color import config import os os.path.exists(_d_out) or os.makedirs(_d_out) import file_unzip with file_unzip.file_unzip() as _zip: # detect the extent of input file _ext = load_shp(_f_inp) if _opts.input.endswith('.shp') else load_img(_f_inp, _zip) logging.info('detected extent %s' % str(_ext)) print 'detected extent', _ext _ps = [] for _lev in xrange(_opts.levels[0], _opts.levels[1]+1): print ' - checking level', _lev for _lev, _num, _col, _row in tiles().list(_lev, _ext): _ps.append((_f_inp, _lev, _num, _col, _row, _opts.percent, _f_clr, _d_out)) logging.info('found %s task' % len(_ps)) print 'found %s tasks' % len(_ps) print 'write map.html' if _opts.instance_pos == 0: import geo_raster_c as ge _ext_geo = _ext.to_polygon().segment_ratio(30).project_to(ge.proj_from_epsg()).extent() _f_out = os.path.join(_d_out, 'map.html') with open(config.cfg.get('conf', 'openlayers_temp'), 'r') as _fi, open(_f_out, 'w') as _fo: _fo.write(_fi.read() % { 'title': os.path.basename(_f_inp), 'xmin': _ext_geo.minx, 'xmax': _ext_geo.maxx, 'ymin': _ext_geo.miny, 'ymax': _ext_geo.maxy, 'zmin': _opts.levels[0], 'zmax': _opts.levels[1] }) import multi_task multi_task.Pool(make_tile, [_ps[i] for i in xrange(_opts.instance_pos, len(_ps), _opts.instance_num)], _opts.task_num, True).run()
def make_tile(f, lev, num, col, row, percent, f_clr, d_out): import file_unzip import os with file_unzip.file_unzip() as _zip: _d = os.path.join(d_out, str(lev), str(col)) try: os.path.exists(_d) or os.makedirs(_d) except Exception: pass _f = os.path.join(_d, '%s.png' % row) if os.path.exists(_f) and os.path.getsize(_f) > 0: return if percent != None: band(f, _zip).make_perc(tiles().extent(lev, col, row), percent, f_clr, _f) else: band(f, _zip).make(tiles().extent(lev, col, row), f_clr, _f)