通過Python指令碼建立ArcGIS地圖服務緩衝

來源:互聯網
上載者:User

緩衝建立工作通過ArcGIS Toolbox工具完成,在ArcPy中,可以通過函數調用相應的工具,來實現指令碼自動化建立緩衝。

建立緩衝有幾個步驟,首先設定Python環境變數,代碼如下:

# 設定環境變數def SetWorkspace(folder):    if os.path.isdir(folder) == False:        print "輸入的工作空間路徑無效!"        return    env.workspace = folder

其次需要設定記錄檔儲存路徑,代碼如下:

def SetLogPath(logPath):    currentTime = datetime.datetime.now()    arg1 = currentTime.strftime("%H-%M")    arg2 = currentTime.strftime("%Y-%m-%d %H:%M")    global logfile    logfile = os.path.join(logPath, 'report_%s.txt' % arg1)    print "設定記錄檔路徑:"+logfile

然後建立緩衝切片方案,包括建立快取檔案夾、產生conf.xml設定檔等,對應的函數為CreateMapServerCache_server,調用代碼和參數說明如下:

# 建立切片模式檔案def CreateCacheTilingScheme(server, service, dataFrame, cacheDir,                            tilingScheme, scalesType, scales, dpi,                            tileWidth, tileHeight, cacheType,                            pathToXml, tileOrigin, scaleValues,                            inputLayers, antialiasing, tileFormat,                            tileCompressionQuality,                            storageFormat, useLocalCacheDir):    # print results of the script to a report    global logfile    print "記錄檔路徑:"+logfile    report = open(logfile,'w')    try:        starttime = time.clock()        # server:伺服器名稱        # service:服務名稱        # dataFrame:資料框名稱        # cacheDir:緩衝目錄        # tilingScheme:NEW表示建立新的模式檔案,PREDEFINED表示使用預定義的模式檔案        # scalesType:建立新的緩衝模式時,使用STANDARD比例尺自動分級或CUSTOM自訂比例尺        # scales:建立新的緩衝模式時,如果用STANDARD方式,需要設定比例尺級數        # dpi:螢幕解析度,一般96即可        # tileWidth:緩衝圖片的寬度,一般256或512像素        # tileHeight:緩衝圖片的高度,一般256或512像素        # cacheType:通常使用FUSED,也可使用MULTI_LAYER        # pathToXml:預定義的緩衝模式檔案路徑        # tileOrigin:切片原點,即左上方座標        # scaleValues:如果scalesType=CUSTOM,自訂的比例尺,例如"600265;350200;225400;44000"        # inputLayers:如果cacheType=MULTI_LAYER時,需要切片的圖層名稱        # antialiasing:是否反鋸齒,NONE或ANTIALIASING        # tileFormat:圖片格式,PNG8、PNG24、PNG32、JPEG、MIXED        # tileCompressionQuality:圖片壓縮比,0~100的整數        # storageFormat:儲存形式,Compact或Exploded        # useLocalCacheDir:是否使用本機快取目錄,TRUE或FALSE        result = arcpy.CreateMapServerCache_server(server, service, dataFrame, cacheDir,                                          tilingScheme, scalesType, scales, dpi,                                          tileWidth, tileHeight, cacheType,                                          pathToXml, tileOrigin, scaleValues,                                          inputLayers, antialiasing, tileFormat,                                          tileCompressionQuality,                                          storageFormat, useLocalCacheDir)        finishtime = time.clock()        elapsedtime = finishtime - starttime        #print messages to a file        while result.status < 4:            time.sleep(0.2)        resultValue = result.getMessages()        report.write ("completed " + str(resultValue))        print "Created cache schema with custom scales successfully for "        + service + " in " + str(elapsedtime) + " sec \n on " + arg2     except Exception, e:        # If an error occurred, print line number and error message        tb = sys.exc_info()[2]        report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)        report.write(e.message)    print "已完成地圖緩衝模式建立。"    report.close()

建立了緩衝切片模式之後,需要調用ManageMapServerCacheTiles_server函數,具體代碼如下:

# 管理緩衝切片def ManageCacheTiles(server, service, dataFrame,                     inputLayers, scaleValues, updateMode,                     extents, threadCount ,Antialiasing,                     pathToFeatureClass, ignoreStatus):        # 開啟記錄檔    global logfile    report = open(logfile,'w')    try:        starttime = time.clock()        # server:伺服器名稱        # service:服務名稱        # dataFrame:資料框名稱        # scaleValues:需要建立緩衝的比例尺        # inputLayers:需要建立緩衝的圖層名稱        # antialiasing:該參數可忽略,因為緩衝模式檔案中已包含反鋸齒設定        # updateMode:更新模式,Recreate Empty Tiles、Recreate All Tiles或Delete Tiles        # extents:緩衝建立的範圍        # threadCount:緩衝建立過程使用的線程數        # pathToFeatureClass:用於限定緩衝建立地區的要素類        # ignoreStatus:是否忽略要素類的緩衝建立狀態,預設為IGNORE_COMPLETION_STATUS_FIELD,TRACK_COMPLETION_STATUS表示跟蹤狀態        result = arcpy.ManageMapServerCacheTiles_server(server, service, dataFrame,                                               inputLayers, scaleValues, updateMode,                                               extents, threadCount ,Antialiasing,                                               pathToFeatureClass, ignoreStatus)        finishtime = time.clock()        elapsedtime= finishtime - starttime        # 列印日誌資訊        while result.status < 4:            time.sleep(0.2)        resultValue = result.getMessages()        report.write ("completed " + str(resultValue))        print "Created cache tiles for given schema successfully for "        + service + " in " + str(elapsedtime) + " sec \n on " + arg2            except Exception, e:        # If an error occurred, print line number and error message        tb = sys.exc_info()[2]        report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)        report.write(e.message)            report.close()            print "已完成地圖服務緩衝建立。"

以上代碼是寫在一個cacheHelper.py檔案中,調用的時候將其當做模組引入,然後依次調用上述方法完成緩衝的建立工作,代碼如下:

import syssys.path.append("E:\\Codes\\Python")from cacheHelper import SetWorkspace,SetLogPath,CreateCacheTilingScheme,ManageCacheTilesSetWorkspace("D:/TestData")SetLogPath("D:\\TestData")CreateCacheTilingScheme("localhost","sichuan","圖層","D:\\arcgisserver\\arcgiscache","PREDEFINED","STANDARD","4","96","256","256","FUSED","D:\\TestData\\conf.xml","","","","ANTIALIASING","JPEG","75","Compact","TRUE")ManageCacheTiles("localhost","sichuan","圖層","xzqh","1000000;500000;250000","Recreate All Tiles","","2","","TRACK_COMPLETION_STATUS")

通過以上代碼即可完成指令碼自動切圖工作。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.