通过Python脚本创建ArcGIS地图服务缓存

缓存创建工作通过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 sys
sys.path.append("E:\\Codes\\Python")
from cacheHelper import SetWorkspace,SetLogPath,CreateCacheTilingScheme,ManageCacheTiles
SetWorkspace("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")

通过以上代码即可完成脚本自动切图工作。

转载自:https://blog.csdn.net/iteye_8029/article/details/82334117

You may also like...