茶凉曲落 2021-07-10 18:37 采纳率: 0%
浏览 170

ArcGIS for Android SDK读取shp文件读不出东西

相同的实现方式,放一个程序中正常读取加载,另一个程序读出来的全是空数据
异常的程序:
MapLayerManager包用来读文件

public String COMPath;
File loadfile = null;
Context context;
ShpMapListener shpMapListener;
//获取文件路径
    public String getFilePath(String FileName){
        if (COMPath == null) {
            //COMPath = context.getFilesDir().getPath();
            COMPath = Environment.getExternalStorageDirectory().getPath()+"/map";
        }
        return COMPath+"/"+FileName;
    }
//加载文件
    public void LoadFile(String filename){
        String u_filetype[] = filename.split("\\.");
        String filetype = u_filetype[u_filetype.length - 1];
        switch (filetype){
            case "shp": loadShpFile(filename); break;
            case "tif": loadTifFile(filename); break;
        }
    }
    //加载tif文件
    private void loadTifFile(String filename) {
    }
    //加载shp文件
    private void loadShpFile(String filename) {
        String filepath = getFilePath(filename);
        File file = new File(filepath);
        if (file.exists()) {
            ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(filepath);
            shapefileFeatureTable.loadAsync();
            shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
                @Override
                public void run() {
                    Log.d("DZR", "开始加载地图");
                    shpMapListener.onGetFeatureTable(shapefileFeatureTable);
                    Log.d("DZR", "加载完成");
                }
            });
        }
    }
//加载shp文件的回调函数
    public interface ShpMapListener{
        public void  onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable);
    }
//设置shp回调
    public void setShpMapListener(ShpMapListener shpMapListener){
        this.shpMapListener = shpMapListener;
    }

HomeFragment extends Fragment对上文的调用:

private FragmentHomeBinding binding;
private MapView mapView;
Button button1;
FeatureLayer featureLayer1;
public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        //不知道干啥的
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
  
        //对象初始化
        mapView = binding.HOMEMAP1;
        imageButton = binding.HOMEIMGBUT1;
        button = binding.HOMEBUT1;
        button1 = binding.HOMEBUT2;
        textView1 = binding.HOMETEXT1;
        dzReadFile.initContext(getContext());
        //获取GPS定位信息
        locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        textView1.setText("获取GPS位置");
        if (getAdress()) textView1.setText("获取位置完成");else textView1.setText("位置获取失败");
        //绘制基础地图
        ArcGISMap arcGISMap;
        if (getAdress()) {
            arcGISMap = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 16);
        }else {
            arcGISMap = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 10);
        }
        mapView.setAttributionTextVisible(false);//去水印
        mapView.setMap(arcGISMap);
        //设置位置显示
        mapView.getLocationDisplay().addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
            @Override
            public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {
                if (dataSourceStatusChangedEvent.isStarted()) {
                    return;
                }
                if (dataSourceStatusChangedEvent.getError() == null) {
                    return;
                }
            }
        });
        mapView.getLocationDisplay().startAsync();
}
//按钮按下开始读取shp文件并加载
button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dzReadFile.setShpMapListener(new ShpMapListener() {
                    @Override
                    public void onGetFeatureTable(ShapefileFeatureTable shapefileFeatureTable) {
                        featureLayer1 = new FeatureLayer(shapefileFeatureTable);
                        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.DKGRAY, 1.0f);
                        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
                        SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
                        featureLayer1.setRenderer(renderer);
                        binding.HOMEMAP1.getMap().getOperationalLayers().add(featureLayer1);
                        //mapView.setViewpointAsync(new Viewpoint(featureLayer1.getFullExtent()));
                        Log.d("HF","townbount.shp已加载");
                    }
                });
                dzReadFile.initContext(getContext());
                dzReadFile.LoadFile("townbount.shp");
            }
        });

        return root;
    }

正常程序的代码:
路径获取

public class MapFilePathManager {

    public static final String dirName = "/map/";

    public static final String townbount = getShpFile("townbount.shp");
    public static final String disasters = getShpFile("disasters.shp");
    public static final String Gangu_bount = getShpFile("Gangu_bount.shp");
    public static final String all2020 = getShpFile("all2020.tif");
    
    public static String getShpFile(String fileName) {
        return  getMapFileDIRPath() + fileName;
    }
    
    public static String getMapFileDIRPath() {
        String filePath = Environment.getExternalStorageDirectory().getPath() + dirName;
//        String filePath =
        return filePath;
    }
}

文件读取

public class MapLayerManager {

    //成功加载地图文件的回调
    public interface MapLayerListener {
        public void  onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable);

    }
    //成功加载Feature的回调
    public interface QueryFeaturesListener {
        public void  onGetFeatureQueryResult(FeatureQueryResult featureQueryResult);

    }
    
    //异步加载图层中的Feature
    public static void queryFeaturesAsync(FeatureLayer featureLayer ,QueryParameters query ,  QueryFeaturesListener queryFeaturesListener ) {
        FeatureTable mTable = featureLayer.getFeatureTable();//得到查询属性表
        final ListenableFuture<FeatureQueryResult> featureQueryResult
                = mTable.queryFeaturesAsync(query);
        featureQueryResult.addDoneListener(new Runnable() {
            @Override
            public void run() {
                try {
                    FeatureQueryResult featureResul = featureQueryResult.get();
                    queryFeaturesListener.onGetFeatureQueryResult(featureResul);


                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }
        });
    }
    
    //异步加载本地shp文件
    public static void loadShpFile(AssetManager assetManager, String fileName  ,MapLayerListener mapLayerListener ){
        File shpFile = new File(fileName);
        Log.d("PP",fileName);
        if (shpFile.exists()){
            ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(fileName);
            shapefileFeatureTable.loadAsync();
            shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
                @Override
                public void run() {
                    mapLayerListener.onGetFeatureTable(shapefileFeatureTable);
                }
            });
        }else {
            Log.d("11111","no file!!!!!");
        }

    }
    
    //加载tif文件
    public static RasterLayer loadTifFile(String filePath ) {

        File shpFile = new File(filePath);
        if (shpFile.exists()){// 判断是否存储这个文件
            Raster raster = new Raster(filePath);
            RasterLayer rasterLayer = new RasterLayer(raster);

            List<Integer> colors = new ArrayList<>();
            for (int i = 0; i <= 6; i++) {
                if (i == 0) {
                    colors.add(i, Color.RED);
                } else if (i == 1 ) {
                    colors.add(i, Color.BLUE);

                }else if (i == 2) {
                    colors.add(i, Color.BLACK);

                }else if (i == 3) {
                    colors.add(i, Color.GREEN);

                }else if (i == 4) {
                    colors.add(i, Color.WHITE);

                } else {
                    colors.add(i, Color.YELLOW);
                }
            }

            ColormapRenderer colormapRenderer = new ColormapRenderer(colors);
            rasterLayer.setRasterRenderer(colormapRenderer);
            return rasterLayer;
        }else {
            Log.d("11111","no file!!!!!");
        }
        return null;
    }
    
}

调用

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private MapView mapView;
    FeatureLayer featureLayer;
    private double latitude = 0.0;
    private double longitude = 0.0;
    private LocationManager locationManager;
    private TextView info;
    Button nextButton;
    TextView text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = findViewById(R.id.mapaView);
        nextButton = findViewById(R.id.runButton);
        text1 = findViewById(R.id.ACT_MAIN_TEXT1);

        //获取GPS定位信息
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        toggleGPS();
        text1.setText("纬度:" + latitude + "\n" + "经度:" + longitude );

        ArcGISMap map = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 17);
        mapView.setMap(map);
        loadMapLayer();
     }
    public void loadMapLayer() {

        loadShpFile();
        loadGangu_bount();

    }
    public void loadShpFile(){
        AssetManager assetManager = getAssets();
        MapLayerManager.loadShpFile(assetManager, MapFilePathManager.townbount, new MapLayerManager.MapLayerListener() {
            @Override
            public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable) {
                featureLayer = new FeatureLayer(ashapefileFeatureTable);
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.DKGRAY, 1.0f);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
                SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
                featureLayer.setRenderer(renderer);


                mapView.getMap().getOperationalLayers().add(featureLayer);
                mapView.setViewpointAsync(new Viewpoint(featureLayer.getFullExtent()));
                Log.d("HF","townbount.shp已加载"+featureLayer.getFullExtent().toString());
            }
        });


    }
    public void loadGangu_bount() {
        AssetManager assetManager = getAssets();
        MapLayerManager.loadShpFile(assetManager, MapFilePathManager.Gangu_bount, new MapLayerManager.MapLayerListener() {
            @Override
            public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable) {
                FeatureLayer featureLayer = new FeatureLayer(ashapefileFeatureTable);
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 5);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
                SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
                featureLayer.setRenderer(renderer);

                mapView.getMap().getOperationalLayers().add(featureLayer);
            }
        });
    }
}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 17:55
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 7月10日

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并