欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

arcgis for Android 100.2 加载shp

程序员文章站 2023-11-01 08:55:58
arcgis for android 100.2 加载shp。 1、显示效果 2、实现代码 package com.arcgis.activity; import android.gr...

arcgis for android 100.2 加载shp。

1、显示效果

arcgis for Android 100.2 加载shp

arcgis for Android 100.2 加载shp

2、实现代码

package com.arcgis.activity;

import android.graphics.color;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.util.log;
import android.view.motionevent;

import com.arcgis.r;
import com.arcgis.until.filepathmanage;
import com.esri.arcgisruntime.concurrent.listenablefuture;
import com.esri.arcgisruntime.data.feature;
import com.esri.arcgisruntime.data.featurequeryresult;
import com.esri.arcgisruntime.data.featuretable;
import com.esri.arcgisruntime.data.queryparameters;
import com.esri.arcgisruntime.data.shapefilefeaturetable;
import com.esri.arcgisruntime.geometry.envelope;
import com.esri.arcgisruntime.geometry.geometry;
import com.esri.arcgisruntime.geometry.geometrytype;
import com.esri.arcgisruntime.geometry.point;
import com.esri.arcgisruntime.geometry.spatialreference;
import com.esri.arcgisruntime.layers.featurelayer;
import com.esri.arcgisruntime.mapping.arcgismap;
import com.esri.arcgisruntime.mapping.basemap;
import com.esri.arcgisruntime.mapping.view.defaultmapviewontouchlistener;
import com.esri.arcgisruntime.mapping.view.graphic;
import com.esri.arcgisruntime.mapping.view.graphicsoverlay;
import com.esri.arcgisruntime.mapping.view.mapview;
import com.esri.arcgisruntime.mapping.view.sketcheditor;
import com.esri.arcgisruntime.mapping.view.sketchstyle;
import com.esri.arcgisruntime.symbology.simplefillsymbol;
import com.esri.arcgisruntime.symbology.simplelinesymbol;
import com.esri.arcgisruntime.symbology.simplemarkersymbol;
import com.esri.arcgisruntime.symbology.simplerenderer;
import java.util.iterator;
import java.util.map;
import java.util.concurrent.executionexception;

/**
 * 数据量大的时候,加载速度比较慢,有时候还没加载出来,一直在找
 * 最后又pc上软件知道shp的位置,比较好测试
 *
 * 还有shp 必须有least three files (.shp, .shx, .dbf) 如果有prj文件那就更好。位置准确
 *
 * 受之前10的版本影响,我一直在找怎么设置投影,其实是多余的。不用设置,接口都没有。里面已经处理好
 *
 */
public class arcgisshpmapactivity extends appcompatactivity {
 private mapview mmapview;
 private shapefilefeaturetable shapefilefeaturetable;
 private arcgismap arcgismap;
 private featurelayer featurelayer;
 private sketcheditor mainsketcheditor;
 private sketchstyle mainsketchstyle;

 private string shpname = "/dljx.shp";
// private string shpname = "/未命名_20171130163924.shp";

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_map);
  mmapview = (mapview) findviewbyid(r.id.map_view);


  showshapefile();
//  openshp();
 }

 //加载shp方式一
 private void openshp() {
  // 构建shapefilefeaturetable,引入本地存储的shapefile文件
  shapefilefeaturetable = new shapefilefeaturetable(
 filepathmanage.getinstance().getshpdirectory() + shpname);
  shapefilefeaturetable.loadasync();
  // 构建featurelayerr
  featurelayer = new featurelayer(shapefilefeaturetable);
  // 设置shapefile文件的渲染方式
  simplelinesymbol linesymbol = new simplelinesymbol(simplelinesymbol.style.solid, color.red, 1.0f);
  simplefillsymbol fillsymbol = new simplefillsymbol(simplefillsymbol.style.solid, color.yellow, linesymbol);
  simplerenderer renderer = new simplerenderer(fillsymbol);
  featurelayer.setrenderer(renderer);
  // 添加到地图的业务图层组中
  arcgismap = new arcgismap(basemap.createtopographic());
  arcgismap.getoperationallayers().add(featurelayer);
  // set the map to the map view
  mmapview.setmap(arcgismap);
 }

 //加载shp方式二
 private void showshapefile() {
  mmapview.setattributiontextvisible(false);
  arcgismap = new arcgismap(new basemap().createimagerywithlabelsvector());
  mmapview.setmap(arcgismap);
  final shapefilefeaturetable shapefilefeaturetable = new shapefilefeaturetable(
 filepathmanage.getinstance().getshpdirectory() + shpname);
  shapefilefeaturetable.loadasync();
  shapefilefeaturetable.adddoneloadinglistener(new runnable() {
@override
public void run() {
 geometrytype gt = shapefilefeaturetable.getgeometrytype();
 string name = shapefilefeaturetable.gettablename();
 featurelayer = new featurelayer(shapefilefeaturetable);
 if (featurelayer.getfullextent() != null) {
  mmapview.setviewpointgeometryasync(featurelayer.getfullextent());
 } else {
  featurelayer.adddoneloadinglistener(new runnable() {
@override
public void run() {
 mmapview.setviewpointgeometryasync(featurelayer.getfullextent());
}
  });
 }
 arcgismap.getoperationallayers().add(featurelayer);
}
  });


  simplelinesymbol linesymbol = new simplelinesymbol(simplelinesymbol.style.solid, color.red, 1.0f);
  simplefillsymbol fillsymbol = new simplefillsymbol(simplefillsymbol.style.solid, color.yellow, linesymbol);
  simplerenderer renderer = new simplerenderer(fillsymbol);
  featurelayer.setrenderer(renderer);

 }

 public void queryshp2(){
  //单击shp 进行查询
  mmapview.setontouchlistener(new defaultmapviewontouchlistener(this, mmapview) {
@override
public boolean  onsingletapconfirmed(motionevent v) {
 android.graphics.point screenpoint=new android.graphics.point(math.round(v.getx()), math.round(v.gety()));
 point clickpoint = mmapview.screentolocation(screenpoint);
 queryparameters query = new queryparameters();
 query.setgeometry(clickpoint);// 设置空间几何对象
 featuretable mtable = featurelayer.getfeaturetable();//得到查询属性表
 final listenablefuture featurequeryresult
= mtable.queryfeaturesasync(query);
 featurequeryresult.adddonelistener(new runnable() {
  @override
  public void run() {
try {
 featurequeryresult featureresul = featurequeryresult.get();
 for (object element : featureresul) {
  if (element instanceof feature) {
feature mfeaturegrafic = (feature) element;
map mquerrystring = mfeaturegrafic.getattributes();
for(string key : mquerrystring.keyset()){
 log.i("show"+key,string.valueof(mquerrystring.get(key)));
}
  }
 }
}catch (exception e){
 e.printstacktrace();
}
  }
 });
 return true;
}
  });
 }


 //查询shp2
 public void queryshp() {
  try {
mainsketcheditor = new sketcheditor();
mainsketchstyle = new sketchstyle();
mainsketcheditor.setsketchstyle(mainsketchstyle);
mmapview.setsketcheditor(mainsketcheditor);
mmapview.setontouchlistener(
  new defaultmapviewontouchlistener(this, mmapview) {
@override
public boolean onsingletapconfirmed(motionevent e) {
 point clickpoint = mmapview.screentolocation(new android.graphics.point(math.round(e.getx()), math.round(e.gety())));
 int tolerance = 1;
 double maptolerance = tolerance * mmapview.getunitsperdensityindependentpixel();
 envelope envelope = new envelope(clickpoint.getx() - maptolerance, clickpoint.gety() - maptolerance, clickpoint.getx() + maptolerance, clickpoint.gety() + maptolerance, mmapview.getspatialreference());
 queryparameters query = new queryparameters();
 query.setgeometry(envelope);
 query.setspatialrelationship(queryparameters.spatialrelationship.within);
 final listenablefuture future = featurelayer.selectfeaturesasync(query, featurelayer.selectionmode.new);
 future.adddonelistener(new runnable() {
  @override
  public void run() {
try {
 featurequeryresult result = future.get();
 //mainshapefilelayer.getfeaturetable().deletefeaturesasync(result);
 iterator iterator = result.iterator();

} catch (exception e) {
 e.getcause();
}
  }
 });

 return super.onsingletapconfirmed(e);
}
  }
);
  } catch (exception e) {
e.getcause();
  }
 }

 //增加
 public void addshp() {
  // 调用iseditable和canadd方法判断文件是否支持编辑操作,是否可添加要素;否,则抛出信息
  if (shapefilefeaturetable.iseditable() && shapefilefeaturetable.canadd()) {

// 构建新增几何
point mappoint = new point(140.0, 39.0, spatialreference.create(4326));

// 构建待增加的feature对象,设置几何,设置属性
feature feature = shapefilefeaturetable.createfeature();
feature.setgeometry(mappoint);
feature.getattributes().put("name", "测试点");

// 调用addfeatureasync方法增加要素
final listenablefuture addfeatureoper = shapefilefeaturetable.addfeatureasync(feature);

// 在操作完成的监听事件中判断操作是否成功
addfeatureoper.adddonelistener(new runnable() {
 @override
 public void run() {
  try {
addfeatureoper.get();
if (addfeatureoper.isdone()) {
 log.i("shapefileedit:", "feature added!");
}
  } catch (interruptedexception interruptedexceptionexception) {
// 处理异常
  } catch (executionexception executionexception) {
// 处理异常
  }
 }
});
  } else {
log.i("shapefileedit:", "the shapefile cann't be edited");
  }
 }

 protected void onresume() {
  super.onresume();
  mmapview.resume();
 }

 @override
 protected void onpause() {
  super.onpause();
  mmapview.pause();
 }


}
测试的注意事项,自已看啦
* 数据量大的时候,加载速度比较慢,有时候还没加载出来,一直在找
* 最后又pc上软件知道shp的位置,比较好测试
*
* 还有shp 必须有least three files (.shp, .shx, .dbf) 如果有prj文件那就更好。位置准确
*
* 受之前10的版本影响,我一直在找怎么设置投影,其实是多余的。不用设置,接口都没有。里面已经处理好

3、权限

对于新手开发,我每次都强调权限