PostGIS特殊函式 ☞ 根據BOX3D查詢某一空間範圍內的物件

appleyk發表於2018-07-19

 

 

一、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。

 

 

 

 

 

 

六、查詢示例演示

 

 

 

 

 

 

相關文章