GeoTools读取shp文件中文乱码解决方案汇总

Java在GeoTools组件读取Shp文件属性乱码问题,解决汇总(持续更新,暂时没有完美解决方案)

GeoTools组件在读取Shp文件的属性表信息时,当读取到中文字符时,在代码中的显示为乱码。

问题分析

通过代码分析得到Geotools中打开shapefile文件采用的编码格式为ISO-8859-1,而在读取中文字符中采用ISO-8859-1读取时,便会出现乱码的情况。

1. 手动指定GeoTools读取shp文件的

ShapefileDataStore shape = new ShapefileDataStore(url);
shape.setStringCharset(Charset.forName("GBK"));

2. shp文件中对于中文编码有多种格式,即存在GBK又存在UTF-8

由于ArcGIS版本的问题,在高版本中默认Shp文件的字符编码为UTF-8,而上述代码中指定编码格式为GBK,在代码中需要动态指定一下GBK和UTF-8,文件中的介绍如下: https://www.cnblogs.com/gisoracle/p/8098900.html

我们采用的方法时读取shp文件同名的cpg文件,中存储了当前dbf文件的编码格式,可以通过打开cpg文件的内容,使用文件中的内容作为GeoTools的字符编码格式。

private static String getShapeFileCharsetName(String path) throws Exception {
    File pFile = new File(path);
    if (pFile.exists() && !pFile.isFile()) {
        return "GBK";
    }
    File file = new File(path);
    String encode = "GBK";
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        String tempString = null;
        // 一次读入一行,直到读入null为文件结束
        while ((tempString = reader.readLine()) != null) {
            // 显示行号
            if ("UTF-8".equals(tempString.toUpperCase())) {
                encode = "UTF-8";
                break;
            }
            break;
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e1) {
            }
        }
    }
    return encode;
}

3. 未完待续…

后续,当我们将dbf文件单独拿到一个文件夹中,使用Excel、Access打开时,他也是可以做到中文字符读取正确(没有乱码),而这是本文中暂时没有get到的点,没有明白基于什么认出文件中的编码格式。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在使用Geotoolsshp文件时,中文属性乱码问题可能是因为编码不一致所致。可以尝试在写入shp文件之前,设置相应的编码格式,例如: ```java // 创建shapefile数据存储对象 Map<String, Serializable> params = new HashMap<>(); params.put("url", new File("path/to/shapefile.shp").toURI().toURL()); params.put("create spatial index", Boolean.TRUE); DataStore newDataStore = DataStoreFinder.getDataStore(params); // 定义shapefile属性字段 SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:Point:srid=4326," + // 必须包含一个几何属性 "name:String"); // 设置中文属性字段编码 ((ShapefileDataStore)newDataStore).setCharset(Charset.forName("GBK")); // 创建要素并写入shapefile文件 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Point point = geometryFactory.createPoint(new Coordinate(116.404, 39.915)); featureBuilder.add(point); featureBuilder.add("北京市"); SimpleFeature feature = featureBuilder.buildFeature(null); Transaction transaction = newDataStore.getTransaction(); SimpleFeatureSource featureSource = newDataStore.getFeatureSource(TYPE.getName().getLocalPart()); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); try { featureStore.addFeatures(DataUtilities.collection(feature)); transaction.commit(); } catch (Exception e) { transaction.rollback(); } finally { transaction.close(); } } newDataStore.dispose(); ``` 在上述代码中,通过`((ShapefileDataStore)newDataStore).setCharset(Charset.forName("GBK"));`来设置中文属性字段的编码格式为GBK。可以根据实际情况进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

自己的九又四分之三站台

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

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

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

打赏作者

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

抵扣说明:

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

余额充值