Java生成GIS shapefile类型文件

之前做过关于shapefile文件的解析,这次来做将数据库返回给我们的数据解析成shapefile的一系列文件输出。这里我们需要用到外部的包geotools,其maven依赖为

<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-shapefile</artifactId>
  <version>17.2</version>
</dependency>

接着我们对进来的数据进行处理,以下是函数相关代码

private static final String GEOM_KEY = "the_geom";
private static final String PLOT_TYPE = "Coordinate"; //数据库里面用来表示坐标的属性

public static File searchResultToShp(List<Map<String, Object>> dataList) {
        Set<String> keys = new LinkedHashSet<>(dataList.get(0).keySet());
    keys.remove(PLOT_TYPE);
    final long snowflakeId = Math.random(); //这个ID可以自己写个函数定义
    String localDirPath = "修改为自己的路径"+ snowflakeId;
    File dirFile = new File(localDirPath);
    if (!dirFile.exists()) {
        dirFile.mkdirs();
    }
    File shpFile = 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java可以利用开源库GeoTools生成shp文件。GeoTools是一个开发平台,用于处理空间数据。它提供了丰富的API来读取、写入和操作各种GIS数据格式,包括矢量数据格式(如Shapefile、GML、KML等)和栅格数据格式(如GeoTIFF、NetCDF等)。 要使用GeoTools生成shp文件,您需要在Java项目中添加GeoTools的库文件。接着,您需要创建一个FeatureCollection对象,该对象将包含要在shp文件中写入的所有矢量要素(例如点、线、多边形等)。要创建FeatureCollection对象,您可以使用DefaultFeatureCollection类。 然后,您需要使用FeatureTypeBuilder类创建FeatureType对象。 FeatureType定义要素的结构,包括属性名称、数据类型和坐标参考系等。最后,您可以使用FeatureBuilder类将要素添加到FeatureCollection对象中,并调用FeatureWriter类将FeatureCollection写入shp文件。 例如,以下是使用GeoTools在Java中创建一个包含两个点的shp文件的示例: ``` CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName("Location"); builder.setCRS(crs); builder.add("the_geom", Point.class); builder.add("name", String.class); SimpleFeatureType featureType = builder.buildFeatureType(); DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("locations", featureType); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); Point point1 = geometryFactory.createPoint(new Coordinate(2.0, 1.0)); featureBuilder.set("the_geom", point1); featureBuilder.set("name", "Point 1"); SimpleFeature feature1 = featureBuilder.buildFeature(null); Point point2 = geometryFactory.createPoint(new Coordinate(4.0, 3.0)); featureBuilder.set("the_geom", point2); featureBuilder.set("name", "Point 2"); SimpleFeature feature2 = featureBuilder.buildFeature(null); featureCollection.add(feature1); featureCollection.add(feature2); File newFile = new File("locations.shp"); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map<String, Serializable> params = new HashMap<>(); params.put("url", newFile.toURI().toURL()); params.put("create spatial index", Boolean.TRUE); ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); dataStore.createSchema(featureType); String typeName = dataStore.getTypeNames()[0]; FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = dataStore.getFeatureWriter(typeName, Transaction.AUTO_COMMIT); try { while (featureCollection.features().hasNext()) { SimpleFeature feature = featureCollection.features().next(); SimpleFeature copy = featureWriter.next(); copy.setAttributes(feature.getAttributes()); featureWriter.write(); } } finally { featureWriter.close(); dataStore.dispose(); } ``` 在此示例中,我们创建了包含两个点的FeatureCollection对象。它们分别被命名为“Point 1”和“Point 2”。我们还使用CoordinateReferenceSystem对象定义了编码为“EPSG:4326”的坐标参考系。最后,我们使用ShapefileDataStore类将FeatureCollection对象写入名为“locations”的shp文件中。 总之,使用GeoTools可以方便地在Java生成shp文件。它为开发人员提供了强大且易于使用的API,可以生成各种GIS数据格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值