def publish_to_agol(self, mxd_path, service_name, tags="None", description="None"): """ publishes a service to AGOL """ mxd = mapping.MapDocument(mxd_path) sddraftFolder = env.scratchFolder + os.sep + "draft" sdFolder = env.scratchFolder + os.sep + "sd" sddraft = sddraftFolder + os.sep + service_name + ".sddraft" sd = sdFolder + os.sep + "%s.sd" % service_name mxd = self._prep_mxd(mxd) if os.path.isdir(sddraftFolder) == False: os.makedirs(sddraftFolder) else: shutil.rmtree(sddraftFolder, ignore_errors=True) os.makedirs(sddraftFolder) if os.path.isfile(sddraft): os.remove(sddraft) analysis = mapping.CreateMapSDDraft(mxd, sddraft, service_name, "MY_HOSTED_SERVICES") sddraft = self._modify_sddraft(sddraft) analysis = mapping.AnalyzeForSD(sddraft) if os.path.isdir(sdFolder): shutil.rmtree(sdFolder, ignore_errors=True) os.makedirs(sdFolder) else: os.makedirs(sdFolder) if analysis['errors'] == {}: # Stage the service arcpy.StageService_server(sddraft, sd) print "Created {}".format(sd) else: # If the sddraft analysis contained errors, display them and quit. print analysis['errors'] sys.exit() # POST data to site content = self.getUserContent() #Title, item for item in content['items']: if item['title'] == service_name and \ item['item'] == os.path.basename(sd): print self.deleteItem(item['id']) elif item['title'] == service_name: print self.deleteItem(item['id']) self._agol_id = self._upload_sd_file(sd, service_name=service_name, tags=tags, description=description) if self._agol_id != "Error Uploadings": p_vals = self._publish(self._agol_id) for service in p_vals['services']: print self.enableSharing(service['serviceItemId']) del service del p_vals del mxd
def MXDtoFeatureServiceDef( mxd_path, service_name=None, tags=None, description=None, folder_name=None, capabilities='Query,Create,Update,Delete,Uploads,Editing,Sync', maxRecordCount=1000, server_type='MY_HOSTED_SERVICES'): """ converts an MXD to a service defenition Inputs: mxd_path - Path to the ArcMap Map Document(MXD) service_name - Name of the Feature Service tags - Tags for the service, if none, the tags from the MXD are used description - Summary for the Feature Service, if none, info from the MXD is used folder_name - Folder in the Data store capabilities - A Comma delimited list of feature service capabolities 'Query,Create,Update,Delete,Uploads,Editing,Sync' maxRecordCount - The max returned record count for the feature service server_type - The type of connection or publishing server Values: ARCGIS_SERVER | FROM_CONNECTION_FILE | SPATIAL_DATA_SERVER | MY_HOSTED_SERVICES Output: Service Definition File - *.sd """ if not os.path.isabs(mxd_path): sciptPath = os.getcwd() mxd_path = os.path.join(sciptPath, mxd_path) mxd = mapping.MapDocument(mxd_path) sddraftFolder = env.scratchFolder + os.sep + "draft" sdFolder = env.scratchFolder + os.sep + "sd" sddraft = sddraftFolder + os.sep + service_name + ".sddraft" sd = sdFolder + os.sep + "%s.sd" % service_name mxd = _prep_mxd(mxd) res = {} if service_name is None: service_name = mxd.title.strip().replace(' ', '_') if tags is None: tags = mxd.tags.strip() if description is None: description = mxd.description.strip() if os.path.isdir(sddraftFolder) == False: os.makedirs(sddraftFolder) else: shutil.rmtree(sddraftFolder, ignore_errors=True) os.makedirs(sddraftFolder) if os.path.isfile(sddraft): os.remove(sddraft) res['service_name'] = service_name res['tags'] = tags res['description'] = description analysis = mapping.CreateMapSDDraft(map_document=mxd, out_sddraft=sddraft, service_name=service_name, server_type=server_type, connection_file_path=None, copy_data_to_server=False, folder_name=folder_name, summary=description, tags=tags) sddraft = _modify_sddraft(sddraft=sddraft, capabilities=capabilities, maxRecordCount=maxRecordCount) analysis = mapping.AnalyzeForSD(sddraft) if os.path.isdir(sdFolder): shutil.rmtree(sdFolder, ignore_errors=True) os.makedirs(sdFolder) else: os.makedirs(sdFolder) if analysis['errors'] == {}: # Stage the service arcpy.StageService_server(sddraft, sd) res['servicedef'] = sd return res else: # If the sddraft analysis contained errors, display them and quit. print analysis['errors'] return None
import arcpy import arcpy.mapping as mapping wrkspc =r'D:\\bysj\\data\\service' mxd=mapping.MapDocument(wrkspc+r'\\max4.mxd') service='max4' sddraft=wrkspc+service+'.sddraft' mapping.CreateMapSDDraft(mxd,sddraft,service) analysis=mapping.AnalyzeForSD(wrkspc+'max4.sddraft') if analysis['errors']=={}: arcpy.StageService_server(sddraft,sd)