利用python进行矢量转栅格操作
基于rasterio和geopandas两个库包对矢量进行栅格化操作
#!usr/bin/env python
# -*- coding: utf-8 -*-
"""
date:2021/7/8
author:甲戌_Tr
email: liu_xxxi@163.com
"""
import sys,os
import geopandas as gpd
import rasterio
from rasterio import features
from rasterio.crs import CRS
from rasterio.transform import Affine
class Polygon2Raster:
def polygon_to_raster(self,shp,raster,pixel,field,code=4326):
'''
矢量转栅格
:param shp: 输入矢量全路径,字符串,无默认值
:param raster: 输出栅格全路径,字符串,无默认值
:param pixel: 像元大小,与矢量坐标系相关
:param field: 栅格像元值字段
:param Code: 输出坐标系代码,默认为4326
:return: None
'''
# 判断字段是否存在
shapefile = gpd.read_file(shp)
if not field in shapefile.columns:
raise Exception ('输出字段不存在')
# 判断数据类型
f_type = shapefile.dtypes.get(field)
if 'int' in str(f_type):
shapefile[field] = shapefile[field].astype('int16')
dtype = 'int16'
elif 'float' in str(f_type):
shapefile[field] = shapefile[field].astype('float32')
dtype = 'float32'
else:
raise Exception ('输入字段数据类型为{},无法进行栅格化操作'.format(f_type))
bound = shapefile.bounds
width = int((bound.get('maxx').max()-bound.get('minx').min())/pixel)
height = int((bound.get('maxy').max()-bound.get('miny').min())/pixel)
transform = Affine(pixel, 0.0, bound.get('minx').min(),
0.0, -pixel, bound.get('maxy').max())
meta = {'driver': 'GTiff',
'dtype': dtype,
'nodata': 0,
'width': width,
'height': height,
'count': 1,
'crs': CRS.from_epsg(code),
'transform': transform}
with rasterio.open(raster, 'w+', **meta) as out:
out_arr = out.read(1)
shapes = ((geom,value) for geom, value in zip(shapefile.get('geometry'), shapefile.get(field)))
burned = features.rasterize(shapes=shapes, fill=0, out=out_arr, transform=out.transform)
out.write_band(1, burned)
if __name__ == '__main__':
in_shp = r'test.shp'
out_tif = r'test.tif'
pixel_size = 0.01
field = 'data'
Polygon2Raster().polygon_to_raster(in_shp,out_tif,pixel_size,field)
print('finish')
欢迎指正~