36 lines
1.5 KiB
Python
36 lines
1.5 KiB
Python
import csv
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
|
|
if len(sys.argv)<3:
|
|
sys.argv.append("010")
|
|
sys.argv.append(r"D:\德清研究院加密\项目\项目文档归档\空基算法\算法验证文档\样本\地表覆盖样本\ROI #1.xml")
|
|
sys.argv.append(r"D:\德清研究院加密\项目\项目文档归档\空基算法\算法验证文档\样本\地表覆盖样本\ROI #1.csv")
|
|
|
|
# 替换为你的XML文件路径
|
|
roixmlcode=sys.argv[-3]
|
|
xml_file =sys.argv[-2]
|
|
csv_file=sys.argv[-1]
|
|
# 解析XML
|
|
tree = ET.parse(xml_file)
|
|
root = tree.getroot()
|
|
|
|
|
|
# 创建CSV文件
|
|
with open(csv_file, 'a', newline='', encoding='UTF-8') as csvfile:
|
|
csvwriter = csv.writer(csvfile)
|
|
# 写入CSV标题行
|
|
csvwriter.writerow(['parent_id','id','covernm','roi_polygon'])
|
|
|
|
# 遍历每个区域并写入CSV
|
|
for region in root.findall('Region'):
|
|
for GeometryDef in region.findall("GeometryDef"):
|
|
for polygonnode in GeometryDef.findall("Polygon"):
|
|
name = region.get('name')
|
|
color = region.get('color')
|
|
coord_sys_str = region.find('GeometryDef/CoordSysStr').text
|
|
# 将所有坐标连接成一个字符串,用逗号分隔
|
|
coordinates = ', '.join([coord.strip() for coord in polygonnode.find('Exterior/LinearRing/Coordinates').text.split()])
|
|
polygonstr="POLYGON(({}))".format(coordinates)
|
|
# 写入CSV数据行
|
|
csvwriter.writerow([roixmlcode,roixmlcode ,name.replace(" ",""), polygonstr]) |