zoukankan      html  css  js  c++  java
  • 导出所有图层到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

  • 相关阅读:
    .net core 操作域控 活动目录 ladp -- Support for System.DirectoryServices for Windows
    运行在 Android 系统上的完整 Linux -- Termux
    Windows Python 版本切换工具 --- Switch Python Version Tool For Windows
    简单的NLog配置文件
    python之set基本使用
    复习PHP的数组
    php动态修改配置文件
    制作一个php万年历
    一个简单的选项卡
    python操作文件的笔记
  • 原文地址:https://www.cnblogs.com/publiter/p/15481522.html
Copyright © 2011-2022 走看看