# ST_Distance

## 定义

ST_Distance 用于返回两个几何之间的距离。这一距离是两个几何的最近折点之间的距离。

## 语法

`sde.st_distance (g1 sde.st_geometry, g2 sde.st_geometry)`

## 示例

``````CREATE TABLE buildings (building_id integer, lot_id integer,
footprint sde.st_geometry);

CREATE TABLE parcels (apn integer unique, parcel sde.st_geometry);

INSERT INTO buildings (building_id, lot_id, footprint) VALUES (
1,
400,
sde.st_polygon ('polygon ((0 0, 0 10, 10 10, 10 0, 0 0))', 0)
);

INSERT INTO buildings (building_id, lot_id, footprint) VALUES (
2,
400,
sde.st_polygon ('polygon ((12 3, 12 6, 15 6, 15 3, 12 3))', 0)
);

INSERT INTO buildings (building_id, lot_id, footprint) VALUES (
3,
400,
sde.st_polygon ('polygon ((20 0, 20 10, 30 10, 30 0, 20 0))', 0)
);

INSERT INTO buildings (building_id, lot_id, footprint) VALUES (
4,
402,
sde.st_polygon ('polygon ((40 0, 40 10, 50 10, 50 0, 40 0))', 0)
);

INSERT INTO parcels (apn, parcel) VALUES (
400,
sde.st_polygon ('polygon ((-1 -1, -1 11, 11 11, 19 11, 31 11, 31 -1, 19 -1, 11 -1, -1 -1))', 0)
);

INSERT INTO parcels (apn, parcel) VALUES (
402,
sde.st_polygon ('polygon ((39 -1, 39 11, 51 11, 51 -1, 39 -1))', 0)
);
``````

Oracle

``````SELECT UNIQUE p.apn, b.building_id, sde.st_distance(b.footprint, sde.st_boundary(p.parcel)) DISTANCE
FROM BUILDINGS b, PARCELS p
WHERE b.lot_id = p.apn
AND p.apn = 400
ORDER BY DISTANCE;

APN		BUILDING_ID      DISTANCE

400		           1					1
400   			  3					1
400    			  3					4
``````

PostgreSQL

``````SELECT DISTINCT p.apn, b.building_id, sde.st_distance(b.footprint, sde.st_boundary(p.parcel)) AS Distance
FROM buildings b, parcels p
WHERE b.lot_id = p.apn
AND p.apn = 400
ORDER BY Distance;

apn	     building_id		distance

400            1				1
400            3				1
400     	   2				4
``````
9/15/2013