好久没用geotools去写东西了,因为近几年一直在接触所谓数字孪生和可视化相关项目,个人的重心也往前端可视化去倾斜,在后端的开发上到变得停滞下来。
这次用的是geotools 28.4版本,生成等值线的方法在
<dependency><groupId>org.geotools</groupId><artifactId>gt-process-feature</artifactId><version>28.4</version></dependency>
这是使用矢量点生成等值线,geotools中也提供了栅格影像生成等值线的方法,都是一个名字
ContourProcess,栅格的方法在:
<dependency><groupId>org.geotools</groupId><artifactId>gt-process-raster</artifactId><version>${geotools.version}</version></dependency>
中,本文仅结束矢量点生成等值线方法:
全部代码如下:
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;import org.geotools.data.DefaultTransaction;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.process.vector.ContourProcess;public class MainP {public static void main(String[] args) {//点shpSimpleFeatureCollection sf = getGeometries("D:\\tmp\\inPoints.shp");ContourProcess cp = new ContourProcess();double[] levels = new double[]{};//如果进行线平滑,可能出现线相交情况!,“zfirst”为点shp图层中描述高度的属性字段名称,这里设置20米间隔进行等高线划分SimpleFeatureCollection outSF = cp.execute(sf, "zfirst", levels, 20.0, false, false, null);buildShpByFeatureCollection(outSF,"D:\\tmp\\contour.shp");}public static SimpleFeatureCollection getGeometries(String shpFilePath) {try {FileDataStore store = FileDataStoreFinder.getDataStore(new File(shpFilePath));SimpleFeatureSource featureSource = store.getFeatureSource();SimpleFeatureCollection simpleFeatureCollection = featureSource.getFeatures();return simpleFeatureCollection;}catch(Exception ex) {System.out.println(ex.getMessage());}return null;}public static void buildShpByFeatureCollection(SimpleFeatureCollection collection, String outShpPath) {File outShpFile = new File(outShpPath);ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();try {Map<String, Serializable> params = new HashMap<>();params.put("url", outShpFile.toURI().toURL());params.put("create spatial index", Boolean.TRUE);ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);newDataStore.createSchema(collection.getSchema());Transaction transaction = new DefaultTransaction("create");String typeName = newDataStore.getTypeNames()[0];SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);if (featureSource instanceof SimpleFeatureStore) {SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;featureStore.setTransaction(transaction);try {featureStore.addFeatures(collection);transaction.commit();} catch (Exception problem) {problem.printStackTrace();transaction.rollback();} finally {transaction.close();}System.exit(0);} else {System.out.println(typeName + " does not support read/write access");System.exit(1);}} catch (Exception e) {throw new RuntimeException(e.getMessage(), e);}}}