@RequestMapping("/Api/GDB")
@Api(tags = "gdb数据导入")
public class GdbToPgController {

    @GetMapping("execute")
    public void execute() {
        gdal.AllRegister();
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        String sourceFilePath="\\\\1.1.8.47\\data\\b.gdb";
        String pgConnection= "PG:host=localhost dbname=gis user=postgres password=postgres";
        GdbToPgUtil.convert(sourceFilePath,pgConnection,"TEST","4548");
    }


}
public class GdbToPgUtil {
    /**
     * gdb数据转为pg数据工具方法
     * @param sourceFilePath
     * @param pgConnection
     * @param layerName
     * @param epsgCode
     * @return
     */
    public static  String convert(String sourceFilePath,String pgConnection,String layerName,String epsgCode) {
        String result="";
        try {
            // 打开GDB数据源
            Vector vector =new Vector();
            vector.add("OpenFileGDB");
            Dataset srcDS = gdal.OpenEx(sourceFilePath, gdalconst.OF_VECTOR, vector);
            Dataset dstDS = gdal.OpenEx(pgConnection, gdalconst.OF_UPDATE | gdalconst.OF_VECTOR);
            gdal.SetConfigOption("PG_USE_COPY","YES");
            // 配置转换选项
            Vector optionsVector =new Vector();
            optionsVector.add("-f");
            optionsVector.add("PostgreSQL");
            optionsVector.add("-overwrite");
            optionsVector.add("-sql");
            optionsVector.add("select * from "+layerName);
            optionsVector.add("-a_srs");
            optionsVector.add("EPSG:"+epsgCode);
            optionsVector.add("-lco");
            optionsVector.add("COLUMN_TYPES=ALL_LOWER");
            VectorTranslateOptions options = new VectorTranslateOptions(optionsVector);

            // 执行转换
            int ret = gdal.VectorTranslate(dstDS, srcDS, options);
            if (ret == 1) {
                System.out.println(layerName+"转换成功!");
                result="转换成功";
            } else {
                System.out.println(":错误 " + gdal.GetLastErrorMsg());
                result="错误"+ gdal.GetLastErrorMsg();
            }

            srcDS.delete();
            dstDS.delete();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            gdal.GDALDestroyDriverManager();
        }
        return result;

    }


}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐