Answers:
DEWright击败了我,他是对的,就像在ArcCatalog中一样使用连接。但是,这是我在ArcMap中的Python提示符下使用sde连接文件的直接完整路径完成的操作:
>>> import arcpy
>>> arcpy.env.workspace = "C:\\Users\\chad\\AppData\\Roaming\\ESRI\\Desktop10.0\\ArcCatalog\\anrc_water (anrcuser).sde"
>>> fdlist = arcpy.ListDatasets()
>>> for fd in fdlist:
... print fd
...
anrc_water.DBO.ChadTest
anrc_water.DBO.Temp_Data
anrc_water.DBO.Master_Datasets
ANRC_WATER.DBO.ENF_FILL_FACC
ANRC_WATER.DBO.ENF_FILL_FDIR
>>>
要获取sde连接文件的路径,只需在目录树中的SDE数据库上单击鼠标右键,转到属性,然后在“常规”选项卡上,从“名称”字段复制路径:
此页面上的示例3到5对于这个问题是惊人的:http : //help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//0017000000q7000000
这是我制作的简化版本,可让我仅使用Sql Server直接连接在python中进行即时连接。
"""
Name: sdeconn.py
Description: Utility functions for sde connections
"""
# Import system modules
import arcpy, os, sys
def connect(database, server="<default server>", username="<default user>", password="<default password>", version="SDE.DEFAULT"):
# Check if value entered for option
try:
#Usage parameters for spatial database connection to upgrade
service = "sde:sqlserver:" + server
account_authentication = 'DATABASE_AUTH'
version = version.upper()
database = database.lower()
# Check if direct connection
if service.find(":") <> -1: #This is direct connect
ServiceConnFileName = service.replace(":", "")
ServiceConnFileName = ServiceConnFileName.replace(";", "")
ServiceConnFileName = ServiceConnFileName.replace("=", "")
ServiceConnFileName = ServiceConnFileName.replace("/", "")
ServiceConnFileName = ServiceConnFileName.replace("\\", "")
else:
arcpy.AddMessage("\n+++++++++")
arcpy.AddMessage("Exiting!!")
arcpy.AddMessage("+++++++++")
sys.exit("\nSyntax for a direct connection in the Service parameter is required for geodatabase upgrade.")
# Local variables
Conn_File_NameT = server + "_" + ServiceConnFileName + "_" + database + "_" + username
if os.environ.get("TEMP") == None:
temp = "c:\\temp"
else:
temp = os.environ.get("TEMP")
if os.environ.get("TMP") == None:
temp = "/usr/tmp"
else:
temp = os.environ.get("TMP")
Connection_File_Name = temp + os.sep + Conn_File_NameT + ".sde"
if os.path.isfile(Connection_File_Name):
return Connection_File_Name
# Check for the .sde file and delete it if present
arcpy.env.overwriteOutput=True
# Variables defined within the script; other variable options commented out at the end of the line
saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME
saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION
print "\nCreating ArcSDE Connection File...\n"
# Process: Create ArcSDE Connection File...
# Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password, version, save_version_info
print temp
print Conn_File_NameT
print server
print service
print database
print account_authentication
print username
print password
print saveUserInfo
print version
print saveVersionInfo
arcpy.CreateArcSDEConnectionFile_management(temp, Conn_File_NameT, server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo)
for i in range(arcpy.GetMessageCount()):
if "000565" in arcpy.GetMessage(i): #Check if database connection was successful
arcpy.AddReturnMessage(i)
arcpy.AddMessage("\n+++++++++")
arcpy.AddMessage("Exiting!!")
arcpy.AddMessage("+++++++++\n")
sys.exit(3)
else:
arcpy.AddReturnMessage(i)
arcpy.AddMessage("+++++++++\n")
return Connection_File_Name
#Check if no value entered for option
except SystemExit as e:
print e.code
return
使用此脚本,我可以通过简单地调用以下命令即时创建连接文件:
import arcpy, sdeconn
myconnect1 = sdeconn.connect("database1", "server")
myconnect2 = sdeconn.connect("database2", "server")
这消除了数据库连接文件是从机不一致机或用户配置文件,用户配置文件的问题。
md5.new( server + "_" + ServiceConnFileName + "_" + database + "-" + version + "_" + username + password).hexdigest()
-回邮发布中的缩进不正确,因此我不知道我的连接失败。-代码将版本更改为大写,我的版本为小写
您需要像在ArcCatalog中通常那样定义SDE连接文档。然后,您将像这样在Python中创建图层的路径:
DataConnections = "C:\\AGS_GCSS_Tools\\DatabaseConnections\\"
TCA_Connection = "prod_sde.sde\\prod_SDE.GIS.PropertyTax" + CAPSYear + "\\prod_SDE.GIS.Tca"
TCA_Layer = DataConnections + TCA_Connection
这会将您的路径设置为您的.SDE文件所在的位置,但是随后您将该连接内的路径设置为您要查找的图层。就我而言,我还设置了Year变量。
您还可以直接在Query中定义连接路径。
PathSdeConnection= "C:\\Users\\{Username Of windows}\\AppData\\Roaming\\ESRI\\Desktop10.2\\ArcCatalog\\{name of ConenctionString}.sde
并在搜索等中使用它
with arcpy.da.SearchCursor(PathSdeConnection,("OBJECTID","SHAPE@","SHAPE@JSON"),{WhereClause})as cursor:
for row in cursor:
.
.
.