gdal for android 开发包,字段属性为中文出现乱码

GDAL  Android库,创建shp,如果字段属性需要中文,网上的解决方法都是

gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

测试发现,没有作用。

public void SetField(int id, String value) {
    ogrJNI.Feature_SetField__SWIG_0(this.swigCPtr, this, id, value);
}

public void SetField(String name, String value) {
    ogrJNI.Feature_SetField__SWIG_1(this.swigCPtr, this, name, value);
}

为什么会乱码,编码不对。

如果直接使用SetField没几个能对得,所以转换思路,从编码方式入手。

解决思路——>SetFieldBinaryFromHexString

Feature.SetFieldBinaryFromHexString(1,Until.str2HexStr(“测试”,”GBK”));
new String(oFeature.GetFieldAsBinary(iField),”GBK”) ;

以下是所有用到的方法

1.这两句加上,不然不能用中文路径

gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

2.String——>Byte[]——>HexString

//charsetName="GBK"
public static String str2HexStr(String origin,String charsetName) throws UnsupportedEncodingException {
    byte[] bytes = origin.getBytes(charsetName);
    String hex = bytesToHexString(bytes);
    return hex;
}
private static String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("");
    if (src == null || src.length <= 0) {
        return null;
    }
    for (int i = 0; i < src.length; i++) {
        int v = src[i] & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
    }
    return stringBuilder.toString();
}

3.读取的时候

new String(oFeature.GetFieldAsBinary(iField),”GBK”) ;

这样就保证了,ArcGIS 和Android shp文件显示都不是乱码

最后附上完整代码和GDAL Android库,不要问我怎么编译,我也不会,还在学习中

private void writeShp() throws UnsupportedEncodingException {

    ogr.RegisterAll();
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

    String strDriverName = "ESRI Shapefile";
    org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
    if (oDriver == null) {
        System.out.println(strDriverName + " 驱动不可用!\n");
        return;
    }

    //shpPath为shp存储位置:内存/test.shp
    DataSource oDS = oDriver.CreateDataSource(shpPath, null);
    if (oDS == null) {
        System.out.println("创建矢量文件【" + shpPath + "】失败!\n");
        return;
    }

    Layer oLayer = oDS.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null);
    if (oLayer == null) {
        System.out.println("图层创建失败!\n");
        return;
    }

    // 下面创建属性表
    // 先创建一个叫FieldID的整型属性
    FieldDefn oFieldID = new FieldDefn("FieldID", ogr.OFTInteger);
    oLayer.CreateField(oFieldID);

    // 再创建一个叫FeatureName的字符型属性,字符长度为50
    FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString);
    oFieldName.SetWidth(100);
    oLayer.CreateField(oFieldName);

    FeatureDefn oDefn = oLayer.GetLayerDefn();

    // 创建三角形要素
    Feature oFeatureTriangle = new Feature(oDefn);
    oFeatureTriangle.SetField(0, 0);

    oFeatureTriangle.SetFieldBinaryFromHexString(1, Until.str2HexStr("中文测试","GBK"));
    Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");
    oFeatureTriangle.SetGeometry(geomTriangle);
    oLayer.CreateFeature(oFeatureTriangle);

    // 创建矩形要素
    Feature oFeatureRectangle = new Feature(oDefn);
    oFeatureRectangle.SetField(0, 1);
    oFeatureRectangle.SetFieldBinaryFromHexString(1,Until.str2HexStr("测试","GBK"));
    Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");
    oFeatureRectangle.SetGeometry(geomRectangle);
    oLayer.CreateFeature(oFeatureRectangle);

    // 创建五角形要素
    Feature oFeaturePentagon = new Feature(oDefn);
    oFeaturePentagon.SetField(0, 2);
    oFeaturePentagon.SetFieldBinaryFromHexString(1, Until.str2HexStr("中文","GBK"));
    Geometry geomPentagon = Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))");
    oFeaturePentagon.SetGeometry(geomPentagon);
    oLayer.CreateFeature(oFeaturePentagon);

    try {
        oLayer.SyncToDisk();
        oDS.SyncToDisk();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("\n数据集创建完成!\n");
}

本人使用的资源文件GDAL Android 2.2.3

https://download.csdn.net/download/qq_34045114/10788020

ArcGIS 中文乱码注册表10.2

这个就不上传了,直接贴出来,把以下内容新建txt,后缀改为reg使用

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\ESRI\Desktop10.2\Common\CodePage]
“dbfDefault”=”UTF-8”

 

以上就是解决GDAL Android 创建shp文件中文乱码,ArcGIS 无法查看的问题

 

转载自:https://blog.csdn.net/qq_34045114/article/details/84133815

You may also like...