上傳gdb檔案地理資料庫所有圖層到企業級空間庫
阿新 • • 發佈:2021-10-29
本例子支援上傳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空間參考檔案:CGCS2000.prj" 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_pathtry: 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 returnif 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"