问题:将多城市经纬度坐标的RD_LINK/NODE转换成平面坐标
背景数据:
1、需要被投影的源数据:60个不同城市,分文件夹下有经纬度坐标的RD_LINK,RD_NODE。以北京为例:
2、全部城市对应的平面坐标投影文件已有:
代码实现:
第一步:列出分城市的成果文件夹们
#批量经纬度转平面投影——投影成果文件夹批量生成
#-*- coding: utf-8 -*-
import os
#模仿的文件夹路径
result_dir='D:/19Q2/result20190122'
citys=os.listdir(result_dir)
#新建的文件夹路径
prj_result_dir='D:/19Q2/prj_result20190122'
for city in citys:
mkfile_dir=os.path.join(prj_result_dir,city)
os.mkdir(mkfile_dir)
print(city+'已新建成功!')
第二步:调用arcgis中arcpy包里的投影方法进行循环投影
# coding=utf-8
# ---------------------------------------------------------------------------
# Def_Projection.py
# Description: Batch projection
# ---------------------------------------------------------------------------
#导入Arcgis的Arcpy包,以使用投影方法
import arcpy
#列出输入,处理,输出过程涉及的根目录
inWorkspace='D:/19Q2/result20190122/'#输入文件result的上级根目录
prjdir='D:/PythonBatchReproject/prjFile/'#投影文件所在路径
outWorkspace='D:/19Q2/prj_result20190122/'#投影后成果文件夹路径
#使用字典使60个城市与其投影文件匹配
city_names={#配合arcpy包使用python2.7,注意语法
u'三亚':'SANYA',
u'东莞':'DONGGUAN',
u'中山':'ZHONGSHAN',
u'乌鲁木齐':'WULUMUQI',
u'佛山':'FOSHAN',
u'保定':'BAODING',
u'兰州':'LANZHOU',
u'南京':'NANJING',
u'南宁':'NANNING',
u'南昌':'NANCHANG',
u'南通':'NANTONG',
u'厦门':'XIAMEN',
u'台州':'TAIZHOU',
u'合肥':'HEFEI',
u'呼和浩特':'HUHEHAOTE',
u'哈尔滨':'HAERBIN',
u'唐山':'TANGSHAN',
u'大连':'DALIAN',
u'天津':'TIANJIN',
u'太原':'TAIYUAN',
u'宁波':'NINGBO',
u'常州':'CHANGZHOU',
u'徐州':'XUZHOU',
u'惠州':'HUIZHOU',
u'成都':'CHENGDU',
u'扬州':'YANGZHOU',
u'无锡':'WUXI',
u'昆明':'KUNMING',
u'杭州':'HANGZHOU',
u'柳州':'LIUZHOU',
u'武汉':'WUHAN',
u'泉州':'QUANZHOU',
u'济南':'JINAN',
u'海口':'HAIKOU',
u'温州':'WENZHOU',
u'潍坊':'WEIFANG',
u'烟台':'YANTAI',
u'珠海':'ZHUHAI',
u'盐城':'YANCHENG',
u'石家庄':'SHIJIAZHUANG',
u'福州':'FUZHOU',
u'秦皇岛':'QINHUANGDAO',
u'绍兴':'SHAOXING',
u'芜湖':'WUHU',
u'苏州':'SUZHOU',
u'西宁':'XINING',
u'西安':'XIAN',
u'贵阳':'GUIYANG',
u'郑州':'ZHENGZHOU',
u'重庆':'CHONGQING',
u'金华':'JINHUA',
u'银川':'YINCHUAN',
u'长春':'CHANGCHUN',
u'长沙':'CHANGSHA',
u'青岛':'QINGDAO',
u'沈阳':'SHENYANG',
u'广州':'GUANGZHOU',
u'上海':'SHANGHAI',
u'深圳':'SHENZHEN',
u'北京':'BEIJING'
}
#循环使用成对的中英文城市名拼接出路径字符串
for chinese_name, pinyin_name in city_names.items():
print chinese_name + u"投影中..."
#参数1:待投影文件——裁剪后link&node源数据
RD_LINK = inWorkspace + chinese_name + '/RD_LINK.shp'
RD_NODE = inWorkspace + chinese_name + '/RD_NODE.shp'
#参数2:投影后输出成果文件
prj_link = outWorkspace + chinese_name + '/RD_LINK.shp'
prj_node = outWorkspace + chinese_name + '/RD_NODE.shp'
#参数3:投影文件
prj_file = prjdir+ pinyin_name + '.prj'
# Process: 批量投影
arcpy.Project_management(RD_LINK, prj_link, prj_file)
arcpy.Project_management(RD_NODE, prj_node, prj_file)
#arcpy.BatchProject_management("RD_LINK;RD_NODE", prj_dir, prj_file)
print chinese_name + u"投影已成功!"
print "All is ok!"
加入容错和计数后的投影代码:
# coding=utf-8
# ---------------------------------------------------------------------------
# Def_Projection.py
# ---------------------------------------------------------------------------
import arcpy
import os
inWorkspace=u'D:/PythonCode_inWork/02打断数据包自动生成/result_EX2/'#输入文件result的上级根目录
prjdir=u'W:/E3D三维/04投影文件/'#投影文件所在路径
outWorkspace=u'D:/PythonCode_inWork/02打断数据包自动生成/prj_result_EX2/'#投影后成果文件夹路径
#带投影城市列表
city_names=os.listdir(inWorkspace)
num_ok = 0
num_fail = 0
#新建城市成果文件夹并投影
for city in city_names:
if os.path.exists(outWorkspace+city) == False:
os.mkdir(outWorkspace+city)
print city + u"投影中..."
#参数1:待投影文件——裁剪后link&node源数据
RD_LINK = inWorkspace + city + '/RD_LINK.shp'
RD_NODE = inWorkspace + city + '/RD_NODE.shp'
#参数2:投影后输出成果文件
prj_link = outWorkspace + city + '/RD_LINK.shp'
prj_node = outWorkspace + city + '/RD_NODE.shp'
#参数3:投影文件
prj_file = prjdir+ city.lower() + '.prj'
# Process: 批量投影
try:
arcpy.Project_management(RD_LINK, prj_link, prj_file)
arcpy.Project_management(RD_NODE, prj_node, prj_file)
#arcpy.BatchProject_management("RD_LINK;RD_NODE", prj_dir, prj_file)
print city + u"投影已成功!"
num_ok += 1
except:
print city + u"投影失败!请确认!"
num_fail += 1
print str(num_ok)+" ok!"
print str(num_fail)+" fail!"