本例子支持导出Oracle、PostgreSQL空间数据图层到gdb个人地理数据库,并自动去除sde自动创建的一些字段,同时保留图层别名、字段别名。
#从数据库导出gdb import os import arcpy from arcpy import env print "begin" out_path = "E:out" gdbname = "test.gdb" #Set SpatialReference sr = arcpy.SpatialReference("C:/data/CGCS2000.prj") sde_path = r"C:UsersAdministratorAppDataRoamingESRIDesktop10.2ArcCatalog192.168.1.30.sde" #PostgreSQL is "sde.username.", Oracle is "username." user_name = "sde.username." count = 0 #create gdb file env.workspace = out_path arcpy.CreateFileGDB_management(out_path, gdbname) #List all datasets, create gdb with dataset name env.workspace = sde_path datasets = arcpy.ListDatasets("","All") for ds in datasets: dsname = ds.replace(user_name, "") print str(count) + " dataset >" + dsname count = count + 1 #Change workspace, and create dataset env.workspace = out_path + "\" + gdbname try: arcpy.CreateFeatureDataset_management(out_path + "\" + gdbname, dsname, sr) except: print "dataset exist" #Change workspace, and list featureclass env.workspace = sde_path #List all featureClasses in a dataset fcs = arcpy.ListFeatureClasses("","",ds) #when featureClass name = dataset name, ListFeatureClasses can not return if user_name + dsname not in fcs: if arcpy.Exists(user_name + dsname + "\" + user_name + dsname): fcs.append(user_name + dsname) #export featureClass for fc in fcs: fcname = fc.replace(user_name, "") print fcname infc = user_name + dsname + "\" + user_name + 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 #Out featureclass to gdb arcpy.FeatureClassToFeatureClass_conversion(infc, out_path + "\" + gdbname + "\" + dsname, fcname, "", fieldmappings) del fieldmappings #set aliasName desc = arcpy.Describe(infc) if desc.aliasName != "": arcpy.AlterAliasName(out_path + "\" + gdbname + "\" + dsname + "\" + fcname, desc.aliasName) print "done" print "all done"
空间参考文件:CGCS2000.prj