I. 幾何對象操作與地理處理
幾何對象(Geometry)的操作和地理處理(GeoProcessing/GP)是在地圖基礎上實現更複雜GIS能力的必要功能,在這一章中讓我們來看看在ArcGIS Android API中的如何對幾何對象進行操作和如何使用GP服務。
幾何對象的操作
ArcGIS Android API對幾何對象的操作和其它ArcGIS的Web API有很大區別,它並不是依賴ArcGIS Sever的Geometry Service,而是在API庫中本身就包含了對幾何對象的定義和處理。
如果我們相對一個幾何對象進行buffer操作,這時不需要再指定一個Geometry Service的地址了,而是使用ArcGIS Android API提供的GeometryEngine類就可以實現這些類似的功能。
比如下面的代碼實現了一個buffer的功能:
map.setOnLongPressListener(new OnLongPressListener() {
public void onLongPress(float x, float y) {
Point pt = map.toMapPoint(new Point(x, y));
Polygon pg = GeometryEngine.buffer(pt, map
.getSpatialReference(), 1000000, null); //null表示採用地圖單位
Graphic g = new Graphic();
g.setGeometry(pg);
AgsGeometryEngine.this.gLayer.addGraphic(g);
AgsGeometryEngine.this.gLayer.postInvalidate();
}
});
這樣,當我在地圖上長按時,程式會捕捉到地圖上的座標,然後通過GeometryEngine的靜態方法buffer對這個點進行一個緩衝操作,最後把結果繪製到GraphicsLayer上去,這個程式執行的效果是這個樣子的:
圖 29 使用GeometryEngine對點進行緩衝的結果
如果我還想對產生的這個多邊形進行一下裁剪,依然可以使用GeometryEngine實現:
map.setOnLongPressListener(new OnLongPressListener() {
public void onLongPress(float x, float y) {
Point pt = map.toMapPoint(new Point(x, y));
Polygon pg = GeometryEngine.buffer(pt, map
.getSpatialReference(), 2000000, null);
Geometry geo = GeometryEngine.clip(pg, map.getExtent(), map
.getSpatialReference());//用當前地圖範圍進行裁剪
Graphic g = new Graphic();
g.setGeometry(geo);
AgsGeometryEngine.this.gLayer.addGraphic(g);
AgsGeometryEngine.this.gLayer.postInvalidate();
}
});
這樣,當我在程式左上方邊界附近處長按螢幕產生一個圓形的多邊形後,程式還會調用GeometryEngine進行一個clip操作,通過這個操作多邊形將只保留在當前程式視窗內的部分,所以,如果再拖動一下地圖就可以看到,這個多邊形已經成為了一個餅狀的圖形, 30。
圖 30 使用GeometryEngine對一個多邊形進行裁剪後的結果