def publish_images(draft_workspace, input_raster, con_path, service_name): import arcpy try: start_timeStampName = time.strftime('%Y_%m_%d %H:%M:%S', time.localtime(time.time())) start = time.time() s = start print("Publishing Services,Start time:" + start_timeStampName) arcpy.env.overwriteOutput = True # Set output file names sddraft_filename = os.path.join(draft_workspace, service_name + '.sddraft') arcpy.CreateImageSDDraft(input_raster, sddraft_filename, service_name, 'ARCGIS_SERVER', con_path, False, None, "Publish image service for smart show", "lands,image service") end_timeStampName = time.strftime('%Y_%m_%d %H:%M:%S', time.localtime(time.time())) end = time.time() elapse_time = end - start print("Create image sddraft finished, at time:" + end_timeStampName, "Elapsed time:", elapse_time, "s") # Stage Service start = time.time() sd_filename = service_name + ".sd" sd_output_filename = os.path.join(draft_workspace, sd_filename) arcpy.StageService_server(sddraft_filename, sd_output_filename) end_timeStampName = time.strftime('%Y_%m_%d %H:%M:%S', time.localtime(time.time())) end = time.time() elapse_time = end - start print("Stage Service finished, at time:" + end_timeStampName, "Elapsed time:", elapse_time, "s") # Share to portal start = time.time() arcpy.UploadServiceDefinition_server(sd_output_filename, con_path) end_timeStampName = time.strftime('%Y_%m_%d %H:%M:%S', time.localtime(time.time())) end = time.time() elapse_time = end - start print( "Uploading service definition finished, at time:" + end_timeStampName, "Elapsed time:", elapse_time, "s") elapse_time = end - s print( "Completed publishing images service,ending time:" + end_timeStampName, "Total Elapsed time:", elapse_time, "s") except: print("Publish images failed.") print() arcpy.GetMessages()
def create_image_SD_draft(self, product, layer, sd_draft, service, connection_file): _product_summary = product.summary _product_tags = product.tags # summaryP = summary[product] # tagsP = tags[product] arcpy.CreateImageSDDraft(raster_or_mosaic_layer=layer, out_sddraft=sd_draft, service_name=service, server_type='FROM_CONNECTION_FILE', connection_file_path=connection_file, copy_data_to_server=False, folder_name='Climate', summary=_product_summary, tags=_product_tags) logger.debug("SDDraft file for {0} is created".format( product.product_name)) return None
def publish_image_service(self, config_entry, filename, sddraft): self.message( "Generating service definition draft for image service...") arcpy.CreateImageSDDraft( raster_or_mosaic_layer=config_entry["input"], out_sddraft=sddraft, service_name=config_entry["serviceName"] if "serviceName" in config_entry else filename, connection_file_path=self.connection_file_path, server_type=config_entry["serverType"] if "serverType" in config_entry else 'ARCGIS_SERVER', copy_data_to_server=config_entry["copyDataToServer"] if "copyDataToServer" in config_entry else False, folder_name=config_entry["folderName"] if "folderName" in config_entry else None, summary=config_entry["summary"] if "summary" in config_entry else None, tags=config_entry["tags"] if "tags" in config_entry else None) return arcpy.mapping.AnalyzeForSD(sddraft)
staging_folder_path, username, password, "SAVE_USERNAME") print ' ' + out_name.decode('utf-8') + u' 已创建' # Step2 创建影像服务定义草稿文件(.sddraft) print u'Step2 创建影像服务定义草稿文件...' connoctionfile = os.path.join(outdir, out_name) service = servicename sddraft = os.path.join(outdir, service + '.sddraft') ARCPY.CreateImageSDDraft(mdpath, sddraft, service, 'ARCGIS_SERVER', copy_data_to_server=False) print ' ' + service + '.sddraft' + u' 已创建' # Step3 分析服务定义草稿 print u'Step3 分析服务定义草稿文件...' analysis = ARCPY.mapping.AnalyzeForSD(sddraft) print(u" 分析服务定义草稿结果:") for key in list(analysis.keys()): print(" ---{}---".format(key.upper())) for ((message, code), layerlist) in analysis[key].items(): print(" (CODE {}) {} ".format(code, message)) # Step4 过渡 sddraft 到服务定义文件sd print u'Step4 过渡sddraft 到服务定义文件sd...' outSDfile = os.path.join(outdir, service+".sd")
def processJob(ProjectJob, project, ProjectUID, serverConnectionFile, serverFunctionPath, update=False, runCount=0): ProjectFolder = ProjectFolders.getProjectFolderFromDBRow( ProjectJob, project) ProjectID = ProjectJob.getProjectID(project) ProjectState = ProjectJob.getState(project) ProjectYear = ProjectJob.getYear(project) ProjectAlias = ProjectJob.getAlias(project) ProjectAliasClean = ProjectJob.getAliasClean(project) project_wmx_jobid = ProjectJob.getWMXJobID(project) Deliver = CMDR.Deliver() #delivery = Deliver.getDeliver(project_wmx_jobid) delivery = Deliver.getDeliver(ProjectID) dateDeliver = Deliver.getDeliverDate(delivery) startupType = "STARTED" Utility.printArguments([ "ProjectJob", "project", "ProjectUID", "serverConnectionFile", "serverFunctionPath", "update", "runCount", "ProjectFolder", "ProjectID", "ProjectState", "ProjectYear", "ProjectAlias", "ProjectAliasClean", "startupType" ], [ ProjectJob, project, ProjectUID, serverConnectionFile, serverFunctionPath, update, runCount, ProjectFolder, ProjectID, ProjectState, ProjectYear, ProjectAlias, ProjectAliasClean, startupType ], "A07_A Publish Project") # serverFunctionPath = Raster.getServerRasterFunctionsPath(jobID) ssFunctions = None if serverFunctionPath is not None: ssFunctions = Raster.getServerSideFunctions(serverFunctionPath) folderName = ProjectState # If the project has been moved for publishing, update the project directory old_path, new_path = updateJobDirectory(project_wmx_jobid, ProjectJob, project) old_ProjectID = ProjectID arcpy.AddMessage( "\n\n-----------------------------------------------------------") try: arcpy.AddMessage("Job directory paths: \n\tOLD: {}\n\tNEW: {}".format( old_path, new_path)) doRepath = True if str(old_path).lower().strip() == str(new_path).lower().strip(): arcpy.AddMessage( "Job directory paths old/new match, checking MD first record project folder value" ) filegdb_name = "{}_{}.gdb".format( ProjectFolder.published.fgdb_name[:-4], FoldersConfig.DTM) arcpy.AddMessage("checking fgdb '{}' ".format(filegdb_name)) dtm_md_path = os.path.join(new_path, ProjectID, FoldersConfig.published_dir, filegdb_name, FoldersConfig.DTM) arcpy.AddMessage( "checking MD '{}' first record project folder value".format( dtm_md_path)) record_project_path, uid = Utility.getExistingRecord( dtm_md_path, ["Project_Dir"], 0) arcpy.AddMessage("first record is {}".format(record_project_path)) record_project_path = list(record_project_path)[0] arcpy.AddMessage( "MD first record project folder value is {}".format( record_project_path)) # add a slash back in because strings remove it and remove the project name at the end old_path, old_ProjectID = os.path.split("\{}".format( str(record_project_path).strip())) arcpy.AddMessage( "Job directory paths: \n\tOLD: {}\n\tNEW: {}\n\tOLD Project ID: {}\n\tNEW Project ID: {}" .format(old_path, new_path, old_ProjectID, ProjectID)) if str(old_path).lower().strip() == str(new_path).lower().strip(): doRepath = False arcpy.AddMessage("Job directory paths match, doRepath = False") except: pass arcpy.AddMessage( "-----------------------------------------------------------\n\n") md_list = [ FoldersConfig.DTM, FoldersConfig.DSM, FoldersConfig.DLM, FoldersConfig.DHM, FoldersConfig.DCM, FoldersConfig.INT ] for md_name in md_list: update_paths_success = False # @TODO Add more info here! serviceDescription = "for project '{}' within state {} published in the year {}".format( ProjectAlias, ProjectState, ProjectYear) serviceTags = ",".join( [ProjectID, ProjectAliasClean, ProjectState, str(ProjectYear)]) filegdb_name = "{}_{}.gdb".format(ProjectFolder.published.fgdb_name, md_name) if ProjectFolder.published.fgdb_name.endswith(".gdb"): filegdb_name = "{}_{}.gdb".format( ProjectFolder.published.fgdb_name[:-4], md_name) #ProjectMDs_fgdb_path = os.path.join(ProjectFolder.published.path, filegdb_name) new_project_path = os.path.join(new_path, ProjectID) old_project_path = os.path.join(old_path, ProjectID) if str(ProjectID).lower().strip() != str( old_ProjectID).lower().strip(): old_project_path = os.path.join(old_path, old_ProjectID) #arcpy.AddMessage("OLD File Geodatabase Path: {0}".format(ProjectMDs_fgdb_path)) new_publish_path = os.path.join(new_project_path, "PUBLISHED") old_publish_path = os.path.join(old_project_path, "PUBLISHED") #arcpy.AddMessage("OLD File Geodatabase Path: {0}".format(ProjectMDs_fgdb_path)) new_delivered_path = os.path.join(new_project_path, "DELIVERED") old_delivered_path = os.path.join(old_project_path, "DELIVERED") new_projectMDs_fgdb_path = os.path.join(new_publish_path, filegdb_name) arcpy.AddMessage( "File Geodatabase Path: {0}".format(new_projectMDs_fgdb_path)) # Ensure the master_md_path exists if arcpy.Exists(new_projectMDs_fgdb_path): project_md_path = os.path.join(new_projectMDs_fgdb_path, md_name) arcpy.AddMessage( "Mosaic Dataset Path: {0}".format(project_md_path)) if arcpy.Exists(project_md_path): try: arcpy.AddMessage( "Repairing Mosaic Dataset Paths: {}\n\told: {}\n\tnew: {}" .format(new_projectMDs_fgdb_path, old_project_path, new_project_path)) arcpy.RepairMosaicDatasetPaths_management( in_mosaic_dataset=project_md_path, paths_list="# {0} {1}".format(old_project_path, new_project_path), where_clause="1=1") Utility.addToolMessages() #arcpy.AddMessage("Repairing Mosaic Dataset Paths: {}\n\told: {}\n\tnew: {}".format(new_projectMDs_fgdb_path, old_delivered_path, new_delivered_path)) #arcpy.RepairMosaicDatasetPaths_management(in_mosaic_dataset=project_md_path, paths_list="# {0} {1}".format(old_delivered_path, new_delivered_path), where_clause="1=1") update_paths_success = True except: if md_name <> FoldersConfig.DHM and md_name <> FoldersConfig.DCM: arcpy.AddWarning( "Failed to update paths, mosaic dataset paths should be verified and updated by hand if necessary. {}" .format(project_md_path)) try: out_table = "{}_Paths".format(project_md_path) arcpy.ExportMosaicDatasetPaths_management( in_mosaic_dataset=project_md_path, out_table=out_table, where_clause="1=1", export_mode="ALL", types_of_paths="RASTER;ITEM_CACHE") Utility.addToolMessages() arcpy.AddMessage( "List of repaired Mosaic Dataset Paths: {}".format( out_table)) except: pass project_md_ocs_path = "{}_OCS".format(project_md_path) if arcpy.Exists(project_md_ocs_path): try: arcpy.AddMessage( "Repairing Mosaic Dataset Paths: {}\n\told: {}\n\tnew: {}" .format(project_md_ocs_path, old_project_path, new_project_path)) arcpy.RepairMosaicDatasetPaths_management( in_mosaic_dataset=project_md_ocs_path, paths_list="# {0} {1}".format( old_project_path, new_project_path), where_clause="1=1") Utility.addToolMessages() except: arcpy.AddWarning( "Failed to update paths, mosaic dataset paths should be verified and updated by hand if necessary. {}" .format(project_md_ocs_path)) try: out_table = "{}_Paths".format(project_md_ocs_path) arcpy.ExportMosaicDatasetPaths_management( in_mosaic_dataset=project_md_ocs_path, out_table=out_table, where_clause="1=1", export_mode="ALL", types_of_paths="RASTER;ITEM_CACHE") Utility.addToolMessages() arcpy.AddMessage( "List of repaired Mosaic Dataset Paths: {}". format(out_table)) except: pass serviceName = "{}_{}".format(ProjectID, md_name) arcpy.AddMessage("Service Name: {0}".format(serviceName)) # Retrieve some properties from the Mosaic Dataset to place in the tags field cellsizeResult = arcpy.GetRasterProperties_management( project_md_path, property_type="CELLSIZEX", band_index="") Utility.addToolMessages() cellsizeX = cellsizeResult.getOutput(0) # Get the units of the Mosaic Dataset descMD = arcpy.Describe(project_md_path) SpatRefMD = descMD.SpatialReference SpatRefUnitsMD = SpatRefMD.linearUnitName SpatRefNameMD = SpatRefMD.name arcpy.AddMessage( "Spatial Reference name of Mosaic Dataset: {0}".format( SpatRefNameMD)) arcpy.AddMessage( "Spatial Reference X,Y Units of Mosaic Dataset: {0}". format(SpatRefUnitsMD)) # append the cellsize and units of the Mosaic Dataset to the tags serviceTags = "{}, {}, {}".format(serviceTags, cellsizeX, SpatRefUnitsMD) serviceDescription = "{} {}. Horizontal spatial reference is {} and cell size is {} {}.".format( md_name, serviceDescription, SpatRefNameMD, cellsizeX, SpatRefUnitsMD) serviceDescription = "{}. Please note that cell size does not refer to the underlying data's cell size.".format( serviceDescription) serviceDescription = "{}. You must check the meta-data for the underlying elevation data's resolution information (cell width, cell height, and Lidar point spacing).".format( serviceDescription) arcpy.AddMessage("Service Tags: {0}".format(serviceTags)) arcpy.AddMessage( "Service description: {0}".format(serviceDescription)) # Look for RasterPath in the list of allowed fields, and if found, don't publish # the mosaic dataset. Exposing the contents of RasterPath could compromise the # security of the Image Service. allowedFieldListMD = descMD.AllowedFields arcpy.AddMessage("AllowedFields in MD Properties: {0}".format( allowedFieldListMD)) if True or "RASTERPATH;" not in allowedFieldListMD.upper(): # Create a list to manipulate server-side functions # Bring Hillshade to the top of the list so it is default ssFunctionsLst = list([]) ssFunctionsList = "" if ssFunctions is not None: ssFunctionsLst = ssFunctions.split(";") if len(ssFunctionsLst) > 0: foundHillshade = False if md_name <> FoldersConfig.INT: for i, s in enumerate(ssFunctionsLst): if 'HILLSHADE' in s.upper(): arcpy.AddMessage( "Will re-order SS Functions for {} so {} is default" .format(md_name, s)) foundHillshade = True break else: for i, s in enumerate(ssFunctionsLst): if 'METER' in s.upper(): arcpy.AddMessage( "Will re-order SS Functions for {} so {} is default" .format(md_name, s)) foundHillshade = True break # if Hillshade is found then re-order the list # Don't apply hillshade to intensity if foundHillshade: ssFunctionsLst.insert(0, ssFunctionsLst.pop(i)) arcpy.AddMessage( "Re-ordered SS Functions to (first is default): " .format(ssFunctionsLst)) # convert the list of server-side functions into a comma delimited string ssFunctionsList = ",".join(ssFunctionsLst) arcpy.AddMessage( "Server-side Functions: {0}\n".format( ssFunctionsList)) # Create image service definition draft arcpy.AddMessage( "Creating image service definition draft file: ") wsPath = os.path.dirname(os.path.dirname(project_md_path)) sddraftPath = os.path.join(wsPath, serviceName + ".sddraft") arcpy.Delete_management(sddraftPath) arcpy.AddMessage("\tMDPath='{}'".format(project_md_path)) arcpy.AddMessage("\tSDPath='{}'".format(sddraftPath)) arcpy.AddMessage("\tServiceName='{}'".format(serviceName)) arcpy.AddMessage("\tFolderName='{}'".format(folderName)) arcpy.AddMessage( "\tSummary='{}'".format(serviceDescription)) arcpy.AddMessage("\tTags='{}'".format(serviceTags)) arcpy.CreateImageSDDraft(project_md_path, sddraftPath, serviceName, "ARCGIS_SERVER", connection_file_path=None, copy_data_to_server=False, folder_name=folderName, summary=serviceDescription, tags=serviceTags) # Edit the service definition draft if user specified server-side functions # or if user wants to enable download on the Image Service updateSDServerSideFunctions(ssFunctionsLst, ssFunctionsList, sddraftPath, update) # Analyze service definition draft arcpy.AddMessage( "Analyzing service definition draft file...") analysis = arcpy.mapping.AnalyzeForSD(sddraftPath) for key in ('messages', 'warnings', 'errors'): arcpy.AddMessage("----" + key.upper() + "---") analysis_vars = analysis[key] for ((message, code), data ) in analysis_vars.iteritems(): # @UnusedVariable msg = (" ", message, " (CODE %i)" % code) arcpy.AddMessage("".join(msg)) if analysis['errors'] == {}: arcpy.AddMessage( "Staging and publishing service definition...") # StageService arcpy.AddMessage("Staging sddraft file to sd file") sdPath = sddraftPath.replace(".sddraft", ".sd") arcpy.Delete_management(sdPath) RunUtil.runTool(r'ngce\pmdm\a\A07_B_StageSD.py', [ sddraftPath, sdPath, serverConnectionFile, startupType ], bit32=True, log_path=ProjectFolder.derived.path) # arcpy.StageService_server(sddraftPath, sdPath) # # try: # # # UploadServiceDefinition # # arcpy.AddMessage("Publishing mosaic data set as image service.") # # arcpy.UploadServiceDefinition_server(sdPath, serverConnectionFile, "#", "#", "#", "#", startupType) # # except Exception as e: # # if runCount < 1: # ## PublishMosaicDataset(jobID, serverConnectionFile, True, 1) # # processJob(ProjectJob, project, ProjectUID, serverConnectionFile, serverFunctionPath, update=True, runCount=1) # # break # # else: # # raise e else: # if the sddraft analysis contained errors, display them arcpy.AddError(analysis['errors']) else: arcpy.AddError( "Exiting: Found 'RasterPath' in list of allowed MD fields. Please remove this field from the list before publishing." ) arcpy.AddError( " To remove RasterPath from the list, go to Mosaic Dataset Properties, Defaults tab, Allowed Fields..." ) # Clean up and delete the .sd file Utility.deleteFileIfExists(sdPath, False) # For some reason publishing breaks the referenced mosaics. # The function paths also don't update properly. # So delete them and re-create later. if md_name == FoldersConfig.DHM or md_name == FoldersConfig.DCM: arcpy.AddMessage( "Deleting Mosaic Dataset to recreate later {}".format( project_md_path)) Utility.deleteFileIfExists(project_md_path, True) else: arcpy.AddWarning( "Project mosaic dataset not found '{}'.".format( project_md_path)) else: arcpy.AddError( "Project file geodatabase not found '{}'. Please add this before proceeding." .format(new_projectMDs_fgdb_path)) # FOR LOOP ## ## Re-create the MD if it is FoldersConfig.DHM, FoldersConfig.DCM ## A06_A_CreateProjectMosaicDataset.CreateProjectMDs(project_wmx_jobid, dateDeliver=dateDeliver)
def execute(self, parameters, messages): """The source code of the tool.""" agspath = "C:/Users/Mr.HL/AppData/Roaming/ESRI/Desktop10.2/ArcCatalog/arcgis on localhost_6080 (admin).ags" mxdpath = parameters[0].valueAsText new_mxd = arcpy.mapping.MapDocument(mxdpath) dirname = os.path.dirname(mxdpath) mxdname = os.path.basename(mxdpath) dotindex = mxdname.index('.') servicename = mxdname[0:dotindex] messages.addMessage('service_name {0}'.format(servicename)) sddratf = os.path.abspath(servicename + ".sddraft") sd = os.path.abspath(servicename + ".sd") if os.path.exists(sd): os.remove(sd) arcpy.CreateImageSDDraft(new_mxd, sddratf, servicename, "ARCGIS_SERVER", agspath, False, None, "Ortho Images", "dddkk") analysis = arcpy.mapping.AnalyzeForSD(sddratf) # 打印分析结果 messages.addMessage( "the following information wa returned during analysis of the MXD") for key in ('messages', 'warnings', 'errors'): messages.addMessage('---- {0} ----'.format(key.upper())) vars = analysis[key] for ((message, code), layerlist) in vars.iteritems(): messages.addMessage( ' message (CODE {0}) applies to:'.format(code)) for layer in layerlist: messages.addMessage(layer.name) if analysis['errors'] == {}: try: arcpy.StageService_server(sddratf, sd) arcpy.UploadServiceDefinition_server(sd, agspath) messages.addMessage('service successfully published') # 定义服务器缓存 messages.addMessage("开始定义服务器缓存") # list of input variable for map service properties tiling_scheme_type = "NEW" scale_type = "CUSTOM" num_of_scales = 9 scales = [7688, 7200, 6600, 6000, 5400, 4800, 4200, 3600, 3000] dot_per_inch = 96 tile_origin = "-20037700 30198300" # <X>-20037700</X> <Y>30198300</Y> tile_size = "256 x 256" cache_tile_format = "PNG8" tile_compression_quality = "" storage_format = "EXPLODED" predefined_tile_scheme = "" input_service = 'GIS Servers/arcgis on localhost_6080 (admin)/{0}.MapServer'.format( servicename) cache_path = "D:/arcgisserver/directories-new/arcgiscache" try: start_time = time.clock() result = arcpy.CreateMapServerCache_server( input_service, cache_path, tiling_scheme_type, scale_type, num_of_scales, dot_per_inch, tile_size, predefined_tile_scheme, tile_origin, scales, cache_tile_format, tile_compression_quality, storage_format) # message to a file while result.status < 4: time.sleep(0.2) # result_value = result.getMessage() # messages.addMessage('completed {0}'.format(str(result_value))) except Exception, e: tb = sys.exc_info()[2] messages.addMessage('Failed to stop 1 \n Line {0}'.format( tb.tb_lineno)) messages.addMessage(e.message) # 生成切片 messages.addMessage("开始切片") scales = [7688, 7200, 6600, 6000, 5400, 4800, 4200, 3600, 3000] num_of_caching_service_instance = 2 update_mode = "RECREATE_ALL_TILES" area_of_interest = "" wait_for_job_completion = "WAIT" update_extents = "" input_service = 'C:/Users/Mr.HL/AppData/Roaming/ESRI/Desktop10.2/ArcCatalog/arcgis on localhost_6080 (' \ 'admin)/{0}.MapServer'.format(servicename) try: result = arcpy.ManageMapServerCacheTiles_server( input_service, scales, update_mode, num_of_caching_service_instance, area_of_interest, update_extents, wait_for_job_completion) while result.status < 4: time.sleep(0.2) # result_value = result.getMessage() # messages.addMessage('completed {0}'.format(str(result_value))) messages.addMessage( 'Created cache tiles for given schema successfully') except Exception, e: tb = sys.exc_info()[2] messages.addMessage('Failed at step 1 \n Line {0}'.format( tb.tb_lineno)) messages.addMessage(e.message) messages.addMessage("Created Map Service Cache Tiles")
def PublishService(mxdpath, agspath): new_mxd = arcpy.mapping.MapDocument(mxdpath) dirname = os.path.dirname(mxdpath) mxdname = os.path.basename(mxdpath) dotindex = mxdname.index('.') servicename = mxdname[0:dotindex] sddraft = os.path.abspath(servicename + '.sddraft') sd = os.path.abspath(servicename + '.sd') if os.path.exists(sd): os.remove(sd) #创建服务定义草稿draft arcpy.CreateImageSDDraft(new_mxd, sddraft, servicename, 'ARCGIS_SERVER', agspath, False, None, "Ortho Images", "ortho images,image service") #修改草稿draft # read sddraft xml doc = DOM.parse(sddraft) # turn on caching in the configuration properties configProps = doc.getElementsByTagName('ConfigurationProperties')[0] propArray = configProps.firstChild propSets = propArray.childNodes for propSet in propSets: keyValues = propSet.childNodes for keyValue in keyValues: if keyValue.tagName == 'Key': if keyValue.firstChild.data == "minScale": # turn on caching keyValue.nextSibling.firstChild.data = "32000000000" # output to a new sddraft if os.path.exists(sddraft): os.remove(sddraft) f = open(sddraft, 'w') doc.writexml(f) f.close() #分析草稿draft analysis = arcpy.mapping.AnalyzeForSD(sddraft) #打印分析结果 print "The following information was returned during analysis of the MXD:" for key in ('messages', 'warnings', 'errors'): print '----' + key.upper() + '---' vars = analysis[key] for ((message, code), layerlist) in vars.iteritems(): print ' ', message, ' (CODE %i)' % code print ' applies to:', for layer in layerlist: print layer.name, print # 发送草稿至服务器 if analysis['errors'] == {}: # Execute StageService. This creates the service definition. arcpy.StageService_server(sddraft, sd) # Execute UploadServiceDefinition. This uploads the service definition and publishes the service. arcpy.UploadServiceDefinition_server(sd, agspath) print "Service successfully published" else: print "Service could not be published because errors were found during analysis." print arcpy.GetMessages() return agspath.replace(".ags", "/" + servicename + ".MapServer")
print( f'Generating referenced mosaic {mosaic["referenced_dataset"]} from derived mosaic {mosaic["derived_dataset"]}' ) arcpy.management.CreateReferencedMosaicDataset( mosaic['derived_dataset'], mosaic['referenced_dataset'], 'PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",' 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",' 'SPHEROID["WGS_1984",6378137.0,298.257223563]],' 'PRIMEM["Greenwich",0.0],' 'UNIT["Degree",0.0174532925199433]],' 'PROJECTION["Mercator_Auxiliary_Sphere"],' 'PARAMETER["False_Easting",0.0],' 'PARAMETER["False_Northing",0.0],' 'PARAMETER["Central_Meridian",0.0],' 'PARAMETER["Standard_Parallel_1",0.0],' 'PARAMETER["Auxiliary_Sphere_Type",0.0],' 'UNIT["Meter",1.0]]') # Generate service definition from the dataset with tempfile.NamedTemporaryFile(suffix='.sddraft') as sd_draft: print( f'Generating draft service definition for {mosaic["referenced_dataset"]}' ) arcpy.CreateImageSDDraft(mosaic['referenced_dataset'], sd_draft.name, mosaic['service_name']) print( f'Generating final service definition for {mosaic["referenced_dataset"]}' ) arcpy.server.StageService(sd_draft.name, mosaic['service_definition'])
def PublishMDMasterMosaicDataset(jobID, serverFunctionPath, update=False, runCount=0): Utility.printArguments(["jobID", "serverFunctionPath"], [jobID, serverFunctionPath], "B03 PublishMDMasterMosaicDataset") startupType = None # serverFunctionPath = Raster.getServerRasterFunctionsPath(jobID) ssFunctions = None if serverFunctionPath is not None: ssFunctions = Raster.getServerSideFunctions(serverFunctionPath) # ssFunctions = Raster.getServerSideFunctions() Utility.setWMXJobDataAsEnvironmentWorkspace(jobID) MDMaster = CMDR.MDMaster() mdMaster_row = MDMaster.getMDMaster(jobID) if mdMaster_row is not None: mdMaster_path = MDMaster.getMDPath( mdMaster_row ) # ProjectFolders.getProjectFolderFromDBRow(ProjectJob, project) MasterMDName = MDMaster.getMDName( mdMaster_row) # ProjectJob.getProjectID(project) serverConnectionFile = MDMaster.getMDConFilePath(mdMaster_row) folderName = MDMaster.getMDServiceFolder(mdMaster_row) cellSize_m = MDMaster.getMDCellSize(mdMaster_row) Utility.printArguments([ "folderName", "serverConnectionFile", "MasterMDName", "mdMaster_path", "cellSize_m" ], [ folderName, serverConnectionFile, MasterMDName, mdMaster_path, cellSize_m ], "PublishMDMasterMosaicDataset") if not update: B04PublishContourMaster.publishContourMaster( mdMaster_path, serverConnectionFile, MasterMDName, folderName) # ProjectState = MDMaster#ProjectJob.getState(project) # ProjectYear = ProjectJob.getYear(project) # ProjectAlias = ProjectJob.getAlias(project) # ProjectAliasClean = ProjectJob.getAliasClean(project) # @TODO Add more info here! serviceDescription = "Elevation master service '{}'.".format( MasterMDName) serviceTags = ",".join( [MasterMDName, "Master", "Elevation", "Mosaic", "Dataset"]) md_list = [ FoldersConfig.DTM, FoldersConfig.DSM, FoldersConfig.DLM, FoldersConfig.DHM, FoldersConfig.DCM, FoldersConfig.INT ] for md_name in md_list: serviceName = "{}_{}".format(MasterMDName, md_name) local_fgdb_name = "{}.gdb".format(serviceName) arcpy.AddMessage("local_fgdb_name '{}'".format(local_fgdb_name)) local_fgdb_path = os.path.join(mdMaster_path, local_fgdb_name) arcpy.AddMessage("local_fgdb_path '{}'".format(local_fgdb_path)) project_md_path = os.path.join(local_fgdb_path, md_name) arcpy.AddMessage("MD path '{}'".format(project_md_path)) if arcpy.Exists(project_md_path): # Get the units of the Mosaic Dataset descMD = arcpy.Describe(project_md_path) SpatRefMD = descMD.SpatialReference SpatRefUnitsMD = SpatRefMD.linearUnitName SpatRefNameMD = SpatRefMD.name # Retrieve some properties from the Mosaic Dataset to place in the tags field cellsizeResult = arcpy.GetRasterProperties_management( project_md_path, property_type="CELLSIZEX", band_index="") Utility.addToolMessages() cellsizeX = cellsizeResult.getOutput(0) if cellsizeX != cellSize_m: arcpy.AddMessage( "mosaic dataset cell size {} != requested cell size {}" .format(cellsizeX, cellSize_m)) arcpy.AddMessage( "Spatial Reference name of Mosaic Dataset: {0}".format( SpatRefNameMD)) arcpy.AddMessage( "Spatial Reference X,Y Units of Mosaic Dataset: {0}". format(SpatRefUnitsMD)) serviceDescription = "{} {} horizontal spatial reference is {} and cell size is {} {}".format( md_name, serviceDescription, SpatRefNameMD, cellsizeX, SpatRefUnitsMD) serviceTags = ",".join([ serviceTags, str(cellsizeX), SpatRefUnitsMD, SpatRefNameMD ]) arcpy.AddMessage("Service Tags: {0}".format(serviceTags)) arcpy.AddMessage( "Service description: {0}".format(serviceDescription)) # Look for RasterPath in the list of allowed fields, and if found, don't publish # the mosaic dataset. Exposing the contents of RasterPath could compromise the # security of the Image Service. # allowedFieldListMD = descMD.AllowedFields # arcpy.AddMessage("AllowedFields in MD Properties: {0}".format(allowedFieldListMD)) # if True or "RASTERPATH;" not in allowedFieldListMD.upper(): # Create a list to manipulate server-side functions # Bring Hillshade to the top of the list so it is default ssFunctionsLst = ssFunctions.split(";") if len(ssFunctionsLst) > 0: foundHillshade = False for i, s in enumerate(ssFunctionsLst): if 'HILLSHADE' in s.upper(): arcpy.AddMessage( "Will re-order SS Functions so Hillshade is default" ) foundHillshade = True break # if Hillshade is found then re-order the list if foundHillshade: ssFunctionsLst.insert(0, ssFunctionsLst.pop(i)) arcpy.AddMessage( "Re-ordered SS Functions so Hillshade is default") # convert the list of server-side functions into a comma delimited string ssFunctionsList = ",".join(ssFunctionsLst) arcpy.AddMessage( "Server-side Functions: {0}\n".format(ssFunctionsList)) # Create image service definition draft arcpy.AddMessage( "Creating image service definition draft file...") wsPath = os.path.dirname(os.path.dirname(mdMaster_path)) sddraftPath = os.path.join(wsPath, serviceName + ".sddraft") arcpy.Delete_management(sddraftPath) arcpy.CreateImageSDDraft(project_md_path, sddraftPath, serviceName, "ARCGIS_SERVER", connection_file_path=None, copy_data_to_server=False, folder_name=folderName, summary=serviceDescription, tags=serviceTags) # Edit the service definition draft if user specified server-side functions # or if user wants to enable download on the Image Service updateSDServerSideFunctions(ssFunctionsLst, ssFunctionsList, sddraftPath, update) # @ TODO Include this to overwrite a service # doc = DOM.parse(sddraftPath) # tagsType = doc.getElementsByTagName('Type') # for tagType in tagsType: # if tagType.parentNode.tagName == 'SVCManifest': # if tagType.hasChildNodes(): # tagType.firstChild.data = "esriServiceDefinitionType_Replacement" # # tagsState = doc.getElementsByTagName('State') # for tagState in tagsState: # if tagState.parentNode.tagName == 'SVCManifest': # if tagState.hasChildNodes(): # tagState.firstChild.data = "esriSDState_Published" # Analyze service definition draft arcpy.AddMessage("Analyzing service definition draft file...") analysis = arcpy.mapping.AnalyzeForSD(sddraftPath) for key in ('messages', 'warnings', 'errors'): arcpy.AddMessage("----" + key.upper() + "---") analysis_vars = analysis[key] for ((message, code), data) in analysis_vars.iteritems(): # @UnusedVariable msg = (" ", message, " (CODE %i)" % code) arcpy.AddMessage("".join(msg)) if analysis['errors'] == {}: arcpy.AddMessage( "Staging and publishing service definition...") # StageService arcpy.AddMessage("Staging sddraft file to sd file") sdPath = sddraftPath.replace(".sddraft", ".sd") arcpy.Delete_management(sdPath) arcpy.StageService_server(sddraftPath, sdPath) try: # UploadServiceDefinition if not update: arcpy.AddMessage( "Publishing mosaic data set as image service.") else: arcpy.AddMessage( "Updating mosaic data set as image service.") arcpy.UploadServiceDefinition_server( sdPath, serverConnectionFile, "#", "#", "#", "#", startupType) except Exception as e: if runCount < 1: arcpy.AddMessage( "image service already exists, trying to update instead." ) PublishMDMasterMosaicDataset( jobID, serverFunctionPath, True, 1) break else: raise e else: # if the sddraft analysis contained errors, display them arcpy.AddError(analysis['errors']) # else: # arcpy.AddError("Exiting: Found 'RasterPath' in list of allowed MD fields. Please remove this field from the list before publishing.") # arcpy.AddError(" To remove RasterPath from the list, go to Mosaic Dataset Properties, Defaults tab, Allowed Fields...") else: arcpy.AddWarning( "Project mosaic dataset not found '{}'.".format( project_md_path)) # FOR LOOP # # else: # arcpy.AddError("Project file geodatabase not found '{}'. Please add this before proceeding.".format(ProjectMDs_fgdb_path)) else: arcpy.AddError( "MDMaster record not found in the CMDR. Please add this to the CMDR before proceeding." ) arcpy.AddMessage("Operation complete")
# If Image Service does not exist create Service drafts to publih the service # Otherwise add new raster to existing Image Service # When Image Service does not exist yet create a service definition draft and publish the Service if not os.path.exists(workspace_gdb+"\\"+collection_id+ "Service.sd"): try: print "Try Creating SD draft" if data_store_path not in [i[2] for i in arcpy.ListDataStoreItems(con, 'FOLDER')]: # Register folder with ArcGIS Server site --> both the server path(out_folder_path 1.) and client path (out_folder_path 2.) are the same dsStatus = arcpy.AddDataStoreItem(con, "FOLDER", "Workspace for " + collection_id + 'Service', data_store_path, data_store_path) print "Data store : " + str(dsStatus) Sddraft = os.path.join(workspace_gdb, collection_id+"Service"+".sddraft") # Name = Name der Bilddateien/ Ordner bzw. des sddraft # vorletzter Parameter der createImageSdd Draft Funktion muss nachher mit den Metadaten besetzt werden, als description des Services arcpy.CreateImageSDDraft(os.path.join(workspace_gdb, collection_id+'.gdb\\Master_'+collection_id), Sddraft, collection_id+"Service", 'ARCGIS_SERVER', None, False, 'WaCoDiS', str(json_metadata['productType']), str(json_metadata['productType'])+",image service, WaCoDiS") except arcpy.ExecuteError: e = sys.exc_info()[1] print(e.args[0]) print arcpy.GetMessages() + "\n\n" sys.exit("Failed in creating SD draft") # Analyze the service definition draft Sddraft = os.path.join(workspace_gdb, collection_id+"Service"+".sddraft") Sd = os.path.join(workspace_gdb, collection_id+"Service"+".sd") analysis = arcpy.mapping.AnalyzeForSD(Sddraft) print "The following information was returned during analysis of the image service:" for key in ('messages', 'warnings', 'errors'): print '----' + key.upper() + '---' vars = analysis[key]
# Define local variables: # The folder for service definition draft and service definition files MyWorkspace = r"C:\Users\LinZiXiang\Desktop\SpringbootVueGis\ArcPyPublishing" Name = sys.argv[1] InputData = sys.argv[2] Sddraft = os.path.join(MyWorkspace, Name + ".sddraft") Sd = os.path.join(MyWorkspace, Name + ".sd") con = os.path.join(MyWorkspace, "arcgis on localhost_6080 (admin).ags") # Create service definition draft print("Start Create Service Definition Draft") try: print("Creating SD draft") arcpy.CreateImageSDDraft(InputData, Sddraft, Name, 'ARCGIS_SERVER', con, False, "WCS", "WCS", "WCS, image service") except Exception as err: print(str(err)) sys.exit("Failed to create SD draft") print("Start Analyze the service") # Analyze the service definition draft analysis = arcpy.mapping.AnalyzeForSD(Sddraft) print("The following was returned during analysis of the image service:") for key in analysis.keys(): print("---{}---".format(key.upper())) for ((message, code), layerlist) in analysis[key].iteritems(): print(" {} (CODE {})".format(message, code)) print(" applies to: {}".format(" ".join(
print e.message agscon = os.path.join(MyWorkspace + '/', host + ".ags") if not os.path.isfile(agscon): print("Unable to connect to ArcGIS Server -- exiting") sys.exit(1) #con = 'D:/EclipseCode/GisAccessTool/localhost.ags' con = MyWorkspace + '/' + host + '.ags' # Create service definition draft try: print "Creating SD draft" arcpy.CreateImageSDDraft(Md, Sddraft, Name, 'ARCGIS_SERVER', con, False, None, "Ortho Images", "ortho images,image service") except: print arcpy.GetMessages() + "\n\n" sys.exit("Failed in creating SD draft") # Analyze the service definition draft analysis = arcpy.mapping.AnalyzeForSD(Sddraft) print "The following information was returned during analysis of the image service:" for key in ('messages', 'warnings', 'errors'): print '----' + key.upper() + '---' vars = analysis[key] for ((message, code), layerlist) in vars.iteritems(): print ' ', message, ' (CODE %i)' % code print ' applies to:', for layer in layerlist: