R语言学习笔记(四)计算一定范围内的遥感影像栅格个数

jpg_file <-"D:/qunluo/qunluo.jpg"
qunluo <- raster(jpg_file)
qunluo_matrix <- as.matrix(qunluo)
qunluo_rgb <- rgb(qunluo_matrix,qunluo_matrix,qunluo_matrix,maxColorValue = 255)
writeRaster(qunluo_rgb, filename = "D:/qunluo/qunluorgb1.tif", format = "GTiff", overwrite = TRUE) 
#出现函数‘writeRaster’标签‘"character", "character"’找不到继承方法的错误解决不了
TIF_extent <- extent(0,0,0,0)   #里面的数字表示裁剪的影像xmin,xmax,ymin,ymax
tif_extent <- crop(tif,TIFextent)  #在tif中按照TIFextent的范围裁剪,形成新文件
tif_pixel <- ncell(tif_extent)    #计算tif_extent的栅格数量
library(magick)  #jpg图片转换为TIF文件
photo <- image_read("D:/qunluo/qunluo.jpg")
image_write(photo,path = "D:/qunluo/qunluo1.tif")
count <- length(which(carex[] >= 0 & carex[] <= 1))
#计算一定值域范围内的像元数量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于GDAL库的python代码,用于批量将遥感影像栅格化为矢量文件: ```python import os import glob import gdal from osgeo import ogr path = 'path/to/raster/files' output_path = 'path/to/vector/files' def raster_to_vector(raster_file, vector_file): # Open raster file and get metadata ds = gdal.Open(raster_file) band = ds.GetRasterBand(1) geotransform = ds.GetGeoTransform() proj = ds.GetProjection() cols = ds.RasterXSize rows = ds.RasterYSize # Create output shapefile and layer driver = ogr.GetDriverByName('ESRI Shapefile') out_ds = driver.CreateDataSource(vector_file) out_layer = out_ds.CreateLayer(vector_file, srs=ogr.osr.SpatialReference(proj), geom_type=ogr.wkbPolygon) # Create field for pixel value field_def = ogr.FieldDefn('Value', ogr.OFTInteger) out_layer.CreateField(field_def) # Create polygons from raster cells for y in range(rows): for x in range(cols): # Get pixel value and check if it is nodata value = band.ReadAsArray(x, y, 1, 1)[0, 0] if value == band.GetNoDataValue(): continue # Calculate polygon vertices ulx, xres, xskew, uly, yskew, yres = geotransform xcoord = ulx + (x + 0.5) * xres ycoord = uly + (y + 0.5) * yres ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(xcoord - 0.5 * xres, ycoord - 0.5 * yres) ring.AddPoint(xcoord + 0.5 * xres, ycoord - 0.5 * yres) ring.AddPoint(xcoord + 0.5 * xres, ycoord + 0.5 * yres) ring.AddPoint(xcoord - 0.5 * xres, ycoord + 0.5 * yres) ring.AddPoint(xcoord - 0.5 * xres, ycoord - 0.5 * yres) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) # Create feature and add to layer feature = ogr.Feature(out_layer.GetLayerDefn()) feature.SetGeometry(poly) feature.SetField('Value', value) out_layer.CreateFeature(feature) # Clean up ds = None out_ds = None # Loop through raster files for raster_file in glob.glob(os.path.join(path, '*.tif')): # Create output vector file name vector_file = os.path.join(output_path, os.path.splitext(os.path.basename(raster_file))[0] + '.shp') # Convert raster to vector raster_to_vector(raster_file, vector_file) ``` 这个代码将遥感影像文件夹中的所有.tif文件转换为矢量文件,输出到指定的路径下。可以根据需要对输出的矢量文件进行进一步处理,如合并、剪裁等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邢庆阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值