Skip To Content

例: テキスト ファイルにリストされたフォルダーとデータベースの登録

この ArcGIS 11.0 ドキュメントはアーカイブされており、今後更新されません。 コンテンツとリンクが古い場合があります。 最新のドキュメントをご参照ください

このスクリプト例では、ArcGIS Server サイトに登録するフォルダーおよびデータベースのテキスト ファイル リストを使用します。 データベースとフォルダーは、ArcGIS Pro または ArcGIS Enterprise ポータルを使用してフェデレーション ArcGIS Server サイトに登録することも、ArcGIS Pro または ArcGIS Server Manager を使用してスタンドアロン ArcGIS Server サイトに登録することもできますが、多数のデータ ソースの場所を登録する場合は、このスクリプトを使用する方が簡単です。

ヒント:

データの場所を ArcGIS Server サイトに登録する理由については、「ArcGIS Server のデータ ソース」をご参照ください。

入力テキスト ファイルは次のような構造になっています。

#dsName|dsType|serverPath|clientPath|hostname

dsName=<data store name>|dsType={FOLDER | DATABASE}|serverPath=<path to folder or database connection file used by the ArcGIS Server site>|clientPath=<path to folder or database connection file used by the publishing client>

  • dsName - データ ストア アイテムの名前。 任意の名前を指定できます。
  • dsType - 登録するデータ ストア アイテムのタイプ。 この例では、FOLDER または DATABASE を使用できます。
  • serverPath - サーバー コンピューターからアクセスする際のデータのパス。
  • clientPath - 公開者のコンピューターからアクセスする際のデータのパス。 指定しない場合は、サーバー パスが使用されます。 managed というキーワードを指定すると、データ ストア アイテムは ArcGIS Server サイトの管理されたデータベースとして登録されます。
  • hostname - 公開者のコンピューターの名前。 clientPath がフォルダーベースのパスの場合のみ必要です。 このプロパティを省略すると、スクリプトを実行しているコンピューターの名前が使用されます。

この例では、2 つのフォルダー パスと 2 つのデータベース接続ファイルがテキスト ファイルにリストされています。 2 つ目のフォルダー パス (testData) は ArcGIS Server サイトとローカル ArcGIS クライアントの別の場所を示しています。

#dsName|dsType|serverPath|clientPath|hostname

dsName=sharedData|dsType=FOLDER|serverPath=\\sharedserver\data|clientPath=\\sharedserver\data
dsName=testData|dsType=FOLDER|serverPath=\\test_server\data|clientPath=c:\mydata|hostname=qalab_publisher
dsName=entGeodatabase|dsType=DATABASE|serverPath=C:\data\db_connections\Connection to enterprisegdb1.sde|clientPath=C:\data\db_Connections\Connection to enterprisegdb1.sde
dsName=spatialDatabase|dsType=DATABASE|serverPath=C:\data\db_Connections\Connection to projectdb.sde|clientPath=C:\data\db_Connections\Connection to projectdb.sde

次のサンプル スクリプトでは、ArcGIS Server 接続ファイルを作成するために ArcGIS Server サイトの接続情報の入力をユーザーに求めます。 また、データ ソースのリストを含むテキスト ファイルの場所を指定するように要求し、ファイル内のデータ ソースを ArcGIS Server サイトに登録します。

import sys
import os
import getpass
import arcpy
import codecs


def main(argv=None):
    
    # Ask for admin user name and password
    
    username = input("Enter user name: ")
    password = getpass.getpass("Enter password: ")
    
    # Ask for server name & port
    serverName = input("Enter server name: ")
    serverPort = input("Enter server port: ")
 
    # Create a connection file to the server and save it in the same location as that of the script
    serverURL="http://"+serverName+":"+str(serverPort)+"/arcgis/admin"
    try:
        arcpy.mapping.CreateGISServerConnectionFile("PUBLISH_GIS_SERVICES",os.curdir,serverName+".ags",serverURL,"ARCGIS_SERVER",username=username,password=password)
    except Exception as e:
        print(e)
    
    agsConnection = os.path.join(os.curdir, serverName+".ags")
    
    if not os.path.isfile(agsConnection):
        print("Unable to connect to ArcGIS Server. Exiting.")
        sys.exit(1)

    # Input File that contains the data store information
    dataStoresFile = input("Path to pipe-delimited text file containing datastore information: ")
    
    num = 0 
    datastores = {}
    
    for datastoreRow in readlinesFromInputFile(dataStoresFile):
            
        datastoreEntry = {}
        
        for index in range(len(datastoreRow)):
            
            datastoreProp = datastoreRow[index].split("=")
            
            if datastoreProp[0] == "dsName":
                datastoreEntry["connection_name"] = datastoreProp[1]
            if datastoreProp[0] == "dsType":
                datastoreEntry["datastore_type"] = datastoreProp[1]
            if datastoreProp[0] == "serverPath":
                datastoreEntry["server_path"] = datastoreProp[1]
            if datastoreProp[0] == "clientPath":
                datastoreEntry["client_path"] = datastoreProp[1]
            if datastoreProp[0] == "hostname":
                datastoreEntry["hostname"] = datastoreProp[1]
         
            # Add the datastore information to a dictionary
            datastores["datastore" + str(num)] = datastoreEntry
        
        num +=1

    # Call helper functions to register datastores
    addDataStores(datastores,agsConnection)


# A function that reads lines from the input file
def readlinesFromInputFile(filename, delim='|'):
    file = codecs.open(filename,'r','utf-8-sig')
    for line in file.readlines():
        # Remove the trailing whitespaces and the newline characters
        line = line.rstrip()
        
        if line.startswith('#') or len(line) == 0:
            pass # Skip the lines that contain # at the beginning or any empty lines
        else:
            # Split the current line into list
            yield line.split(delim)
    file.close()


def addDataStores(datastoresDict,agsConnection):
    
    for datastoreToAdd in datastoresDict:
        # Build the dictionary with the role name and description               
        datastoresDict[datastoreToAdd]["connection_file"] =  agsConnection
        
        print("Adding the datastore: " + datastoresDict[datastoreToAdd]['connection_name'])
        
        try:
            arcpy.AddDataStoreItem(**datastoresDict[datastoreToAdd])
            print("Successfully added the datastore: " + datastoresDict[datastoreToAdd]['connection_name'])
        except Exception as e:
            print("Adding of the datastore: " + datastoresDict[datastoreToAdd]['connection_name'] + " failed.")
            print(e)

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))