def test_project(): data = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=np.int32) geodict = { 'xmin': 50, 'xmax': 50.4, 'ymin': 50, 'ymax': 50.4, 'dx': 0.1, 'dy': 0.1, 'nx': 5, 'ny': 5 } gd = GeoDict(geodict) grid = GDALGrid(data, gd) projstr = "+proj=utm +zone=40 +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs " newgrid = grid.project(projstr, method='nearest') try: tdir = tempfile.mkdtemp() outfile = os.path.join(tdir, 'output.bil') grid.save(outfile) with rasterio.open(outfile) as src: aff = get_affine(src) data = src.read(1) src_crs = CRS().from_string(GeoDict.DEFAULT_PROJ4).to_dict() dst_crs = CRS().from_string(projstr).to_dict() nrows, ncols = data.shape left = aff.xoff top = aff.yoff right, bottom = aff * (ncols - 1, nrows - 1) dst_transform, width, height = calculate_default_transform( src_crs, dst_crs, ncols, nrows, left, bottom, right, top) destination = np.zeros((height, width)) reproject(data, destination, src_transform=aff, src_crs=src_crs, dst_transform=dst_transform, dst_crs=dst_crs, src_nodata=src.nodata, dst_nodata=np.nan, resampling=Resampling.nearest) x = 1 except: pass finally: shutil.rmtree(tdir)
def test_project(): data = np.array([[0,0,1,0,0], [0,0,1,0,0], [1,1,1,1,1], [0,0,1,0,0], [0,0,1,0,0]],dtype=np.int32) geodict = {'xmin':50,'xmax':50.4,'ymin':50,'ymax':50.4,'dx':0.1,'dy':0.1,'nx':5,'ny':5} gd = GeoDict(geodict) grid = GDALGrid(data,gd) projstr = "+proj=utm +zone=40 +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs " newgrid = grid.project(projstr,method='nearest') try: tdir = tempfile.mkdtemp() outfile = os.path.join(tdir,'output.bil') grid.save(outfile) with rasterio.open(outfile) as src: aff = src.transform data = src.read(1) src_crs = CRS().from_string(GeoDict.DEFAULT_PROJ4).to_dict() dst_crs = CRS().from_string(projstr).to_dict() nrows,ncols = data.shape left = aff.xoff top = aff.yoff right,bottom = aff * (ncols-1, nrows-1) dst_transform,width,height = calculate_default_transform(src_crs,dst_crs, ncols,nrows, left,bottom, right,top) destination = np.zeros((height,width)) reproject(data, destination, src_transform=aff, src_crs=src_crs, dst_transform=dst_transform, dst_crs=dst_crs, src_nodata=src.nodata, dst_nodata=np.nan, resampling=Resampling.nearest) x = 1 except: pass finally: shutil.rmtree(tdir)
def test_project(): # test projecting a grid that wraps the 180 meridian gd = GeoDict.createDictFromBox(175, -175, -5, 5, 1.0, 1.0) ncells = gd.ny * gd.nx data = np.arange(0.0, ncells).reshape(gd.ny, gd.nx) grid = GDALGrid(data, gd) projstr = "+proj=merc +lat_ts=55 +lon_0=180 +ellps=WGS84" newgrid = grid.project(projstr, method='nearest') proj = pyproj.Proj(projstr) # what would the ul/lr corners be? ulx, uly = proj(grid._geodict.xmin, grid._geodict.ymax) lrx, lry = proj(grid._geodict.xmax, grid._geodict.ymin) # what if we back-project? newxmin, newymax = proj(newgrid._geodict.xmin, newgrid._geodict.ymax, inverse=True) newxmax, newymin = proj(newgrid._geodict.xmax, newgrid._geodict.ymin, inverse=True) x = 1 # test simple projection data = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=np.int32) geodict = {'xmin': 50, 'xmax': 50.4, 'ymin': 50, 'ymax': 50.4, 'dx': 0.1, 'dy': 0.1, 'nx': 5, 'ny': 5} gd = GeoDict(geodict) grid = GDALGrid(data, gd) projstr = "+proj=utm +zone=40 +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs " newgrid = grid.project(projstr, method='nearest') try: tdir = tempfile.mkdtemp() outfile = os.path.join(tdir, 'output.bil') grid.save(outfile) with rasterio.open(outfile) as src: aff = get_affine(src) data = src.read(1) src_crs = CRS().from_string(GeoDict.DEFAULT_PROJ4).to_dict() dst_crs = CRS().from_string(projstr).to_dict() nrows, ncols = data.shape left = aff.xoff top = aff.yoff right, bottom = aff * (ncols-1, nrows-1) dst_transform, width, height = calculate_default_transform(src_crs, dst_crs, ncols, nrows, left, bottom, right, top) destination = np.zeros((height, width)) reproject(data, destination, src_transform=aff, src_crs=src_crs, dst_transform=dst_transform, dst_crs=dst_crs, src_nodata=src.nodata, dst_nodata=np.nan, resampling=Resampling.nearest) x = 1 except: pass finally: shutil.rmtree(tdir)
def makeTestData(): # make test layers X = ['friction', 'slope', 'vs30', 'cti1', 'precip'] config = OrderedDict() config.setdefault('logistic_models', {}).setdefault('test_model', {}) config['logistic_models']['test_model'].setdefault('shortref', 'Name et al. year') config['logistic_models']['test_model'].setdefault('longref', 'full reference') config['logistic_models']['test_model'].setdefault('layers', {}) config['logistic_models']['test_model'].setdefault('interpolations', {}) config['logistic_models']['test_model'].setdefault('terms', {}) config['logistic_models']['test_model'].setdefault('coefficients', {})['b0'] = 3.5 for k, items in enumerate(X): coef = 'b%1d' % (k+1) # make a GDALGrid object testgrid = GDALGrid(eval(items), geodict) # Save the file if items == 'precip': try: os.mkdir('test_precip') except: pass filename = 'test_precip/prec_Jan.bil' # Only make January for testing else: filename = 'test_%s.bil' % (items) testgrid.save(filename, format='EHdr') # add to test config file config['logistic_models']['test_model']['layers'].update({items: {'file': filename.split('/')[0], 'units': units[k], 'longref': 'longref', 'shortref': 'shortref'}}) config['logistic_models']['test_model']['interpolations'].update({items: 'nearest'}) config['logistic_models']['test_model']['terms'].update({coef: terms[k]}) config['logistic_models']['test_model']['coefficients'].update({coef: coefficients[k]}) config['logistic_models']['test_model']['gfeype'] = 'landslide' config['logistic_models']['test_model']['baselayer'] = 'slope' config['logistic_models']['test_model']['slopemin'] = 5. config['logistic_models']['test_model']['slopemax'] = 90. # Make test_shakegrid and test_uncert eventDict = OrderedDict([('event_id', 'test'), ('lon', 0.5), ('lat', 0.5), ('event_timestamp', datetime(2000, 1, 5, 0, 30, 55)), ('event_network', 'na'), ('magnitude', 6.0), ('event_description', 'Test event'), ('depth', 5.0)]) shakeDict = OrderedDict([('process_timestamp', datetime(2000, 1, 6, 20, 38, 19)), ('event_id', 'test'), ('shakemap_version', 2), ('code_version', '1 billion'), ('shakemap_event_type', 'TEST'), ('map_status', 'TEST'), ('shakemap_id', 'test'), ('shakemap_originator', 'na')]) uncertaintyDict = {} layers1 = {'pga': pga, 'pgv': pgv} shakegrid = ShakeGrid(layers1, geodict, eventDict, shakeDict, uncertaintyDict) shakegrid.save('test_shakegrid.xml') layers2 = {'stdpga': stdpga} uncertgrid = ShakeGrid(layers2, geodict, eventDict, shakeDict, uncertaintyDict) uncertgrid.save('test_uncert.xml') C = ConfigObj(config) C.filename = 'test.ini' C.write() return config
def makeTestData(): # make test layers and config files (model, and mapping) Xmod = ['friction', 'slope', 'vs30', 'cti1', 'precip'] terms = ['friction', 'slope/100.', 'log(vs30)', 'cti1', 'precipMONTH'] coefficients = [0.3, 2.1, -0.5, 0.01, 1.0] Xhaz = ['susceptibility'] Xgod = ['cohesion', 'friction', 'slope'] Xcla = ['cohesion', 'friction', 'slope', 'watertable'] configModel = OrderedDict() configHazus = OrderedDict() configGodt = OrderedDict() configClassic = OrderedDict() # Work on model configs #LOGISTIC configModel.setdefault('test_model', {}) configModel['test_model'].setdefault('shortref', 'Name et al. year') configModel['test_model'].setdefault('longref', 'full reference') configModel['test_model'].setdefault('layers', {}) configModel['test_model'].setdefault('interpolations', {}) configModel['test_model'].setdefault('terms', {}) configModel['test_model'].setdefault('coefficients', {})['b0'] = '3.5' configModel['test_model'].setdefault( 'display_options', { 'lims': { 'model': 'np.linspace(0., 0.4, 10)' }, 'colors': { 'default': 'cm.inferno', 'alpha': '0.7', 'model': 'cm.jet' }, 'logscale': { 'model': 'False' }, 'maskthresholds': { 'model': '0.001' } }) configModel['test_model']['gfeype'] = 'landslide' configModel['test_model']['baselayer'] = 'slope' configModel['test_model']['slopemin'] = 5. configModel['test_model']['slopemax'] = 90. configModel['test_model']['display_options'].setdefault( 'pga', { 'lims': 'None', 'colors': 'cm.jet', 'logscale': 'True' }) configModel['test_model']['display_options'].setdefault( 'pgv', { 'lims': 'None', 'colors': 'cm.jet', 'logscale': 'False' }) # HAZUS configHazus.setdefault('hazus', {}) configHazus['hazus'].setdefault('shortref', 'Name et al. year') configHazus['hazus'].setdefault('longref', 'full reference') configHazus['hazus']['gfeype'] = 'landslide' configHazus['hazus'].setdefault('layers', {}) configHazus['hazus'].setdefault('parameters', {'dnthresh': 5.}) configHazus['hazus'].setdefault( 'display_options', { 'lims': { 'model': 'np.linspace(0., 0.4, 10)' }, 'colors': { 'default': 'cm.inferno', 'alpha': '0.7', 'model': 'cm.jet' }, 'logscale': { 'model': 'False' }, 'maskthresholds': { 'model': '0.001' } }) configHazus['hazus']['display_options'].setdefault('susceptibility', { 'lims': 'None', 'colors': 'cm.jet', 'logscale': 'True' }) # GODT configGodt.setdefault('godt_2008', {}) configGodt['godt_2008'].setdefault('shortref', 'Name et al. year') configGodt['godt_2008'].setdefault('longref', 'full reference') configGodt['godt_2008']['gfeype'] = 'landslide' configGodt['godt_2008'].setdefault('layers', {}) configGodt['godt_2008'].setdefault( 'parameters', { 'thick': '2.4', 'uwt': '15.7', 'nodata_cohesion': '1.0', 'nodata_friction': '26.', 'dnthresh': '5.', 'fsthresh': '1.01', 'acthresh': '0.05' }) configGodt['godt_2008'].setdefault( 'display_options', { 'lims': { 'model': 'np.linspace(0., 0.4, 10)' }, 'colors': { 'default': 'cm.inferno', 'alpha': '0.7', 'model': 'cm.jet' }, 'logscale': { 'model': 'False' }, 'maskthresholds': { 'model': '0.001' } }) configGodt['godt_2008']['display_options'].setdefault( 'pga', { 'lims': 'None', 'colors': 'cm.jet', 'logscale': 'True' }) # NEWMARK configClassic.setdefault('classic_newmark', {}) configClassic['classic_newmark'].setdefault('shortref', 'Name et al. year') configClassic['classic_newmark'].setdefault('longref', 'full reference') configClassic['classic_newmark']['gfeype'] = 'landslide' configClassic['classic_newmark'].setdefault('layers', {}) configClassic['classic_newmark'].setdefault( 'parameters', { 'thick': '2.4', 'uwt': '15.7', 'nodata_cohesion': '1.0', 'nodata_friction': '26.', 'dnthresh': '5.', 'fsthresh': '1.01', 'acthresh': '0.05', 'slopethresh': '5.', 'm': '0.5' }) configClassic['classic_newmark'].setdefault( 'display_options', { 'lims': { 'model': 'np.linspace(0., 0.4, 10)' }, 'colors': { 'default': 'cm.inferno', 'alpha': '0.7', 'model': 'cm.jet' }, 'logscale': { 'model': 'False' }, 'maskthresholds': { 'model': '0.001' } }) configClassic['classic_newmark']['display_options'].setdefault( 'pga', { 'lims': 'None', 'colors': 'cm.jet', 'logscale': 'True' }) for k, items in enumerate(Xmod): coef = 'b%1d' % (k + 1) # make a GDALGrid object testgrid = GDALGrid(eval(items), geodict) # Save the file if items == 'precip': try: os.mkdir('test_precip') except: pass filename = 'test_precip/prec_Jan.bil' # Only make January for testing else: filename = 'test_%s.bil' % (items) testgrid.save(filename, format='EHdr') # add to test config file configModel['test_model']['layers'].update({ items: { 'file': filename.split('/')[0], 'units': units[items], 'longref': 'longref', 'shortref': 'shortref' } }) configModel['test_model']['interpolations'].update({items: 'nearest'}) configModel['test_model']['terms'].update({coef: terms[k]}) configModel['test_model']['coefficients'].update( {coef: coefficients[k]}) configModel['test_model']['display_options']['lims'].update( {items: 'None'}) configModel['test_model']['display_options']['colors'].update( {items: 'None'}) configModel['test_model']['display_options']['logscale'].update( {items: 'False'}) configModel['test_model']['display_options']['maskthresholds'].update( {items: 'None'}) for k, items in enumerate(Xhaz): # Save the file if not already saved filename = 'test_%s.bil' % (items) if items == 'susceptibility': testgrid = GDALGrid(eval(items), susgeodict) testgrid.save(filename, format='EHdr') # add to test config file configHazus['hazus']['layers'].update({ items: { 'file': filename, 'units': units[items], 'longref': 'longref', 'shortref': 'shortref' } }) configHazus['hazus']['display_options']['lims'].update({items: 'None'}) configHazus['hazus']['display_options']['colors'].update( {items: 'None'}) configHazus['hazus']['display_options']['logscale'].update( {items: 'False'}) configHazus['hazus']['display_options']['maskthresholds'].update( {items: 'None'}) for k, items in enumerate(Xgod): # make a GDALGrid object if items == 'slope': testgrid = GDALGrid(eval('slopeGodt'), geodict) else: testgrid = GDALGrid(eval(items), geodict) # Save the file filename = 'test_%s.bil' % (items) word = 'file' if items == 'slope': word = 'filepath' try: os.mkdir('test_slope_quantiles_godt') except: pass for j, slp in enumerate(slopequants): filename = 'test_slope_quantiles_godt/slope%s.bil' % ( slopequantnames[j]) # Only make January for testing temp = GDALGrid(eval('slopeGodt') * slopequants[j], geodict) temp.save(filename, format='EHdr') filename = 'test_slope_quantiles_godt' elif items == 'cohesion': testgrid.save(filename, format='EHdr') # add to test config file configGodt['godt_2008']['layers'].update({ items: { word: filename, 'units': units[items], 'longref': 'longref', 'shortref': 'shortref' } }) configGodt['godt_2008']['display_options']['lims'].update( {items: 'None'}) configGodt['godt_2008']['display_options']['colors'].update( {items: 'None'}) configGodt['godt_2008']['display_options']['logscale'].update( {items: 'False'}) configGodt['godt_2008']['display_options']['maskthresholds'].update( {items: 'None'}) for k, items in enumerate(Xcla): # make a GDALGrid object testgrid = GDALGrid(eval(items), geodict) # Save the file filename = 'test_%s.bil' % (items) if items == 'watertable': testgrid.save(filename, format='EHdr') # add to test config file configClassic['classic_newmark']['layers'].update({ items: { 'file': filename, 'units': units[items], 'longref': 'longref', 'shortref': 'shortref' } }) configClassic['classic_newmark']['display_options']['lims'].update( {items: 'None'}) configClassic['classic_newmark']['display_options']['colors'].update( {items: 'None'}) configClassic['classic_newmark']['display_options']['logscale'].update( {items: 'False'}) configClassic['classic_newmark']['display_options'][ 'maskthresholds'].update({items: 'None'}) # Make test_shakegrid and test_uncert eventDict = OrderedDict([ ('event_id', 'test'), ('lon', 0.5), ('lat', 0.5), ('event_timestamp', datetime(2000, 1, 5, 0, 30, 55)), ('event_network', 'na'), ('magnitude', 6.0), ('event_description', 'Test event'), ('depth', 5.0) ]) shakeDict = OrderedDict([('process_timestamp', datetime(2000, 1, 6, 20, 38, 19)), ('event_id', 'test'), ('shakemap_version', 2), ('code_version', '1 billion'), ('shakemap_event_type', 'TEST'), ('map_status', 'TEST'), ('shakemap_id', 'test'), ('shakemap_originator', 'na')]) uncertaintyDict = {} layers1 = {'pga': pga, 'pgv': pgv} shakegrid = ShakeGrid(layers1, geodict, eventDict, shakeDict, uncertaintyDict) shakegrid.save('test_shakegrid.xml') layers2 = {'stdpga': stdpga, 'stdpgv': stdpgv} uncertgrid = ShakeGrid(layers2, geodict, eventDict, shakeDict, uncertaintyDict) uncertgrid.save('test_uncert.xml') layers3 = {'pga': pga1, 'pgv': pgv1} shakegrid = ShakeGrid(layers3, geodict1, eventDict, shakeDict, uncertaintyDict) shakegrid.save('test_shakegrid_bounds.xml') C = ConfigObj(configModel) C.filename = 'testconfig_logimodel.ini' C.write() C = ConfigObj(configHazus) C.filename = 'testconfig_hazus.ini' C.write() C = ConfigObj(configClassic) C.filename = 'testconfig_classic.ini' C.write() C = ConfigObj(configGodt) C.filename = 'testconfig_godt.ini' C.write() configMap = OrderedDict({ 'dem': { 'file': 'None' }, 'roads': { 'file': 'None' }, 'cities': { 'file': 'None' }, 'ocean': { 'file': 'None' }, 'colors': { 'roadcolor': '808080', 'countrycolor': '474747', 'watercolor': 'B8EEFF' } }) C = ConfigObj(configMap) C.filename = 'testconfig_map.ini' C.write()