PostGIS特殊函数 ☞ 根据BOX3D查询某一空间范围内的对象

 

 

一、geotools依赖的maven包

 

 

<properties>
	<geotools.version>17.0</geotools.version>
</properties>
<!-- 添加GeoTools依赖 -->
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-shapefile</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-swing</artifactId>
	<version>${geotools.version}</version>
</dependency>

 

 

 

二、demo模拟查询条件 == 按bbox查询

 

 

Junit单元测试依赖的Maven包

 

 

<!-- JUnit单元测试 -->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
</dependency>

 

 

 

import org.junit.Test;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class QueryByBboxTest {

	
	@Test
	public void query(){
			
		//几何构建工厂
		GeometryFactory factory  = new GeometryFactory();
		//矩形框
		Envelope envelope = null;
		
		//坐标集合1
		Coordinate coordinate1 = new Coordinate(113.565619, 113.565619);
		//工厂创建一个点1
		Point point1 = factory.createPoint(coordinate1);
	
		
		if(point1!=null){
			//拿到几何Point的外界矩形
			envelope = point1.getEnvelopeInternal();
		}
			
		//坐标集合2
		Coordinate coordinate2 = new Coordinate(113.565550, 113.565721);
		//工厂再创建一个点2
		Point point2 = factory.createPoint(coordinate2);
	
		
		if(point2!=null){
			if(envelope == null){
				//如果等于null,拿到Point2的范围(矩形框)
				envelope = point2.getEnvelopeInternal();
			}else{
				//叠加envelope
				envelope.expandToInclude(point2.getEnvelopeInternal());
			}
		}
		
		String bboxStr = String.format("st_3dmakebox(st_makepoint(%f, %f, 0),st_makepoint(%f, %f, 0))",
				envelope.getMaxX(), envelope.getMaxY(), envelope.getMinX(), envelope.getMinY());
		
		System.out.println("select*from object where bbox &&& "+bboxStr);
	}
}

 

 

效果:

 

select*from object where bbox &&& st_3dmakebox(st_makepoint(113.565619, 113.565619, 0),st_makepoint(113.565619, 113.565619, 0))

 

 

 

三、bbox字段在PostGreSql数据库中的类型

 

 

 

注意:先装PostGis插件,才能使PostGreSql数据库支持空间对象数据类型

 

 

 

 

 

 

四、BOX3D用法

 

 

PostGIS函数目录官网地址:Chapter 14. PostGIS Special Functions Index

 

 

BOX3D用法链接:ST_3DMakeBox — Creates a BOX3D defined by the given 3d point geometries.

 

 

构建一个BOX3D

 

 

 

 

 

五、&&&用算符的用法

 

 

&&& — Returns TRUE if A’s n-D bounding box intersects B’s n-D bounding box.

 

当几何A的n-D边界框与几何B的n-D边界框相交时,&&&运算符返回TRUE。

 

 

 

 

 

 

六、查询示例演示

 

 

 

 

 

 

转载自:https://blog.csdn.net/Appleyk/article/details/81112690

You may also like...