gdal连接pg(java案例)
1、下载gdal
https://www.gisinternals.com/archive.php 打开官方下载地址。
我下载的是3.8.5版本,MSVC2022,x64,release-1930-x64-gdal-3-8-5-mapserver-8-0-1,区别不大,自身系统比较低的下载低版本。
进入3.8.5的下载列表,我选择的第一个,编译后的二进制压缩包
解压压缩包,cmd进入命令行C:\Users\HIGHGO\Desktop\GIS\release-1930-x64-gdal-3-8-5-mapserver-8-0-1\bin\gdal\apps
查看支持格式.
ogr2ogr --formats
1.2、配置环境变量
PATH=G:\release-1930-x64-gdal-3-8-5-mapserver-8-0-1\bin
PATH=G:\release-1930-x64-gdal-3-8-5-mapserver-8-0-1\bin\gdal\apps
GDAL_DATA=G:\release-1930-x64-gdal-3-8-5-mapserver-8-0-1\bin\gdal-data
PROJ_LIB=G:\release-1930-x64-gdal-3-8-5-mapserver-8-0-1\proj
2、导入shp
#导入shp-linux
ogr2ogr -f "PostgreSQL" PG:"host='127.0.0.1' dbname='xxxxxx' user='xxxxx' password='xxxxx@123' port=5432" /opt/110m_physical/ne_110m_ocean.shp
#导入shp-win
ogr2ogr -f "PostgreSQL" PG:"host='xxxxxx' dbname='postgres' user='postgres' password='postgres' port=5432" C:\Users\HIGHGO\Desktop\GIS\data\NewYork\110m_physical\ne_110m_ocean.shp
3、导入xlsx
#导入xls-linux
ogr2ogr -f "PostgreSQL" PG:"host='127.0.0.1' dbname='highgo' user='sysdba' password='Test@123' port=5866" /opt/dltbxz_tqmj.xlsx
#导入xls-win
ogr2ogr -f "PostgreSQL" PG:"host='192.168.110.217' dbname='postgres' user='postgres' password='postgres' port=5432" C:\Users\HIGHGO\Desktop\GIS\data\dltbxz_tqmj.xlsx
4、导入gdb
#导入gdb-win
ogr2ogr -f "PostgreSQL" PG:"host='192.168.110.217' dbname='postgres' user='postgres' password='postgres' port=5432" C:\Users\HIGHGO\Desktop\GIS\data\test_highgo.gdb -nln chinasheng -overwrite chinasheng -progress -lco GEOMETRY_NAME=geom --config PG_USE_COPY YES
5、java调用(非最优方法)
注明:本方法只是快速使用,尽量不改变jdk目录结构,如果使用框架或更深入,请采用其他方法。
把gdal.jar和gdalalljni.dll拷贝到项目的lib下,然后配置maven
<dependency><groupId>org.gdal</groupId><artifactId>gdal</artifactId><version>3.8.5</version><scope>system</scope><systemPath>E:\space\workspace4.30\test/lib/gdal.jar</systemPath></dependency>
添加运行参数
通过gdal把shp导入pg数据库
package test;
import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;public class ShapefileToPostgres {public static void main(String[] args) {// 初始化 GDAL 和 OGRgdal.AllRegister();ogr.RegisterAll();// Shapefile 的路径String shpFilePath = "C:/Users/HIGHGO/Desktop/GIS/data/NewYork/110m_physical/ne_110m_coastline.shp";// PostgreSQL 的连接字符串String pgConnectionString = "PG:host=192.168.110.217 user=postgres dbname=postgres password=postgres";// 打开 Shapefile 数据源DataSource shpDataSource = ogr.Open(shpFilePath, 0); // 0 表示只读if (shpDataSource == null) {System.err.println("无法打开 Shapefile 文件: " + shpFilePath);return;}// 打开 PostgreSQL 数据源DataSource pgDataSource = ogr.Open(pgConnectionString, 1); // 1 表示读写if (pgDataSource == null) {System.err.println("无法连接 PostgreSQL 数据库: " + pgConnectionString);return;}// 获取 Shapefile 中的第一个图层Layer shpLayer = shpDataSource.GetLayer(0);// 创建 PostgreSQL 图层,名称为 Shapefile 图层的名称String layerName = shpLayer.GetName();Layer pgLayer = pgDataSource.CreateLayer(layerName, shpLayer.GetSpatialRef(), shpLayer.GetGeomType());if (pgLayer == null) {System.err.println("无法在 PostgreSQL 中创建图层: " + layerName);return;}// 将 Shapefile 中的每个字段添加到 PostgreSQL 图层中for (int i = 0; i < shpLayer.GetLayerDefn().GetFieldCount(); i++) {pgLayer.CreateField(shpLayer.GetLayerDefn().GetFieldDefn(i));}// 开始复制 Shapefile 中的每个要素到 PostgreSQL 图层shpLayer.ResetReading();org.gdal.ogr.Feature feature;while ((feature = shpLayer.GetNextFeature()) != null) {// 克隆要素以插入到 PostgreSQL 图层org.gdal.ogr.Feature pgFeature = feature.Clone();pgLayer.CreateFeature(pgFeature);// 清理资源pgFeature.delete();feature.delete();}// 关闭数据源shpDataSource.delete();pgDataSource.delete();System.out.println("Shapefile 成功导入 PostgreSQL 数据库!");}
}