geotools中文乱码问题

前言:是的,这只是个标题。不过解决办法是有的。先说之前,在20.0版本时候,采用这样的办法来解决乱码问题:
在这里插入图片描述
或者 修改源码。
然而现在不用这么改了。

我试了下25.0版本,geotools修复了中文乱码问题。

注意:如果这并不是你的乱码的问题根源,你就要了解一件事了,比如:
当从数据库中导出shp数据时,你必须规定导出数据时的格式,同时,导出shp的文件也必须要设置编码格式;
当导入shp至数据库后,导出的编码应该和导入的一样。(如果不一样还不报错就要恭喜你了,要么是你幸运,要么是被修复了)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Geotools写shp文件时,中文属性乱码问题可能是因为编码不一致所致。可以尝试在写入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。可以根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值