Exemplo n.º 1
0
def NumPyArrayToRaster(nparr, proj, geot, nodata_value, out_raster_path, dtype=None):
    gdal.AllRegister()
    np_dt = nparr.dtype 
    if dtype == None:
        dtype = gdal_array.NumericTypeCodeToGDALTypeCode(np_dt)

    print( "saving")
    # Check if working with multiband raster
    if len(nparr.shape) == 3:
        n_bands = nparr.shape[0] 
        for x in range(0, n_bands):
            driver = gdal.GetDriverByName('GTIFF')
            outDs = driver.Create(out_raster_path, nparr.shape[2], nparr.shape[1], n_bands, dtype,
                                  ['COMPRESS=LZW', 'TILED=YES', 'BLOCKXSIZE=128', 'BLOCKYSIZE=128'])
            outDs.GetRasterBand(x + 1).WriteArray(nparr[x])
            outDs.GetRasterBand(x + 1).SetNoDataValue(nodata_value)
            outDs.GetRasterBand(x + 1).FlushCache()
            outDs.SetProjection(proj)
            outDs.SetGeoTransform(geot)

            outDs = None
    else:
        driver = gdal.GetDriverByName('GTIFF')
        outDs = driver.Create(out_raster_path, nparr.shape[1], nparr.shape[0], 1, dtype,
                              ['COMPRESS=LZW', 'TILED=YES', 'BLOCKXSIZE=128', 'BLOCKYSIZE=128'])
        outDs.GetRasterBand(1).WriteArray(nparr)
        outDs.GetRasterBand(1).SetNoDataValue(nodata_value)
        outDs.GetRasterBand(1).FlushCache()
        outDs.SetProjection(proj)
        outDs.SetGeoTransform(geot)
        outDs = None
Exemplo n.º 2
0
def elevation_data(coRd):

    #provide with coordinates
    lat = float(coRd[0])
    lon = float(coRd[1])

    gdal.AllRegister()

    # open the image
    ds = gdal.Open('elev.tif', GA_ReadOnly)

    if ds is None:
        print 'Could not open image'
        sys.exit(1)

    #getting the image details
    rows = ds.RasterYSize
    cols = ds.RasterXSize
    bands = ds.RasterCount

    ##Getting the georefinformation - Use affine transformation matrix  - pixel coords to world coords
    tr0 = ds.GetGeoTransform()
    xOrigin = tr0[0]
    yOrigin = tr0[3]
    pixelWidth = tr0[1]
    pixelHeight = tr0[5]
    ##Convert to raster points
    pixel = int((lat - xOrigin) / pixelWidth)
    line = int((lon - yOrigin) / pixelHeight)
    #pixel and line
    b = ds.GetRasterBand(1)

    dat = b.ReadAsArray(3341, 1244, 1, 1)
    return dat  ##this is wrong, this is giving me zero everytime? am i making a fundamental mistake? revisit!
Exemplo n.º 3
0
 def __init__(self, grid, data, gformat):
     self.grid = grid
     self.rows = data.shape[0]
     self.columns = data.shape[1]
     self.data = data
     self.gformat = gformat  #List of formats at http://www.gdal.org/formats_list.html
     gdal.AllRegister()
Exemplo n.º 4
0
def get_array_from_tiff(filename):
    gdal.AllRegister()
    file = filename
    ds = gdal.Open(file)
    band = ds.GetRasterBand(1)
    data = band.ReadAsArray()
    return data
Exemplo n.º 5
0
def retrieve_dem(
    min_lon: float,
    min_lat: float,
    degrees_lon: float,
    degrees_lat: float,
    sampling_rate: int = 1,
    output_path: str = "/tmp/elevation.dem",
) -> Tuple[np.ndarray, np.array]:
    """
    Load SRTM tiles for a bouding rectangle defined by an upper left point (min_long, min_lat) and a width and height
    in degrees. Optionally, an integer sampling rate greater than 1 may be passed in to downsample the DEM.
    :param min_lon: x component of the upper left corner of the bounding rectangle
    :param min_lat: y component of the upper left corner of the bounding rectangle
    :param degrees_lon: width of the DEM in degrees
    :param degrees_lat: height of the DEM in degrees
    :param sampling_rate: sampling rate
    :param output_path: Path where the dem file is saved
    :return: A numpy ndarray with elevation data, followed by the geotransform of the elevation data wrapped in a Tuple
    """
    load_dem(
        min_lon,
        min_lat,
        degrees_lon,
        degrees_lat,
        rate=sampling_rate,
        data_source="AWS",
        output_name=output_path,
    )
    gdal.AllRegister()
    dem_dataset = gdal.Open(output_path)
    elevation_data = dem_dataset.ReadAsArray()
    geo_transform = np.array([*dem_dataset.GetGeoTransform()])
    return elevation_data, geo_transform
Exemplo n.º 6
0
def array_to_raster(array, cell_sizes, topleft_corner, noDataValue,
                    out_filepath):
    """Array > Raster
    Save a raster from a C order array.

    :param array: ndarray
    """
    gdal.AllRegister()
    y_pixels, x_pixels = array.shape
    cellsize_long, cellsize_lat = cell_sizes
    x_min, y_max = topleft_corner

    #wkt_projection = 'a projection in wkt that you got from other file'

    driver = gdal.GetDriverByName('GTiff')

    dataset = driver.Create(out_filepath, x_pixels, y_pixels, 1,
                            gdal.GDT_Float32)

    dataset.SetGeoTransform((
        x_min,  # 0
        cellsize_long,  # 1
        0,  # 2
        y_max,  # 3
        0,  # 4
        -cellsize_lat))

    #dataset.SetProjection(wkt_projection)
    dataset.GetRasterBand(1).SetNoDataValue(noDataValue)
    dataset.GetRasterBand(1).WriteArray(array)
    dataset.FlushCache()  # Write to disk.
Exemplo n.º 7
0
def freadbk(path_file,line_start=1, pixels_start=1,nofLines1=None,nofPixels1=None):
    #Driver
    driver=gdal.GetDriverByName('MFF')
    driver.Register()
    gdal.AllRegister()
    thisBurstData_file=gdal.Open(path_file,GA_ReadOnly)
    if thisBurstData_file is None:
        print 'Could not open'+Path_MFF_HDR
        sys.exit(1)
    #print 'Driver: ', thisBurstData_file.GetDriver().ShortName,'/', \
    #      thisBurstData_file.GetDriver().LongName
    #print 'Size is ',thisBurstData_file.RasterXSize,'x',thisBurstData_file.RasterYSize, \
    #      'x',thisBurstData_file.RasterCount
    #print 'Projection is ',thisBurstData_file.GetProjection()
    geotransform = thisBurstData_file.GetGeoTransform()
    if not geotransform is None:
        print 'Origin = (',geotransform[0], ',',geotransform[3],')'
        print 'Pixel Size = (',geotransform[1], ',',geotransform[5],')'

    cint_srd=thisBurstData_file.GetRasterBand(1)
    #print 'Band Type=',gdal.GetDataTypeName(cint_srd.DataType)

    if cint_srd.GetOverviewCount() > 0:
            print 'Band has ', cint_srd.GetOverviewCount(), ' overviews.'
    thisBurstData= cint_srd.ReadAsArray(int(pixels_start-1),int(line_start-1),nofPixels1,nofLines1)
    return thisBurstData
Exemplo n.º 8
0
def save_array_as_image(image_path, image_array):

    image_array = image_array.astype(np.uint8)
    if not image_path.lower().endswith(".png") and not image_path.lower(
    ).endswith(".jpg") and not image_path.lower().endswith(".tif"):
        print(image_path)
        print("Error! image_path has to end with .png, .jpg or .tif")
    height = image_array.shape[0]
    width = image_array.shape[1]
    if height * width < Image.MAX_IMAGE_PIXELS:
        newIm = Image.fromarray(image_array, "RGB")
        newIm.save(image_path)

    else:
        gdal.AllRegister()
        driver = gdal.GetDriverByName('MEM')
        ds1 = driver.Create('', width, height, 3, gdal.GDT_Byte)
        ds = driver.CreateCopy(image_path, ds1, 0)

        image_array = np.swapaxes(image_array, 2, 1)
        image_array = np.swapaxes(image_array, 1, 0)
        ds.GetRasterBand(1).WriteArray(image_array[0], 0, 0)
        ds.GetRasterBand(2).WriteArray(image_array[1], 0, 0)
        ds.GetRasterBand(3).WriteArray(image_array[2], 0, 0)
        gdal.Translate(image_path,
                       ds,
                       options=gdal.TranslateOptions(bandList=[1, 2, 3],
                                                     format="png"))
Exemplo n.º 9
0
def test_read_ds_value_from_wgs84():
    expected = 151.0
    gdal.AllRegister()
    data_source = gdal.Open(DS_FILENAME, GA_ReadOnly)
    actual = geods.read_ds_value_from_wgs84(data_source, 43.602091, 1.441183)

    assert abs(expected - actual) <= EPSILON
Exemplo n.º 10
0
def readraster(infile):
    """ Loads the data from any raster-type file
        eg. *.dem, *.grd,...    
    """
    # register all of the drivers
    gdal.AllRegister()
    # open the image
    ds = gdal.Open(infile, GA_ReadOnly)
    
    # Read the x and y coordinates
    cols = ds.RasterXSize
    rows = ds.RasterYSize
    bands = ds.RasterCount
    
    geotransform = ds.GetGeoTransform()
    originX = geotransform[0]
    originY = geotransform[3]
    pixelWidth = geotransform[1]
    pixelHeight = geotransform[5]
    
    x = originX + np.linspace(0,cols-1,cols)*pixelWidth
    y = originY + np.linspace(0,rows-1,rows)*pixelHeight
    
    # Read the actual data
    data = ds.ReadAsArray(0,0,cols,rows)
    
    # Remove missing points
    data[data==-32767]=np.nan

    return x, y, data
Exemplo n.º 11
0
def saltepath(path):
    # 处理卫星数据,最后返回文件列表,path为卫星数据目录
    if not os.path.exists(path):
        print('The given path does not exist! Do not use modis data!')
        return None
    now = dt.datetime.now().strftime('%Y%m%d')
    pattern = r'(\w' + now + '.*?tif)'  # 匹配卫星文件名
    strings = os.listdir(path)
    tiflists = Znwg.regex(pattern, strings)  # 返回当前卫星数据文件列表
    if len(tiflists) == 0:
        print(r'MODIS tiff don\'t exist, call model_2!')
        return None
    else:
        print('MODIS exists, call model_1!')
        gdal.AllRegister()
        dataset = gdal.Open(tiflists)
        rtf = Datainterface.ReadTiff(dataset)
        px, py = rtf.imagexy2pro()
        pro2geov = np.vectorize(rtf.pro2geo)
        lon, lat = pro2geov(px, py)  # 此处执行很慢,循环操作
        # newlat =np.linspace(31.4,39.4,801)
        # newlon =np.linspace(89.3,103.1,1381)
        # *_, newdata = rtf.equalatlon('SnowDepth', dataset.ReadAsArray(), lat, lon, newlat, newlon)
        *_, newdata = rtf.equalatlon('SnowDepth', dataset.ReadAsArray(), lat,
                                     lon, glovar.lat, glovar.lon)
        return newdata
Exemplo n.º 12
0
def main(argv):

    gdal.AllRegister()
    ds = gdal.Open(argv[1])
    rowscount = ds.RasterYSize
    myarray_t = np.array(ds.GetRasterBand(1).ReadAsArray(), dtype=np.integer)

    cols = ds.RasterXSize
    rows = ds.RasterYSize
    print("x3 ndim: ", myarray_t.ndim)
    print("x3 shape:", myarray_t.shape)
    print("x3 size: ", myarray_t.size)
    print("cols: ", cols)
    print("rows: ", rows)
    #附加的一列
    blas = np.ones((rows, 1))
    for i in range(rows):
        blas[i][0] = i

    myarray_t = np.hstack((myarray_t, blas))

    d = {}
    for i in range(cols + 1):
        row = myarray_t[:, i]
        d[str(i)] = row

    df = pd.DataFrame(d)
    table = pa.Table.from_pandas(df)
    pq.write_table(table,
                   argv[1] + '.parquet',
                   compression='GZIP',
                   use_dictionary=False)
Exemplo n.º 13
0
def compute_distance(gdb_dir, shapefile):
    '''For every point in `shapefile' find the diatance to the neartest
  feature (in our case a road) and save it in a new field.  The field
  has the same name as the layer in the feature file (perhaps truncated
  due to length).

  '''

    # FIXME: What is this?
    gdal.AllRegister()

    # Use OGR specific exceptions
    ogr.UseExceptions()

    # Open DB directory
    db = open_db(gdb_dir)

    # try to open source shapefile
    if int(gdal.VersionInfo()) > 1990000:
        shape = ogr.Open(shapefile.name, gdal.OF_VECTOR)
    else:
        shape = ogr.Open(shapefile.name, 1)
    if shape is None:
        print('Unable to open shapefile', in_shapefile)
        sys.exit(1)

    layer = shape.GetLayer(0)
    # add new fields to the shapefile
    create_fields(layer, db)
    process(layer, db, False)

    # clean close
    del db
Exemplo n.º 14
0
        def long2ShortName(self, longName):
            if longName == '':
                return ''

            if longName in self.dict_long2shortName:
                return self.dict_long2shortName[longName]

            # first get the GDAL driver manager
            if gdal.GetDriverCount() == 0:
                gdal.AllRegister()

            shortName = ''

            # for each loaded GDAL driver
            for i in range(gdal.GetDriverCount()):
                driver = gdal.GetDriver(i)
                if driver == None:
                    continue

                # if this is the driver we searched for then return its short name
                if FileFilter.getFilterName(driver.LongName) == longName:
                    shortName = FileFilter.getFilterName(driver.ShortName)
                    self.dict_long2shortName[longName] = shortName
                    break

            return shortName
Exemplo n.º 15
0
 def buildOverviews():
     try:
         image=os.path.join(uploadfiles[0],'chaneltransformRPC.tif')
         gdal.AllRegister()
         TransformDS = gdal.Open(image.encode('utf-8').decode(), gdal.GA_ReadOnly)
         Width = TransformDS.RasterXSize
         Heigh = TransformDS.RasterYSize
         PixelNum = Width * Heigh
         TopNum = 4096
         CurNum = PixelNum / 4
         anLevels = []
         nLevelCount = 0
         while (CurNum > TopNum):
             anLevels.append(pow(2, nLevelCount + 2))
             nLevelCount += 1
             CurNum /= 4
         TransformDS.BuildOverviews(overviewlist=anLevels)
         cat = Catalog(map_url,'admin', 'geoserver')
         wkspce = cat.get_workspace('Map')
         cat.create_coveragestore_external_geotiff(name=id, data='file://' + image.encode('utf-8').decode('utf-8'),
                                                   workspace=wkspce)
         cat.reload()
         TransformDS = None
     except Exception as e:
         return Exception("上传失败,建立金字塔出错"+str(e))
Exemplo n.º 16
0
def numpy_rw_1():

    gdaltest.numpy_drv = None
    try:
        import gdalnumeric
        gdalnumeric.zeros
    except:
        try:
            import osgeo.gdal_array as gdalnumeric
        except ImportError:
            return 'skip'

    try:
        import _gdal
        _gdal.GDALRegister_NUMPY()  # only needed for old style bindings.
        gdal.AllRegister()
    except:
        pass

    gdaltest.numpy_drv = gdal.GetDriverByName('NUMPY')
    if gdaltest.numpy_drv is None:
        gdaltest.post_reason('NUMPY driver not found!')
        return 'fail'

    return 'success'
Exemplo n.º 17
0
    def raster2array(self, file_name, *nan_thresholds):
        # nan_threshold[0] = float number below which all raster values will be set to np.nan
        # nan_threshold[1] = float number above which all raster values will be set to np.nan

        file_name = self.align_raster(file_name)
        gdal.AllRegister()
        print('* screeninfo: processing ' + file_name)
        try:
            logging.info(" *** opening file " + str(file_name) + " ...")
            file = gdal.Open(file_name)
            ras = file.GetRasterBand(1)
        except:
            logging.info('WARNING: Could not open ' + str(file_name))
        try:
            logging.info(" *** converting Raster to Array ...")
            ras_array = ras.ReadAsArray()
        except:
            logging.info('WARNING: Could not convert raster to array.')

        # make non-relevant pixels np.nan
        logging.info(
            " *** normalizing Array (identify nan and apply sigmoid normalization) ..."
        )
        try:
            ras_array = ras_array.astype(float)
        except:
            logging.info(
                'WARNING: Could not convert array(raster) to floats --> error will likely occur.'
            )

        ras_array[ras_array <= self.no_data_value] = np.nan
        try:
            ras_array[ras_array < nan_thresholds[0][0]] = np.nan
            ras_array[ras_array > nan_thresholds[0][1]] = np.nan
        except:
            pass
        ras_array[ras_array == 0] = np.nan

        array_dim = ras_array.shape
        for i in range(0, array_dim[0] - 1):
            for j in range(0, array_dim[1] - 1):
                if not (np.isnan(ras_array[i][j])
                        or np.round(ras_array[i][j], 1) == self.no_data_value
                        or np.round(ras_array[i][j], 4) == 0.0000):
                    try:
                        ras_array[i][j] = 1 / (1 + np.exp(-ras_array[i][j]))
                    except:
                        ras_array[i][j] = np.nan
                else:
                    ras_array[i][j] = np.nan
                if ras_array[i][j] == 0:
                    ras_array[i][j] = np.nan

        logging.info("     Max array value: " + str(np.nanmax(ras_array)))
        logging.info("     Min array value: " + str(np.nanmin(ras_array)))
        logging.info(" *** calculating Array statistics ...")
        stats = ras.GetStatistics(0, 1)
        print('* processed * ' + str(file_name))

        return [ras_array, stats]
Exemplo n.º 18
0
def test_read_ds_data():
    expected = 151.0
    gdal.AllRegister()
    data_source = gdal.Open(DS_FILENAME, GA_ReadOnly)
    actual = geods.read_ds_data(data_source, 529, 477)

    assert abs(expected - actual) <= EPSILON
Exemplo n.º 19
0
 def Converte_Rasters_to_HDF(self, arquivo_HDF):
     '''
     #Cria um HDF5 a partir das imagens na pasta
     '''
     #criar varuavel HDf
     self.arquivo_HDF = arquivo_HDF
     #criar arquivo HDF
     f_hdf = h5py.File(self.arquivo_HDF, "w", libver='latest')
     #percorrer cada raster e armazenar no HDF5
     for caminho, nome in self.caminho_nome_rasters:
         print 'Nomes rasters: ', nome
         #Obter drives para todos os tipos de imagem
         gdal.AllRegister()
         #Get metadata image
         ds = gdal.Open(caminho, gdal.GA_ReadOnly)
         #Total de colunas e linhas
         colunas = ds.RasterXSize
         linhas = ds.RasterYSize
         #Criar conjunto de dados HDF
         conjunto_dados = f_hdf.create_dataset(
             nome, (linhas, colunas), chunks=True,
             dtype=np.float32)  #(linhas,1)
         #percorrer cada linha da imageme  armazenar no HDF
         for i in xrange(linhas):
             #Read segmentation how array
             conjunto_dados[i, :] = ds.GetRasterBand(1).ReadAsArray(
                 0, i, colunas, 1)
     return f_hdf
Exemplo n.º 20
0
def read_ascii_to_array(ascii_path, xmin, ymax, cols_wide, rows_high):
    """ Reads an ascii grid file into a numpy array and returns the array. This
        function first checks that the number of rows and columns and the upper
        left corner co-ordinates are as expected.
    """
    # Register drivers
    gdal.AllRegister()

    # Open the file with GDAL
    ds = gdal.Open(ascii_path, gdalconst.GA_ReadOnly)
    if ds is None:
        print('Could not open ' + ascii_path)
        sys.exit(1)

    # Get dataset properties
    geotransform = ds.GetGeoTransform()
    origin_x = geotransform[0]
    origin_y = geotransform[3]

    # Read the data to an array and return it
    band = ds.GetRasterBand(1)
    data = band.ReadAsArray()

    # Get the no data value
    ndv = band.GetNoDataValue()

    # Check properties are correct
    assert origin_x == xmin
    assert origin_y == ymax
    assert data.shape == (rows_high, cols_wide)

    # Mask the array and return it
    masked = ma.masked_equal(data, ndv)
    return masked
Exemplo n.º 21
0
def MonthlyStats(outputfilepath, filelist, year, ring, section):
    '''
    '''
    NumberOfDays = len(filelist)
    monthDict={1:'January', 2:'February', 3:'March', 4:'April', 5:'May', \
               6:'June', 7:'July', 8:'August', 9:'September', 10:'October', \
               11:'November', 12:'December'}    
    outputtextfile = outputfilepath + "MonthlyStatistics" + str(year) + ".txt"
    cumulativepermonth = 0
    cumulativeperday = 0 
    
    for ringsectionfile in filelist:
        (ringsectionfilenamepath, ringsectionfilename) = os.path.split(ringsectionfile)
        year = int(ringsectionfilename[3:7])
        monthAsNumber = int(ringsectionfilename[7:9])
        month = monthDict[int(ringsectionfilename[7:9])]
        
        gdal.AllRegister()
        
        # open the image
        ds = gdal.Open(ringsectionfile, gdalconst.GA_ReadOnly)
        if ds is None:
            print 'Could not open '
            sys.exit(1)
        referencefilename = '//home//max//Documents//DagIskart//Rasterize' + section + '.tif'
        referencefile = gdal.Open(referencefilename, gdalconst.GA_ReadOnly)
        
        # get image size
        rows = ds.RasterYSize
        cols = ds.RasterXSize

        #Read input raster into array
        ringsectionraster = ds.ReadAsArray()
        referenceraster = referencefile.ReadAsArray()
        
        percentage = 0
        cumulativeperday = 0
        count = 0
        for i in range(rows):
            for j in range(cols):
                if (referenceraster[i,j] == int(ring)) and \
                   ( 0<= ringsectionraster[i,j] <= 250):
                    percentage = ringsectionraster[i,j] *100/250.0
                    count = count + 1
                    cumulativeperday = cumulativeperday + percentage
                    
        cumulativeperday = cumulativeperday / count
        cumulativepermonth = cumulativepermonth + cumulativeperday / NumberOfDays
        ds = None
        referencefile = None

    print 'Year and month = ', str(year), month
    print 'Mean Ice Concentration = ', cumulativepermonth
    textfile = open( outputtextfile, 'a')
    textfile.write(str(year) + ', ' + month + ', ' + ring + ', ' + section + ', ' \
                      + str(cumulativepermonth) + '\n' )   
    textfile.close()
    ds = None
    CreateRingStatistics(ring, section, year, monthAsNumber, cumulativepermonth)
Exemplo n.º 22
0
def abrir(endArquivo, nomeImagem):
    gdal.AllRegister()

    entrada = gdal.Open(endArquivo + nomeImagem, GA_ReadOnly)
    if entrada is None:
        return False, 'Erro ao abrir o arquivo: ' + endArquivo + nomeImagem

    return True, entrada
Exemplo n.º 23
0
def merge(path):
    print '========================='
    print '     Merge VV and VH'
    print '========================='
    print time.asctime()  
    print 'Directory %s'%path 
    
    gdal.AllRegister()
    try:
    #  get sorted lists of VV and VH files    
        files = os.listdir(path)
        vvfiles = []
        vhfiles = []
        for afile in files:
            if re.search('\.VV_[0-9]{1,2}\.tif',afile):   
                vvfiles.append(afile)
            elif re.search('\.VH_[0-9]{1,2}\.tif',afile): 
                vhfiles.append(afile)
        if (len(vvfiles) != len(vhfiles)) or (len(vvfiles)==0) :
            raise Exception('file mismatch or no files found')
        os.chdir(path)
        vvfiles.sort()
        vhfiles.sort()
        inDataset1 = gdal.Open(vvfiles[0],GA_ReadOnly) 
        driver = inDataset1.GetDriver() 
        cols = inDataset1.RasterXSize
        rows = inDataset1.RasterYSize        
        for i in range(len(vvfiles)):
            outfile = vvfiles[i].replace('VV','VVVH')
            inDataset1 = gdal.Open(vvfiles[i],GA_ReadOnly)  
            inDataset2 = gdal.Open(vhfiles[i],GA_ReadOnly)   
            geotransform = inDataset1.GetGeoTransform()
            projection = inDataset1.GetProjection()
            outDataset = driver.Create(outfile,cols,rows,2,GDT_Float32)
            if geotransform is not None:
                outDataset.SetGeoTransform(geotransform)        
            if projection is not None:
                outDataset.SetProjection(projection)
            inArray = inDataset1.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
            outBand = outDataset.GetRasterBand(1)    
            outBand.WriteArray(inArray,0,0)
            outBand.FlushCache()
            outBand = outDataset.GetRasterBand(2)
            inArray = inDataset2.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
            outBand.WriteArray(inArray,0,0) 
            outBand.FlushCache()
            print '%s and %s merged to %s'%(vvfiles[i],vhfiles[i],outfile) 
        inDataset1 = None
        inDataset2 = None
        outDataset = None     
        for i in range(len(vvfiles)):
            os.remove(vvfiles[i].replace('.tif','.tfw'))
            os.remove(vhfiles[i].replace('.tif','.tfw'))
            os.remove(vvfiles[i])
            os.remove(vhfiles[i])
    except Exception as e:
        print 'Error %s'%e 
        return None     
Exemplo n.º 24
0
def get_raster(filename, points, quiet = False):
    """
    Reads the value of attributes in a raster file at a list of points.
    """

    if quiet: gdal.PushErrorHandler('CPLQuietErrorHandler') 

    # register all of the drivers

    gdal.AllRegister()

    # open the image

    dataset = gdal.Open(filename, GA_ReadOnly)

    # get the coordinate transformation

    transform = get_NAD1983_transform(dataset)

    # get image size

    rows  = dataset.RasterYSize
    cols  = dataset.RasterXSize
    bands = dataset.RasterCount

    # get georeference info

    x0, width, x_rotation, y0, y_rotation, height = dataset.GetGeoTransform()

    # loop through the points and get the raster values

    values = []
    for point in points:

        # get x,y

        x, y, z = transform.TransformPoint(point[0], point[1]) 

        # transform the easting and northing to pixel space

        pixel_x = int((x - x0) / width)
        pixel_y = int((y - y0) / height)

        # loop through the bands and find the values

        for i in range(1, bands + 1):

            band = dataset.GetRasterBand(i)

            # read data and add the value to the string

            value = band.ReadRaster(pixel_x, pixel_y, 1, 1)
            if value is None: value = -1
            else: value = int.from_bytes(value, byteorder = 'little')

        values.append(value)

    return values
Exemplo n.º 25
0
def test_read_ds_data_np():
    expected = np.array([195, 182, 176, 177, 175, 160, 136, 130, 109, 113])
    gdal.AllRegister()
    data_source = gdal.Open(DS_FILENAME, GA_ReadOnly)
    actual = geods.read_ds_data(data_source, np.linspace(300, 400, 10, dtype=int),
                                np.linspace(400, 300, 10, dtype=int))

    for exp, act in zip(expected, actual):
        assert abs(exp - act) <= EPSILON
Exemplo n.º 26
0
def pyWriteMaskedGTiff(rasterfile, maschera, out_name):
    import numpy as np
    import gdal
    gdal.AllRegister()
    # Prendo i driver del GeoTiff
    driver = gdal.GetDriverByName("GTiff")
    driver.Register()

    # la apro in GDAL
    #rasterfile = gdal.Open(rasterfile_nome)
    # conto le bande
    rasterfile_nbands = rasterfile.RasterCount
    # creo un array vuoto con le dimensioni del raster che ho importato
    rasterfile_3Darray = np.empty(
        (rasterfile.RasterYSize, rasterfile.RasterXSize,
         rasterfile.RasterCount))

    # riempio l'array con i valori delle bande del raster
    # NOTA BENE range(1:8) crea un vettore di 7 elementi da 1 a 7
    maschera_band = maschera.GetRasterBand(1)
    maschera_array = maschera_band.ReadAsArray(0, 0, maschera.RasterXSize,
                                               maschera.RasterYSize)
    for i in range(1, (rasterfile_nbands + 1)):
        rasterfile_band = rasterfile.GetRasterBand(i)
        rasterfile_array = rasterfile_band.ReadAsArray(0, 0,
                                                       rasterfile.RasterXSize,
                                                       rasterfile.RasterYSize)
        rasterfile_array = rasterfile_array * maschera_array
        rasterfile_array[rasterfile_array == -1.6999999999999999e+308] = np.nan
        rasterfile_3Darray[:, :, (i - 1)] = rasterfile_array
    print "ciclo NaN_natore finito"

    driver.Register()  # l'ho ripetuto ma non dovrebbe servire
    cols = rasterfile.RasterXSize  # numero colonne
    rows = rasterfile.RasterYSize  # numero righe
    bands = rasterfile.RasterCount  # numero bande
    # estraggo il datatype dal file originale
    banda1 = rasterfile.GetRasterBand(1)
    datatype = banda1.DataType

    # creo un file GeoTiff vuoto (NB mentre l'array ragiona in Righe e Colonne GDAL ragiona in colonne e righe)
    out_rasterfile = driver.Create(out_name, cols, rows, bands, datatype)
    # estraggo il sistema di riferimento dal file originale e lo inserisco in quello nuovo
    geoTransform = rasterfile.GetGeoTransform()
    out_rasterfile.SetGeoTransform(geoTransform)
    proj = rasterfile.GetProjection()
    out_rasterfile.SetProjection(proj)

    # riempio le bande del nuovo file con le bande dell'array 3D che avevo creato
    for i in range(1, (rasterfile_nbands + 1)):
        out_rasterfile.GetRasterBand(i).WriteArray(
            rasterfile_3Darray[:, :, (i - 1)], 0, 0)
    print "ciclo scrivi file finito"
    # il comando FlushCache scrive effettivamente tutto quello che ho richiesto sul file richiesto
    out_rasterfile.FlushCache()
    return "file pronto"
def rasterReproject():
    # 获取源数据及栅格信息
    gdal.AllRegister()
    src_data = gdal.Open("smallaster.img")
    # 获取源的坐标信息
    srcSRS_wkt = src_data.GetProjection()
    srcSRS = osr.SpatialReference()
    srcSRS.ImportFromWkt(srcSRS_wkt)
    # 获取栅格尺寸
    src_width = src_data.RasterXSize
    src_height = src_data.RasterYSize
    src_count = src_data.RasterCount
    # 获取源图像的仿射变换参数
    src_trans = src_data.GetGeoTransform()
    OriginLX_src = src_trans[0]
    OriginTY_src = src_trans[3]
    pixl_w_src = src_trans[1]
    pixl_h_src = src_trans[5]

    OriginRX_src = OriginLX_src + pixl_w_src * src_width
    OriginBY_src = OriginTY_src + pixl_h_src * src_height
    # 创建输出图像
    driver = gdal.GetDriverByName("GTiff")
    driver.Register()
    dst_data = driver.Create("tpix1.tif", src_width, src_height, src_count)
    # 设置输出图像的坐标
    dstSRS = osr.SpatialReference()
    dstSRS.ImportFromEPSG(4326)
    # 投影转换
    ct = osr.CoordinateTransformation(srcSRS, dstSRS)
    # 计算目标影像的左上和右下坐标,即目标影像的仿射变换参数
    OriginLX_dst, OriginTY_dst, temp = ct.TransformPoint(
        OriginLX_src, OriginTY_src)
    OriginRX_dst, OriginBY_dst, temp = ct.TransformPoint(
        OriginRX_src, OriginBY_src)

    pixl_w_dst = (OriginRX_dst - OriginLX_dst) / src_width
    pixl_h_dst = (OriginBY_dst - OriginTY_dst) / src_height
    dst_trans = [OriginLX_dst, pixl_w_dst, 0, OriginTY_dst, 0, pixl_h_dst]
    # print outTrans
    dstSRS_wkt = dstSRS.ExportToWkt()
    # 设置仿射变换系数及投影
    dst_data.SetGeoTransform(dst_trans)
    dst_data.SetProjection(dstSRS_wkt)
    # 重新投影
    gdal.ReprojectImage(src_data, dst_data, srcSRS_wkt, dstSRS_wkt,
                        GRA_Bilinear)
    # 创建四级金字塔
    gdal.SetConfigOption('HFA_USE_RRD', 'YES')
    dst_data.BuildOverviews(overviewlist=[2, 4, 8, 16])
    # 计算统计值
    for i in range(0, src_count):
        band = dst_data.GetRasterBand(i + 1)
        band.SetNoDataValue(-99)
        print
        band.GetStatistics(0, 1)
Exemplo n.º 28
0
 def __init__(self, data_path, connection, job_id):
     super(GMSDB, self).__init__()
     gdal.AllRegister()  # TODO: register the ENVI driver only
     self.job = {
         'id': job_id,
         'data_path': data_path,
         'connection': connection,
         'skip_pan': False,
         'skip_thermal': False
     }
Exemplo n.º 29
0
def scale_image(img_path, nodata, outdir=None, datatype_out=gdal.GDT_UInt16):
    """
    Scale the range of values for an image from 0-100.
    First, sets the 2% lowest values to the 2% mark and the 2% highest values to the 98% mark.
        # ALTERNATE VERSION: replaces the brightest values with nodata
    Then takes the output, sets the lowest value to 0 and the highest to 100.
    Writes a float.
    :param img_path:
    :param nodata: no-data value in input image
    :param datatype_out: GDAL data type to save outputs, e.g. gdal.GDT_Float32.
    :return:
    """
    gdal.AllRegister()
    img_src = gdal.Open(img_path)
    cols = img_src.RasterXSize
    rows = img_src.RasterYSize
    bands = img_src.RasterCount
    count_thresh = 0.02

    bands_array = []
    nodata_array = np.zeros([
        rows, cols
    ])  # will store locations where a pixel is no-data. If no-data, then 1.0.
    for band in range(bands):
        temp_array = np.array(
            img_src.GetRasterBand(band + 1).ReadAsArray()).astype(np.float)
        size = temp_array.size
        upper_cutoff = np.sort(temp_array,
                               axis=None)[int(size * (1 - count_thresh))]
        np.place(temp_array, temp_array >= upper_cutoff, nodata)
        bands_array.append(temp_array / np.max(temp_array) * 100.)
        nodata_array = np.where(temp_array == nodata, 1.0, nodata_array)

    # if a pixel is no-data in one band, set to no-data in all
    for band in range(bands):
        # np.place(bands_array[:,:,band], nodata_array==1.0, nodata)    # maybe restore later
        np.place(bands_array[band], nodata_array == 1.0, nodata)

    if outdir:
        dir_target = outdir
    else:
        dir_target = os.path.split(img_path)[0]
    src_filename = os.path.split(img_path)[1]
    outfile = os.path.join(dir_target, src_filename[:-4] + "_scaled.tif")
    target_DS = gdal.GetDriverByName('GTiff').Create(outfile, cols, rows,
                                                     bands, datatype_out)
    for band in range(bands):
        target_DS.GetRasterBand(band + 1).WriteArray(bands_array[band])
    target_DS.SetGeoTransform(img_src.GetGeoTransform())
    target_DS.SetProjection(img_src.GetProjection())
    target_DS.FlushCache()
    target_DS = None
    img_src = None
    return
Exemplo n.º 30
0
    def align_raster(self, raster2align_path):
        gdal.AllRegister()
        logging.info(' *** -- aligning raster ... ')
        input = gdal.Open(raster2align_path, gdalconst.GA_ReadOnly)
        input_proj = input.GetProjection()

        try:
            if not self.reference_set:
                logging.info('        -- setting up reference raster ... ')
                self.set_reference_raster()
                logging.info('           > OK -- cooling down ... ')
                time.sleep(60)
        except:
            logging.info(
                'WARNING: Could not set reference raster for alignment.')

        try:
            outputfile = raster2align_path.split(
                '.tif')[0] + 'a.tif'  # Path to output file
        except:
            logging.info('WARNING: Invalid raster path for alignment: ' +
                         str(raster2align_path))

        try:
            if os.path.exists(outputfile):
                try:
                    logging.info('        -- removing existing file (' +
                                 outputfile + ') ... ')
                    os.remove(outputfile)
                except:
                    logging.info('WARNING: Cannot write aligned output (' +
                                 outputfile + ' is locked).')
            logging.info('        -- set driver ...')
            driver = gdal.GetDriverByName('GTiff')
            driver.Register()
            output = driver.Create(outputfile, self.x_ref, self.y_ref, 1,
                                   self.band_ref.DataType)
            logging.info('        -- apply transformation ...')
            output.SetGeoTransform(self.reference_trans)
            logging.info('        -- apply projection ...')
            output.SetProjection(self.reference_proj)
            logging.info('        -- project image ..')
            gdal.ReprojectImage(input, output, input_proj, self.reference_proj,
                                gdalconst.GRA_Bilinear)
            # dst_ds = driver.CreateCopy(destFile, output, 0)
            new_path = outputfile
            logging.info(' *** -- OK - aligned raster path: ' + new_path +
                         '... cooling down ...')
            time.sleep(5)
        except:
            logging.info('ERROR: Alignment failed.')
            new_path = 'none'
        return new_path