Android 读shp属性字段

我是使用gdal来读取shp的,使用gdal需要到相关的文件到项目中,主要是:

下面是正式代码:

//指定文件的名字和路径
        String strVectorFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/cs/ky.shp";
        //注册驱动
        ogr.RegisterAll();
        // 为了支持中文路径,请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        // 为了使属性表字段支持中文,请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING", "CP936");

        //读取数据,这里以ESRI的shp文件为例
        String strDriverName = "ESRI Shapefile";

        //创建一个文件,根据strDriverName扩展名自动判断驱动类型
        Driver oDriver = ogr.GetDriverByName(strDriverName);

        if (oDriver == null) {
            System.out.println(strDriverName + " 驱动不可用!\n");
            return;
        }

        DataSource dataSource = oDriver.Open(strVectorFile, 1);

        File file = new File(strVectorFile.trim());
        String name = file.getName();
        String[] strings = name.split("\\.");

        Layer layer = dataSource.GetLayer(strings[0]);

        String layerName = layer.GetName();

        System.out.println("图层名称:" + layerName);

        SpatialReference spatialReference = layer.GetSpatialRef();

        System.out.println("空间参考坐标系:" + spatialReference.GetAttrValue("AUTHORITY", 0) + spatialReference.GetAttrValue("AUTHORITY", 1));

        double[] layerExtent = layer.GetExtent();

        System.out.println("图层范围:minx:" + layerExtent[0] + ",maxx:" + layerExtent[1] + ",miny:" + layerExtent[2] + ",maxy:" + layerExtent[3]);

        FeatureDefn featureDefn = layer.GetLayerDefn();

        int fieldCount = featureDefn.GetFieldCount();

        for (int i = 0; i < fieldCount; i++) {

            FieldDefn fieldDefn = featureDefn.GetFieldDefn(i);
            //得到属性字段类型
            int fieldType = fieldDefn.GetFieldType();
            String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType);
            //得到属性字段名称
            String fieldName = fieldDefn.GetName();

            fieldMap.put(fieldName, fieldTypeName);

        }

        long featureCount = layer.GetFeatureCount();
        System.out.println("图层要素个数:" + featureCount);

    }

属性字段为:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老杜_d

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值