python for ArcGIS 绘制成都市板块地图

python for ArcGIS 绘制成都市板块地图

  • 原数据
  • 完整代码
  • 代码解读
  • 错误代码

利用python的arcpy模块绘制出成都市板块地图如下
python for ArcGIS 绘制成都市板块地图_第1张图片

原数据

python for ArcGIS 绘制成都市板块地图_第2张图片

完整代码

# -*- coding: utf-8 -*-
"""
Project_name:drawing plate for Chengdu
@author: 帅帅de三叔
Created on Wed Oct 30 15:16:44 2019
"""
import sys
arcpy_path = [r'D:\Python27\ArcGIS10.6\Lib\site-packages',
              r'D:\Program Files (x86)\ArcGIS\Desktop10.6\arcpy',
              r'D:\Program Files (x86)\ArcGIS\Desktop10.6\bin',
              r'D:\Program Files (x86)\ArcGIS\Desktop10.6\ArcToolbox\Scripts']
sys.path.extend(arcpy_path)
stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr 
reload(sys) #通过import引用进来时,setdefaultencoding函数在被系统调用后被删除了,所以必须reload一次
sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stde 
sys.setdefaultencoding('utf-8')

import arcpy #导入地理处理模块
from arcpy import env #导入环境类
env.workspace=r"D:\python for ArcGIS\绘制成都板块"
env.overwriteOutput=True #是否开启复写
import pandas as pd #导入数据分析模块
plate_data=pd.read_excel(u"成都板块.xlsx") #读取板块数据
rows,cols=plate_data.shape #数据框尺寸
lng_lat=plate_data[u'边界坐标'] #经纬度数据
plate_name=plate_data[u'板块'] #板块名称

#factoryCode = arcpy.GetParameterAsText(4490) #WGS_1984_World_Mercator投影坐标系工厂代码4490,3395
#spatial_ref = arcpy.SpatialReference(factoryCode) #设置空间参考参数           
spatial_ref = arcpy.SpatialReference('China Geodetic Coordinate System 2000') #China Geodetic Coordinate System 2000 or WGS 1984 World Mercator

polygonPoints=arcpy.Array() #用来存放构成多边形的折点
polygonGeometryList=[] #用来存放多边形几何对象组

for row in range(0,rows): #按行循环
    points=lng_lat[row].split(";") #折点
    for spot in points:
        xy=spot.split(",") #折点经纬度
        if len(xy)==2:
            point=arcpy.Point() #几何对象,用来存放折点对象
            point.id=row;point.X=float(xy[0]);point.Y=float(xy[1])  #转为点对象
            polygonPoints.add(point) #构成一串折点
            #print(point.id)
    polygon=arcpy.Polygon(polygonPoints,spatial_ref,"","") #利用折点构造多边形带空间参考
    polygonGeometryList.append(polygon) #把多边形追加到数组  
    polygonPoints.removeAll() #移除折点
result=arcpy.CopyFeatures_management(polygonGeometryList,r"D:\python for ArcGIS\绘制成都板块\plate_Chengdu.shp","POLYGON")

代码解读

整个实现过程包括3步

读数,即读取板块边界经纬度数据
拆点,即采用split()函数将经纬度数据分割,构成折点
连线,即将折点连起来形成封闭多边形,即板块

错误代码

D:\python for ArcGIS\绘制成都板块>C:/Python27/ArcGIS10.6/python.exe "d:/python for ArcGIS/绘制成都板块/plot_plate.py"
Traceback (most recent call last):
  File "d:/python for ArcGIS/绘制成都板块/plot_plate.py", line 23, in <module>
    plate_data=pd.read_excel("鎴愰兘鏉垮潡.xlsx") #璇诲彇鏉垮潡鏁版嵁
  File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\io\excel.py", line 170, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\io\excel.py", line 227, in __init__
    self.book = xlrd.open_workbook(io)
  File "C:\Python27\ArcGIS10.6\lib\site-packages\xlrd\__init__.py", line 395, in open_workbook
    with open(filename, "rb") as f:
IOError: [Errno 2] No such file or directory: '\xe6\x88\x90\xe9\x83\xbd\xe6\x9d\xbf\xe5\x9d\x97.xlsx'

因为编译器没法识别出原数据文件,可能是编码问题,在前面加一个u,标拾Unicode

plate_data=pd.read_excel(u"成都板块.xlsx") #读取板块数据

若还有不明白的,可以来“三行科创”微信公众号交流群。

1,python for ArcGIS 绘制上海市板块地图
2,python for ArcGIS 绘制上海市环线地图
3,python for ArcGIS 绘制北京市板块地图
4,python for ArcGIS 绘制广州市板块地图
5,python for ArcGIS 绘制深圳市板块地图

你可能感兴趣的:(python,for,ArcGIS)