| | |
| | | |
| | | import java.io.File; |
| | | |
| | | /** |
| | | * GDAL帮助类 |
| | | * |
| | | * @author WWW |
| | | * @date 2024-09-12 |
| | | */ |
| | | @Slf4j |
| | | @SuppressWarnings("ALL") |
| | | public class GdalHelper { |
| | |
| | | } |
| | | } |
| | | |
| | | // 支持中文路径 |
| | | gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); |
| | | // 属性表支持中文:CP936 |
| | | gdal.SetConfigOption("SHAPE_ENCODING", ""); |
| | | gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); |
| | | gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); |
| | | |
| | | // 注册所有的驱动 |
| | | gdal.AllRegister(); |
| | | ogr.RegisterAll(); |
| | | initSr(); |
| | | } |
| | | |
| | | /** |
| | | * 初始化坐标系 |
| | | * <p> |
| | | * https://blog.csdn.net/CallmeAdo/article/details/127558139 |
| | | */ |
| | | public static void initSr() { |
| | | try { |
| | | SR4326 = new SpatialReference(); |
| | | SR4326.ImportFromEPSG(I4326); |
| | | // 对于lat/long顺序的地理CRS,数据仍然是long/lat顺序的 |
| | | SR4326.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |
| | | |
| | | SR4490 = new SpatialReference(); |
| | |
| | | return sr; |
| | | } |
| | | |
| | | /** |
| | | * 创建金字塔 |
| | | */ |
| | | public static void createPyramid(String file) { |
| | | Dataset ds = null; |
| | | try { |
| | |
| | | return; |
| | | } |
| | | |
| | | // 创建金字塔 |
| | | Band band = ds.GetRasterBand(1); |
| | | if (0 == band.GetOverviewCount()) { |
| | | ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 销毁资源 |
| | | */ |
| | | public static void delete(Layer layer, DataSource dataSource, Driver driver) { |
| | | try { |
| | | if (null != layer) { |
| | |
| | | |
| | | public static Geometry getMaxPoint(Dataset ds) |
| | | { |
| | | /* |
| | | * transform[0] 左上角x坐标 |
| | | * transform[1] 东西方向分辨率 |
| | | * transform[2] 旋转角度, 0表示图像 "北方朝上" |
| | | * |
| | | * transform[3] 左上角y坐标 |
| | | * transform[4] 旋转角度, 0表示图像 "北方朝上" |
| | | * transform[5] 南北方向分辨率 |
| | | */ |
| | | double[] transform = new double[6]; |
| | | ds.GetGeoTransform(transform); |
| | | |
| | |
| | | return point; |
| | | } |
| | | |
| | | /** |
| | | * 转换为WGS84坐标 |
| | | */ |
| | | public static Geometry toWgs84(SpatialReference sr, double x, double y) { |
| | | Geometry point = new Geometry(ogr.wkbPoint); |
| | | point.AssignSpatialReference(sr); |
| | |
| | | return point; |
| | | } |
| | | |
| | | /** |
| | | * WGS84转换为目标坐标 |
| | | */ |
| | | public static double[] fromWgs84(SpatialReference sr, double x, double y) { |
| | | // https://blog.csdn.net/weixin_34910922/article/details/129208661 |
| | | CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr); |
| | | if (sr.IsProjected() != 1) { |
| | | sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |
| | |
| | | return ct.TransformPoint(x, y); |
| | | } |
| | | |
| | | /** |
| | | * WGS84转换为目标坐标 |
| | | */ |
| | | public static int fromWgs84(SpatialReference sr, Geometry g) { |
| | | // https://blog.csdn.net/weixin_34910922/article/details/129208661 |
| | | CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr); |
| | | if (sr.IsProjected() != 1) { |
| | | sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |