Android GIS開發系列-- 入門季(5) FeatureLayer載入本地shp檔案與要素查詢

weixin_34391854發表於2017-10-29

 

FeatureLayer是要素圖層,也是Arcgis的主要圖層。用這個圖層可以載入本地的shp檔案。下面我們看怎樣載入shp檔案到MapView中。檢視ArcGis API可知FeatureLayer的構造方法要傳一個FeatureTable,而FeatureTable可用子類ShapefileFeatureTable來實現。程式碼如下,其中shpPath為shp檔案的本地路徑。\

 

  1. private void initLayer() {  
  2.         try {  
  3.   
  4.             featureLayer = new FeatureLayer(new ShapefileFeatureTable(  
  5.                     shpPath));  
  6.             featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol(  
  7.                     Color.GREEN)));  
  8.   
  9.             mapView.addLayer(featureLayer);  
  10.   
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }

效果圖如下。

這樣就可以很簡單地載入shp檔案了。下面我們來學習一下FeatureLayer的查詢。

一、FeatureLayer點選查詢主要用到的方法是featureLayer.getFeatureIDs(float x, float y, int tolerance),方法與上一講GraphicsLayer的查詢差不多。可以獲取每個要素的屬性屬性值。

 

  1. private void handleSingleTap(float x, float y) {  
  2.   
  3.         long[] featureIds = featureLayer.getFeatureIDs(x, y, 8);  
  4.         if (featureIds != null && featureIds.length > 0) {  
  5.             for (long id : featureIds) {  
  6.                 com.esri.core.map.Feature feature = featureLayer.getFeature(id);  
  7.                 Map<String, Objectattrs = feature.getAttributes();  
  8.                 Set<Entry<String, Object>setEntry = attrs.entrySet();  
  9.                 for (Entry<String, Object> entryItem : setEntry) {  
  10.                     Log.e(TAG,  
  11.                             entryItem.getKey() + "====" + entryItem.getValue());  
  12.                 }  
  13.             }  
  14.   
  15.         }  
  16.     }

二、利用FeatureTable查詢,這是FeatureLayer特別的地方。比如在一個shp檔案中,有多個要素,我們知道有每個要素中有name這個屬性,怎樣快速查詢name為shanghai的要素呢?要用到FeatureTable的queryFeatures(QueryParameters query, CallbackListener<FeatureResult> callback)方法或者是queryIds(QueryParameters query, CallbackListener<long[]> callback)這個方法。

 

  1. private void queryTable() {  
  2.         FeatureTable featureTable = featureLayer.getFeatureTable();  
  3.         QueryParameters parameters = new QueryParameters();  
  4.         //查詢條件,有點像資料庫中的查詢  
  5.         parameters.setWhere("name = 'shanghai'");<span style="white-space:pre">                     </span>Future<long[]queryIds = featureTable.queryIds(parameters,new CallbackListener<long[]>() {  
  6.   
  7.                     @Override  
  8.                     public void onError(Throwable arg0) {  
  9.                         // TODO Auto-generated method stub  
  10.   
  11.                     }  
  12.   
  13.                     @Override  
  14.                     public void onCallback(long[] arg0) {  
  15.                         // TODO Auto-generated method stub  
  16.   
  17.                     }  
  18.                 });  
  19.   
  20.         try {  
  21.             long[] Ids = queryIds.get();  
  22.             if (Ids != null && Ids.length > 0) {  
  23.                 for (int i = 0; i Ids.length; i++) {  
  24.                     Feature feature = featureLayer.getFeature(Ids[i]);  
  25.                     Log.i(TAG, feature.getAttributes().get("name") + "");  
  26.                 }  
  27.             }  
  28.         } catch (InterruptedException e) {  
  29.             e.printStackTrace();  
  30.         } catch (ExecutionException e) {  
  31.             e.printStackTrace();  
  32.         }

 

這樣就可以快速查詢到某個具體的要素。

package com.huang.featuer;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Future;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import com.esri.android.map.FeatureLayer;

import com.esri.android.map.MapView;

import com.esri.android.map.event.OnSingleTapListener;

import com.esri.core.geodatabase.ShapefileFeatureTable;

import com.esri.core.map.CallbackListener;

import com.esri.core.map.Feature;

import com.esri.core.renderer.SimpleRenderer;

import com.esri.core.symbol.SimpleFillSymbol;

import com.esri.core.table.FeatureTable;

import com.esri.core.tasks.query.QueryParameters;

public class FeatureLayerActivity extends Activity implements OnClickListener {

private static final String TAG = FeatureLayerActivity.class.getName();

private MapView mMapView;

private String shpPath = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/test/test.shp";

private FeatureLayer featureLayer;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



mMapView = (MapView) findViewById(R.id.mapView);

findViewById(R.id.query_btn).setOnClickListener(this);

initLayer();



mMapView.setOnSingleTapListener(new OnSingleTapListener() {

public void onSingleTap(float x, float y) {

handleSingleTap(x, y);

}

});

}



private void initLayer() {

try {

featureLayer = new FeatureLayer(new ShapefileFeatureTable(shpPath));

featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol(

Color.GREEN)));

mMapView.addLayer(featureLayer);

} catch (Exception e) {

e.printStackTrace();

}

}

private void handleSingleTap(float x, float y) {

long[] featureIds = featureLayer.getFeatureIDs(x, y, 8);

if (featureIds != null && featureIds.length > 0) {

for (long id : featureIds) {

com.esri.core.map.Feature feature = featureLayer.getFeature(id);

Map<String, Object> attrs = feature.getAttributes();

Set<Entry<String, Object>> setEntry = attrs.entrySet();

for (Entry<String, Object> entryItem : setEntry) {

Log.e(TAG,

entryItem.getKey() + "====" + entryItem.getValue());

}

}

}

}



public void onClick(View v) {

switch (v.getId()) {

case R.id.query_btn:

query();

break;

default:

break;

}

}



private void query() {

FeatureTable featureTable = featureLayer.getFeatureTable();

QueryParameters parameters = new QueryParameters();

// 查詢條件,有點像資料庫中的查詢

parameters.setWhere("name = 'shanghai'");

Future<long[]> queryIds = featureTable.queryIds(parameters,

new CallbackListener<long[]>() {

public void onError(Throwable e) {

// TODO Auto-generated method stub

}

public void onCallback(long[] objs) {

// TODO Auto-generated method stub

}

});

try {

long[] Ids = queryIds.get();

if (Ids != null && Ids.length > 0) {

for (int i = 0; i < Ids.length; i++) {

Feature feature = featureLayer.getFeature(Ids[i]);

Log.i(TAG, feature.getAttributes().get("name") + "");

}

}

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

}

}

}
Code for all

 

相關文章