实现的功能主要是通过导出ArcMap创建的镶嵌影像数据集中FootPrint,增加影像存储的Path属性,导入SDE并发布要素服务,更新SDE中的要素服务属性,从而达到更新前端影像FootPrint要素服务的目的。
FootPrint要素数据就是影像数据的边界。
首先从镶嵌影像数据集导出获取到FootPrint要素数据,用于发布要素服务。
参考官网:
https://pro.arcgis.com/zh-cn/pro-app/latest/tool-reference/data-management/export-mosaic-dataset-geometry.htm
Python代码:
#Export Mosaic Dataset Geometry
import arcpy
arcpy.env.workspace = "D:/TestData/mosaicceshi/"
#Export footprint in a mosaic dataset
mdname = "ceshi.gdb/mosaictest"
out_FC = "D:/TestData/mosaicceshi/ceshi.gdb/testfp"
where_clause = "OBJECTID >0"
geometry_type = "FOOTPRINT"
arcpy.ExportMosaicDatasetGeometry_management(
mdname, out_FC, where_clause, geometry_type)
从镶嵌影像数据集获取到影像存储的路径,并导出到数据表。
官网参考:
https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/data-management-toolbox/export-mosaic-dataset-paths.htm
Python代码:
#Export broken raster path in FGDB Mosaic Dataset to dbf table
import arcpy
arcpy.env.workspace = "D:/TestData/mosaicceshi/"
mdname = "ceshi.gdb/mosaictest"
outtable = "D:/TestData/mosaicceshi/ceshi.gdb/path.dbf"
query = "#"
mode = "ALL"
pathtype = "RASTER"
arcpy.ExportMosaicDatasetPaths_management(mdname, outtable, query,
mode, pathtype)
将导出的dbf中的path字段写入到FootPrint的path属性字段中。
import arcpy
arcpy.env.workspace = "D:/TestData/mosaicceshi/"
fc = 'D:/TestData/mosaicceshi/ceshi.gdb/path_dbf'
fields = ['Path']
pathArray = []
#read path from the path table
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
#print(row[0])
pathArray.append(row[0])
print(pathArray)
#add path field to the feature and update the path value
fpFc = 'D:/TestData/mosaicceshi/ceshi.gdb/testfp'
fpFields = ['OBJECTID']
#count the number of feature in the footprint
fNumber = 0
#read path from the path table
with arcpy.da.SearchCursor(fpFc, fpFields) as cursor:
for row in cursor:
fNumber+=1
#print(fNumber)
#To judge whether the path table number is equal to mosaic dataset number
if len(pathArray)==fNumber:
fNumber = 0
print(000)
try:
arcpy.AddField_management(fpFc, "Path", "TEXT")
arcpy.AddMessage("New Field Add Successfully, Name is :Path")
print(1111)
except:
arcpy.AddWarning("The Field is existed, failed to add")
print(222)
#update path value
cursor = arcpy.UpdateCursor(fpFc)
for row in cursor:
pathValue = pathArray[fNumber]
print(pathValue)
fNumber +=1
row.setValue("Path", pathValue)
cursor.updateRow(row)
pathArray = []
之后就是将要素导入到SDE以及发布要素服务,这里就没有写脚本了,感兴趣的可以自己研究一下。
发布完要素服务之后,如果在ArcMap中镶嵌影像数据集增加了新的影像,想要更新发布的FoorPrint边界要素服务,则需要停止服务,然后更新SDE中的数据源,再启动服务。
官方代码文件夹下启动、停止所有服务参考:
https://enterprise.arcgis.com/zh-cn/server/10.8/administer/windows/example-stop-or-start-all-services-in-a-folder.htm
Python代码,停止启动指定服务:
# -*- coding: utf-8 -*-
#启动或者停止文件夹下所有服务
import httplib, urllib, json
import sys
import getpass
#import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')
#function to get token
def getToken(username, password, serverName, serverPort):
tokenURL = "/arcgis/admin/generateToken"
params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
httpconn = httplib.HTTPConnection(serverName, serverPort)
httpconn.request("POST", tokenURL, params, headers)
response = httpconn.getresponse()
if (response.status != 200):
httpconn.close()
print "Error while attempting to start the services"
return
else:
data = response.read()
httpconn.close()
token = json.loads(data)
print(token)
return token['token']
def assertJsonSuccess(data):
obj = json.loads(data)
if 'status' in obj and obj['status'] == "error":
print "Error: JSON object returns an error. " + str(obj)
return False
else:
return True
#userInfo
userName = 'siteadmin'
password = 'admin'
serverName = 'localhost' #ip
serverPort = 6080
folder = 'root'
#whether want to Start or Stop the services: Stop/Start
stopOrStart = 'Start'
print(222)
token = getToken(userName, password, serverName, serverPort)
if token == "":
print "Could not generate a token with the username and password provided."
print(111);
if str.upper(folder) == "ROOT":
folder = ""
else:
folder += "/"
#构造服务rest接口
folderURL = "/arcgis/admin/services" + "/" + folder
print(folderURL)
params = urllib.urlencode({'token': token, 'f': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request("POST", folderURL, params, headers)
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print "Could not read folder information!"
else:
data = response.read()
if not assertJsonSuccess(data):
print "Error when reading the folder information!" + str(data)
else:
print "Processing folder information successfully. Now processing services......"
dataobj = json.loads(data)
httpConn.close()
for item in dataobj['services']:
fullservicename = item['serviceName'] + "." + item['type']
print fullservicename
#judge whether is the certain service
if item['serviceName'] == 'testfp':
stopOrStartURL = "/arcgis/admin/services/" + folder + fullservicename + "/" + stopOrStart
print stopOrStartURL
httpConn.request("POST", urllib.quote(stopOrStartURL.encode('utf-8')), params, headers)
stopStartresponse = httpConn.getresponse()
if (stopStartresponse.status != 200):
httpConn.close()
print "Error while excuting stop or start. Please check the url and try again!"
else:
stopStartdata = stopStartresponse.read()
if not assertJsonSuccess(stopStartdata):
if str.upper(stopOrStart) == "START":
print "Error returned when start service" + fullservicename
else:
print "Error returned when stop services" + fullservicename
print str(stopStartdata)
else:
print "Service " + fullservicename + " processed successfully!"
httpConn.close()
首先需要使用一中的方法重新导出获取到新的FootPrint要素。
如果需要将FileGDB中的要素复制到SDE,可以参考官网:
https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/data-management-toolbox/copy-features.htm
Python代码:
import arcpy
arcpy.env.workspace = "D:/TestData/mosaicceshi/ceshi.gdb"
arcpy.CopyFeatures_management("testfp1", \
"C:/Users/kfk/AppData/Roaming/ESRI/Desktop10.7/ArcCatalog/Connection to localhost.sde/sde.sde.testfp2")
print "successfully copied to SDE"
然后覆盖更新原来SDE中的源数据,Python代码:
参考:http://zhihu.geoscene.cn/article/3990
# coding:utf-8
# encoding=utf-8
# Import arcpy module
import arcpy
# 默认工作空间必须设,否则会报错ERROR 000732 不存在或不受支持
arcpy.env.workspace = r"D:\\TestData\\mosaicceshi\\"
# dict_fc_source类型是字典(对应java就是Map),key是数据同步的目标图层,value是数据同步的源图层
dict_fc_source = {}
# 以下两条是例子,内容可以使用catalog批量处理和excel结合使用来生成
dict_fc_source[
r"C:\\Users\\kfk\\AppData\\Roaming\\ESRI\\Desktop10.7\\ArcCatalog\\Connection to localhost.sde\\sde.sde.testfp"] = r"C:\\Users\\kfk\\AppData\\Roaming\\ESRI\\Desktop10.7\\ArcCatalog\\Connection to localhost.sde\\sde.sde.testfp1"
# 遍历图层每个处理
for fc_target in dict_fc_source:
# key是数据同步的目标图层,value是数据同步的源图层
fc_source = dict_fc_source[fc_target]
# 先删除要素(只删数据,不删图层)
arcpy.DeleteFeatures_management(fc_target)
print '完成删除要素:' + fc_target
# 然后把源图层的要素复制到目标图层
arcpy.Append_management(fc_source, fc_target, "TEST")
print '完成复制要素,从 ' + fc_source + ' 复制到 ' + fc_target
print '处理完成'
采用四中的方法启动服务即可。之前发布的边界要素服务就得到了更新。
后面对以上脚本做了整理,主要划分为process和update两块。process主要是用于导出要素,update则用于对服务的源数据进行更新。
代码下载地址:https://download.csdn.net/download/suntongxue100/33153857
(以上所有运行环境为Python2.7环境)