79 lines
3.9 KiB
Java
79 lines
3.9 KiB
Java
|
|
package org.example.test;
|
||
|
|
|
||
|
|
import org.gdal.gdal.gdal;
|
||
|
|
import org.geotools.api.data.FileDataStore;
|
||
|
|
import org.geotools.api.data.FileDataStoreFinder;
|
||
|
|
import org.geotools.api.data.SimpleFeatureSource;
|
||
|
|
import org.geotools.coverage.grid.GridCoverage2D;
|
||
|
|
import org.geotools.coverage.grid.GridCoverageFactory;
|
||
|
|
import org.geotools.coverage.processing.Operations;
|
||
|
|
import org.geotools.data.shapefile.ShapefileDataStore;
|
||
|
|
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
|
||
|
|
//import org.geotools.data.shapefile.ShapefileDataStoreFinder;
|
||
|
|
import org.geotools.gce.geotiff.GeoTiffFormat;
|
||
|
|
import org.geotools.gce.geotiff.GeoTiffWriteParams;
|
||
|
|
import org.geotools.gce.geotiff.GeoTiffWriter;
|
||
|
|
import org.geotools.geometry.jts.ReferencedEnvelope;
|
||
|
|
import org.opengis.coverage.grid.GridCoverage;
|
||
|
|
import org.opengis.feature.simple.SimpleFeature;
|
||
|
|
import org.opengis.parameter.GeneralParameterValue;
|
||
|
|
import org.opengis.parameter.ParameterValueGroup;
|
||
|
|
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.Serializable;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
public class ShpToDemConverter {
|
||
|
|
|
||
|
|
public static void main(String[] args) throws Exception {
|
||
|
|
// 读取 Shapefile
|
||
|
|
File shapefile = new File("E:/code/CutFillDemo/target/classes/tmp.shp");
|
||
|
|
// ShapefileDataStore store = (ShapefileDataStore)FileDataStoreFinder.getDataStore(shapefile);
|
||
|
|
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
||
|
|
Map<String, Serializable> params = new HashMap<>();
|
||
|
|
params.put("url", shapefile.toURI().toURL());
|
||
|
|
params.put("create spatial index", Boolean.TRUE);
|
||
|
|
ShapefileDataStore store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
||
|
|
|
||
|
|
// SimpleFeatureSource featureSource = store.getFeatureSource();
|
||
|
|
// ShapefileDataStore store = (ShapefileDataStore) ShapefileDataStoreFinder.getDataStore(shapefile);
|
||
|
|
String elevationFieldName = "ELEV"; // 假设字段名为 "elevation"
|
||
|
|
|
||
|
|
// 提取边界和分辨率信息
|
||
|
|
ReferencedEnvelope envelope = store.getFeatureSource().getBounds();
|
||
|
|
double resolution = 0.00025; // 例如,每个像素代表多少地图单位
|
||
|
|
|
||
|
|
// 读取高程信息并创建 GridCoverage
|
||
|
|
GridCoverage2D gridCoverage = createGridCoverageFromShapefile(store, envelope, elevationFieldName, resolution);
|
||
|
|
|
||
|
|
// 写入 GeoTIFF 文件作为 DEM
|
||
|
|
File outputFile = new File("E:/code/CutFillDemo/target/classes/output.tif");
|
||
|
|
writeGridCoverageToGeoTiff(gridCoverage, outputFile);
|
||
|
|
|
||
|
|
// 关闭 Shapefile 数据存储
|
||
|
|
store.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static GridCoverage2D createGridCoverageFromShapefile(ShapefileDataStore store, ReferencedEnvelope envelope, String elevationFieldName, double resolution) throws Exception {
|
||
|
|
// 这里你需要实现具体的逻辑来从 Shapefile 中提取高程信息并创建 GridCoverage2D 对象
|
||
|
|
// 这可能涉及到迭代 Shapefile 中的特征,提取高程值,并使用插值方法填充栅格
|
||
|
|
// ...
|
||
|
|
// gdal.Rasterize()
|
||
|
|
GridCoverageFactory factory = new GridCoverageFactory();
|
||
|
|
// factory.
|
||
|
|
// GridCoverage2D gridCoverage2D = new GridCoverage2D();
|
||
|
|
return null; // 返回一个示例值,你需要替换为实际的 GridCoverage2D 对象
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void writeGridCoverageToGeoTiff(GridCoverage2D gridCoverage, File outputFile) throws Exception {
|
||
|
|
// 设置 GeoTIFF 写入参数
|
||
|
|
// Map<String, Serializable> params = new HashMap<>();
|
||
|
|
// params.put(GeoTiffFormat.GEOTOOLS_WRITE_PARAMS.getName().toString(), new GeoTiffWriteParams());
|
||
|
|
//
|
||
|
|
// // 写入 GeoTIFF 文件
|
||
|
|
// GeoTiffWriter writer = new GeoTiffWriter(outputFile);
|
||
|
|
// writer.write(gridCoverage, (ParameterValueGroup) params.get(GeoTiffFormat.GEOTOOLS_WRITE_PARAMS.getName()));
|
||
|
|
// writer.dispose();
|
||
|
|
}
|
||
|
|
}
|