112 arcpy 发布 mxd地图文件 到 arcgis服务器 为 地图服务

前言

此文档主要是记录一下 最近的一次机遇 arcpy 来发布 地图文件到 arcgis服务器 上面

arcpy 主要是来自于 ArcGIS_Desktop_105_154030.zip 安装之后会在 python 的安装目录 安装另外的一份带 arcgis 的 python 环境, 然后 本文相关类库 也是基于 这个

 

 

arcpy python 环境 

04ddeea03bbc412b84891ecc042b61fb.png

 

然后执行 “import arcpy” 测试 arcpy 的使用, 如下 没有任何 报错, 表示可以正常使用 

8fad0e260782468cbce4592b014ed675.png

 

 

arcgis 服务器的信息 

基于 docker 直接搭建, 然后 不要挂载 任何信息, 相关的问题 会少一些

root@ubuntu:~/docker/arcgis# cat docker-compose.ymlversion: '3'
services:arcgisserver:image: huas/arcgisserver:10.3.1container_name: arcgisserver
#    volumes:
#      - ./gisdata:/arcgisports:- 6080:6080

 

 

测试的 mk.mxd 的文件

看一下 我们这里的 mxd 文件, 在 arcmap 中打开效果如下 

e9cec13f0d8a4404b40d9a3f158899c0.png

 

 

PublishService.py 

然后我们这里发布的代码 也是直接来源于网络 

Github 上面 suwenjiang 的 TinyTools 下面的 Publishservice.py

项目链接如下 TinyTools

核心业务 没有任何修改, 仅仅是 调整了一下 注释, 因为原文 注释似乎是乱了

 

发布代码如下

import sys
import timeimport arcpy__author__ = 'jiangmb'from arcpy import mapping
import xml.dom.minidom as DOM
import os
import tempfile# create connection to arcgis server
class CreateContectionFile(object):def __init__(self):self.__filePath = Noneself.__loginDict = Nonedef CreateContectionFile(self):try:server_url = "http://{}:{}/arcgis/admin".format(self.__loginDict['server'], self.__loginDict['port'])connection_file_path = str(self.__filePath)  #use_arcgis_desktop_staging_folder = Falseif os.path.exists(connection_file_path):os.remove(connection_file_path)out_name = os.path.basename(connection_file_path)path = os.path.split(self.filePath)[0]print ("++++++++ INFO: before connect to arcgis server succeed ++++++++")result = mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES", path, out_name, server_url, "ARCGIS_SERVER", use_arcgis_desktop_staging_folder, path,self.__loginDict['userName'], self.__loginDict['passWord'], "SAVE_USERNAME")print ("++++++++ INFO: connect to arcgis server succeed ++++++++")return connection_file_pathexcept Exception as msg:print (msg)@propertydef filePath(self):return self.__filePath@filePath.setterdef filePath(self, value):self.__filePath = value@propertydef loginInfo(self):return self.__loginDict@loginInfo.setterdef loginInfo(self, value):self.__loginDict = value# create service definition draft
class CreateSddraft:def CreateSddraft(self, mapDocPath, con, serviceName, copy_data_to_server=True, folder=None):mapDoc = mapping.MapDocument(mapDocPath)sddraft = mapDocPath.replace(".mxd", ".sddraft")print ("++++++++ INFO: before " + serviceName + " create draft file ++++++++")result = mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER', con, copy_data_to_server, folder)print ("++++++++ INFO: after " + serviceName + " create draft file ++++++++")return sddraftdef setTheClusterName(self, xml, clusterName):doc = DOM.parse(xml)doc.getElementsByTagName('Cluster')[0].childNodes[0].nodeValue = clusterNameoutXml = xmlf = open(outXml, 'w')doc.writexml(f)f.close()return outXml# publish arcgis service
class PublishServices:def checkfileValidation(self, mxdLists):print ("++++++++ INFO: before before check mxd file list ++++++++")file_to_be_published = []for file in mxdLists:mxd = mapping.MapDocument(file)brknlist = mapping.ListBrokenDataSources(mxd)if not len(brknlist) == 0:print ("++++++++ ERROR: process mxd file " + os.path.split(file)[1] + " ++++++++")else:file_to_be_published.append(file)print ("++++++++ INFO: after before check mxd file list ++++++")return file_to_be_publisheddef publishServices(self, mxdLists, con, clusterName='default', copy_data_to_server=True, folder=None):for file in self.checkfileValidation(mxdLists):serviceName = os.path.splitext(os.path.split(file)[1])[0]clsCreateSddraft = CreateSddraft()sddraft = clsCreateSddraft.CreateSddraft(file, con, serviceName, copy_data_to_server, folder)analysis = arcpy.mapping.AnalyzeForSD(sddraft)dirName = os.path.split(file)[0]if analysis['errors'] == {}:print ("++++++++ WARNING: there are warning as follow +++++++")print (analysis['warnings'])sd = dirName + "\\" + serviceName + ".sd"if (os.path.exists(sd)):print ("++++++++ INFO: remove old service definition :" + serviceName + " +++++++")os.remove(sd)try:print ("++++++++ INFO: before generate service definition from service definition draft : " + serviceName + " +++++++")arcpy.StageService_server(sddraft, sd)print ("++++++++ INFO: after generate service definition from service definition draft : " + serviceName + " +++++++")print ("++++++++ INFO: before upload service definition to arcgis server : " + str(serviceName) + " ++++++")arcpy.UploadServiceDefinition_server(sd, con, in_cluster=clusterName)print ("++++++++ INFO: after upload service definition to arcgis server : " + str(serviceName) + " ++++++")except Exception as msg:print (msg)else:print ('++++++++ ERROR: process error:' + analysis['errors'] + '++++++++')time.sleep(5)sys.exit(1)def checkWarnings(self, warnings):for warning in warnings:if warning[1] == 24011:print ("++++++++ check warning, received error code 24011 +++++++")return Truereturn Falsedef GetMxFileList(self, filePath):if not os.path.exists(filePath):print ("++++++++ ERROR: target mxd folder does not exists +++++++")sys.exit(1)list = []for root, dirname, files in os.walk(filePath):for file in files:if os.path.splitext(file)[1] == '.mxd':mxdfile = os.path.join(root, file)list.append(mxdfile)if list == []:print ("++++++++ INFO: collected empty mxd file list ++++++++")time.sleep(5)sys.exit(1)return listdef publishMxdFolder():server = "192.168.220.133"userName = "admin"passWord = "admin@2021"port = "6080"mxdDir = "D:\\Jobs\\99_arcgis\\mxd\\test01Mk"servic_dir = "jerry_20230620"clusterName = "default"logDict = {'server': server,'userName': userName,'passWord': passWord,'port': port}contionfile = os.path.join(tempfile.mkdtemp(), 'server.ags')instace = CreateContectionFile()instace.filePath = contionfileinstace.loginInfo = logDictinstace.CreateContectionFile()if (os.path.isfile(contionfile) == False):print ("++++++++ ERROR: connect to arcgis server failed ++++++++")time.sleep(5)sys.exit(1)clsPublishservice = PublishServices()fileList = clsPublishservice.GetMxFileList(mxdDir)if len(servic_dir) == 0:servic_dir == Noneif len(clusterName) == 0:clusterName = 'default'clsPublishservice = PublishServices()clsPublishservice.publishServices(fileList, contionfile, clusterName, copy_data_to_server=False, folder=servic_dir)if __name__ == '__main__':publishMxdFolder()

 

 

然后执行给定的脚本, 日志信息如下 

++++++++ INFO: before connect to arcgis server succeed ++++++++
++++++++ INFO: connect to arcgis server succeed ++++++++
++++++++ INFO: before before check mxd file list ++++++++
++++++++ INFO: after before check mxd file list ++++++
++++++++ INFO: before mk create draft file ++++++++
++++++++ INFO: after mk create draft file ++++++++
++++++++WARNING: there are warning as follow +++++++
{(u'Map is being published with data copied to the server using data frame full extent', 10045): [], (u"Layer's data source is not registered with the server and data will be copied to the server", 24011): [<map layer u'mk'>, <map layer u'lyjq'>], (u'Missing Tags in Item Description', 24059): [], (u"Layer's data source doesn't have a spatial index", 10002): [<map layer u'mk'>], (u'Missing Summary in Item Description', 24058): []}
++++++++ INFO: remove old service definition :mk +++++++
++++++++ INFO: before generate service definition from service definition draft : mk +++++++
++++++++ INFO: after generate service definition from service definition draft : mk +++++++
++++++++ INFO: before upload service definition to arcgis server : mk ++++++
++++++++ INFO: after upload service definition to arcgis server : mk ++++++

 

然后 刷新 arcgis服务器 上面的服务信息, 就可以看到对应的服务信息 

a355921d86634ca99b1ae8e8f3a83d05.png

 

mk 服务的 rest url 信息如下 

5ca5c46f4dba4044b00f1691cd6b37cf.png

 

客户端的使用

然后前端这边 使用给定的图层服务信息, 这里使用的是 ol 包 

let tileSources = new TileArcGISRest({url:'http://192.168.220.133:6080/arcgis/rest/services/jerry_20230620/mk/MapServer'
});
let tileLayers = new Tile({className:'testLayer',source: tileSources,zIndex: 1,
});
this.map.addLayer(tileLayers);

 

页面上查看效果, 和 arcmap 中看到的效果 一致

089b68af084c4c38828f638a31454a70.png

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/636439.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《QT实用小工具·三十四》Qt/QML使用WebEngine展示的百度ECharts图表Demo

1、概述 源码放在文章末尾 该项目实现了百度ECharts图表的样式&#xff0c;效果demo如下所示&#xff1a; 项目部分代码如下所示&#xff1a; #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtWebEngine>int main(int argc, ch…

【Web】AFCTF 2021 题解(部分)

目录 BABY_CSP search secret google authenticator 随便做做&#xff0c;环境是NSS上的 BABY_CSP CSP绕过_script-src self-CSDN博客 CSP指令值 *&#xff1a; 星号表示允许任何URL资源&#xff0c;没有限制&#xff1b; self&#xff1a; 表示仅允许来自同源&#xff…

【C语言】——内存函数的使用及模拟实现

【C语言】——内存函数的使用及模拟实现 前言一、 m e m c p y memcpy memcpy 函数1.1、函数功能&#xff08;1&#xff09;函数名理解&#xff08;2&#xff09;函数介绍 1.2、函数的使用1.3、函数的模拟实现 二、 m e m m o v e memmove memmove 函数2.1、函数功能2.2、函数的…

【论文阅读】Attention is all you need

摘要 主要的序列转换模型是基于复杂的循环或卷积神经网络&#xff0c;其中包括一个编码器和一个解码器。性能最好的模型还通过一种注意力机制将编码器和解码器连接起来。我们提出了一种新的简单的网络架构&#xff0c;Transformer&#xff0c;完全基于注意机制&#xff0c;完全…

请编写函数fun,它的功能是:求出1到1000之内能被7或11整除、但不能同时被7和11整除的所有整数并将它们放在a所指的数组中,通过n返回这些数的个数。

本文收录于专栏:算法之翼 https://blog.csdn.net/weixin_52908342/category_10943144.html 订阅后本专栏全部文章可见。 本文含有题目的题干、解题思路、解题思路、解题代码、代码解析。本文分别包含C语言、C++、Java、Python四种语言的解法和详细的解析。 题干 请编写函数fu…

PICkit 3 v3.10中的 Device Family 识别不到芯片

1&#xff1a;现象描述 在使用 PICkit3烧写hex文件的时候&#xff0c;Device Family只有默认芯片&#xff0c;识别不到当前使用的芯片&#xff0c;导致报错“Device Error - hex file not loaded”&#xff0c;我当前使用的是 PIC16F1826芯片&#xff0c;默认不支持&#xff1…

Elastic 网络爬虫:为你的网站添加搜索功能

作者&#xff1a;来自 Elastic Lionel Palacin 为了演示如何使用 Elastic 网络爬虫&#xff0c;我们将以一个具体的网站为例&#xff0c;讲解如何在该网站上添加搜索功能。我们将探讨发现网站的方法&#xff0c;并利用 Elastic 网络爬虫提供的功能&#xff0c;以最佳方式准备待…

【Java笔记】如何创建自己的注解+注解怎么用于反射+SpringBoot常见注解

文章目录 0. 为什么SpringBoot要整这么多注解&#xff1f;1. 一些基础知识1.1 什么是注解1.2 Java内置注解1.2.1 作用在代码上的注解1.2.2 作用在注解的注解&#xff08;元注解&#xff09;1.2.3 Java 7之后的新注解 1.3 注解的作用1.4 如何实现一个自己的注解&#xff1f;1.4.…

【JVM系列】关于静态块、静态属性、构造块、构造方法的执行顺序

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

K-means和DBSCAN

目录 一、K-means和DBSCAN之间的主要区别 二、DBSCAN聚类算法 2.1DBSCAN聚类算法实现点集数据的聚类 2.2DBSCAN聚类算法实现鸢尾花数据集的聚类 三、K-means聚类算法 3.1K-means聚类算法实现随机数据的聚类 3.2K-means聚类算法实现鸢尾花数据集的聚类 一、K-means和DBSC…

【大数据】TiDB: A Raft-based HTAP Database

文章目录 数据库知识介绍数据库系统的ACID特性分布式系统和CAP理论关系型数据库与非关系型数据库关系型数据库非关系型数据库 OldSQL、NoSQL、NewSQLOldSQLNoSQLNewSQL OLTP、OLAP、HTAP 前言&#xff1a;为什么选择TiDB学习&#xff1f;pingCAP介绍TiDB介绍TiDB的影响力TiDB概…

【C】 初识C语言

&#x1f493;博主CSDN主页:麻辣韭菜&#x1f493;   ⏩专栏分类&#xff1a;C语言从入门到精通⏪   &#x1f69a;代码仓库:C语言初级&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学习更多C知识   &#x1f51d;&#x1f51d; 目录 前言 1. 什么是C语言&#…