代码上都有注释,都是按部分编写,可为Giser提供一些思路。此处不过多介绍,需要配合excel实现。 # -*- coding:utf-8 -*-
# @Time: 11:22
# @File:a.py
import arcpy
import os
import xlrd # 导入xlrd
# 防止乱码,只针对Python2.x
import sys
reload(sys)
sys.setdefaultencoding("utf8")
excl_path = r"C:/Users/Administrator/Desktop/建库模板.xls"
sheet_NAME = "数据库"
wb = xlrd.open_workbook(excl_path.decode('utf8')) # 读取工作簿对象。
ws = wb.sheet_by_name(sheet_NAME.decode()) # 读取工作表对象。
# row_count=ws.nrows # 返回工作表中已使用的行数。
# col_count=ws.ncols # 返回工作表中已使用的列数。
'''
prj_wkid:坐标系代码工厂
paj_file:指定坐标系文件
File_path:数据库存储路径
Gdb_name:数据库名称
Gdb_path:数据库绝对路径
Feature_name:数据集名称
'''
prj_wkid = int(ws.cell_value(1, 2))
prj_file = ws.cell_value(2, 2)
File_path = ws.cell_value(3, 2)
Gdb_name = ws.cell_value(4, 2)
Gdb_path = ws.cell_value(5, 2)
Feature_name = ws.cell_value(6, 2)
fc = Gdb_path
arcpy.env.overwriteOutput = True # 覆盖同名文件
arcpy.CreateFileGDB_management(File_path, Gdb_name) # 创建数据库
prj_wkid1 = arcpy.SpatialReference(prj_wkid).exportToString() # 利用代码工厂创建空间参考
# 根据指定坐标系新建要素数据集
if prj_wkid == "" and prj_file == "":
arcpy.CreateFeatureDataset_management(fc, Feature_name)
elif prj_wkid != "" and prj_file == "":
arcpy.CreateFeatureDataset_management(fc, Feature_name, prj_wkid1)
elif prj_wkid == "" and prj_file != "":
arcpy.CreateFeatureDataset_management(fc, Feature_name, prj_file)
elif prj_wkid != "" and prj_file != "":
arcpy.CreateFeatureDataset_management(fc, Feature_name, prj_wkid1)
# 确定工作空间
arcpy.env.workspace = fc
# 创建要素类
ws1 = wb.sheet_by_name("要素类".decode())
row1 = ws1.nrows # 返回工作表中已使用的行数。
col1 = ws1.ncols # 返回工作表中已使用的列数。
'''
Ysmc:要素名称
Ysbm:要素别名
Yslx:要素类型
Zdmc:字段名称
Zdbm:字段别名
Zdlx:字段类型
Xsws:小数位数
Zdcd:长度
Fk:是否为空
Bt:是否必填
Ys:约束值
'''
cf_m = arcpy.CreateFeatureclass_management
Feature_path = fc +"\\" + Feature_name
i = 2
for i in range(int(row1)):
Ysmc = ws1.cell_value(i, 1)
Ysbm = ws1.cell_value(i, 2)
Yslx = ws1.cell_value(i, 3)
Zdmc = ws1.cell_value(i, 4)
Zdbm = ws1.cell_value(i, 5)
Zdlx = ws1.cell_value(i, 6)
Xsws = ws1.cell_value(i, 7)
Zdcd = ws1.cell_value(i, 8)
Fk = ws1.cell_value(i, 9)
Bt = ws1.cell_value(i, 10)
Ys = ws1.cell_value(i, 11)
if Yslx == "点":
# 要素类型0.点;1.线;2.面;3.注记要素类的几何类型。POINT — MULTIPOINT — POLYGON —POLYLINE
geometry_type = 'POINT'
elif Yslx == "线":
geometry_type = 'POLYLINE'
elif Yslx == "面":
geometry_type = 'POLYGON'
elif Yslx == "注记":
geometry_type = 'MULTIPOINT'
else:
continue
if ws1.cell_value(i, 1) != ws1.cell_value(i - 1, 1):
Ys = cf_m(Feature_path, Ysmc, geometry_type)
arcpy.AlterAliasName(Ys, Ysbm) # 函数可用于更新表或要素类上的别名
''''#创建字段:AddField_management
in_table:要素名
field_name:字段名
field_type:字段类型
field_precision:存储在字段中的位数
field_scale :小数位数
field_length:字段长度
field_alias:字段别名
field_is_nullable:是否为空
field_is_required:是否必填
field_domain:约定值
'''
in_table = Ysmc
field_name = Zdmc
field_type = Zdlx
field_scale = Xsws
field_length = Zdcd
field_alias = Zdbm
field_is_nullable = Fk
field_is_required = Bt
field_domain = Ys
arcpy.AddField_management(in_table,
field_name,
field_type,
"",
field_scale,
field_length,
field_alias,
field_is_nullable,
field_is_required,
field_domain)
i = i + 1
'''
输出数据库
'''
# 输出数据库目录:列出地理数据库中所有的要素类,包括要素数据集中的所有要素类
arcpy.env.workspace = r"C:\Users\Administrator\Desktop\新昌裁库1.gdb"
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
for ds in datasets:
for fa in arcpy.ListFeatureClasses(feature_dataset=ds):
# path = os.path.join(arcpy.env.workspace,ds, fa)
print ('要素名:{0},要素别名:{1}'.format(fa, arcpy.Describe(fa).aliasName))
可能存在不足,欢欢迎指正,顺带给GIser们推荐一个蒸汽眼罩,画图、入库、写分析,眼睛是不是特酸,眼睛度数又升高了多少,来一个舒缓下眼疲劳。干活去了~
|