Arcengine二次开发gp调用

 

一、调用gp(以融合为例)

public static void Dissolve(IFeatureClass in_features, string out_feature_class, string field, string statistics_fields, string multi_part)

      //初始化gp服务
     Geoprocessor gp = new Geoprocessor();
     gp.OverwriteOutput = true;

     //工具箱融合服务调用
     ESRI.ArcGIS.DataManagementTools.Dissolve diss = new ESRI.ArcGIS.DataManagementTools.Dissolve();
     diss.in_features = in_features;
     diss.out_feature_class = out_feature_class;
     diss.dissolve_field = field;
     //[[field, {statistic_type}],...]
    diss.statistics_fields = statistics_fields;
    diss.multi_part = multi_part;
    gp.Execute(diss, null);

}

二、多个参数传入gp调用,并返回计算结果

public static IFeatureClass Union(IFeatureClass in_features1, IFeatureClass in_features2 )
{
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;
ESRI.ArcGIS.AnalysisTools.Union Un = new ESRI.ArcGIS.AnalysisTools.Union();
IGpValueTableObject gpValueTableObject = new GpValueTableObjectClass();//对两个要素类进行相交运算
gpValueTableObject.SetColumns(2);
object dltb = in_features1;//输入IFeatureClass 1
object pdt = in_features2;//输入IFeatureClass 2
gpValueTableObject.AddRow(ref dltb);
gpValueTableObject.AddRow(ref pdt);
Un.in_features = gpValueTableObject;
IGeoProcessorResult result = (IGeoProcessorResult)gp.Execute(Un, null); //得到
return (IFeatureClass)gp.Open(result.ReturnValue);
}

三、gp调用python生成的自定义工具

python发布要素服务链接http://zhihu.esrichina.com.cn/article/3566(转)

//Toolboxpath自定义工具路径

//mxdpath、serverusername、serverpassword、mxdfilename、serverurlpath、servername自定义工具箱参数(依据自己gp工具而定)

public bool Pblish(string Toolboxpath, object mxdpath, string serverusername, string serverpassword, string mxdfilename, object serverurlpath, string servername)
{
bool mark = true;
try
{
Geoprocessor gp = new Geoprocessor();
// 2-设置参数
gp.OverwriteOutput = true;
// 3-设置工具箱所在的路径
gp.AddToolbox(Toolboxpath);
//4-设置输入参数
IVariantArray parameters = new VarArrayClass();
parameters.Add(mxdpath);
parameters.Add(serverusername);
parameters.Add(serverpassword);
parameters.Add(mxdfilename);
parameters.Add(serverurlpath);
parameters.Add(servername);
gp.Execute("PbTools", parameters, null);
}
catch { mark = false; }
return mark;
}

符地图服务发布python(发布服务为wfs)

import os as OS
import arcpy
import xml.dom.minidom as DOM
wrkpc = arcpy.GetParameterAsText(0)
out_folder_path = wrkpc
con_Filename = 'test.ags'
server_url = arcpy.GetParameterAsText(4)
staging_folder_path = wrkpc
username = arcpy.GetParameterAsText(1)
password = arcpy.GetParameterAsText(2)

arcpy.mapping.CreateGISServerConnectionFile("PUBLISH_GIS_SERVICES",
out_folder_path,
con_Filename,
server_url,
"ARCGIS_SERVER",
False,
staging_folder_path,
username,
password,
"SAVE_USERNAME")
mxds = arcpy.GetParameterAsText(3)
mxdpath = OS.path.join(wrkpc,mxds) #指定MXD所在的路径
mapDoc = arcpy.mapping.MapDocument(mxdpath)
servicename = "DHCTBFB"
sddraft = OS.path.join(wrkpc,"GeoTurbine_Test.sddraft")
sd = OS.path.join(wrkpc,"GeoTurbine_Test.sd")
connectionfile =OS.path.join(wrkpc,con_Filename)
summary = "this is a test"
tags = "this is a test"

# creste service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc,
sddraft,
servicename,
"ARCGIS_SERVER",
connectionfile,
False,
"WP_MapService",
summary,tags)

newSDdraft='updatedDraft.sddraft'
doc = DOM.parse(sddraft)
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
if typeName.firstChild.data == 'WFSServer':
typeName.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'true'
f = open(newSDdraft, 'w')
doc.writexml(f)
f.close()

#stage and upload the service if the sddraft analysis didn't contain errors
if analysis['errors'] == {}:
# excute StageService
arcpy.StageService_server(newSDdraft,sd)
# excute UploadServiceDfinition
arcpy.UploadServiceDefinition_server(sd,connectionfile)
else:
# if the sddraft analysis contained errors,display them
print analysis['errors']

posted @ 2018-07-30 10:00  边缘人1995  阅读(1100)  评论(0编辑  收藏  举报