Example #1
0
def test_numpy_rw_5():

    if gdaltest.numpy_drv is None:
        pytest.skip()

    from osgeo import gdalnumeric

    array = gdalnumeric.LoadFile('data/rgbsmall.tif', 35, 21, 1, 1)

    assert array[0][0][0] == 78, 'value read improperly.'

    assert array[1][0][0] == 117, 'value read improperly.'

    assert array[2][0][0] == 24, 'value read improperly.'

    array = gdalnumeric.LoadFile('data/rgbsmall.tif',
                                 buf_xsize=1,
                                 buf_ysize=1,
                                 resample_alg=gdal.GRIORA_Bilinear)
    assert array.shape[0] == 3 and array.shape[1] == 1 and array.shape[2] == 1, \
        'wrong array shape.'
    assert array[0][0][0] == 70 and array[1][0][0] == 97 and array[2][0][0] == 29, \
        'value read improperly.'

    import numpy
    array = numpy.zeros([3, 1, 1], dtype=numpy.uint8)
    ds = gdal.Open('data/rgbsmall.tif')
    ds.ReadAsArray(buf_obj=array, resample_alg=gdal.GRIORA_Bilinear)
    assert array[0][0][0] == 70 and array[1][0][0] == 97 and array[2][0][0] == 29, \
        'value read improperly.'
Example #2
0
def default_loader(filename, root1, root2, root3, root4, root5):
    #print(filename)
    img1 = load_img(root1 + '/' + filename)
    img2 = load_img(root2 + '/' + filename)
    mask = gdalnumeric.LoadFile(root3 + '/' + filename).astype(np.float32)
    mask_branch1 = gdalnumeric.LoadFile(root4 + '/' + filename).astype(
        np.float32)
    mask_branch2 = gdalnumeric.LoadFile(root5 + '/' + filename).astype(
        np.float32)
    #print(mask[mask > 0])
    #img = np.concatenate([img1,img2,img1 - img2],axis=-1)
    mask[mask > 0] = 1
    mask_branch2[mask_branch2 > 0] = 1
    mask_branch1[mask_branch1 > 0] = 1
    '''rot_p = random.random()
    flip_p = random.random()
    if (rot_p < 0.5):
        pass
    elif (rot_p >= 0.5):
        for k in range(3):
            img1[k, :, :] = np.rot90(img1[k, :, :])
            img2[k, :, :] = np.rot90(img2[k, :, :])
        mask = np.rot90(mask)
        mask_branch1 = np.rot90(mask_branch1)
        mask_branch2 = np.rot90(mask_branch2)

    if (flip_p < 0.25):
        pass
    elif (flip_p < 0.5):
        for k in range(3):
            img1[k, :, :] = np.fliplr(img1[k, :, :])
            img2[k, :, :] = np.fliplr(img2[k, :, :])
        mask = np.fliplr(mask)
        mask_branch1 = np.fliplr(mask_branch1)
        mask_branch2 = np.fliplr(mask_branch2)
    elif (flip_p < 0.75):
        for k in range(3):
            img1[k, :, :] = np.flipud(img1[k, :, :])
            img2[k, :, :] = np.flipud(img2[k, :, :])
        mask = np.flipud(mask)
        mask_branch1 = np.flipud(mask_branch1)
        mask_branch2 = np.flipud(mask_branch2)
    elif (flip_p < 1.0):
        for k in range(3):
            img1[k, :, :] = np.fliplr(np.flipud(img1[k, :, :]))
            img2[k, :, :] = np.fliplr(np.flipud(img2[k, :, :]))
        mask = np.fliplr(np.flipud(mask))
        mask_branch1 =np.fliplr( np.flipud(mask_branch1))
        mask_branch2 = np.fliplr(np.flipud(mask_branch2))
    #img=np.transpose(img,[2,0,1])'''
    mask = np.expand_dims(mask, axis=0)
    mask_branch1 = np.expand_dims(mask_branch1, axis=0)
    mask_branch2 = np.expand_dims(mask_branch2, axis=0)
    return img1, img2, mask, mask_branch1, mask_branch2
Example #3
0
def convert(infile,dsfile,pixel,output):
    minX1,maxX1,minY1,maxY1 = getLonlat(infile)
    minX2,maxX2,minY2,maxY2 = getLonlat(dsfile)
    
    data1 = gdalnumeric.LoadFile(infile)
    
    #经度范围
    shape = data1.shape
    x1,x2,y1,y2 = getxy(minX1,maxX1,minY1,maxY1,minX2,maxX2,minY2,maxY2,shape,pixel)
    
    data = data1[x1:x2,y1:y2]
    
    #获取目标的经纬度
    ds = gdal.Open(dsfile)
    geos2 = list(ds.GetGeoTransform())
    minX2,maxX2,minY2,maxY2 = getLonlat(dsfile)
    
    #更新新建数据的经纬度
    geos1 = list(ds.GetGeoTransform())
    
    if minX1<minX2:
        geos1[0] = minX2
    else:
        geos1[0] = minX1
        
    if maxY1<maxY2:
        geos1[3] = maxY1
        
    else:
        geos1[3] = maxY2
        
    #获取更新后的经纬度
    minX1 = geos1[0]
    maxY1 = geos1[3]
    maxX1 = geos1[0]+data.shape[1]*pixel
    minY1 = geos1[3]-data.shape[0]*pixel

    data2 = gdalnumeric.LoadFile(dsfile)
    shape = data2.shape
    x1,x2,y1,y2 = getxy(minX2,maxX2,minY2,maxY2,minX1,maxX1,minY1,maxY1,shape,pixel)

    data2 = np.zeros((shape[0],shape[1]))
    data2 = data2.astype(np.float32)
    data2[data2==0]=np.nan
    data2[x1:x2,y1:y2] = data
    
    
    shape = data2.shape
    driver = gdal.GetDriverByName("GTiff") 
    dataset = driver.Create(output, shape[1], shape[0], 1, gdal.GDT_Float32)
    dataset.SetGeoTransform(geos2)
    dataset.SetProjection(ds.GetProjection()) 
    dataset.GetRasterBand(1).WriteArray(data)
    def __init__(self, country, image_file, resolution, geodatafilepath):
        '''
        Constructor
        '''
        #         self.geodatafilepath = geodatafilepath

        # Raster image to clip\
        self.raster = image_file

        # Polygon shapefile used to clip
        if resolution == 'State/Province':
            self.shp = r'%s\Boundaries\ne_10m_admin_1_states_provinces\Separated by countries\ne_10m_admin_1_states_provinces_admin__%s' % (
                geodatafilepath, country)
            self.countryshp = r'%s\Boundaries\ne_10m_admin_0_countries\Separated by countries\ne_10m_admin_0_countries_ADMIN__%s' % (
                geodatafilepath, country)
        elif resolution == 'Country':
            self.shp = r'%s\Boundaries\ne_10m_admin_0_countries\Separated by countries\ne_10m_admin_0_countries_ADMIN__%s' % (
                geodatafilepath, country)

        # Name of clip raster file(s)
        self.output = r'%s\%s\Provinces\Clip\\' % (geodatafilepath, country)

        # Load the source data as a gdalnumeric array
        self.srcArray = gdalnumeric.LoadFile(self.raster)

        # Also load as a gdal image to get geotransform
        # (world file) info
        self.srcImage = gdal.Open(self.raster)

        if resolution == 'State/Province':
            # Apply initial clip to work with country-level raster, instead of global (improves speed)
            self.raster = self.initialClip()
            self.srcArray = gdalnumeric.LoadFile(self.raster)
            self.srcImage = gdal.Open(self.raster)

        # Output resolution to .csv
        geoMatrix = self.srcImage.GetGeoTransform()
        xres = geoMatrix[1]
        yres = geoMatrix[5]
        res_array = np.array([xres, yres])
        res_array = np.reshape(res_array, (1, 2))
        res = pandas.DataFrame(res_array, columns=('XRes', 'YRes'))

        # Start country directory if it doesn't already exist
        if not os.path.exists('%s\%s' % (geodatafilepath, country)):
            os.mkdir('%s\%s' % (geodatafilepath, country))
        res.to_csv('%s\%s\%sResolution.csv' %
                   (geodatafilepath, country, country),
                   columns=('XRes', 'YRes'),
                   index=False)
def Lake_Extract(TM1, TM4, Mask, outpath, TraceLake, Slope):
    """ This function extracts Lake pixels based on an
    input set of 'training lakes' provided as a tif file"""

    t = gdal.Open(TM1)
    cs = t.GetProjection()
    cs_sr = osr.SpatialReference()
    cs_sr.ImportFromWkt(cs)
    tmptif = t.ReadAsArray()
    cols = tmptif.shape[1]
    rows = tmptif.shape[0]
    gt = t.GetGeoTransform()
    del t, tmptif

    TM1_Arr = gdalnumeric.LoadFile(TM1).astype(float)
    TM4_Arr = gdalnumeric.LoadFile(TM4).astype(float)
    TM1_Arr[(TM1_Arr <= 0)] = np.nan
    TM4_Arr[(TM4_Arr <= 0)] = np.nan

    Shadow = gdalnumeric.LoadFile(Mask).astype(float)
    Shadow[(Shadow <= 0)] = np.nan

    Ratio = (TM4_Arr - TM1_Arr) / (TM4_Arr + TM1_Arr)
    del TM1_Arr, TM4_Arr

    Lakes = gdalnumeric.LoadFile(TraceLake).astype(float)
    target = np.where(Lakes == 1)
    LRatio = np.nanmean(Ratio[target]) + 0.05

    Ratio[(Shadow == 1)] = np.nan
    Ratio[(Ratio < LRatio)] = 1
    Ratio[(Ratio != 1)] = np.nan

    Slope_Arr = gdalnumeric.LoadFile(Slope).astype(float)
    Ratio[(Slope_Arr > 3)] = np.nan
    del Lakes, Shadow, Slope_Arr

    driver_tif = gdal.GetDriverByName('GTiff')
    driver_tif.Register()
    if os.path.exists(outpath):
        os.remove(outpath)
    outRaster = driver_tif.Create(outpath, cols, rows, 1, gdal.GDT_Byte)
    outRaster.SetGeoTransform(gt)
    outRaster.SetProjection(cs)
    outband = outRaster.GetRasterBand(1)
    outband.WriteArray(Ratio,0,0)
    outband.FlushCache()
    del driver_tif, outRaster, Ratio
Example #6
0
def numpy_rw_5():

    if gdaltest.numpy_drv is None:
        return 'skip'

    from osgeo import gdalnumeric

    array = gdalnumeric.LoadFile('data/rgbsmall.tif', 35, 21, 1, 1)

    if array[0][0][0] != 78:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    if array[1][0][0] != 117:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    if array[2][0][0] != 24:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    return 'success'
Example #7
0
def Histmaker(wsid,clipped_shp,TIF):
    """Takes single glacier, clips elevation, converts to array,
    creates histogram, returns bins and Histogram values"""
    out = TMP + 'tmp.tif'
    if os.path.exists(out):
        os.remove(out)
    d = ogr.GetDriverByName('ESRI Shapefile')
    ds = d.Open(clipped_shp,0)
    l = ds.GetLayer()
    fc = l.GetFeatureCount()
    ds.Destroy()
    del d, l
    if fc == 0: #If there are no shapes in the input, skip processing
        return [], []
    else: #Clip the DEM/Aspect/Slope/etc Tif to the input feature
        gdal_comm = ['gdalwarp', '-cutline', clipped_shp, '-crop_to_cutline', TIF, out]
        subprocess.call(gdal_comm)
        while os.path.exists(out) == False:
            time.sleep(1)

        Arr = gdalnumeric.LoadFile(out).astype(float)
        try:
            os.remove(out)
        except:
            pass
        Arr = Arr[(Arr > -1000)] #Remove nodata
        Arr = Arr[~np.isnan(Arr)]
        #1000 bins, 0:10:10000
        bins = np.arange(0, 10000, 10)
        Hist, bin_edges = np.histogram(Arr, bins)
        bins_list = bins.tolist()
        Hist_list = Hist.tolist()
        del Hist, bin_edges, bins, Arr

        return bins_list, Hist_list
Example #8
0
def load_dem_tif(dem_fname):
    """
    Load GeoTIFF with gdal and import into landlab
    """
    t = gdal.Open(dem_fname)
    gt = t.GetGeoTransform()
    cs = t.GetProjection()
    cs_sr = osr.SpatialReference()
    cs_sr.ImportFromWkt(cs)
    #open DEM
    dem = gdalnumeric.LoadFile(dem_fname).astype(float)
    cols = dem.shape[1]
    nr_of_x_cells = cols
    rows = dem.shape[0]
    nr_of_y_cells = rows
    if gt[5] < 0:
        dem = np.flipud(dem)
    idx0 = np.where(dem.ravel() == 0)[0]

    #get UTM coordinates into array:
    ul_x = gt[0]
    ul_y = gt[3]
    utm_x = np.arange(ul_x, ul_x + (gt[1] * (cols + 1)), gt[1])
    utm_y = np.arange(ul_y, ul_y + (gt[5] * (rows + 1)), gt[5])

    mg = RasterModelGrid((rows, cols), abs(gt[1]))
    mg.set_closed_boundaries_at_grid_edges(False, False, False, False)
    _ = mg.add_field('topographic__elevation', dem, at='node')

    return mg, gt, cs, nr_of_x_cells, nr_of_y_cells, idx0, utm_x, utm_y
Example #9
0
def ImageToClassify(imgClass, Bool):
    imgarray = gdalnumeric.LoadFile(imgClass)
    imgOriginal = np.concatenate(imgarray.T)
    img = gdal.Open(imgClass)
    shpOriginal = imgarray.shape

    if Bool == True:
        imgaux = img.ReadAsArray()
        imgaaux = imgaux.astype(float)
        imgOriginal = gdal.GetDriverByName('GTiff').Create(
            'newbands.tif', imgarray.shape[2], imgarray.shape[1], 5,
            gdal.GDT_UInt16)
        imgOriginal.GetRasterBand(1).WriteArray(
            (((imgaaux[3] - imgaaux[0]) /
              (imgaaux[3] + imgaaux[0])) + 1) * 127.5)
        imgOriginal.GetRasterBand(2).WriteArray(
            (((imgaaux[1] - imgaaux[0]) /
              (imgaaux[1] + imgaaux[0])) + 1) * 127.5)
        imgOriginal.GetRasterBand(4).WriteArray(
            ((imgaaux[1] - imgaaux[2]) /
             (imgaaux[1] + imgaaux[2]) + 1) * 127.5)
        imgOriginal.GetRasterBand(4).WriteArray(
            ((imgaaux[0] - imgaaux[2]) /
             (imgaaux[0] + imgaaux[2]) + 1) * 127.5)
        imgOriginal.GetRasterBand(5).WriteArray(
            (((imgaaux[3] - imgaaux[1]) / (imgaaux[3] + imgaaux[1]) + 1) *
             127.5))
        imgOriginal = imgOriginal.ReadAsArray()
        shpOriginal = imgOriginal.shape
        imgOriginal = np.concatenate(imgOriginal.T)

    projection = img.GetProjection()
    geotrans = img.GetGeoTransform()
    return projection, geotrans, imgOriginal, shpOriginal
Example #10
0
def main(shapefile_path, raster_path):
    # Load the source data as a gdalnumeric array
    srcArray = gdalnumeric.LoadFile(raster_path)

    # Also load as a gdal image to get geotransform
    # (world file) info
    pdb.set_trace()

    srcImage = gdal.Open(raster_path)
    geoTrans = srcImage.GetGeoTransform()

    # Create an OGR layer from a boundary shapefile
    shapef = ogr.Open(shapefile_path)
    lyr = shapef.GetLayer(
        os.path.split(os.path.splitext(shapefile_path)[0])[1])

    poly = lyr.GetNextFeature()

    # Convert the layer extent to image pixel coordinates
    minX, maxX, minY, maxY = lyr.GetExtent()
    ulX, ulY = world2Pixel(geoTrans, minX, maxY)
    lrX, lrY = world2Pixel(geoTrans, maxX, minY)

    # Calculate the pixel size of the new image
    pxWidth = int(lrX - ulX)
    pxHeight = int(lrY - ulY)

    clip = srcArray[:, ulY:ulY + 1000, ulX:ulX + 1000]

    # Save as an 8-bit jpeg for an easy, quick preview
    clip = clip.astype(gdalnumeric.uint8)
    gdalnumeric.SaveArray(clip, "beijing3.jpg", format="JPEG")
Example #11
0
def numpy_rw_5():

    if gdaltest.numpy_drv is None:
        return 'skip'

    from osgeo import gdalnumeric

    array = gdalnumeric.LoadFile('data/rgbsmall.tif', 35, 21, 1, 1)

    if array[0][0][0] != 78:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    if array[1][0][0] != 117:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    if array[2][0][0] != 24:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    array = gdalnumeric.LoadFile('data/rgbsmall.tif',
                                 buf_xsize=1,
                                 buf_ysize=1,
                                 resample_alg=gdal.GRIORA_Bilinear)
    if array.shape[0] != 3 or array.shape[1] != 1 or array.shape[2] != 1:
        print(array.shape)
        gdaltest.post_reason('wrong array shape.')
        return 'fail'
    if array[0][0][0] != 70 or array[1][0][0] != 97 or array[2][0][0] != 29:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    import numpy
    array = numpy.zeros([3, 1, 1], dtype=numpy.uint8)
    ds = gdal.Open('data/rgbsmall.tif')
    ds.ReadAsArray(buf_obj=array, resample_alg=gdal.GRIORA_Bilinear)
    if array[0][0][0] != 70 or array[1][0][0] != 97 or array[2][0][0] != 29:
        print(array)
        gdaltest.post_reason('value read improperly.')
        return 'fail'

    return 'success'
Example #12
0
def get_pixel_count_by_pixel_values(layer, band, pixel_values=None):
    if pixel_values is None:
        pixel_values = get_pixel_values(layer, band)

    raster_numpy = gdalnumeric.LoadFile(get_file_path_of_layer(layer))
    pixel_counts = []
    for pixel_value in pixel_values:
        pixel_counts.append((raster_numpy == int(pixel_value)).sum())

    return dict(zip(pixel_values, pixel_counts))
Example #13
0
def load_tiff(file):
    """
	Load a GeoTiff raster keeping NDV values using a masked array
	Usage:
			data=LoadTiffRaster(file)
	"""
    NDV, xsize, ysize, GeoT, Projection, DataType = GetGeoInfo(file)
    data = gdalnumeric.LoadFile(file)
    data = np.ma.masked_array(data, mask=data == NDV, fill_value=-np.inf)
    return data
Example #14
0
def default_loader_val(filename, root1, root2, root3, root4, root5):
    #print(filename)
    img1 = load_img(root1 + '/' + filename)
    img2 = load_img(root2 + '/' + filename)
    mask = gdalnumeric.LoadFile(root3 + '/' + filename).astype(np.float32)
    mask_branch1 = gdalnumeric.LoadFile(root4 + '/' + filename).astype(
        np.float32)
    mask_branch2 = gdalnumeric.LoadFile(root5 + '/' + filename).astype(
        np.float32)
    #print(mask[mask > 0])
    #img = np.concatenate([img1,img2,img1 - img2],axis=-1)
    mask[mask > 0] = 1
    mask_branch2[mask_branch2 > 0] = 1
    mask_branch1[mask_branch1 > 0] = 1
    #img=np.transpose(img,[2,0,1])
    mask = np.expand_dims(mask, axis=0)
    mask_branch1 = np.expand_dims(mask_branch1, axis=0)
    mask_branch2 = np.expand_dims(mask_branch2, axis=0)
    return img1, img2, mask, mask_branch1, mask_branch2
Example #15
0
def load_img(path):
    img = gdalnumeric.LoadFile(path)
    #img = np.transpose(img,[1,2,0])
    img = np.array(img, dtype="float")
    '''B, G, R = cv2.split(img)
    B = (B - np.mean(B))
    G = (G - np.mean(G))
    R = (R - np.mean(R))
    img_new = cv2.merge([B, G, R])'''
    img_new = img / 255.0
    return img_new
Example #16
0
def merge_floods(dirPaese):

    os.chdir(dirPaese)
    iso_paese = dirPaese.split("\\")[-1]
    print iso_paese
    stringa_ricerca = "*.tif"
    lista_floods_rcl = glob.glob(stringa_ricerca)
    print lista_floods_rcl
    for floodo in lista_floods_rcl:
        rp = floodo.split("_")[1].split(".")[0]
        if rp == '25':
            im_rp_25 = floodo
        elif rp == '50':
            im_rp_50 = floodo
        elif rp == '100':
            im_rp_100 = floodo
        elif rp == '200':
            im_rp_200 = floodo
        elif rp == '500':
            im_rp_500 = floodo
        elif rp == '1000':
            im_rp_1000 = floodo

    ar25 = gdalnumeric.LoadFile(im_rp_25).astype(np.int16)
    ar50 = gdalnumeric.LoadFile(im_rp_50).astype(np.int16)
    ar100 = gdalnumeric.LoadFile(im_rp_100).astype(np.int16)
    ar200 = gdalnumeric.LoadFile(im_rp_200).astype(np.int16)
    ar500 = gdalnumeric.LoadFile(im_rp_500).astype(np.int16)
    ar1000 = gdalnumeric.LoadFile(im_rp_1000).astype(np.int16)
    somma = ar25 + ar50 + ar100 + ar200 + ar500 + ar1000
    gdalnumeric.SaveArray(somma,
                          dirPaese + "\\" + iso_paese + "_all_rp.tif",
                          format="GTiff",
                          prototype=im_rp_1000)
Example #17
0
def test_numpy_rw_2():

    from osgeo import gdalnumeric

    array = gdalnumeric.LoadFile('data/utmsmall.tif')
    assert array is not None, 'Failed to load utmsmall.tif into array'

    ds = gdalnumeric.OpenArray(array)
    assert ds is not None, 'Failed to open memory array as dataset.'

    bnd = ds.GetRasterBand(1)
    assert bnd.Checksum() == 50054, 'Didnt get expected checksum on reopened file'
    ds = None
Example #18
0
def main(shapefile_path, raster_path, fileName):
    # 读取栅格影像路径
    srcArray = gdalnumeric.LoadFile(raster_path)
    # 读取栅格据
    srcImage = gdal.Open(raster_path)
    geoTrans = srcImage.GetGeoTransform()
    # 读取矢量图形数据路径
    shapef = ogr.Open(shapefile_path)
    lyr = shapef.GetLayer(
        os.path.split(os.path.splitext(shapefile_path)[0])[1])
    poly = lyr.GetNextFeature()
    # 矢量图形坐标换算
    minX, maxX, minY, maxY = lyr.GetExtent()
    ulX, ulY = world2Pixel(geoTrans, minX, maxY)
    lrX, lrY = world2Pixel(geoTrans, maxX, minY)
    # 计算新生成图像大小
    pxWidth = int(lrX - ulX)
    pxHeight = int(lrY - ulY)
    clip = srcArray[:, ulY:lrY, ulX:lrX]
    xoffset = ulX
    yoffset = ulY
    print("Xoffset, Yoffset = ( %f, %f )" % (xoffset, yoffset))
    # 创建geomatrix
    geoTrans = list(geoTrans)
    geoTrans[0] = minX
    geoTrans[3] = maxY
    # 创建一张黑白遮罩图像,将矢量点集映射到像素上
    points = []
    pixels = []
    geom = poly.GetGeometryRef()
    pts = geom.GetGeometryRef(0)
    for p in range(pts.GetPointCount()):
        points.append((pts.GetX(p), pts.GetY(p)))
    for p in points:
        pixels.append(world2Pixel(geoTrans, p[0], p[1]))
    rasterPoly = Image.new("L", (pxWidth, pxHeight), 1)
    rasterize = ImageDraw.Draw(rasterPoly)
    rasterize.polygon(pixels, 0)
    mask = imageToArray(rasterPoly)
    # 裁剪遮罩
    clip = gdalnumeric.choose(mask, (clip, 0)).astype(gdalnumeric.int16)
    clip = clip.astype(gdalnumeric.int16)
    #加载GTiff驱动,存储结果为tiff图像
    gtiffDriver = gdal.GetDriverByName('GTiff')
    if gtiffDriver is None:
        raise ValueError("Can't load GeoTiff Driver")
    print(type(clip))
    gdalnumeric.SaveArray(clip, fileName, format="GTiff")
    print("裁剪完毕")
    gdal.ErrorReset()
def main(delete):
    source=r"D:\Program Files\Python学习文档\samples\NDVI\farm.tif"
    target=r"D:\Program Files\Python学习文档\samples\NDVI\Clip_farm.tif"
    shp=r"D:\Program Files\Python学习文档\samples\NDVI\field.shp"
    output=r"D:\Program Files\Python学习文档\samples\NDVI\clip_ndvi.tif"
    compute_band(source,target)
    srcArray = gdalnumeric.LoadFile(target)
    srcImage = gdal.Open(target)
    geoTrans = srcImage.GetGeoTransform()
    shapef = ogr.Open(shp)
    lyr = shapef.GetLayer(os.path.split(os.path.splitext(shp)[0] )[1] )
    poly = lyr.GetNextFeature()
    minX, maxX, minY, maxY = lyr.GetExtent()
    ulX, ulY = World2Pixel(geoTrans, minX, maxY)
    lrX, lrY = World2Pixel(geoTrans, maxX, minY)
    pxWidth = int(lrX - ulX)
    pxHeight = int(lrY - ulY)
    clip = srcArray[ulY:lrY, ulX:lrX]
    xoffset =  ulX
    yoffset =  ulY
    print("Xoffset, Yoffset = ( %f, %f )" % ( xoffset, yoffset ))
    geoTrans = list(geoTrans)
    geoTrans[0] = minX
    geoTrans[3] = maxY
    points = []
    pixels = []
    geom = poly.GetGeometryRef()
    pts = geom.GetGeometryRef(0)
    for p in range(pts.GetPointCount()):
      points.append((pts.GetX(p), pts.GetY(p)))
    for p in points:
      pixels.append(World2Pixel(geoTrans, p[0], p[1]))
    rasterPoly = Image.new("L", (pxWidth, pxHeight), 1)
    rasterize = ImageDraw.Draw(rasterPoly)
    rasterize.polygon(pixels, 0)
    mask = ImageToArray(rasterPoly)
    clip = gdalnumeric.choose(mask,(clip, 0)).astype(gdalnumeric.uint8)
    gtiffDriver = gdal.GetDriverByName( 'GTiff' )
    if gtiffDriver is None:
        raise ValueError("Can't find GeoTiff Driver")
    gtiffDriver.CreateCopy(output,OpenArray( clip, prototype_ds=target, xoff=xoffset, yoff=yoffset ))
    if delete and os.path.exists(target):
        os.remove(target)
Example #20
0
def getXYgrid(dem):
    """
    takes input geo raster and outputs numpy arrays of X and Y coordinates (center of pixel) 
    """
    # create X and Y
    ds = gdal.Open(dem)
    s = gdalnumeric.LoadFile(dem)
    cols = s.shape[1]
    rows = s.shape[0]
    gt = ds.GetGeoTransform()
    ds = None
    # size of grid (minx, stepx, 0, maxy, 0, -stepy)
    minx, maxy = gt[0], gt[3]
    maxx, miny = gt[0] + gt[1] * cols, gt[3] + gt[5] * rows
    step = gt[1]
    # center of pixel
    ygrid = np.arange(miny + (step / 2), maxy, step)
    xgrid = np.arange(minx + (step / 2), maxx, step)
    xgrid, ygrid = np.meshgrid(xgrid, ygrid)
    ygrid = np.flipud(ygrid)

    return xgrid, ygrid
Example #21
0
def readGDAL2numpy(rasterPath, return_geoInformation=False):
    try:
        ds = gdal.Open(rasterPath)
    except RuntimeError:
        print('Unable to open input file')
        sys.exit(1)

    data = gdalnumeric.LoadFile(rasterPath, False)
    noDataVal = ds.GetRasterBand(1).GetNoDataValue()
    try:
        if data.dtype in ['float16', 'float32', 'float64'
                          ] and noDataVal is not None:
            data[data == noDataVal] = np.NaN
    except:
        print("Issue in no data value")

    if return_geoInformation == False:
        return data
    else:
        geoTransform = ds.GetGeoTransform()
        projection = ds.GetProjection()
        return data, geoTransform, projection
Example #22
0
def imageSpilt_gray(imgName="C:/Users/fitzpc/Desktop/timg.jpg",
                    savePath="C:/Users/fitzpc/Desktop/spilt_img"):
    img = gdalnumeric.LoadFile(imgName)
    h, w = img.shape
    step = 0
    imgs_array = np.zeros([SIZE, SIZE], dtype=np.uint8)
    #labels_array = np.zeros([SIZE, SIZE], dtype=np.uint8)
    for i in range(0, h, STRIDE):
        for j in range(0, w, STRIDE):
            if (i + SIZE <= h and j + SIZE <= w):
                imgs_array = img[i:i + SIZE, j:j + SIZE]
                # labels_array=label[i:i+SIZE,j:j+SIZE]
            elif (i + SIZE <= h and j + SIZE > w):
                imgs_array = img[i:i + SIZE, w - SIZE:]
            elif (i + SIZE > h and j + SIZE <= w):
                imgs_array = img[h - SIZE:, j:j + SIZE]
            elif (i + SIZE > h and j + SIZE > w):
                imgs_array = img[h - SIZE:, w - SIZE:]
            step = step + 1
            cv2.imwrite(savePath + '/' + str(step) + '.png', imgs_array)
        # cv2.imwrite(labels_path + '/' + str(step) + '.png', labels_array)
    print('ok')
Example #23
0
def get_average_pixels_aws(dirpath, lat, long):

    client = boto3.client('s3')  #low-level functional API
    resource = boto3.resource('s3')  #high-level object-oriented API
    my_bucket = resource.Bucket('landsat-pds')
    files = list(my_bucket.objects.filter(Prefix=dirpath))
    is_image = False

    x, y, _, __ = utm.from_latlon(lat, long)

    image = np.zeros((1, 12))

    j = 0
    for i, f in enumerate(files):
        filename = f.key
        if filename.endswith(".TIF") or filename.endswith(".tif"):

            # print(os.path.join(directory, filename))
            srcArray = gdalnumeric.LoadFile(filename)

            # Also load as a gdal image to get geotransform
            # (world file) info
            srcImage = gdal.Open(filename)
            geoTrans = srcImage.GetGeoTransform()

            #gets 10^2 acres surrounding lat long
            ulX, ulY = world2Pixel(geoTrans, x + 100, y + 100)
            lrX, lrY = world2Pixel(geoTrans, x - 100, y - 100)

            clip = srcArray[lrY:lrY + 7, ulX:ulX + 7]

            image[0, j] = np.mean(clip)
            j += 1

        else:
            continue

    return image
Example #24
0
def numpy_rw_2():

    if gdaltest.numpy_drv is None:
        return 'skip'

    from osgeo import gdalnumeric

    array = gdalnumeric.LoadFile('data/utmsmall.tif')
    if array is None:
        gdaltest.post_reason('Failed to load utmsmall.tif into array')
        return 'fail'

    ds = gdalnumeric.OpenArray(array)
    if ds is None:
        gdaltest.post_reason('Failed to open memory array as dataset.')
        return 'fail'

    bnd = ds.GetRasterBand(1)
    if bnd.Checksum() != 50054:
        gdaltest.post_reason('Didnt get expected checksum on reopened file')
        return 'fail'
    ds = None

    return 'success'
Example #25
0
def get_image(dirpath, lat, long):
    #opens image from local machine and returns croped image

    directory = os.fsencode(dirpath)
    is_image = False

    x, y, _, __ = utm.from_latlon(lat, long)

    image = np.zeros((12, 7, 7))

    j = 0
    for i, file in enumerate(os.listdir(directory)):
        filename = os.fsdecode(file)
        if filename.endswith(".TIF") or filename.endswith(".tif"):

            # print(os.path.join(directory, filename))
            srcArray = gdalnumeric.LoadFile(dirpath + '/' + filename)

            # Also load as a gdal image to get geotransform
            # (world file) info
            srcImage = gdal.Open(dirpath + '/' + filename)
            geoTrans = srcImage.GetGeoTransform()

            #gets 10^2 acres surrounding lat long
            ulX, ulY = world2Pixel(geoTrans, x + 100, y + 100)
            lrX, lrY = world2Pixel(geoTrans, x - 100, y - 100)

            clip = srcArray[lrY:lrY + 7, ulX:ulX + 7]

            image[j, :, :] = clip
            j += 1

        else:
            continue

    return image
Example #26
0
def merge_floods(paese):
    os.chdir("C:/data/tools/sparc/input_data/flood/masks/")
    stringa_ricerca = "%s*.tif" % paese
    lista_floods_rcl = glob.glob(stringa_ricerca)
    print lista_floods_rcl
    im_rp_25 = lista_floods_rcl[3]
    im_rp_50 = lista_floods_rcl[5]
    im_rp_100 = lista_floods_rcl[1]
    im_rp_200 = lista_floods_rcl[2]
    im_rp_500 = lista_floods_rcl[4]
    im_rp_1000 = lista_floods_rcl[0]
    ar25 = gdalnumeric.LoadFile(im_rp_25).astype(np.int16)
    ar50 = gdalnumeric.LoadFile(im_rp_50).astype(np.int16)
    ar100 = gdalnumeric.LoadFile(im_rp_100).astype(np.int16)
    ar200 = gdalnumeric.LoadFile(im_rp_200).astype(np.int16)
    ar500 = gdalnumeric.LoadFile(im_rp_500).astype(np.int16)
    ar1000 = gdalnumeric.LoadFile(im_rp_1000).astype(np.int16)
    somma = ar25 + ar50 + ar100 + ar200 + ar500 + ar1000
    gdalnumeric.SaveArray(somma,
                          "C:/data/tools/sparc/input_data/flood/merged/" +
                          paese + "_all_rp.tif",
                          format="GTiff",
                          prototype=im_rp_1000)
        "lifespan":tr.lifespan(), "trlen":tr.trackLength(), "avgarea":tr.avgArea(), \
        "mcc":tr.mcc(), "genLat":tr.data.loc[tr.data['type'] != 0]['lat'].iloc[0], \
        "genLon":tr.data.loc[tr.data['type'] != 0]['long'].iloc[0]},])
        sdf = sdf.append(row, ignore_index=True, sort=True)

pd.to_pickle(
    st, outpath + "/" + name + "_" + V + "/SOS_systemtracks_" + SDAY + "_" +
    EDAY + ".pkl")
sdf.to_csv(outpath + "/" + name + "_" + V + "/SOS_AggregatedStats" + SDAY +
           "_" + EDAY + ".csv",
           index=0)

########## Calculate aggregate stats for these storms############
# Read in attributes of reference files
ref = gdal.Open(suppath + "/" + latN, gdalconst.GA_ReadOnly)
refA = gdalnumeric.LoadFile(suppath + "/" + latN)
cellsize = ref.GetGeoTransform()[1]  # cell size
n = md.daysBetweenDates(starttime,
                        endtime) * 24 / hrs  # Number of valid observations

print("events")
fields1 = md.aggregateEvents([st, [], []], 'system', 0, refA.shape)
print("tracks")
fields2 = md.aggregateTrackWiseStats(st, [0, 0, 0], refA.shape)
statsPDF = fields2[0]
print("points")
try:
    fields3 = md.aggregatePointWiseStats(st, n, refA.shape)
except:
    fields3 = [np.zeros_like(refA), np.zeros_like(refA), np.zeros_like(refA), \
    np.zeros_like(refA), np.zeros_like(refA), np.zeros_like(refA), \
Example #28
0
                   "_AggregatedStats" + SDAY + "_" + EDAY + ".csv")
pdf2 = pd.read_csv(inpath + "/" + name + "_" + V + "/" + T2 +
                   "_AggregatedStats" + SDAY + "_" + EDAY + ".csv")

# Identify the number of storms in each category
n1 = pdf1.shape[0]
n2 = pdf2.shape[0]

# Create Gaussian Kernel
x = np.arange(-1 * kSize, kSize + 1, 1)
y = np.arange(-1 * kSize, kSize + 1, 1)
xx, yy = np.meshgrid(x, y)
k = np.exp(-1 / (2 * sigma**2) * (xx**2 + yy**2))

# Identify Locations of Interest
td1 = gdalnumeric.LoadFile(inpath + "/" + name + "_" + V + "/" + T1 +
                           "_trkdenField" + SDAY + "_" + EDAY + ".tif")
td2 = gdalnumeric.LoadFile(inpath + "/" + name + "_" + V + "/" + T2 +
                           "_trkdenField" + SDAY + "_" + EDAY + ".tif")
ref = gdal.Open(suppath + "/" + latN)

########### AGGREGATE CYCLONE LOCATIONS ############
print("Calculate Observed Dissimilarity")
# create an list to store the counts
counts1 = []  # For First Category
counts2 = []  # For Second Category

# Find each storm for PDF #1
print(T1 + " Count: " + str(n1))

for i in range(n1):
    if i % 10 == 0:
    def distribute_locs(self):

        # Create an OGR layer from a boundary shapefile
        DriverName = "ESRI Shapefile"
        driver = ogr.GetDriverByName(DriverName)
        shapef = driver.Open('%s.shp' % self.shp)
        lyr = shapef.GetLayer()

        # Province names from portfolio data
        province_names = self.portfile[:, 0]

        # Province names from boundary shapefile
        shp_province_names = []
        for i in range(lyr.GetFeatureCount()):
            province_feature = lyr.GetFeature(i)
            shp_province_names.append(
                province_feature.GetField(
                    province_feature.GetFieldIndex('name')))

        # Pair province names with portfolio data
        cnt = dict(zip(self.portfile[:, 0], self.portfile[:, 1]))
        tiv = dict(zip(self.portfile[:, 0], self.portfile[:, 2]))

        # Correct any mismatches between province lists
        province_names, corrected_pairs = self.provinceQC(
            province_names, shp_province_names)

        # Replace any mismatched provinces with correct province names
        for i in corrected_pairs:
            if corrected_pairs[
                    i] == 'None':  # Remove any data that does not match known provinces
                cnt.pop(i)
                tiv.pop(i)
                continue
            cnt[corrected_pairs[i]] = cnt.pop(i)
            tiv[corrected_pairs[i]] = tiv.pop(i)

        if self.resolution == 'Country':
            cnt[self.country] = int(cnt[self.country])
            tiv[self.country] = float(tiv[self.country])
        for key in cnt:  # Clean number strings, remove commas, set dtype
            if self.isNumber(cnt[key]):
                cnt[key] = int(cnt[key])
            else:
                cnt[key] = int(cnt[key].replace(',', ''))
            if self.isNumber(tiv[key]):
                tiv[key] = float(tiv[key])
            else:
                tiv[key] = float(tiv[key].replace(',', ''))

        # Check to see if output directory exists - clear if any old data is present
        output = r'%s\%s\Provinces\Points' % (self.geodatafilepath,
                                              self.country)
        if not os.path.exists(output):
            os.makedirs(output)
        else:
            filelist = [f for f in os.listdir(output) if f.endswith('.csv')]
            os.chdir(output)
            for f in filelist:
                os.remove(f)

        for province in province_names:
            lyr.SetAttributeFilter("name = '%s'" % province)
            poly = lyr.GetNextFeature()

            # Get extent of polygon for country/state/province
            try:
                minX, maxX, minY, maxY = poly.GetGeometryRef().GetEnvelope()
            except:  # Check for province name not matching shapefile data
                print("Invalid province name: ", province)
                continue

            if maxY > 75:
                maxY = 75

            # Load clipped light image file
            try:
                provArray = gdalnumeric.LoadFile(
                    r'%s\%s\Provinces\Clip\%s.tif' %
                    (self.geodatafilepath, self.country, province))
            except:  # Check for image file not being produced
                print("Invalid province name: ", province)
                province = input(
                    "Please input correct province name, or None if not available. "
                )
                if province == 'None' or province == 'none':
                    continue
                provArray = gdalnumeric.LoadFile(
                    r'%s\%s\Provinces\Clip\%s.tif' %
                    (self.geodatafilepath, self.country, province))

            # Calculate average value per location
            if self.resolution == 'State/Province':
                try:
                    avg_TIV = np.float(tiv[province]) / cnt[province]
                except ZeroDivisionError:
                    avg_TIV = 0
            elif self.resolution == 'Country':
                avg_TIV = np.float(self.portfile[0, 2]) / np.float(
                    self.portfile[0, 1])

            # Produce weighted distribution
            cumdist = list(self.accumulate(provArray.flat))

            # Check for light data not existing - generally uninhabited islands, etc.
            if np.max(cumdist) == 0:
                print(province, "does not have any available light data.")
                continue

            # Weighted distribution of lat/lon, randomly distributed within ~1km grid resolution
            # Add some variability to average TIV - need to refine with better data.
            loc = np.zeros((cnt[province], 2))
            locdist = np.zeros((cnt[province], 3))
            for i in range(cnt[province]):
                loc[i, :] = self.setpt(provArray, cumdist, minX, minY, maxX,
                                       maxY)
                locdist[i, 1] = loc[i, 1] - random.random() * self.xres
                locdist[i, 0] = loc[i, 0] + random.random() * self.yres
                locdist[i,
                        2] = np.random.normal(loc=avg_TIV, scale=avg_TIV /
                                              10.)  # Note: refine std dev est

            # Scale randomly-produced insured values to match known total for region
            sum_TIV = np.sum(locdist[:, 2])
            scale_TIV = np.float(tiv[province]) / sum_TIV
            locdist[:, 2] = locdist[:, 2] * scale_TIV

            locdist_pandas = pandas.DataFrame(locdist,
                                              columns=['Lat', 'Lon', 'TIV'])
            locdist_pandas['State/Province'] = province
            locdist_pandas['Country'] = self.country
            locdist_pandas['LOB'] = self.LOB
            locdist_pandas['Peril'] = self.peril
            if self.resolution == 'Country':
                locdist_pandas.to_csv(
                    '%s\%s.csv' % (output, province),
                    columns=['Lat', 'Lon', 'TIV', 'Country', 'LOB', 'Peril'],
                    index=False)
            else:
                locdist_pandas.to_csv('%s\%s.csv' % (output, province),
                                      columns=[
                                          'Lat', 'Lon', 'TIV',
                                          'State/Province', 'Country', 'LOB',
                                          'Peril'
                                      ],
                                      index=False)
Example #30
0
    def processQuality(self, bandlayer, qualityInfo):
        if bandlayer not in self.bandfiles:
            raise Exception('Given quality band is not available!')
        qualityFile = self.bandfiles[bandlayer]
        LOGGER.info('QualityFile '+str(qualityFile))
        qualityArray = gdalnumeric.LoadFile(qualityFile)
        qualityValues = np.unique(qualityArray)
        qualityBand = self.bands[bandlayer]

        if not os.path.exists(self.publishPath+'/data/mask'):
            os.makedirs(self.publishPath+'/data/mask')
        if not os.path.exists(self.publishPath+'/data/output'):
            os.makedirs(self.publishPath+'/data/output')

        finalfiles = dict()
        for key, band in self.bands.items():
            if band['imagetype'] == 'qualityInformation':
                continue

            dataFile = self.bandfiles[key]
            maskFile = self.publishPath+'/data/mask/'+os.path.basename(dataFile)
            dataFile = self.publishPath+'/data/output/'+os.path.basename(dataFile)

            dataDS = gdal.Open(self.bandfiles[key])
            dataBand = dataDS.GetRasterBand(1)
            dataArray = dataBand.ReadAsArray(0, 0, dataDS.RasterXSize, dataDS.RasterYSize)
            dataNoData = dataBand.GetNoDataValue()

            maskArray = np.copy(dataArray)
            maskArray[:] = dataNoData

            if qualityBand['quality_datatype'] == 'int':
                values = qualityInfo.split(';')   #0;1 (e.g., good data and marginal data for MOD13Q1)
                for quality in qualityValues:
                    if str(quality) not in values:
                        dataArray[qualityArray==quality] = dataNoData
                        maskArray[qualityArray==quality] = 0
                    else:
                        maskArray[qualityArray==quality] = 1
            elif qualityBand['quality_datatype'] == 'bit':
                values = qualityInfo.split(';')  #2-5=0000;6-7<10
                valuesAr = self.splitBinaryQualityInfo(values)
                for quality in qualityValues:
                    val = np.binary_repr(quality).zfill(16)[::-1]   #flipped
                    result = self.checkQualityBinary(val, valuesAr)
                    LOGGER.info('Quality value '+val+' set to '+str(result))

                    if result:
                        maskArray[qualityArray==quality] = 1
                    else:
                        maskArray[qualityArray==quality] = 0
                        dataArray[qualityArray==quality] = dataNoData
            else:
                LOGGER.error('No quality info')
                raise Exception('No quality info')

            dataDSMasked = gdal_array.SaveArray(dataArray, dataFile, 'HDF4Image')
            gdal_array.CopyDatasetInfo(dataDS, dataDSMasked)

            maskDSMasked = gdal_array.SaveArray(maskArray, maskFile, 'HDF4Image')
            gdal_array.CopyDatasetInfo(dataDSMasked, maskDSMasked)
            finalfiles[key] = dataFile

            maskDS, maskDSMasked, dataDS, dataDSMasked = [None]*4
            del maskDS, maskDSMasked, dataDS, dataDSMasked
        return finalfiles