Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. >>> import arcpy
  2. >>> arcpy.env.workspace = "C:\Users\chad\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\anrc_water (anrcuser).sde"
  3. >>> fdlist = arcpy.ListDatasets()
  4. >>> for fd in fdlist:
  5. ... print fd
  6. ...
  7. anrc_water.DBO.ChadTest
  8. anrc_water.DBO.Temp_Data
  9. anrc_water.DBO.Master_Datasets
  10. ANRC_WATER.DBO.ENF_FILL_FACC
  11. ANRC_WATER.DBO.ENF_FILL_FDIR
  12.  
  13. >>>
  14.  
  15. """
  16. Name: sdeconn.py
  17. Description: Utility functions for sde connections
  18. """
  19.  
  20. # Import system modules
  21. import arcpy, os, sys
  22.  
  23. def connect(database, server="<default server>", username="<default user>", password="<default password>", version="SDE.DEFAULT"):
  24. # Check if value entered for option
  25. try:
  26. #Usage parameters for spatial database connection to upgrade
  27. service = "sde:sqlserver:" + server
  28. account_authentication = 'DATABASE_AUTH'
  29. version = version.upper()
  30. database = database.lower()
  31.  
  32. # Check if direct connection
  33. if service.find(":") <> -1: #This is direct connect
  34. ServiceConnFileName = service.replace(":", "")
  35. ServiceConnFileName = ServiceConnFileName.replace(";", "")
  36. ServiceConnFileName = ServiceConnFileName.replace("=", "")
  37. ServiceConnFileName = ServiceConnFileName.replace("/", "")
  38. ServiceConnFileName = ServiceConnFileName.replace("\", "")
  39. else:
  40. arcpy.AddMessage("n+++++++++")
  41. arcpy.AddMessage("Exiting!!")
  42. arcpy.AddMessage("+++++++++")
  43. sys.exit("nSyntax for a direct connection in the Service parameter is required for geodatabase upgrade.")
  44.  
  45. # Local variables
  46. Conn_File_NameT = server + "_" + ServiceConnFileName + "_" + database + "_" + username
  47.  
  48. if os.environ.get("TEMP") == None:
  49. temp = "c:\temp"
  50. else:
  51. temp = os.environ.get("TEMP")
  52.  
  53. if os.environ.get("TMP") == None:
  54. temp = "/usr/tmp"
  55. else:
  56. temp = os.environ.get("TMP")
  57.  
  58. Connection_File_Name = temp + os.sep + Conn_File_NameT + ".sde"
  59. if os.path.isfile(Connection_File_Name):
  60. return Connection_File_Name
  61.  
  62. # Check for the .sde file and delete it if present
  63. arcpy.env.overwriteOutput=True
  64.  
  65.  
  66. # Variables defined within the script; other variable options commented out at the end of the line
  67. saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME
  68. saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION
  69.  
  70.  
  71. print "nCreating ArcSDE Connection File...n"
  72. # Process: Create ArcSDE Connection File...
  73. # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password, version, save_version_info
  74. print temp
  75. print Conn_File_NameT
  76. print server
  77. print service
  78. print database
  79. print account_authentication
  80. print username
  81. print password
  82. print saveUserInfo
  83. print version
  84. print saveVersionInfo
  85. arcpy.CreateArcSDEConnectionFile_management(temp, Conn_File_NameT, server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo)
  86. for i in range(arcpy.GetMessageCount()):
  87. if "000565" in arcpy.GetMessage(i): #Check if database connection was successful
  88. arcpy.AddReturnMessage(i)
  89. arcpy.AddMessage("n+++++++++")
  90. arcpy.AddMessage("Exiting!!")
  91. arcpy.AddMessage("+++++++++n")
  92. sys.exit(3)
  93. else:
  94. arcpy.AddReturnMessage(i)
  95. arcpy.AddMessage("+++++++++n")
  96. return Connection_File_Name
  97. #Check if no value entered for option
  98. except SystemExit as e:
  99. print e.code
  100. return
  101.  
  102. import arcpy, sdeconn
  103. myconnect1 = sdeconn.connect("database1", "server")
  104. myconnect2 = sdeconn.connect("database2", "server")
  105.  
  106. DataConnections = "C:\AGS_GCSS_Tools\DatabaseConnections\"
  107. TCA_Connection = "prod_sde.sde\prod_SDE.GIS.PropertyTax" + CAPSYear + "\prod_SDE.GIS.Tca"
  108. TCA_Layer = DataConnections + TCA_Connection
  109.  
  110. PathSdeConnection= "C:\Users\{Username Of windows}\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\{name of ConenctionString}.sde
  111.  
  112. with arcpy.da.SearchCursor(PathSdeConnection,("OBJECTID","SHAPE@","SHAPE@JSON"),{WhereClause})as cursor:
  113. for row in cursor:
  114. .
  115. .
  116. .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement