java operates gis geometry type data
I am currently doing gis business, so I need to operate the geometry object in postgis. I have found a lot of libraries, such as geotools, but I can't download it for some reason.
There is also jts, but it is not easy to use and very complicated to operate. Found another class library--geolatte-geom and geolatte-geojson.
Used to operate the conversion between geometry, String and json. My personal understanding of json and geojson is that the output formats are different. There are some more geometry-specific properties.
Mainly used to convert String into geometry objects, wkt and wkb are convenient and easy to use.
The pom.xml file is as follows
<!-- https://mvnrepository.com/artifact/org.geolatte/geolatte-geom --> <dependency> <groupId>org.geolatte</groupId> <artifactId>geolatte-geom</artifactId> <version>1.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.geolatte/geolatte-geojson --> <dependency> <groupId>org.geolatte</groupId> <artifactId>geolatte-geojson</artifactId> <version>1.6.0</version> </dependency>
public static void main(String[] args) { // 模拟数据库中直接取出的geometry对象值(他是二进制的) // WKT 是字符串形式,类似"POINT(1 2)"的形式 // 所以WKT转 geometry,相当于是字符串转geometry // WKB转 geometry,相当于是字节转geometry String s="01020000800200000097E5880801845C404D064F3AF4AE36400000000000000000290A915F01845C40DC90B1A051AE36400000000000000000"; Geometry geo = Wkb.fromWkb(ByteBuffer.from(s)); // geometry对象和WKT输出一致 // Geometry geometry1 = Wkt.fromWkt(wkt); System.out.println("-----Geometry------"+geo.getPositionN(1)); System.out.println("-----wkt------"+ Wkt.toWkt(geo)); System.out.println("-----wkb------"+Wkb.toWkb(geo)); }
java reads database geometry
Recently, because I need to save some longitude and latitude block information to the database, I used the Geometry attribute (geometry) in mysql object). I have collected a lot of information on the Internet, but there are still various problems when I actually use it, so I recommend a method that may be a bit stupid but practical (my usage environment springboot tool is sts). Here is an example to illustrate.
Operation
First understand what spatial data types are in the database
Type | Description | Introduction | Example |
Space data | Any space type | ||
Point | Coordinate value | POINT(104.00924 30.46872) | |
Line | Line is formed by connecting a series of points | LINESTRING(1 1, 1 1, 1 1) | |
Polygon | is composed of multiple lines | POLYGON((1 1, 2 2, 3 3, 4 4, 5 5)) | |
Point collection | Collection class, containing multiple points | MULTIPOINT(1 1, 2 2, 1 1) | |
Line collection | Collection class, containing multiple lines | MULTILINESTRING((1 1, 2 2), (1 1, 1 1)) | |
Polygon collection | Collection class, containing multiple polygons | MULTIPOLYGON(((0 0 , 1 0, 1 1, 0 1, 0 0)), ((1 1, 1 1, 1 1, 1 1, 1 1))) | |
Spatial data collection | Collection class, which can include multiple points, lines, and polygons | GEOMETRYCOLLECTION(POINT(1 1), POINT(3 3), LINESTRING(1 1, 2 2)) |
When the data is ready, start preparing for the read operation.
Add dependencies for operating objects such as Geometry in pom.xml.
<dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts</artifactId> <version>1.13</version> </dependency>
Originally, I wanted to directly determine the type in the entity class and convert it directly to the object, but after using it, I found that it didn’t work, so I directly set it to Object. Binary is used to store Geometry in mysql, so I directly convert the binary Convert to Geometry object through jts.
//private Geometry geom; 不可行 private Object geomAsBytes; //可行 最终得到的是一个byte数组 //直接把数据库中的byte[]转Geometry对象 public static Geometry getGeometryByBytes( byte[] geometryAsBytes) throws Exception { Geometry dbGeometry = null; // 字节数组小于5,说明geometry有问题 if (geometryAsBytes.length < 5) { return null; } //这里是取字节数组的前4个来解析srid byte[] sridBytes = new byte[4]; System.arraycopy(geometryAsBytes, 0, sridBytes, 0, 4); boolean bigEndian = (geometryAsBytes[4] == 0x00); // 解析srid int srid = 0; if (bigEndian) { for (int i = 0; i < sridBytes.length; i++) { srid = (srid << 8) + (sridBytes[i] & 0xff); } } else { for (int i = 0; i < sridBytes.length; i++) { srid += (sridBytes[i] & 0xff) << (8 * i); } } //use the JTS WKBReader for WKB parsing WKBReader wkbReader = new WKBReader(); // 使用geotool的WKBReader 把字节数组转成geometry对象。 byte[] wkb = new byte[geometryAsBytes.length - 4]; System.arraycopy(geometryAsBytes, 4, wkb, 0, wkb.length); dbGeometry = wkbReader.read(wkb); dbGeometry.setSRID(srid); return dbGeometry; }
Complete usage example, parse the geometry object in the database and get the point data we need.
//返回一个区域集合 区域由若干个点组成 public List < Area > geometryCollection2PressAreas(byte[] data) { List < Area > areas= new ArrayList < > (); try { //解析出空间集合层 GeometryCollection geometryCollection = (GeometryCollection) GeometryUtil.getGeometryByBytes(data); int geometrySize = geometryCollection.getNumGeometries(); for (int i1 = 0; i1 < geometrySize; i1++) { try { //解析出多边形集合层 MultiPolygon multiPolygon = (MultiPolygon) geometryCollection.getGeometryN(i1); int size = (int) multiPolygon.getNumPoints(); for (int i = 0; i < size; i++) { try { //解析出多边形 Polygon polygon = (Polygon) multiPolygon.getGeometryN(i); //解析出多边形中的多个点位 Coordinate[] coordinates2 = polygon.getCoordinates(); int size2 = coordinates2.length; Area area = new Area(); area.area_pts = new ArrayList < > (); for (int j = 0; j < size2; j++) { //点位对象 就一个x,一个y数据 Point point = new Point(); point.x = coordinates2[j].x; point.y = coordinates2[j].y; //点位集合 area.area_pts.add(point); } areas.add(area); } catch (Exception e) { break; } } } catch (Exception e) { break; } } } catch (Exception e) { e.printStackTrace(); } return areas; }
The above is the detailed content of How to operate GIS Geometry type data in Java?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
