使用GEOTools创建空shp文件

在地理信息中最常用的数据就是shp矢量数据,下面介绍使用GEOTools创建shp文件的大概步骤 (这里考虑到创建要素的代码繁复,不容易看懂,所以先抽丝剥茧地把新建一个空的SHP文件的原理介绍一下)

1、设置SHP的文件属性
2、创建一个数据存储空间生成工厂
3、创建一个数据存储空间
4、通过数据存储空间生成带文件属性架构的SHP文件

具体代码如下:
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataUtilities;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用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。可以根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值