geotools将shp数据存入postgres

article/2025/10/7 21:41:44

一、引入geotools的依赖

引入geotools的方法

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

二、代码编写

1、代码如下;

package com.example.forestry.util.geotools;import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;import java.io.File;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;public class Shp2Pgsql {/*** 获取数据库连接对象** @return* @throws Exception*/public JDBCDataStore getDataStore(){Map<String, Object> params = new HashMap<>();// 必须是字符串 postgisparams.put("dbtype", "postgis");// ipparams.put("host", "192.168.xx.xx");// 端口params.put("port", 5432);// 数据库模式params.put("schema", "public");// 数据库名称params.put("database", "cim");params.put("user", "postgres");params.put("passwd", "postgres");JDBCDataStore dataStore = null;try {DataStore ds = DataStoreFinder.getDataStore(params);if(ds==null){System.out.println("连接postgres失败");}else {dataStore = (JDBCDataStore) ds;System.out.println("连接postgres成功");}}catch (Exception e) {e.printStackTrace();System.out.println("连接postres出错");}return dataStore;}/*** 读取shapefile文件** @param file* @return*/public SimpleFeatureSource readShp(File file){SimpleFeatureSource featureSource = null;ShapefileDataStore shpDataStore = null;try {shpDataStore = new ShapefileDataStore(file.toURL());//设置编码Charset charset = Charset.forName("UTF8");shpDataStore.setCharset(charset);String tableName = shpDataStore.getTypeNames()[0];featureSource = shpDataStore.getFeatureSource(tableName);} catch (Exception e) {e.printStackTrace();} finally {shpDataStore.dispose();}return featureSource;}/*** 创建表** @param ds* @param source* @return*/public JDBCDataStore createTable(JDBCDataStore ds , SimpleFeatureSource source){SimpleFeatureType schema = source.getSchema();try {ds.createSchema(schema);}catch (Exception e){e.printStackTrace();System.out.println("创建postgres数据表失败");}return ds;}/*** 将shapefile的数据存入数据表中** @param ds* @param source*/public void shp2Table(JDBCDataStore ds , SimpleFeatureSource source){SimpleFeatureIterator features  = null;//表名为shp文件的名字String tableName = source.getSchema().getTypeName();try {FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(tableName, Transaction.AUTO_COMMIT);SimpleFeatureCollection featureCollection = source.getFeatures();//创建图层数据迭代器features = featureCollection.features();//进行逐行写入while (features.hasNext()) {try {writer.hasNext();SimpleFeature next = writer.next();SimpleFeature feature = features.next();for (int i = 0; i < feature.getAttributeCount(); i++) {next.setAttribute(i, feature.getAttribute(i));}writer.write();} catch (Exception e) {e.printStackTrace();System.out.println("添加数据的方法错误:" + e.toString());continue;}}writer.close();System.out.println("导入成功");}catch (Exception e){e.printStackTrace();System.out.println("创建写入条件失败:"+e.toString());}finally {ds.dispose();features.close();}}//进行测试public static void main(String[] args) {Shp2Pgsql shp2Pgsql = new Shp2Pgsql();File file = new File("D:\\data\\shapefile\\tree\\tree.shp");Long startTime = System.currentTimeMillis();//获取postgres连接对象JDBCDataStore dataStore = shp2Pgsql.getDataStore();//读取shapefile文件SimpleFeatureSource simpleFeatureSource = shp2Pgsql.readShp(file);//创建数据表JDBCDataStore ds = shp2Pgsql.createTable(dataStore, simpleFeatureSource);//进行数据写入shp2Pgsql.shp2Table(ds , simpleFeatureSource);Long endTime = System.currentTimeMillis();Long time = (endTime - startTime)/1000;System.out.println("时间为: "+time+" 秒");}}

2、插入速度;我电脑i5-10th的cpu,8G内存,跑49万条数据用了255秒;
在这里插入图片描述


http://chatgpt.dhexx.cn/article/W1d9FWKS.shtml

相关文章

全国高铁线路及站点shp数据(2020年)

名称&#xff1a;全国高铁线路及站点shp数据&#xff08;2020年&#xff09; 区域范围&#xff1a;全国 时间范围&#xff1a;2020 语言&#xff1a;中文 格式&#xff1a;.shp 该数据为2020年的全国范围的高铁线路和站点数据&#xff0c;格式为shp格式&#xff0c;坐标为wgs19…

深圳市“详规一张图”shp数据爬取后数据表无法打开的问题解决办法

最近在研究爬取深圳市规自局网站上的“详规一张图”数据&#xff0c;无奈爬虫技术太低&#xff0c;无意中看到了大佬“Atom数据”的“深圳详规“一张图”数据获取【代码更新】”一文中给出的代码爬取方案&#xff0c;再次感谢&#xff01; 然而在经历了千辛万苦之后终于将详规一…

FME将用SHP数据对栅格影像数据进行裁剪

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、需求二、实操 1.数据准备2.FME的流程设置3.裁剪的结果总结 前言 利用FME对数据进行操作与处理的过程。 一、需求 裁剪出shp范围内或者范围外的栅格影像数…

三步教你免费下载省,市,区县行政区Shp数据

摘要&#xff1a;一般非专业的GIS应用通常会用到省市等行政区区划边界空间数据做分析&#xff0c;本文简单介绍了如何在互联网上下载省&#xff0c;市&#xff0c;区县的shp格式空间边界数据&#xff0c;并介绍了一个好用的在线数据转换工具&#xff0c;并且开源。 一、首先&a…

数据处理(1):十万加SHP数据重载导入标准字段图层

使用工具&#xff1a;Arcgis 时常我们会从客户、网络得到数据&#xff0c;但是数据里面字段名于公司规定得字段名有出入&#xff0c;此时就需要进行数据调整&#xff0c;首先我们打开软件“ArcCatalog” 在任意文件夹下右击&#xff0c;选择“新建-文件地理数据库”&#xff0c…

shp数据中文乱码的一种恢复方法

一、准备 1、QGIS软件 2、乱码识别网站 二、转换步骤 &#xff08;一&#xff09;示例展示 如图&#xff0c;有一个不知道怎么产生的shp文件&#xff0c;在arcmap中&#xff0c;中文字段显示乱码&#xff0c;添加cpg文件后&#xff0c;仍然无法解决。 &#xff08;二&…

【GlobalMapper精品教程】053:打开dbf文件并生成有坐标系的shp数据

本文讲解在globalmapper汇总打开dbf文件并生成有坐标系的shp数据。 文章目录 一、dbf文件解读二、打开dbf文件二、另存为shp文件一、dbf文件解读 我们可以通过Excel或FME等多种软件查看dbf的结构,字段有:Name,kind,Lat,Lon,其中Lon为经度,Lat为纬度,我们后续一定是通过…

html 显示shp,cesium加载本地shp数据

用户选择本地磁盘shp数据后,直接在三维球上展示。本文默认已经有了vue和cesium整合后的工程。 官网地址:git://github.com/wavded/js-shapefile-to-geojson.git 1、vue-cli3.x+cesium的项目目录 image.png 2、CesiumViewer.vue代码: 加载shp数据 import Cesium from cesium/…

将shp数据导入SQL Server

记录Shape2SQL的使用过程及注意事项 20220322更新&#xff1a;出现30万行线要素导入后不显示的问题&#xff0c;分两次导入就正常了&#xff0c;没有找到原因&#xff0c;隔壁30万行点要素没出问题 一、Shape2SQL工具可以将shape数据导入SQL Server 2008R2 下载地址&#xf…

重庆市最新轨道交通SHP数据 - 202201

重庆市最新轨道交通SHP数据 - 202201 由于最近需要用到重庆市的轨道交通数据&#xff0c;但下载到的数据略有缺&#xff0c;因此对着重庆轨道交通集团提供的图及QGIS中的OSM&#xff0c;对下载的数据进行了修正。 下图是在QGIS中结果。共有站点193个&#xff0c;路线11条&…

shp数据制作3DTiles白膜

3D Tiles格式介绍 3D Tiles用于大场景的三维模型。 3D Tiles是一个开放的规范&#xff0c;用于传输海量的异构三维地理空间数据集。使用概念上类似于terrain和imagery的瓦片流技术&#xff0c;3D Tiles 使得建筑物数据集、BIM模型、点云和摄影测量模型等大模型比较流畅的在Web…

osgEarth 加载矢量shp数据

加载osgearth自带的world.shp矢量数据&#xff1a; #include "stdafx.h" #include <Windows.h> #include <iostream> #include <string> using namespace std;#include <osgViewer/Viewer> #include <osg/Node> #include <osg/Ge…

导入shp数据到postgis库

1、安装完postgis之后&#xff0c;打开PostGIS PostGIS Bundle 3 for PostgreSQL x64 12 Shapefile and DBF Loader Exporter 2、填写数据库信息 3、填完后就显示成功了 4、点击Add File 添加shp文件 5、点击import导入 6、去数据库查看有表存在了 其他拓展&#xff1a;postgis…

推荐一个免费下载省-市-区县行政区Shp数据的方法

推荐一个免费下载省-市-区县行政区Shp数据的方法&#xff0c;简单实用。 文章目录 一、选择下载数据二、保存json格式三、json转shp格式 一、选择下载数据 本文讲解利用阿里云提供的地图选择器网站选择想要下载的行政区的方法&#xff0c;点击打开阿里云地图选择器网站&#x…

GeoServer发布shp数据

前段项目中应甲方要求&#xff0c;需要将地图服务过程简化到越简单越好&#xff0c;由于该项目中地图只作为底图&#xff0c;只是看看而已&#xff0c;并未涉及到空间数据分析之类的。所以&#xff0c;项目中裁掉了空间数据库这一部分。在没空间数据库的情况下&#xff0c;空间…

世界城市数据集(shp格式,2种数据源)

今天&#xff0c;给大家分享世界城市数据集shapefile文件&#xff0c;包括两种数据源&#xff0c;一个是Natural Earth的ne_10m_populated_places数据集&#xff0c;其属性字段包含了中文标签&#xff1b;另一个是来于ArcGIS Hub的World Cities数据集&#xff0c;其属性字段为英…

shp数据入库、出库、可视化

shp数据入库、出库、可视化 今天给npy写了次作业&#xff0c;由于各种原因&#xff0c;不能用我用了很久的SSM或者SpringBoot框架&#xff0c;只用了最原始的servletjsp&#xff0c;然后我发现…自己已经不太会用了&#xff0c;写这个博客记录一下整套流程~ 首先&#xff0c;…

Shp数据格式说明

复制于维基百科&#xff1a;https://zh.wikipedia.org/wiki/Shapefile&#xff0c;因为的确经常会把数据格式的一些细节忘记&#xff0c;所以复制过来&#xff0c;以供后续有必要的时候&#xff0c;查缺补漏。 一、简介 ESRI Shapefile&#xff08;shp&#xff09;&#xff0…

gis利器之Gdal(二)shp数据读取

本文将简单介绍shp数据的基本知识&#xff0c;以java语言为例&#xff0c;介绍如何基于java调用gdal完成对shp数据的解析&#xff0c;主要包括空间信息解析、属性信息解析、空间参考信息解析&#xff0c;bbox信息解析等等。 shp文件是一种矢量图形存储文件&#xff0c;可以用于…

全国矢量shp数据:行政区划,县界,道路,河流....都可下载

前段时间我们分享过一次全国矢量数据&#xff0c;包含道路&#xff0c;水系&#xff0c;铁路等等 不过第二天我们就把文章删除了 以至于后台不少留言问为啥删除 主要原因是数据精度稍微有点差&#xff08;部分数据偏差过大&#xff09; 不过 今天我们来重新分享 这次本着授…