Create ArcGIS map service cache using Python script

Source: Internet
Author: User

Cache creation is completed using ArcGIS toolbox. In arcpy, you can use functions to call corresponding tools to automate Script Creation of cache.

There are several steps to create a cache. First, set the python environment variable. The Code is as follows:

# Set the environment variable def setworkspace (folder): If OS. Path. isdir (folder) = false: Print "the input workspace path is invalid! "Return env. workspace = folder

Second, you need to set the log file storage path. The Code is as follows:

Def setlogpath (logpath): currenttime = datetime. datetime. now () arg1 = currenttime. strftime ("% H-% m") arg2 = currenttime. strftime ("% Y-% m-% d % H: % m") Global logfile = OS. path. join (logpath, 'report_cmds.txt '% arg1) print "set the log file path:" + logfile

Create a cache slicing scheme, including creating a cache folder and generating the conf. xml configuration file. The corresponding function is createmapservercache_server. The call code and parameters are described as follows:

# Create a slicing mode file def evaluate (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 "log file path:" + logfile report = open (logfile, 'w ') try: startt IME = time. clock () # server: server name # service: Service name # dataframe: Data box name # cachedir: cache directory # tilingscheme: New indicates creating a new mode file, predefined indicates the use of the predefined mode file # scalestype: when creating a new cache mode, the standard scale is used for automatic grading or custom m scale # scales: when creating a new cache mode, if you use the standard method, you need to set the scale level # DPI: screen resolution, generally 96 # tilewidth: the width of the cached image, generally 256 or 512 pixels # tileheight: the height of the cached image, generally, 256 pixels or 512 pixels # cachetype: fused is usually used, or multi_layer # pathtoxml: pre-defined cache mode file path # tileorigin: Slice origin, that is, top left Corner coordinate # scalevalues: If scalestype = custom, the custom scale, for example, "600265; 350200; 225400; 44000" # inputlayers: If cachetype = multi_layer, name of the layer to be sliced # antialiasing: whether it is anti-sawtooth, none or antialiasing # tileformat: image format, png8, png24, png32, JPEG, mixed # tilecompressionquality: image compression ratio, 0 ~ 100 integer # storageformat: storage format, compact or exploded # uselocalcachedir: whether to use the local cache directory, true or false result = arcpy. partition (server, service, dataframe, cachedir, tilingscheme, scalestype, scales, DPI, expires, tileheight, cachetype, pathtoxml, tileorigin, scalevalues, inputlayers, antialiasing, tileformat, contents, storageformat, uselocalcachedir) finishtime = time. clock () Ela Export dtime = 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 failed t 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 "the map cache mode has been created. "Report. Close ()

After the cache slicing mode is created, you need to call the managemapservercachetiles_server function. The specific code is as follows:

# Manage cache slices def managecachetiles (server, service, dataframe, inputlayers, scalevalues, updatemode, extents, threadcount, antialiasing, pathtofeatureclass, ignorestatus ): # Open the log file global logfile report = open (logfile, 'w') Try: starttime = time. clock () # server: server name # service: Service name # dataframe: Data box name # scalevalues: the size of the cache to be created # inputlayers: the name of the layer to be created # antialiasing: this parameter can be ignored because the cache mode file already contains the anti-sawtooth setting # updatemode: Update Mode: recreate empty tiles, recreate all tiles, or delete tiles # extents: cache creation scope # threadcount: Number of threads used during cache creation # pathtofeatureclass: element class used to limit the cache creation region # ignorestatus: whether to ignore the cache creation status of the element class. The default value is ignore_completion_status_field, and track_completion_status indicates the tracking status result = arcpy. managemapservercachetiles_server (server, service, dataframe, inputlayers, scalevalues, updatemode, extents, threadcount, antialiasing, pathtofeatureclass, Ignorestatus) finishtime = time. clock () elapsedtime = finishtime-starttime # print the log information 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 failed t 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 "the map service cache has been created."

The above code is written in a cachehelper. py file. It is introduced as a module during the call, and then the above method is called in sequence to complete the cache creation. The Code is as follows:

Import syssys. path. append ("e :\\ codes \ Python") from cachehelper import setworkspace, setlogpath, createcachetilingscheme, managecachetilessetworkspace ("D:/testdata") setlogpath ("D: \ testdata ") createcachetilingscheme (" localhost "," Sichuan "," layer "," d: \ arcgisserver \ arcgiscache "," predefined "," standard ", "4", "96", "256", "256", "fused", "d: \ testdata \ conf. XML "," "," antialiasing "," Jpeg "," 75 "," Compact "," true ") managecachetiles (" localhost ", "Sichuan", "layer", "xzqh", "1000000; 500000; 250000", "recreate all tiles", "", "2", "", "track_completion_status ")

You can use the above Code to complete automatic graph cutting of the script.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.