java实现将shp文件倒入到postgresql

本文介绍了如何使用GeoTools将.shp文件导入到PostgreSQL数据库,包括依赖包下载、Maven仓库配置及具体代码实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用GeoTools将shp文件倒入到postgresql,首先需要将geotools下的gt-shapefile包下载下来,但是这个包下载的时候一般不能直接下载下来。
pom文件中加入的依赖内容是

        <dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-shapefile</artifactId>
			<version>24.0</version>
		</dependency>
		<dependency>
			<groupId>org.geotools.jdbc</groupId>
			<artifactId>gt-jdbc-postgis</artifactId>
			<version>24.0</version>
		</dependency>

gt-jdbc-postgis这个可以直接下载

解决方式:
1:从maven仓库直接下来然后找到本地仓库的org.geotools.gt-shapefile.版本号,然后将jar包放进去,更新项目的pom文件即可
maven仓库的地址:https://mvnrepository.com/artifact/org.geotools

2:我这里已经下载了24.0的版本
百度网盘下载

以上内容解决之后还需要将repository的内容的加上

        <repository>
			<id>osgeo</id>
			<name>OSGeo Release Repository</name>
			<url>https://repo.osgeo.org/repository/release/</url>
			<snapshots><enabled>false</enabled></snapshots>
			<releases><enabled>true</enabled></releases>
		</repository>
		<repository>
			<id>osgeo-snapshot</id>
			<name>OSGeo Snapshot Repository</name>
			<url>https://repo.osgeo.org/repository/snapshot/</url>
			<snapshots><enabled>true</enabled></snapshots>
			<releases><enabled>false</enabled></releases>
		</repository>

加完之后,需要等待pom文件更新,完成之后准备工作已经完成

以下是实现倒入的代码

public static final String pg_dbtype = "postgis";

	public static final String pg_host = "127.0.0.1"; 

	public static final Integer pg_port = 5432;

	public static final String pg_schema = "public";

	public static final String pg_database = "test";

	public static final String pg_user = "postgres";

	public static final String pg_password = "123456";

	public static void main(String[] args) {
		File file = new File("自己的shp文件的地址");
		importShpFileMethod(file);
	}

	/**
	 * @param file shp文件的位置
	 * @return
	 * @author 
	 * @description 倒入shp文件
	 */
	public static void importShpFileMethod(File file) {
		JDBCDataStore jdbcDataStore = connectPostgis();
		SimpleFeatureSource simpleFeatureSource = readSHP(file);
		JDBCDataStore ds = createTable(jdbcDataStore, simpleFeatureSource);
		writeShp2Postgis(ds, simpleFeatureSource);
	}

	/**
	 * 链接到postgis
	 *
	 * @return
	 */
	private static JDBCDataStore connectPostgis() {
		JDBCDataStore jdbcDataStore = null;
		DataStore dataStore;
		Map<String, Object> params = new HashMap<>(16);
		params.put("dbtype", pg_dbtype);
		params.put("host", pg_host);
		params.put("port", pg_port);
		params.put("schema", pg_schema);
		params.put("database", pg_database);
		params.put("user", pg_user);
		params.put("passwd", pg_password);

		try {
			dataStore = DataStoreFinder.getDataStore(params);
			if (dataStore != null) {
				jdbcDataStore = (JDBCDataStore) dataStore;
				System.out.println("链接数据库成功");
			} else {
				System.out.println("链接数据库失败");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jdbcDataStore;
	}

	/**
	 * 读取shp文件
	 *
	 * @param file
	 * @return
	 */
	private static SimpleFeatureSource readSHP(File file) {
		//数据源
		SimpleFeatureSource simpleFeatureSource = null;
		try {
			//定义数据存储
			ShapefileDataStore shapefileDataStore = new ShapefileDataStore(file.toURI().toURL());
			//设置编码
			Charset charset = shapefileDataStore.getCharset();
			shapefileDataStore.setCharset(charset);
			String tableName = shapefileDataStore.getTypeNames()[0];
			//获取数据源
			simpleFeatureSource = shapefileDataStore.getFeatureSource(tableName);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return simpleFeatureSource;
	}


	/**
	 * 创建表
	 */
	private static JDBCDataStore createTable(JDBCDataStore jdbcDataStore, SimpleFeatureSource simpleFeatureSource) {
		SimpleFeatureType simpleFeatureType = simpleFeatureSource.getSchema();
		try {
			jdbcDataStore.createSchema(simpleFeatureType);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jdbcDataStore;
	}

	/**
	 * 把shp写入postgis
	 *
	 * @param jdbcDataStore
	 * @param simpleFeatureSource
	 */
	private static void writeShp2Postgis(JDBCDataStore jdbcDataStore, SimpleFeatureSource simpleFeatureSource) {
		//获取模式
		SimpleFeatureType simpleFeatureType = simpleFeatureSource.getSchema();
		FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
		//开始写入数据
		try {
			//获取要素写入
			writer = jdbcDataStore.getFeatureWriter(simpleFeatureType.getTypeName().toLowerCase(), Transaction.AUTO_COMMIT);
			//获取数据源
			SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures();
			//获取要素遍历器
			SimpleFeatureIterator iterator = simpleFeatureCollection.features();
			while (iterator.hasNext()) {
				//写入下一个
				writer.hasNext();
				//要写入的空要素
				SimpleFeature writeNext = writer.next();
				//遍历获得的要素
				SimpleFeature simpleFeature = iterator.next();
				//赋值属性
				for (int i = 0; i < simpleFeature.getAttributeCount(); i++) {
					writeNext.setAttribute(i, simpleFeature.getAttribute(i));
				}
				//写入
				writer.write();
			}
			System.out.println("导入成功");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (jdbcDataStore != null) {
				jdbcDataStore.dispose();
			}
		}
	}

按照自己的需求配置数据库和文件的位置即可完成

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值