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

缓存创建工作通过ArcGIS Toolbox工具完成,在ArcPy中,可以通过函数调用相应的工具,来实现脚本自动化创建缓存。

创建缓存有几个步骤,首先设置Python环境变量,代码如下:
[python]  view plain  copy
  1. # 设置环境变量  
  2. def SetWorkspace(folder):  
  3.     if os.path.isdir(folder) == False:  
  4.         print "输入的工作空间路径无效!"  
  5.         return  
  6.     env.workspace = folder  

其次需要设置日志文件保存路径,代码如下:

[python]  view plain  copy
  1. def SetLogPath(logPath):  
  2.     currentTime = datetime.datetime.now()  
  3.     arg1 = currentTime.strftime("%H-%M")  
  4.     arg2 = currentTime.strftime("%Y-%m-%d %H:%M")  
  5.     global logfile  
  6.     logfile = os.path.join(logPath, 'report_%s.txt' % arg1)  
  7.     print "设置日志文件路径:"+logfile  

然后创建缓存切片方案,包括创建缓存文件夹、生成conf.xml配置文件等,对应的函数为CreateMapServerCache_server,调用代码和参数说明如下:

[python]  view plain  copy
  1. # 创建切片模式文件  
  2. def CreateCacheTilingScheme(server, service, dataFrame, cacheDir,  
  3.                             tilingScheme, scalesType, scales, dpi,  
  4.                             tileWidth, tileHeight, cacheType,  
  5.                             pathToXml, tileOrigin, scaleValues,  
  6.                             inputLayers, antialiasing, tileFormat,  
  7.                             tileCompressionQuality,  
  8.                             storageFormat, useLocalCacheDir):  
  9.     # print results of the script to a report  
  10.   
  11.     global logfile  
  12.     print "日志文件路径:"+logfile  
  13.     report = open(logfile,'w')  
  14.   
  15.     try:  
  16.         starttime = time.clock()  
  17.         # server:服务器名称  
  18.         # service:服务名称  
  19.         # dataFrame:数据框名称  
  20.         # cacheDir:缓存目录  
  21.         # tilingScheme:NEW表示创建新的模式文件,PREDEFINED表示使用预定义的模式文件  
  22.         # scalesType:创建新的缓存模式时,使用STANDARD比例尺自动分级或CUSTOM自定义比例尺  
  23.         # scales:创建新的缓存模式时,如果用STANDARD方式,需要设置比例尺级数  
  24.         # dpi:屏幕分辨率,一般96即可  
  25.         # tileWidth:缓存图片的宽度,一般256或512像素  
  26.         # tileHeight:缓存图片的高度,一般256或512像素  
  27.         # cacheType:通常使用FUSED,也可使用MULTI_LAYER  
  28.         # pathToXml:预定义的缓存模式文件路径  
  29.         # tileOrigin:切片原点,即左上角坐标  
  30.         # scaleValues:如果scalesType=CUSTOM,自定义的比例尺,例如"600265;350200;225400;44000"  
  31.         # inputLayers:如果cacheType=MULTI_LAYER时,需要切片的图层名称  
  32.         # antialiasing:是否反锯齿,NONE或ANTIALIASING  
  33.         # tileFormat:图片格式,PNG8、PNG24、PNG32、JPEG、MIXED  
  34.         # tileCompressionQuality:图片压缩比,0~100的整数  
  35.         # storageFormat:存储形式,Compact或Exploded  
  36.         # useLocalCacheDir:是否使用本地缓存目录,TRUE或FALSE  
  37.         result = arcpy.CreateMapServerCache_server(server, service, dataFrame, cacheDir,  
  38.                                           tilingScheme, scalesType, scales, dpi,  
  39.                                           tileWidth, tileHeight, cacheType,  
  40.                                           pathToXml, tileOrigin, scaleValues,  
  41.                                           inputLayers, antialiasing, tileFormat,  
  42.                                           tileCompressionQuality,  
  43.                                           storageFormat, useLocalCacheDir)  
  44.         finishtime = time.clock()  
  45.         elapsedtime = finishtime - starttime  
  46.   
  47.         #print messages to a file  
  48.         while result.status < 4:  
  49.             time.sleep(0.2)  
  50.         resultValue = result.getMessages()  
  51.         report.write ("completed " + str(resultValue))  
  52.   
  53.         print "Created cache schema with custom scales successfully for "  
  54.         + service + " in " + str(elapsedtime) + " sec \n on " + arg2   
  55.   
  56.     except Exception, e:  
  57.         # If an error occurred, print line number and error message  
  58.         tb = sys.exc_info()[2]  
  59.         report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)  
  60.         report.write(e.message)  
  61.   
  62.     print "已完成地图缓存模式创建。"  
  63.   
  64.     report.close()  

创建了缓存切片模式之后,需要调用ManageMapServerCacheTiles_server函数,具体代码如下:

[python]  view plain  copy
  1. # 管理缓存切片  
  2. def ManageCacheTiles(server, service, dataFrame,  
  3.                      inputLayers, scaleValues, updateMode,  
  4.                      extents, threadCount ,Antialiasing,  
  5.                      pathToFeatureClass, ignoreStatus):      
  6.     # 打开日志文件  
  7.     global logfile  
  8.     report = open(logfile,'w')  
  9.   
  10.     try:  
  11.         starttime = time.clock()  
  12.         # server:服务器名称  
  13.         # service:服务名称  
  14.         # dataFrame:数据框名称  
  15.         # scaleValues:需要创建缓存的比例尺  
  16.         # inputLayers:需要创建缓存的图层名称  
  17.         # antialiasing:该参数可忽略,因为缓存模式文件中已包含反锯齿设置  
  18.         # updateMode:更新模式,Recreate Empty Tiles、Recreate All Tiles或Delete Tiles  
  19.         # extents:缓存创建的范围  
  20.         # threadCount:缓存创建过程使用的线程数  
  21.         # pathToFeatureClass:用于限定缓存创建区域的要素类  
  22.         # ignoreStatus:是否忽略要素类的缓存创建状态,默认为IGNORE_COMPLETION_STATUS_FIELD,TRACK_COMPLETION_STATUS表示跟踪状态  
  23.         result = arcpy.ManageMapServerCacheTiles_server(server, service, dataFrame,  
  24.                                                inputLayers, scaleValues, updateMode,  
  25.                                                extents, threadCount ,Antialiasing,  
  26.                                                pathToFeatureClass, ignoreStatus)  
  27.         finishtime = time.clock()  
  28.         elapsedtime= finishtime - starttime  
  29.   
  30.         # 打印日志信息  
  31.         while result.status < 4:  
  32.             time.sleep(0.2)  
  33.         resultValue = result.getMessages()  
  34.         report.write ("completed " + str(resultValue))  
  35.   
  36.         print "Created cache tiles for given schema successfully for "  
  37.         + service + " in " + str(elapsedtime) + " sec \n on " + arg2  
  38.           
  39.     except Exception, e:  
  40.         # If an error occurred, print line number and error message  
  41.         tb = sys.exc_info()[2]  
  42.         report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)  
  43.         report.write(e.message)  
  44.           
  45.     report.close()  
  46.           
  47.     print "已完成地图服务缓存创建。"  

以上代码是写在一个cacheHelper.py文件中,调用的时候将其当做模块引入,然后依次调用上述方法完成缓存的创建工作,代码如下:
[python]  view plain  copy
  1. import sys  
  2. sys.path.append("E:\\Codes\\Python")  
  3. from cacheHelper import SetWorkspace,SetLogPath,CreateCacheTilingScheme,ManageCacheTiles  
  4. SetWorkspace("D:/TestData")  
  5. SetLogPath("D:\\TestData")  
  6. CreateCacheTilingScheme("localhost","sichuan","图层","D:\\arcgisserver\\arcgiscache","PREDEFINED","STANDARD","4","96","256","256","FUSED","D:\\TestData\\conf.xml","","","","ANTIALIASING","JPEG","75","Compact","TRUE")  
  7. ManageCacheTiles("localhost","sichuan","图层","xzqh","1000000;500000;250000","Recreate All Tiles","","2","","TRACK_COMPLETION_STATUS")  
通过以上代码即可完成脚本自动切图工作。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值