您好,欢迎访问一九零五行业门户网

如何在Java中操作GIS Geometry类型数据?

java操作gis geometry类型数据现在做的gis方面的业务,所以需要操作postgis中的geometry对象,找了很多的库,比如geotools,但是莫名下载不下来。
还有就是jts,但是不好用,操作起来很复杂。找到了一个其他的类库--geolatte-geom 和geolatte-geojson。
用于操作geometry和string以及json的互相转化。而json和geojson个人理解就是输出格式不同。多了一些geometry特有的属性。
主要用于将string转geometry对象、wkt和wkb方便好用。
pom.xml文件如下<!-- 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读取数据库geometry最近因为需要存一些经纬度块信息到数据库,所以用到了mysql中的geometry属性(几何对象)。在网上搜集了很多资料,到真正用的时候还是各种问题,所以下面推荐一种可能有点笨但是实用的方法(我的使用环境springboot工具是sts),下面就举个例子来说明一下。
操作先了解一下数据库中空间数据类型有哪些
类型 说明 简介 例子
geometry 间数据 任意一种空间类型
point 点 坐标值 point(104.00924 30.46872)
linestring 线 线,由一系列点连接而成 linestring(1 1, 1 1, 1 1)
polygon 多边形 由多条线组成 polygon((1 1, 2 2, 3 3, 4 4, 5 5))
multipoint 点集合 集合类,包含多个点 multipoint(1 1, 2 2, 1 1)
multilinestring 线集合 集合类,包含多条线 multilinestring((1 1, 2 2), (1 1, 1 1))
multipolygon 多边形集合 集合类,包含多个多边形 multipolygon(((0 0, 1 0, 1 1, 0 1, 0 0)), ((1 1, 1 1, 1 1, 1 1, 1 1)))
geometrycollection 空间数据集合 集合类,可以包括多个点、线、多边形 geometrycollection(point(1 1), point(3 3), linestring(1 1, 2 2))
接着往数据库插入一个测试数据,插入的是一个空间数据集合里面包含多个多边形集合。
insert into `geometry`(`geome`) values(geomfromtext('geometrycollection(multipolygon(((104.009241 30.468972,104.009229 30.468961,104.009225 30.468997)),((104.009241 30.468972,104.009229 30.468961,104.009225 30.468997))),multipolygon(((104.009241 30.468972,104.009229 30.468961,104.009225 30.468997))))'));
数据准备好了就准备开始准备读取操作。
在pom.xml添加操作geometry等对象的依赖。
<dependency> <groupid>com.vividsolutions</groupid> <artifactid>jts</artifactid> <version>1.13</version></dependency>
本来先是想直接在实体类确定类型直接转对象,但是用了后发现不行,所以我就直接设置成object,在mysql中存储geometry使用的是二进制,所以下面直接把二进制通过jts转成geometry对象。
//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; }
完整使用例子,解析数据库中的geometry对象,得到我们需要的点位数据。
//返回一个区域集合 区域由若干个点组成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;}
以上就是如何在java中操作gis geometry类型数据?的详细内容。
其它类似信息

推荐信息