(三十四)arcpy开发&gdb、shapefile创建与投影定义,字段创建
今天我们来学习这个例子是创建一个gdb、然后在该gdb目录下创建一个shapefile文件,再为这个矢量文件定义投影,相应的添加到字段。这过程中使用的函数有arcpy.CreateFileGDB_management、arcpy.CreateFeatureclass_management、arcpy.Project_management、arcpy.AddField_management。最后创建的gdb文件将会在我们指定的目录下生成。
对应的shapefile文件,在ArcGIS Desktop中如下所示。
我们来看一下实现的代码。
import arcpy, datetime, os
nGDB = r"\test2.gdb"
aPath = r"C:\Users\qin\Desktop\zxy"
arcpy.env.workspace = aPath + nGDB
aFC = aPath + nGDB + r"\demoshp"
print "starting"
def newGdb():
if arcpy.Exists(aPath + nGDB):
arcpy.Delete_management(aPath + nGDB)
arcpy.CreateFileGDB_management(aPath, nGDB)
ws, fc_name = os.path.split(aFC)
arcpy.CreateFeatureclass_management(ws, fc_name, "POLYGON", "#", "ENABLED", "ENABLED", arcpy.SpatialReference(4326))
def checkProjection():
for i in arcpy.ListFeatureClasses():
desc = arcpy.Describe(i)
pList = desc.spatialReference.type
rName = aPath + nGDB + "\\" + i + "_P"
if pList == 'Geographic':
arcpy.Project_management(i, rName, arcpy.SpatialReference(4326))
else:
print "xxxxx"
def newField():
arcpy.AddField_management(aFC, "divIndex", "DOUBLE")
newGdb()
checkProjection()
newField()
print datetime.datetime.now()
print "EOF!!!\n"
更多内容,请微信扫二维码关注公众号,或者加入arcpy开发qq学习群:487352121
转载自:https://blog.csdn.net/u010608964/article/details/89325378