GP使用说明

GP使用说明
GP使用说明

学习在 ArcEngine 中使用 Geoprocessing

摘要:Geoprocessing对于ArcGIS使用者来说,是一种非常方便实用的工具,它可以利用ArcToolbox中的各种工具为我们的地理空间工作流进行框架建模,自动执行空间分析与处理。现在ArcEngine 9.2单独提供了com.esri.arcgis.geoprocessing.tools工具包,使得在二次开发中通过Geoprocessing构建应用模型,将ArcGIS众多分析工具集成到我们的应用中成为现实。看看在ArcEngine for Java环境中如何使用它。

Geoprocessing对于ArcGIS使用者来说,是一种非常方便实用的工具,它可以利用ArcToolbox中的各种工具为我们的地理空间工作流进行框架建模,自动执行空间分析与处理。

过去我们可以在ArcToolbox中新建Model,或是写python脚本、AML来构建新的模型,现在ArcEngine 9.2单独提供了com.esri.arcgis.geoprocessing.tools工具包,使得在二次开发中通过Geoprocessing构建应用模型,将ArcGIS众多分析工具集成到我们的应用中成为现实。

我们就在ArcEngine 9.2 for Java环境中看看如何使用Geoprocessing。

1.import com.esri.arcgis.geoprocessing.GeoProcessor;

2.import com.esri.arcgis.geoprocessing.tools.analysistools.Clip;

3.// Initialize the GeoProcessor

4.GeoProcessor gp = new GeoProcessor();

5.Clip clip= new Clip("c:/data/mjrroads.shp",

"c:/data/coasts.shp", "c:/output/clipOutput.shp");

复制代码

设置参数

1.Clip clip = new Clip();

2.clip.setInFeatures = "c:/data/mjrroads.shp";

3.clip.setClipFeatures = "c:/data/coasts.shp";

4.clip.setOutFeatureClass = "c:/output/clipOutput.shp";

复制代码

代码中Clip构造方法,以及clip对象参数的设置,均和ArcToolbox-->Clip工具相对应。

Geoprocessor类用来执行Geoprocessing相关工具的操作,geoprocessor的角色是一种helper对象,它的重载方法execute用来运行之前所定义的操作集。

1.GP.execute(clip, null);

复制代码

com.esri.arcgis.geoprocessing.IGeoProcessorResult接口的对象可以捕获execute执行

后的结果。

1.// Intialize the Geoprocessor

2.GeoProcessor gp = new GeoProcessor();

3.// 使用web service中的toolbox

4.gp.addToolbox("http://flame7:8399/arcgis/services;GP/Bestpathtoolbox");

5.// 导入本地的shape文件

6.ArrayList parameters = new ArrayList;

7.parameters.add("C:\\sandiego\\source.shp");

8.parameters.add("C:\\sandiego\\destination.shp");

9.// 捕获execute执行结果

10.IGeoProcessorResult result;

11.result = gp.execute("CalculateBestPath", parameters, null);

复制代码

名字冲突

和OO语言处理名字冲突一样,当可能出现名字冲突时,可以使用全名来唯一指定所使用的

工具:

ArcToolbox-->Analysis Tools-->Extract-->Clip

ArcToolbox-->Data Management-->Raster-->Clip

https://www.360docs.net/doc/043934078.html,.esri.arcgis.geoprocessing.tools.analysistools.Clip

https://www.360docs.net/doc/043934078.html,.esri.arcgis.geoprocessing.tools.datamanagementtools.Clip

复制代码

Geoprocessing编程的时候,还可以使用AO作为输入的工具

1.// Initialize the Geoprocessor

2.GPUtilities gpUtilities = new GPUtilities();

3.IFeatureClass inputFeatureClass =

gpUtilities.openFeatureClassFromString(inputData+"/canada/mjrroads.shp");

4.IFeatureClass clipFeatureClass =

gpUtilities.openFeatureClassFromString(inputData+"/canada/coasts.shp");

5.Clip clip = new Clip(inputFeatureClass, clipFeatureClass,

outputDirectory+"/clipOutput.shp");

6.gp.execute(clip, null);

复制代码

关于GPUtilities和Geoprocessor区别,可以看看这两段描述以及各自的类方法:

The GPUtilities object is mainly intended for developers building custom tools. For more information about building custom tools, refer to the technical document

Building Geoprocessing Function Tools.

A geoprocessing tool is executed by a geoprocessor. The geoprocessor is a helper object that simplifies the task of executing tools. Toolboxes define the set of tools available for the geoprocessor. Toolboxes can be added and removed from the

geoprocessor.

ArcToolbox工具都有自己的环境设置,一般情况下我们使用默认值,在AE中可以用

setEnvironmentValue方法来设置环境变量的值

1.// Get the Cell Size environment value

2.gp.setEnvironmentValue("cellsize", Double.valueOf(10.0));

3.String env = (String) gp.getEnvironmentValue("cellsize");

4.

5.// Set the output Coordinate System environment

6.gp.setEnvironmentValue("outputCoordinateSystem", "c:/Program

Files/ArcGIS/Coordinate Systems/Projected Coordinate Systems/UTM/Nad 1983/NAD

1983 UTM Zone 21N.prj");

7.

8.// Reset the environment values to their defaults.

9.gp.resetEnvironments();

复制代码

需要注意的是,在使用Geoprocessing建模进行各种空间操作时,我们需要相应的license 授权来完成这些操作,如Spatial Join需要ArcInfo License,各种级别License描述可以参

考下图:

Blogjava上的链接:

https://www.360docs.net/doc/043934078.html,/flyingis/archive/2007/04/04/108483.htm

Geoprocessing 数据批处理

摘要:ArcGIS使用者经常要面对大量的数据处理工作,如果要在自己的程序中使用Geoprocessing,更多的时候我们是要进行对数据进行批处理分析,Geoprocessing为我们

提供了丰富的支持批处理的功能。

1.工作空间中查询所需数据

2.模型处理过程中各种输入数据的处理

3.枚举、循环执行

--------------------

1.工作空间中查询所需数据

要对数据进行批处理操作,首先需要知道工作空间中有哪些数据,怎么从工作空间大量数据中提取出我们所需要的数据。GeoProcessor类为我们提供了一些提取数据的方法。

listDatasets (string wildCard, string datasetType) listFeatureClasses (string wildCard, string featureType, string dataset)

listRasters (string wildCard, string rasterType)

listTables (string wildCard, string tableType)

listToolboxes(string wildCard)

listWorkspaces (string wildCard, string workspaceType)

看看代码段怎么写:

1.//list all the featureClasses starting with c

2.

3.gp.setEnvironmentValue("workspace", inputWorkspace);

4.

5.IGpEnumList featureClasses = gp.listFeatureClasses("c*", "", "");

6.

7.String featureClass = featureClasses.next();

8.

9.System.out.println("-----------Feature Classes starting with c-----------");

10.

11.while (! "".equals(featureClass)) {

12.

13.System.out.println(featureClass);

14.

15.featureClass = featureClasses.next();

16.

17.}

复制代码

通过指定wildCard字符串,搜索所有"c"开头的feature class,将结果存放在com.esri.arcgis.geoprocessing.IGpEnumList枚举List中。看到IGpEnumList千万不要将它和Java数据结构中各种List相提并论,它仅仅具有顺序枚举next和重置查询指针

reset的功能,可以被序列化。

再参考另外两个例子,相信对在工作空间中查询数据会有更多的认识。

返回所有面状要素

1.System.out.println("\n-----------Polygon Feature Classes-----------");

2.

3.gp.setEnvironmentValue("workspace", inputWorkspace);

4.

5.featureClasses = gp.listFeatureClasses("", "polygon", "");

6.

7.featureClass = featureClasses.next();

8.

9.while (! "".equals(featureClass)) {

10.

11.System.out.println(featureClass);

12.

13.featureClass = featureClasses.next();

14.

15.}

复制代码

返回所有TIF格式的Raster数据

1.// List all TIF files in the workspace and build pyramids

2.

3.gp.setEnvironmentValue("workspace", inputWorkspace);

4.

5.IGpEnumList rasters = gp.listRasters("", "TIF");

6.

7.String raster = rasters.next();

8.

9.

10.

11.BuildPyramids buildPyramids = new BuildPyramids(raster);

12.

13.while (! "".equals(raster)) {

14.

15.System.out.println("\n------------Building pyramids for: " + raster +

"----------");

16.

17.gp.execute(buildPyramids, null);

18.

19.raster = rasters.next();

20.

21.}

复制代码

关于各种list方法TYPE类型,可以参考下表

Method Type Keywords

ListDatasets All, Feature, Coverage, RasterCatalog, CAD, VPF,

TIN, Topology

ListFeatureClasses All, Point, Label, Node, Line, Arc, Route, Polygon, Region ListFields All, SmallInteger, Integer, Single,

Double, String, Date, OID, Geometry, Blob

ListWorkspaces All, Coverage, Access, SDE, Folder

ListTables All, dBASE, INFO

ListRasters All, ADRG, BIL, BIP, BSQ, BMP, CADRG, CIB, ERS, GIF, GIS, GRID, STACK, IMG, JPEG, LAN, SID, SDE, TIF, RAW, PNG, NITF

2.模型处理过程中各种输入数据的处理

Geoprocessing计算过程中会要求多个输入,通常可以用IGpEnumList来捕获。

1.gp.setEnvironmentValue("workspace", multiWorkspace);

2.

3.IGpEnumList polygonFCs = gp.listFeatureClasses("", "polygon", "");

4.

5.String polygon = polygonFCs.next();

6.

7.String polygonsToUnion = "";

8.

9.while (! "".equals(polygon)){

10.

11.polygonsToUnion += polygon;

12.

13.polygon = polygonFCs.next();

14.

15.if (! "".equals(polygon)){

16.

17.polygonsToUnion += ";";

18.

19.}

20.

21.}

22.

23.

24.

25. Union union = new

Union(polygonsToUnion,outputWorkspace+"/unioned.shp");

26.

27.gp.execute(union, null);

复制代码

另外,可以使用表结构来保存每个输入的参数值,避免全部feature保存在一个字符串中。

1.// List all feature classes in the workspace.

2.

3.gp.setEnvironmentValue("workspace", multiWorkspace);

4.

5.IGpEnumList polygonFCs = gp.listFeatureClasses("", "polygon", "");

6.

7.

8.

9.//make the value table

10.

11.GPValueTable gpValueTable = new GPValueTable();

12.

13.

14.

15.String polygon = polygonFCs.next();

16.

17.String row = null;

18.

19.while (! "".equals(polygon)){

20.

21.if ("center".equals(polygon)){

22.

23.row = polygon + " 1";

24.

25.} else {

26.

27.row = polygon + " 2";

28.

29.}

30.

31.

32.

33. gpValueTable.addRow(row);

34.

35.polygon = polygonFCs.next();

36.

37.}

38.

39.

40.

41.Union union = new Union(gpValueTable, outputWorkspace+"/unionedValueTable.shp");

42.

43.gp.execute(union, null);

复制代码

3.枚举、循环执行

前面两点都是针对一个Geoprocessing操作而言,如果需要多个操作,可以用基本程序语言来描述,这分为两种情况,一是多种Geoprocessing的数据处理,一种是同一Geoprocessing循环执行,相比ArcToolbox而言,这里体现的是程序代码带给我们的方便。

Blogjava中的链接:

https://www.360docs.net/doc/043934078.html,/flyingis/archive/2007/04/05/108701.html

IActiveView接口定义了Map对象的数据显示功能。使用该接口可以改变视图的范围,刷新视图。

IActiveView的PartialRefresh(esriViewGeography, pLayer, null)用于刷新指定图层:IActiveView的PartialRefresh(esriViewGeography, null, null) 用于刷新刷新所有图层:IActiveView的PartialRefresh(esriViewGeoSelection, null, null) 用于刷新所选择的对象:IActiveView的PartialRefresh(esriViewGraphics, null, null) 用于刷新图形元素:

IActiveView的PartialRefresh(esriViewGraphics, pElement, null) 用于刷新指定图形元素IActiveView的PartialRefresh(esriViewGraphics, null, null) 用于刷新所有图形元素IActiveView的PartialRefresh(esriViewGraphicSelection, null, null)用于刷新所选择的图元。

相关主题
相关文档
最新文档