arcgis for android 离线空间数据叠加分析

arcgis for android 离线空间数据叠加分析
        arcgis for android 离线空间数据是调用geodatabase文件,geodatabase文件里面可以有很多次图层比如地类、基本农田、建设用地等等。如果我们现在有一个需求要在底图上画一块范围去和各个图层去叠加分析该怎么实现。比如圈定一块范围去和地类分析,分析出圈定的范围都包括什么地类,一种地类占用多少亩等等的一些数据。
        我们就拿分析地类来文章:
        首先我们加入影像底图,这个就不说了很简单导入arcgis for Android sdk 怎么配置如来arcgis 官网
 
  
 
  
String url = Environment.getExternalStorageDirectory().toString() + "/datas/data.tpk";
ArcGISLocalTiledLayer layer = new ArcGISLocalTiledLayer(url);
mMapView.addLayer(layer);
        然后我们加入我们的 geodatabase文件,在这里做了一个判断是判断是不是土地利用现状,因为我们现在就和土地利用现状做分析。
 
  
 
  
private void initGeodatabase() {
    String url = Environment.getExternalStorageDirectory().toString() + "/datas/data.geodatabase";
    Geodatabase localGdb = null;
    try {
        localGdb = new Geodatabase(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (localGdb != null) {
        for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseTables()) {
            if (gdbFeatureTable.hasGeometry()) {
                if (gdbFeatureTable.getTableName().equals("土地利用现状"))
                    dataFeatureLayer = new FeatureLayer(gdbFeatureTable);

                // mMapView.addLayer(dataFeatureLayer);
            }
        }

    }
}
        前面说了我们要和任意圈定的范围去分析,我们在底图上画出一块范围来。用我们在底图上圈出来的范围去和空间数据做分析。
mMapView.setOnSingleTapListener(new OnSingleTapListener() {
   @Override
    public void onSingleTap(float v, float v1) {
        Point mPoint = mMapView.toMapPoint(v, v1);// 选择点
        mPoints.add(mPoint);// 选择点加入点的集合
        Graphic messurePoint = new Graphic(mPoint, mMarkerSymbol);// 点的要素初始化
        messureLayer.addGraphic(messurePoint);// 点的要素添加到图层
        if (mPoints.size() == 1) {
            messurePolygon.startPath(mPoints.get(0));// 设置面的初始点
        }
        if (mPoints.size() > 1) {
            messureLayer.removeAll();
            messurePolyline.setEmpty();
            messurePolygon.lineTo(mPoint);// 连接到当前点击的点
            messurePolygon.lineTo(mPoints.get(0));// 连接到初始点
            for (int i = 0; i < mPoints.size() - 1; i++) {
                messureLine.setStart(mPoints.get(i));
                messureLine.setEnd(mPoints.get(i + 1));
                messurePolyline.addSegment(messureLine, true);
                messureLayer.addGraphic(new Graphic(mPoints.get(i), messureMarkerSymbol));
            }
            messureLayer.addGraphic(new Graphic(mPoints.get(mPoints.size() - 1), messureMarkerSymbol));
            messureLine.setStart(mPoint);
            messureLine.setEnd(mPoints.get(0));
            messurePolyline.addSegment(messureLine, true);
            messureLineGraphic = new Graphic(messurePolyline, messureLineSymbol);
            messureAreaGraphic = new Graphic(messurePolygon, messureFillSymbol);// 初始化面的要素
            messureLayer.addGraphic(messureAreaGraphic);// 将面的要素添加到图层
            messureLayer.addGraphic(messureLineGraphic);
            messurePolygon.startPath(mPoint);// 将当前点设为初始点
        }
    }
});
        下面就是看这篇文章最关心的地方了。我们在下面新建了一个QueryParameters对象,这个对象的作用是什么我想大家看到下面的代码就明白了。我们往 QueryParameters对象里面set一个messurePolygon我想很多人都已经明白了,它就是我们在上面圈定的范围。
        try {
            QueryParameters query = new QueryParameters();
            query.setOutFields(new String[]{"*"});// 设置返回字段的数组
            query.setGeometry(messurePolygon);// 设置空间几何对象
            if (dataFeatureLayer == null) {
                return false;
            } else {
                FeatureTable mTable = dataFeatureLayer.getFeatureTable();
                mTable.queryFeatures(query, new CallbackListener<FeatureResult>() {
                    @Override
                    public void onCallback(FeatureResult featureResul) {
                        //解析分析结果
                        Landresult = getLandUseStatusAnalysisResultByPoints(featureResul);
                        Message msg = new Message();
                        msg.what = ANALYSIS;
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }
                });

            }
        } catch (Exception e) {
            Toast toast = Toast.makeText(MainActivity.this, "查寻异常", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
        return false;
    }
});

public LandUseStatusAnalysisResult getLandUseStatusAnalysisResultByPoints(FeatureResult featureResult) {
    LandUseStatusAnalysisResult resut = new LandUseStatusAnalysisResult();
    // 先将最小粒度的地块的面积、graphic、和数量
    Integer totalNums = 0; // 总计地块数的计算
    Double totalArea = 0.0; // 总计面积数的计算
    Integer farmNums = 0;
    Double farmArea = 0.0;
    Integer buildNums = 0;
    Double buildArea = 0.0;
    Integer nouseNums = 0;
    Double nouseArea = 0.0;
    List<Graphic> graphicList = new ArrayList<>();
    List<Graphic> F = new ArrayList<>();
    List<Graphic> B = new ArrayList<>();
    List<Graphic> N = new ArrayList<>();
    List<LandUseStatusItem> resultList = new ArrayList<>();

    // 以权属单位分组计算
    Map<String, List<PAnalysistItemInfo>> groupMap = getGroupMapByGS(featureResult, "QSDWMC");
    for (Map.Entry<String, List<PAnalysistItemInfo>> entry : groupMap.entrySet()) {
        String qsdwmc = entry.getKey();
        List<PAnalysistItemInfo> qsItemList = entry.getValue();
        LandUseStatusItem landUseStatusItem = getLandUseStatusItem(qsdwmc, qsItemList);// 归类计算
        // landUseStatusItem = getTextOperate(landUseStatusItem);//拼接文本
        totalNums = totalNums + landUseStatusItem.FarmLand.TotalNums + landUseStatusItem.BuildLand.TotalNums
                + landUseStatusItem.NoUseLand.TotalNums;
        totalArea = totalArea + landUseStatusItem.FarmLand.TotalArea + landUseStatusItem.BuildLand.TotalArea
                + landUseStatusItem.NoUseLand.TotalArea;
        farmNums = farmNums + landUseStatusItem.FarmLand.TotalNums;
        farmArea = farmArea + landUseStatusItem.FarmLand.TotalArea;
        buildNums = buildNums + landUseStatusItem.BuildLand.TotalNums;
        buildArea = buildArea + landUseStatusItem.BuildLand.TotalArea;
        nouseNums = nouseNums + landUseStatusItem.NoUseLand.TotalNums;
        nouseArea = nouseArea + landUseStatusItem.NoUseLand.TotalArea;
        F.addAll(landUseStatusItem.FarmLand.GraphicList);
        B.addAll(landUseStatusItem.BuildLand.GraphicList);
        N.addAll(landUseStatusItem.NoUseLand.GraphicList);
        resultList.add(landUseStatusItem);
    }
    // 计算总计数据
    resut.TotalNums = totalNums;
    resut.TotalArea = totalArea;
    resut.FarmNums = farmNums;
    resut.FarmArea = farmArea;
    resut.BuildNums = buildNums;
    resut.BuildArea = buildArea;
    resut.NoUseNums = nouseNums;
    resut.NoUseArea = nouseArea;
    resut.BuildGraphic = B;
    resut.FarmGraphic = F;
    resut.NoUseGraphic = N;
    resut.GraphicList = graphicList;
    resut.ResultList = resultList;
    return resut;
}
 
   
 
   
private Map<String, List<PAnalysistItemInfo>> getGroupMapByGS(FeatureResult featureResult, String qsFieldName) {
    Map<String, List<PAnalysistItemInfo>> groupMap = new HashMap<>();
    pAnalysistItemInfoList = new ArrayList();
    try {
        if (featureResult != null && featureResult.featureCount() > 0) {
            for (Object resultItem : featureResult) {
                Feature feature = (Feature) resultItem;// 遍历出一个结果
                if (feature instanceof Feature) {
                    Feature searchFeature = (Feature) feature;
                    Map<String, Object> mQuerryString = feature.getAttributes();
                    Log.e("feature", feature.toString());
                    Log.e("mQuerryString", mQuerryString.toString());
                    Geometry mGeometry = GeometryEngine.intersect(searchFeature.getGeometry(), messurePolygon,
                            mMapView.getSpatialReference());
                    PAnalysistItemInfo itemInfoTemp = new PAnalysistItemInfo();
                    mGeometry = GeometryEngine.geodesicDensifyGeometry(mGeometry, mMapView.getSpatialReference());
                    String dltype = getLandType("DLBM", mQuerryString).toString();
                    if (dltype.equals("")) {
                        continue;
                    }
                    // 储存属性信息
                    itemInfoTemp.setDlName(mQuerryString.get("DLMC").toString());
                    String area1 = String.format("%.2f",
                            Double.valueOf(
                                    GeometryEngine.geodesicArea(mGeometry, mMapView.getSpatialReference(), null))
                                    * 0.0015);
                    itemInfoTemp.setDlArea(area1);
                    itemInfoTemp.setDlType(dltype);

                    // 储存图形信息
                    Graphic mFeatureResultGraphic = new Graphic(mGeometry, pAnalysistSymbol);
                    pointAnalysistGraphics.add(mFeatureResultGraphic);

                    if (dltype.equals("农用地")) {
                        messureAreaGraphic = new Graphic(mGeometry, messureFillSymbol_green);
                        pointAnalysistAllGraphicsLayer.addGraphic(messureAreaGraphic);
                    } else if (dltype.equals("建设用地")) {
                        messureAreaGraphic = new Graphic(mGeometry, messureFillSymbol_red);
                        pointAnalysistAllGraphicsLayer.addGraphic(messureAreaGraphic);
                    } else if (dltype.equals("未利用地")) {
                        messureAreaGraphic = new Graphic(mGeometry, messureFillSymbol_blue);
                        pointAnalysistAllGraphicsLayer.addGraphic(messureAreaGraphic);
                    }
                    itemInfoTemp.setGraphic(mFeatureResultGraphic);

                    String code = mQuerryString.get(qsFieldName).toString();

                    if (groupMap.isEmpty() || (!groupMap.containsKey(code))) {
                        List<PAnalysistItemInfo> list = new ArrayList<>();
                        groupMap.put(code, list);
                    }
                    groupMap.get(code).add(itemInfoTemp);

                    pAnalysistItemInfoList.add(itemInfoTemp);
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return groupMap;
}
 
   
 
   
private String getLandType(String keyField, Map<String, Object> mQuerryString) {
    String code = mQuerryString.get(keyField).toString();
    String landType = "";
    if (code.equals("011") || code.equals("012") || code.equals("013")// 十二类之耕地01
            || code.equals("021") || code.equals("022") || code.equals("023")// 十二类之园地02
            || code.equals("031") || code.equals("032") || code.equals("033")// 十二类之林地03
            || code.equals("041") || code.equals("042")// 十二类之草地04
            || code.equals("104")// 十二类之水域及水利设施用地11
            || code.equals("114") || code.equals("117") || code.equals("122") || code.equals("123")) {
        landType = "农用地";
    } else if (code.equals("051") || code.equals("052") || code.equals("053") || code.equals("054")
            || code.equals("061") || code.equals("062") || code.equals("063") || code.equals("071")
            || code.equals("072") || code.equals("081") || code.equals("082") || code.equals("083")
            || code.equals("084") || code.equals("085") || code.equals("086") || code.equals("087")
            || code.equals("088") || code.equals("091") || code.equals("092") || code.equals("093")
            || code.equals("094") || code.equals("095") || code.equals("101") || code.equals("102")
            || code.equals("103") || code.equals("104") || code.equals("105") || code.equals("106")
            || code.equals("107") || code.equals("113") || code.equals("118") || code.equals("121")
            || code.equals("201") || code.equals("202") || code.equals("203") || code.equals("204")
            || code.equals("205")) {
        landType = "建设用地";
    } else if (code.equals("043") || code.equals("111") || code.equals("112") || code.equals("115")
            || code.equals("116") || code.equals("119") || code.equals("124") || code.equals("125")
            || code.equals("126") || code.equals("127")) {
        landType = "未利用地";
    }
    return landType;
}
 
   

         然后就是归类统计了,这里举的例子是地类图斑的例子,根据图斑的属性按照农用地,建设用地和未利用地进行归类统计:   

 
   
private LandUseStatusItem getLandUseStatusItem(String qsName, List<PAnalysistItemInfo> paList) {
    LandUseStatusItem item = new LandUseStatusItem();
    item.AdmName = qsName;
    // 初始化
    item.FarmLand = new FarmLand();
    item.FarmLand.TotalNums = 0;
    item.FarmLand.TotalArea = 0.0;
    item.FarmLand.LandTypeMap = new HashMap<>();
    item.FarmLand.GraphicList = new ArrayList<Graphic>();
    item.BuildLand = new BuildLand();
    item.BuildLand.TotalNums = 0;
    item.BuildLand.TotalArea = 0.0;
    item.BuildLand.LandTypeMap = new HashMap<>();
    item.BuildLand.GraphicList = new ArrayList<Graphic>();
    item.NoUseLand = new NoUseLand();
    item.NoUseLand.TotalNums = 0;
    item.NoUseLand.TotalArea = 0.0;
    item.NoUseLand.LandTypeMap = new HashMap<>();
    item.NoUseLand.GraphicList = new ArrayList<Graphic>();
    item.GraphicList = new ArrayList<Graphic>();
    // 归类计算
    if (paList != null && !paList.isEmpty()) {
        for (PAnalysistItemInfo paItem : paList) {
            Double area = 0.0;
            Graphic graphic = null;
            try {
                area = Double.parseDouble(paItem.getDlArea());
                graphic = paItem.getGraphic();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            item.GraphicList.add(graphic);
            if (paItem.getDlType().equals("农用地")) {
                item.FarmLand.TotalNums = item.FarmLand.TotalNums + 1;
                item.FarmLand.TotalArea = item.FarmLand.TotalArea + area;
                item.FarmLand.GraphicList.add(graphic);
                if (item.FarmLand.LandTypeMap.isEmpty()) {
                    // 十二类无数据时处理
                    LandType landTypeItem = new LandType();
                    landTypeItem.TotalNums = 1;
                    landTypeItem.TotalAreas = area;
                    landTypeItem.LandTypeCode = "";
                    landTypeItem.LandTypeName = paItem.getDlName();
                    landTypeItem.Graphic = new ArrayList<Graphic>();
                    landTypeItem.Graphic.add(graphic);
                    item.FarmLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                } else {
                    if (item.FarmLand.LandTypeMap.containsKey(paItem.getDlName())) {
                        // 存在该类数据时处理
                        if (item.FarmLand.LandTypeMap.get(paItem.getDlName()) != null) {
                            try {
                                item.FarmLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums = item.FarmLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums + 1;
                                item.FarmLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas = item.FarmLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas + area;
                                item.FarmLand.LandTypeMap.get(paItem.getDlName()).Graphic.add(graphic);
                            } catch (Exception e) {
                                String ee = e.toString();
                            }
                        }

                    } else {
                        // 已有十二类其他数据而没有该类时处理
                        LandType landTypeItem = new LandType();
                        landTypeItem.TotalNums = 1;
                        landTypeItem.TotalAreas = area;
                        landTypeItem.LandTypeCode = "";
                        landTypeItem.LandTypeName = paItem.getDlName();
                        landTypeItem.Graphic = new ArrayList<Graphic>();
                        landTypeItem.Graphic.add(graphic);
                        item.FarmLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                    }
                }
            } else if (paItem.getDlType().equals("建设用地")) {
                item.BuildLand.TotalNums = item.BuildLand.TotalNums + 1;
                item.BuildLand.TotalArea = item.BuildLand.TotalArea + area;
                item.BuildLand.GraphicList.add(graphic);
                if (item.BuildLand.LandTypeMap.isEmpty()) {
                    // 十二类无数据时处理
                    LandType landTypeItem = new LandType();
                    landTypeItem.TotalNums = 1;
                    landTypeItem.TotalAreas = area;
                    landTypeItem.LandTypeCode = "";
                    landTypeItem.LandTypeName = paItem.getDlName();
                    landTypeItem.Graphic = new ArrayList<Graphic>();
                    landTypeItem.Graphic.add(graphic);
                    item.BuildLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                } else {
                    if (item.BuildLand.LandTypeMap.containsKey(paItem.getDlName())) {
                        // 存在该类数据时处理
                        if (item.BuildLand.LandTypeMap.get(paItem.getDlName()) != null) {
                            try {
                                item.BuildLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums = item.BuildLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums + 1;
                                item.BuildLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas = item.BuildLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas + area;
                                item.BuildLand.LandTypeMap.get(paItem.getDlName()).Graphic.add(graphic);
                            } catch (Exception e) {
                                String ee = e.toString();
                            }
                        }

                    } else {
                        // 已有十二类其他数据而没有该类时处理
                        LandType landTypeItem = new LandType();
                        landTypeItem.TotalNums = 1;
                        landTypeItem.TotalAreas = area;
                        landTypeItem.LandTypeCode = "";
                        landTypeItem.LandTypeName = paItem.getDlName();
                        landTypeItem.Graphic = new ArrayList<Graphic>();
                        landTypeItem.Graphic.add(graphic);
                        item.BuildLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                    }
                }
            } else if (paItem.getDlType().equals("未利用地")) {
                item.NoUseLand.TotalNums = item.NoUseLand.TotalNums + 1;
                item.NoUseLand.TotalArea = item.NoUseLand.TotalArea + area;
                item.NoUseLand.GraphicList.add(graphic);
                if (item.NoUseLand.LandTypeMap.isEmpty()) {
                    // 十二类无数据时处理
                    LandType landTypeItem = new LandType();
                    landTypeItem.TotalNums = 1;
                    landTypeItem.TotalAreas = area;
                    landTypeItem.LandTypeCode = "";
                    landTypeItem.LandTypeName = paItem.getDlName();
                    landTypeItem.Graphic = new ArrayList<Graphic>();
                    landTypeItem.Graphic.add(graphic);
                    item.NoUseLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                } else {
                    if (item.NoUseLand.LandTypeMap.containsKey(paItem.getDlName())) {
                        // 存在该类数据时处理
                        if (item.NoUseLand.LandTypeMap.get(paItem.getDlName()) != null) {
                            try {
                                item.NoUseLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums = item.NoUseLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalNums + 1;
                                item.NoUseLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas = item.NoUseLand.LandTypeMap
                                        .get(paItem.getDlName()).TotalAreas + area;
                                item.NoUseLand.LandTypeMap.get(paItem.getDlName()).Graphic.add(graphic);
                            } catch (Exception e) {
                                String ee = e.toString();
                            }
                        }

                    } else {
                        // 已有十二类其他数据而没有该类时处理
                        LandType landTypeItem = new LandType();
                        landTypeItem.TotalNums = 1;
                        landTypeItem.TotalAreas = area;
                        landTypeItem.LandTypeCode = "";
                        landTypeItem.LandTypeName = paItem.getDlName();
                        landTypeItem.Graphic = new ArrayList<Graphic>();
                        landTypeItem.Graphic.add(graphic);
                        item.NoUseLand.LandTypeMap.put(paItem.getDlName(), landTypeItem);
                    }
                }

            }
        }
    }
    return item;
}

到现在我们就对地类的分析做完了下面给你们附上效果图,更具体的代码就不贴了有想看源码的我在下面附上了连接可以去看看。

        

        这是源码连接如果有需要的可以下载参考点击打开链接

 
  

猜你喜欢

转载自blog.csdn.net/qq_34368586/article/details/80064080