上传gdb文件地理数据库所有图层到企业级空间库
本例子支持上传gdb中所有图层到Oracle、PostgreSQL空间数据库,并自动去除gdb自动创建的一些字段,同时保留图层别名、字段别名。
#导入文件夹中的gdb到地理数据库 import os import arcpy from arcpy import env print "begin" gdb_path = r"E:\test.gdb" #Set SpatialReference sr = arcpy.SpatialReference("C:/data/CGCS2000.prj") sde_path = r"C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\192.168.1.30.sde" user_name = "sde.username." count = 0 #List all datasets, create gdb with dataset name env.workspace = gdb_path datasets = arcpy.ListDatasets("","All") for ds in datasets: dsname = ds print str(count) + " gdb >" + dsname count = count + 1 #Change workspace, and create dataset env.workspace = sde_path try: arcpy.CreateFeatureDataset_management(sde_path, dsname, sr) except: print "dataset exist" #Change workspace, and list featureclass env.workspace = gdb_path fcs = arcpy.ListFeatureClasses("","",ds) #when featureClass name = dataset name, ListFeatureClasses can not return if dsname not in fcs: if arcpy.Exists(dsname + "\\" + dsname): fcs.append(dsname) #iport featureClass for fc in fcs: fcname = fc print fcname infc = dsname + "\\" + fcname #Filter OBJECTID and other similar fields fieldmappings = arcpy.FieldMappings() infields = arcpy.ListFields(infc) for infield in infields: fname = infield.name fname = fname.lower() #Ignore fields if fname == "objectid" or fname == "shape": continue if fname == "shape.area" or fname == "shape.len" or fname == "shape_area" or fname == "shape_len": continue if "objectid" in fname: continue #pg database field if "st_length" in fname or "st_area" in fname: continue #oracle - upper,pg - lower #fname = fname.upper() fieldmap = arcpy.FieldMap() fieldmap.addInputField(infc, infield.name) fieldmappings.addFieldMap(fieldmap) del fieldmap #upload featureclass to db env.workspace = gdb_path arcpy.FeatureClassToFeatureClass_conversion(infc, sde_path + "\\" + user_name + dsname, fcname, "", fieldmappings) del fieldmappings #set aliasName desc = arcpy.Describe(infc) if desc.aliasName != "": arcpy.AlterAliasName(sde_path + "\\" + user_name + dsname + "\\" + user_name + fcname, desc.aliasName) print "done" print "all done"