目录

  • 介绍
  • 项目依赖
  • 创建测试类
  • Mybatis 转换类
  • 总结

介绍

由于试用了JTS Java工具 的精度位置,可以读取数据二进制精度信息,但是生成的数据不能带SRID信息,默认是0,导致插入数据库报错

ERROR: Geometry SRID (0) does not match column SRID (4326)

现在换成 GeoLatte 工具来进行操作

项目依赖

<dependency><groupId>org.geolatte</groupId><artifactId>geolatte-geom</artifactId><version>1.8.2</version>
</dependency><dependency><groupId>org.geolatte</groupId><artifactId>geolatte-geojson</artifactId><version>1.8.2</version>
</dependency>

创建测试类

这个测试类,包含基本操作

package util;import com.zht.redistopost.ProductDataApplication;
import org.geolatte.geom.codec.Wkb;
import org.geolatte.geom.codec.Wkt;
import org.geolatte.geom.crs.CoordinateReferenceSystem;
import org.geolatte.geom.crs.LinearUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.geolatte.geom.*;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import static org.geolatte.geom.builder.DSL.*;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {ProductDataApplication.class})
public class GeolatteGeometryTest {/*** 日志*/private final static Logger LOGGER = LoggerFactory.getLogger(GeolatteGeometryTest.class);/*** 数据库源*/@Autowiredprivate DataSource dataSource;/*** 创建地理位置信息*/@Testpublic void createGeometry2D() {// 2D 点Point<G2D> pnt2D = point(WGS84, g(4.33, 53.21));// 2D 线LineString<G2D> lstr2D = linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19));// 2D 面Polygon<G2D> pgn2D = polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21)));// 2D 多点MultiPoint<G2D> multiPoint2D = multipoint(point(WGS84, g(4.33, 53.21)), point(WGS84, g(4.33, 53.21)), point(WGS84, g(4.33, 53.21)), point(WGS84, g(4.33, 53.21)));// 2D 多线MultiLineString<G2D> multiLineString2D = multilinestring(linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19)), linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19)), linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19)), linestring(WGS84, g(4.43, 53.21), g(4.44, 53.20), g(4.45, 53.19)));// 2D 多面MultiPolygon<G2D> multiPolygon2D = multipolygon(polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))), polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))), polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))), polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))), polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))), polygon(WGS84, ring(g(4.43, 53.21), g(4.44, 53.22), g(4.43, 53.21))));}/*** 创建3D图形*/@Testpublic void createGeometry3D() {// 3D转换CoordinateReferenceSystem<G3D> wgs84E = WGS84.addVerticalSystem(LinearUnit.METER, G3D.class);// 3D 点Point<G3D> point3D = point(wgs84E, g(4.33, 53.21, 350));// 3D 线LineString<G3D> lstr3D = linestring(wgs84E, g(4.43, 53.21, 53.11), g(4.44, 53.20, 53.11), g(4.45, 53.19, 53.11));// 3D 面Polygon<G3D> pgn3D = polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11)));// 3D 多点MultiPoint<G3D> multiPoint3D = multipoint(point(wgs84E, g(4.33, 53.21, 350)), point(wgs84E, g(4.33, 53.21, 350)), point(wgs84E, g(4.33, 53.21, 350)), point(wgs84E, g(4.33, 53.21, 350)));// 3D 多线MultiLineString<G3D> multiLineString3D = multilinestring(linestring(wgs84E, g(4.43, 53.21, 53.11), g(4.44, 53.20, 53.11), g(4.45, 53.19, 53.11)), linestring(wgs84E, g(4.43, 53.21, 53.11), g(4.44, 53.20, 53.11), g(4.45, 53.19, 53.11)), linestring(wgs84E, g(4.43, 53.21, 53.11), g(4.44, 53.20, 53.11), g(4.45, 53.19, 53.11)), linestring(wgs84E, g(4.43, 53.21, 53.11), g(4.44, 53.20, 53.11), g(4.45, 53.19, 53.11)));// 3D 多面MultiPolygon<G3D> multiPolygon2D = multipolygon(polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))), polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))), polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))), polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))), polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))), polygon(wgs84E, ring(g(4.43, 53.21, 53.11), g(4.44, 53.22, 53.11), g(4.43, 53.21, 53.11))));}/*** 生成WKT数据*/@Testpublic void WKT() {CoordinateReferenceSystem<G3D> wgs84E = WGS84.addVerticalSystem(LinearUnit.METER, G3D.class);Point<G2D> pnt2D = point(WGS84, g(4.33, 53.21));Point<G3D> point3D = point(wgs84E, g(4.33, 53.21, 350));String wkt2D = Wkt.toWkt(pnt2D);LOGGER.info("wkt2D: " + wkt2D);// Wkt.Dialect 对应数据库的方言String wkt3D = Wkt.toWkt(point3D, Wkt.Dialect.POSTGIS_EWKT_1);LOGGER.info("wkt3D: " + wkt3D);}/*** 生成WKB数据*/@Testpublic void WKB() {CoordinateReferenceSystem<G3D> wgs84E = WGS84.addVerticalSystem(LinearUnit.METER, G3D.class);Point<G2D> pnt2D = point(WGS84, g(4.33, 53.21));Point<G3D> point3D = point(wgs84E, g(4.33, 53.21, 350));ByteBuffer byteBuffer2D = Wkb.toWkb(pnt2D, Wkb.Dialect.POSTGIS_EWKB_1);LOGGER.info("byteBuffer2D: " + byteBuffer2D);ByteBuffer byteBuffer3D = Wkb.toWkb(point3D, Wkb.Dialect.POSTGIS_EWKB_1);LOGGER.info("byteBuffer3D: " + byteBuffer3D);}/*** 通过JDBC 读取精度信息*/@Testpublic void jdbcReadData() {String sql = "select vehicle_location as vehicle_location from tb_vehicle_step where id = ?::uuid";Connection connection = null;PreparedStatement preparedStatement = null;try {connection = dataSource.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1, "5fbbffd7-9b3d-4acc-863e-efe8327068a0");ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()) {String vehicle_location = resultSet.getString("vehicle_location");LOGGER.info("数据库取到的数据:" + vehicle_location);// 转换数据成为对象Geometry<?> geometry = Wkb.fromWkb(ByteBuffer.from(vehicle_location), Wkb.Dialect.POSTGIS_EWKB_1);LOGGER.info("精度信息:" + Wkt.toWkt(geometry));}} catch (SQLException e) {LOGGER.error("数据库连接对象生成异常", e);}}/*** 通过JDBC 写入精度信息*/@Testpublic void jdbcWriteData() {String sql = "insert into tb_vehicle_step( machno, c_h_eading, v_h_eading, vehicle_location, tm_collect, tm_receive ) values ( 'H348', 119.910, 119.910, ?, to_timestamp(1636037065200::double precision / 1000), to_timestamp(1636037073758::double precision / 1000));";Connection connection = null;PreparedStatement preparedStatement = null;Point<G2D> pnt2D = point(WGS84, g(4.33, 53.21));ByteBuffer byteBuffer = Wkb.toWkb(pnt2D, ByteOrder.XDR, Wkb.Dialect.POSTGIS_EWKB_1);try {connection = dataSource.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1, byteBuffer.toString());boolean bol = preparedStatement.execute();if (bol) {LOGGER.info("执行成功");} else {LOGGER.info("执行失败");}} catch (SQLException e) {LOGGER.error("数据库连接对象生成异常", e);} catch (Exception e) {LOGGER.error("方法执行异常", e);}}
}

Mybatis 转换类

package com.zht.redistopost.typehandler;import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.geolatte.geom.ByteBuffer;
import org.geolatte.geom.ByteOrder;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.codec.Wkb;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/*** @author Lihan* @since 2021年11月15日 10:48:55*/
@MappedTypes(value = {Geometry.class})
public class GeometryTypeHandler extends BaseTypeHandler<Geometry<?>> {@Overridepublic void setNonNullParameter(PreparedStatement preparedStatement, int i, Geometry geometry, JdbcType jdbcType) throws SQLException {ByteBuffer byteBuffer = Wkb.toWkb(geometry, ByteOrder.XDR, Wkb.Dialect.POSTGIS_EWKB_1);preparedStatement.setString(i, byteBuffer.toString());}@Overridepublic Geometry<?> getNullableResult(ResultSet resultSet, String s) throws SQLException {String value = resultSet.getString(s);return Wkb.fromWkb(ByteBuffer.from(value), Wkb.Dialect.POSTGIS_EWKB_1);}@Overridepublic Geometry<?> getNullableResult(ResultSet resultSet, int i) throws SQLException {return null;}@Overridepublic Geometry<?> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {return null;}
}

总结

GeoLatte
项目Git 地址: https://github.com/GeoLatte/

数据库Geometry字段操作相关推荐

  1. mysql 字段操作_Mysql:数据库操作、数据表操作、字段操作整理

    mysql 登录本机 mysql -u root -p password 数据库操作 创建数据库 命令:create database 例如:mysql> create database xhk ...

  2. oracle几何体数据类型,Oracle数据库之spatial操作geometry方法

    本文主要向大家介绍了Oracle数据库之spatial操作geometry方法,通过具体的内容向大家展现,希望对大家学习Oracle数据库有所帮助. 两个对象之间关系: RELATE SDO_GEOM ...

  3. SQL数据库对字段的操作(alter table)

    目录: 1.数据库的连接.创建 2.对字段的操作:(alter table) 3.对数据的操作(插入.修改.删除) 4.数据库的查询操作大全(select) 5.多表查询(join on) 6.约束操 ...

  4. MYSQL数据库字段关联_MySQL数据库 字段操作 多表关系(更新中...)

    外键 (foreign key) ## 外键 ```mysql # 作者(author):id,name,sex,age,mobile, detail_id # 作者详情(author_detail) ...

  5. 2021年大数据Hive(三):手把手教你如何吃透Hive数据库和表操作(学会秒变数仓大佬)

    全网最详细的Hive文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 系列历史文章 前言 Hive数据库和表操作 一.数据库操作 1.创建数据库 2.创建 ...

  6. 盘点一下数据库的误操作有哪些后悔药?

    前言 无论是开发.测试,还是DBA,都难免会涉及到数据库的操作,比如:创建某张表,添加某个字段.添加数据.更新数据.删除数据.查询数据等等. 正常情况下还好,但如果操作数据库时出现失误,比如: 删除订 ...

  7. Linux下Mysql数据库的基础操作

    Linux下Mysql数据库的基础操作 一.Mysql数据介绍 二.数据库相关术语介绍 1.数据库相关名词 2.相关术语介绍 三.Mysql数据库的管理 1.创建数据库用户 2.查询用户状态 3.修改 ...

  8. MySQL数据库的常用操作

    -- 数据库的常用操作 -- 管理数据:查看.添加.修改.删除数据 -- 查询数据:查询所有字段.查询指定字段.查询时指定别名.查询时合并列.查询时添加常量列.查询时取出重复数据-- 条件查询.分页查 ...

  9. like mysql 相反_Mysql数据库的常用操作

    你这么优秀,一定只想把"柠檬班"置顶 ▲ 本文由柠檬班Python10期VIP学员Boy原创. 本文主要介绍mysql数据库的查询操作,捎带脚增删改操作. ·增 · insert  ...

最新文章

  1. LR监测windows资源一般监测哪几个项?
  2. 鼠标滚轮 控制作用滚动
  3. model.fit() 参数详解【TensorFlow2入门手册】
  4. DL之RetinaNet:基于RetinaNet算法(keras框架)利用resnet50_coco数据集(.h5文件)实现目标检测
  5. 【快乐水题】506. 相对名次
  6. 创建故障转移群集,LiveMigration系列之六
  7. docker 目录 挂载
  8. redis☞ python客户端
  9. 前端异步编程之Promise和async的用法
  10. chrome插件分享
  11. iframe框根据内容自适应高度
  12. 思科网络模拟器Packet Tracer教程
  13. 小狼毫[rime_win][眀月拼音]简单配置方法
  14. 三级联动的数据库(贼多)
  15. 家庭局域网接入Internet
  16. 微软报表工具服务器版本,Report Builder 3.0
  17. oracle的gca的文件,GCA文件扩展名 - 什么是.gca以及如何打开? - ReviverSoft
  18. 记录下公司刻录新版本/repo_kitkat 命令
  19. 传感器系列之4.7地磁传感器
  20. FPGA数字IC刷题58道Verilog题解代码及视频讲解【FPGA探索者】【同步/异步FIFO】【跨时钟】

热门文章

  1. 笔记本安装Archlinux笔记
  2. 闲鱼无货源怎样选品,能够做到日入几百?
  3. 关于电信cdma基站nid,sid,bid的解释
  4. 字节测试实习生面试回顾
  5. 人机大战,历史的见证
  6. 社区公共安全治理探索:达观舆情信息智能处理解决方案
  7. linux操作系统版本_史上最全的Linux 各个发行版本的优点、缺点、发展史介绍
  8. 中间件是什么,常用的中间件有哪些(*)
  9. 南京市租房网上提取公积金步骤
  10. 手机卡服务器密码忘记了怎么修改密码,wifi密码忘记了怎么办找回密码 手机怎么修改自家wifi密码...