Android Arcgis入门(五)、FeatureLayer加载本地shp文件与要素查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Gary__123456/article/details/68089509

FeatureLayer是要素图层,也是Arcgis的主要图层。用这个图层可以加载本地的shp文件。下面我们看怎样加载shp文件到MapView中。查看ArcGis API可知FeatureLayer的构造方法要传一个FeatureTable,而FeatureTable可用子类ShapefileFeatureTable来实现。代码如下,其中shpPath为shp文件的本地路径。

	private void initLayer() {
		try {

			featureLayer = new FeatureLayer(new ShapefileFeatureTable(
					shpPath));
			featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol(
					Color.GREEN)));

			mapView.addLayer(featureLayer);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
效果图如下。

这样就可以很简单地加载shp文件了。下面我们来学习一下FeatureLayer的查询。

一、FeatureLayer点击查询主要用到的方法是featureLayer.getFeatureIDs(float x, float y, int tolerance),方法与上一讲GraphicsLayer的查询差不多。可以获取每个要素的属性属性值。

	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());
				}
			}

		}
	}

二、利用FeatureTable查询,这是FeatureLayer特别的地方。比如在一个shp文件中,有多个要素,我们知道有每个要素中有name这个属性,怎样快速查询name为shanghai的要素呢?要用到FeatureTable的queryFeatures(QueryParameters query, CallbackListener<FeatureResult> callback)方法或者是queryIds(QueryParameters query, CallbackListener<long[]> callback)这个方法。

	private void queryTable() {
		FeatureTable featureTable = featureLayer.getFeatureTable();
		QueryParameters parameters = new QueryParameters();
		//查询条件,有点像数据库中的查询
		parameters.setWhere("name = 'shanghai'");						Future<long[]> queryIds = featureTable.queryIds(parameters,new CallbackListener<long[]>() {

					@Override
					public void onError(Throwable arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onCallback(long[] arg0) {
						// 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();
		}

	}


这样就可以快速查询到某个具体的要素。

原码与shp文件下载

由于项目太大,上传不成功,望见谅,测试shp下载:

http://download.csdn.net/detail/gary__123456/9892735

完整源码:

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();
}
}
}


展开阅读全文

没有更多推荐了,返回首页