BaseProcessTool/csv2shape.py

25 lines
884 B
Python

import shapefile
from shapely.geometry import Polygon
import csv
from shapely import wkt
# 从CSV文件读取数据
csv_file_path = r'D:\德清研究院加密\项目\项目文档归档\空基算法\算法验证文档\样本\test_landConver.csv'
with open(csv_file_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
data = list(csv_reader)
# 创建Shapefile文件
with shapefile.Writer('output_shapefile.shp') as shpfile:
shpfile.field('parent_id', 'N', '10')
shpfile.field('id', 'N', '10')
shpfile.field('covernm', 'C', '50')
for item in data:
wkt_polygon = item['WKT']
polygon = wkt.loads(wkt_polygon)
#polygon = Polygon(eval(wkt_polygon))
shpfile.poly([list(polygon.exterior.coords)])
shpfile.record(parent_id=int(item['parent_id']), id=int(item['id']), covernm=item['covernm'])