Android GIS开发系列-- 入门季(5) FeatureLayer加载本地shp文件与要素查询

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值