• 导出所有图层到gdb文件地理数据库


    本例子支持导出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

  • 相关阅读:
    C++ STL Set使用
    C++ STL算法
    C++ STL List使用
    C++中的构造析构函数
    七、Linux进程调度-应用内核设置调度策略和优先级
    Qcom高通相关汇总
    Cgroup内核文档翻译(8)——/dev/cpuctl/*
    用户进程和内核线程的CPU亲和性设置
    Scheduler内核文档翻译(1)——Documentationschedulersched-tune.txt
    uCgui和emWin的区别
  • 原文地址:https://www.cnblogs.com/publiter/p/15481522.html
Copyright © 2020-2023  润新知