1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import arcpy import os import shutil import uuid import argparse import sys import zlog import json
parser = argparse.ArgumentParser() parser.add_argument('-shp', '--shapefile-path', type=str, help='ShapeFile Path') parser.add_argument('-sn', '--service-name', type=str, help='Service Name') args, unknown_args = parser.parse_known_args() shpPath = args.shapefile_path service = args.service_name
portalUrl = "https://wc.geoscene.cn/portal/" portalUserName = "portaladmin" portalPassWord = "1qaz2wsx"
rootWorkDir = os.getcwd().replace("\\", "/") + "/" rootAprxDir = rootWorkDir + "res/aprx/template/" rootTempDir = rootWorkDir + "tmp/" rootLogDir = rootWorkDir + "log/"
logger = zlog.zlog(rootLogDir, zlog.INFO)
def checkDir(dir): if not os.path.exists(dir): os.makedirs(dir)
try: logger.info("创建临时工作目录...") curWorkDir = rootTempDir + str(uuid.uuid4()) + "/" checkDir(curWorkDir) logger.info("创建临时工作目录成功:" + curWorkDir)
logger.info("创建模板工程...") curAprxDir = curWorkDir + "aprx/template/" shutil.copytree(rootAprxDir, curAprxDir) curAprxFile = curAprxDir + "template.aprx" aprx = arcpy.mp.ArcGISProject(curAprxFile) m = aprx.listMaps()[0] defGdb = aprx.defaultGeodatabase.replace("\\", "/") + "/" logger.info("创建模板工程成功:" + curAprxFile)
logger.info("登陆Portal账户...") arcpy.SignInToPortal(portalUrl, portalUserName, portalPassWord) logger.info("登陆Portal账户成功!")
logger.info("创建输出目录...") outDir = curWorkDir + "out/" checkDir(outDir) logger.info("创建输出目录成功:" + outDir)
logger.info("投影...") projectShp = defGdb + "projectFc" proj = '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]]' arcpy.management.Project(shpPath, projectShp, proj) m.addDataFromPath(projectShp) logger.info("投影成功!")
logger.info("创建矢量切片索引...") idxFc = defGdb + "idxFc" arcpy.management.CreateVectorTileIndex(m, idxFc, "ONLINE") logger.info("创建矢量切片索引成功!")
logger.info("创建矢量切片包...") package = outDir + service + ".vtpk" arcpy.management.CreateVectorTilePackage(m, package, "ONLINE", tile_structure="INDEXED", index_polygons = idxFc) logger.info("创建矢量切片包成功:" + package)
logger.info("发布服务...") result = arcpy.management.SharePackage(package, portalUserName, portalPassWord, public = "EVERYBODY", organization = "EVERYBODY", publish_web_layer = "TRUE", portal_folder = "My Hosted Services") if(result.status == 4): json_object = json.loads(result.getOutput(1)) service_url = json_object["publishResult"]["serviceurl"] logger.info("发布服务成功:" + service_url) sys.exit(str.format('{"success": true, "msg": "服务发布成功", "url": "{0}"}', service_url)) else: outputs = [] for i in range(result.outputCount): outputs.append(result.getOutput(i)) logger.info(outputs) logger.error("发布服务成功:" + str(outputs)) sys.exit(str.format('{"success": false, "msg": "服务发布失败:{0}", "url": ""}', str(outputs))) except Exception as ex: logger.error(ex) sys.exit(str.format('{"success": false, "msg": "服务发布失败:{0}", "url": ""}', str(ex)))
|