我认为最简单的方法是提取每个多边形内的栅格值并计算比例。是否可以在不将整个网格作为数组读取的情况下这样做?
我从1992年到2015年有23个年度全球分类栅格(分辨率= 0.00277778度)和354个形状的多边形矢量(在某些部分重叠)。由于重叠(自相交),使用它们作为光栅并不容易。两者都投射在" + proj = longlat + datum = WGS84 + no_defs"。
栅格由10 - 220的类组成 多边形的ABC_ID为1 - 449
一年看起来像: classification and shape example
我需要创建一个像:
这样的表我已经尝试用以下方法实现这一目标:
我听说ArcMap中的Tabulate Area可以做到这一点,但如果有一个开源解决方案就可以了。
答案 0 :(得分:1)
我已经成功地使用Python“rasterio”和“geopandas”
它现在创建一个表格,如: example result
因为我没有找到像R“raster”中的提取物命令类似的东西,所以它只花了不到2行,而不是计算一半的夜晚,现在一年只需要2分钟。 结果是一样的。它基于“https://gis.stackexchange.com/questions/260304/extract-raster-values-within-shapefile-with-pygeoprocessing-or-gdal/260380”
中“基因”的思想import rasterio
from rasterio.mask import mask
import geopandas as gpd
import pandas as pd
print('1. Read shapefile')
shape_fn = "D:/path/path/multypoly.shp"
raster_fn = "D:/path/path/class_1992.tif"
# set max and min class
raster_min = 10
raster_max = 230
output_dir = 'C:/Temp/'
write_zero_frequencies = True
show_plot = False
shapefile = gpd.read_file(shape_fn)
# extract the geometries in GeoJSON format
geoms = shapefile.geometry.values # list of shapely geometries
records = shapefile.values
with rasterio.open(raster_fn) as src:
print('nodata value:', src.nodata)
idx_area = 0
# for upslope_area in geoms:
for index, row in shapefile.iterrows():
upslope_area = row['geometry']
lake_id = row['ABC_ID']
print('\n', idx_area, lake_id, '\n')
# transform to GeJSON format
from shapely.geometry import mapping
mapped_geom = [mapping(upslope_area)]
print('2. Cropping raster values')
# extract the raster values values within the polygon
out_image, out_transform = mask(src, mapped_geom, crop=True)
# no data values of the original raster
no_data=src.nodata
# extract the values of the masked array
data = out_image.data[0]
# extract the row, columns of the valid values
import numpy as np
# row, col = np.where(data != no_data)
clas = np.extract(data != no_data, data)
# from rasterio import Affine # or from affine import Affine
# T1 = out_transform * Affine.translation(0.5, 0.5) # reference the pixel centre
# rc2xy = lambda r, c: (c, r) * T1
# d = gpd.GeoDataFrame({'col':col,'row':row,'clas':clas})
range_min = raster_min # min(clas)
range_max = raster_max # max(clas)
classes = range(range_min, range_max + 2)
frequencies, class_limits = np.histogram(clas,
bins=classes,
range=[range_min, range_max])
if idx_area == 0:
# data_frame = gpd.GeoDataFrame({'freq_' + str(lake_id):frequencies})
data_frame = pd.DataFrame({'freq_' + str(lake_id): frequencies})
data_frame.index = class_limits[:-1]
else:
data_frame['freq_' + str(lake_id)] = frequencies
idx_area += 1
print(data_frame)
data_frame.to_csv(output_dir + 'upslope_area_1992.csv', sep='\t')
答案 1 :(得分:0)
我可以使用R命令提取并使用表格对其进行汇总,如#34; Spacedman"见:https://gis.stackexchange.com/questions/23614/get-raster-values-from-a-polygon-overlay-in-opensource-gis-solutions
shapes <- readOGR("C://data/.../shape)
LClass_1992 <- raster("C://.../LClass_1992.tif")
value_list <- extract (LClass, shapes )
stats <- lapply(value_list,table)
[[354]]
10 11 30 40 60 70 80 90 100 110 130 150 180 190 200 201 210
67 303 233 450 1021 8241 65 6461 2823 88 6396 5 35 125 80 70 1027
但这需要很长时间(半夜)。 我会尝试用Python做它可能会更快。 也许有人做过类似的事情,可以分享代码。