def getFileType(filename): """ Determine whether input file is a shapefile or a grid (ESRI or GMT). Args: filename (str): Path to candidate filename. Returns: str: 'shapefile', 'grid', or 'unknown'. """ # TODO MOVE TO MAPIO. if os.path.isdir(filename): return 'dir' ftype = GMTGrid.getFileType(filename) if ftype != 'unknown': return 'gmt' # Skip over ESRI header files if filename.endswith('.hdr'): return 'unknown' try: GDALGrid.getFileGeoDict(filename) return 'esri' except: pass return 'unknown'
def get_file_type(file): """Internal method to figure out which file type (GMT or GDAL) the population/country code grid files are. """ if GMTGrid.getFileType(file) == 'unknown': try: gdict = GDALGrid.getFileGeoDict(file) return GDALGrid except: pass else: return GMTGrid return None
def getFileType(filename): if os.path.isdir(filename): return 'dir' ftype = GMTGrid.getFileType(filename) if ftype != 'unknown': return 'gmt' #skip over ESRI header files if filename.endswith('.hdr'): return 'unknown' try: GDALGrid.getFileGeoDict(filename) return 'esri' except: pass return 'unknown'
def getFileType(filename): """Determine whether input file is a shapefile or a grid (ESRI or GMT). EVENTUALLY WILL BE MOVED TO MAPIO :param filename: String path to candidate filename. :returns: String, one of 'shapefile','grid','unknown'. """ if os.path.isdir(filename): return 'dir' ftype = GMTGrid.getFileType(filename) if ftype != 'unknown': return 'gmt' #skip over ESRI header files if filename.endswith('.hdr'): return 'unknown' try: GDALGrid.getFileGeoDict(filename) return 'esri' except: pass return 'unknown'
def quickcut(filename, gdict, tempname=None, extrasamp=5., method='bilinear', precise=True, cleanup=True, verbose=False, override=False): """ Use gdal to trim a large global file down quickly so mapio can read it efficiently. (Cannot read Shakemap.xml files, must save as .bil filrst) Args: filename (str): File path to original input file (raster). gdict (geodict): Geodictionary to cut around and align with. tempname (str): File path to desired location of clipped part of filename. extrasamp (int): Number of extra cells to cut around each edge of geodict to have resampling buffer for future steps. method (str): If resampling is necessary, method to use. precise (bool): If true, will resample to the gdict as closely as possible, if False it will just roughly cut around the area of interest without changing resolution cleanup (bool): if True, delete tempname after reading it back in verbose (bool): if True, prints more details override (bool): if True, if filename extent is not fully contained by gdict, read in the entire file (only used for ShakeMaps) Returns: New grid2D layer Note: This function uses the subprocess approach because ``gdal.Translate`` doesn't hang on the command until the file is created which causes problems in the next steps. """ if gdict.xmax < gdict.xmin: raise Exception('quickcut: your geodict xmax is smaller than xmin') try: filegdict = GDALGrid.getFileGeoDict(filename) except: try: filegdict = GMTGrid.getFileGeoDict(filename) except: raise Exception('Cannot get geodict for %s' % filename) if tempname is None: tempdir = tempfile.mkdtemp() tempname = os.path.join(tempdir, 'junk.tif') deltemp = True else: tempdir = None deltemp = False # if os.path.exists(tempname): # os.remove(tempname) # print('Temporary file already there, removing file') filegdict = filegdict[0] # Get the right methods for mapio (method) and gdal (method2) if method == 'linear': method2 = 'bilinear' if method == 'nearest': method2 = 'near' if method == 'bilinear': method = 'linear' method2 = 'bilinear' if method == 'near': method = 'nearest' method2 = 'near' else: method2 = method if filegdict != gdict: # First cut without resampling tempgdict = GeoDict.createDictFromBox(gdict.xmin, gdict.xmax, gdict.ymin, gdict.ymax, filegdict.dx, filegdict.dy, inside=True) try: egdict = filegdict.getBoundsWithin(tempgdict) ulx = egdict.xmin - extrasamp * egdict.dx uly = egdict.ymax + extrasamp * egdict.dy lrx = egdict.xmax + (extrasamp + 1) * egdict.dx lry = egdict.ymin - (extrasamp + 1) * egdict.dy cmd = 'gdal_translate -a_srs EPSG:4326 -of GTiff -projwin %1.8f \ %1.8f %1.8f %1.8f -r %s %s %s' % (ulx, uly, lrx, lry, method2, filename, tempname) except Exception as e: if override: # When ShakeMap is being loaded, sometimes they won't align # right because it's already cut to the area, so just load # the whole file cmd = 'gdal_translate -a_srs EPSG:4326 -of GTiff -r %s %s %s' \ % (method2, filename, tempname) else: raise Exception('Failed to cut layer: %s' % e) rc, so, se = get_command_output(cmd) if not rc: raise Exception(se.decode()) else: if verbose: print(so.decode()) newgrid2d = GDALGrid.load(tempname) if precise: # Resample to exact geodictionary newgrid2d = newgrid2d.interpolate2(gdict, method=method) if cleanup: os.remove(tempname) if deltemp: shutil.rmtree(tempdir) else: ftype = GMTGrid.getFileType(filename) if ftype != 'unknown': newgrid2d = GMTGrid.load(filename) elif filename.endswith('.xml'): newgrid2d = ShakeGrid.load(filename) else: newgrid2d = GDALGrid.load(filename) return newgrid2d