官术网_书友最值得收藏!

Measuring distances between two geometries

In order to measure the distance between two geometries, the ST_Distance function should be used. It accepts two arguments of the geometry (or geography) type, and when using the more common geometry type, it returns a floating point number indicating the distance in the same units as the geometries' SRID. So for UTM the result will be given in meters, for State Plane in feet, and for WGS84 in decimal degrees (which, as I mentioned before, is plainly useless). Both geometries need to have the same SRID or an error will be thrown. For example, we can use the following to calculate the distance between Potsdam and Berlin:

SELECT ST_Distance(
ST_SetSRID(ST_MakePoint(367201,5817855),32633),
ST_SetSRID(ST_MakePoint(391390,5807271),32633)
)

st_distance
------------------
26403.1963405948

But what if the geometries are in a latitude-longitude coordinate system? This is where geodesic measurement comes into play. When the features are in the geography type, all the user has to do is nothing; PostGIS will calculate a geodesic length and return the result in meters. If the geometry type is used, it can be cast to geography:

SELECT ST_Distance(ST_MakePoint(20,50)::geography,ST_MakePoint(21,51)::geography);  

On modern hardware, this will run fast despite the complexity of math involved. But for large number of features it can slow down things anyway. When time and/or processing power is at a premium, and accuracy can be sacrificed - ,PostGIS allows for using a simpler Earth model, a sphere instead of spheroid. For this, an optional second argument use_spheroid = FALSE has to be supplied. As you might remember from geography class, the Earth is not a perfect sphere, but it's slightly flattened. When using a spherical model for calculation, the accuracy of measurement will decrease. Decreased by how much, you ask? This depends on the latitude and distance between features, but here are some examples.

For two landmarks in Berlin, the spheroidal distance is 2157.5 meters and the spherical distance is 2155 meters--a 0.116 percent difference.

For the cities of Brasilia and Rio de Janeiro in Brazil, the spheroidal distance is 988.02 kilometers and the spherical distance is 990.31 kilometers--a 0.232 percent difference.

For the terminal stations of the Trans-Siberian Railway, Moscow and Vladivostok, the spheroidal distance is 6430.7 kilometers and the spherical distance is 6412 kilometers - a 0.44 percent difference.

The query will look like this:

SELECT ST_Distance(ST_MakePoint(20,50)::geography,ST_MakePoint(21,51)::geography, FALSE);  

The speedup for simple points is about 10 percent, but it will be bigger as the geometries become more complex.

Before the introduction of the geography type, specialized functions named ST_DistanceSphere and ST_DistanceSpheroid had to be used for latitude-longitude coordinates.
主站蜘蛛池模板: 巩留县| 靖远县| 太仓市| 微山县| 象山县| 喀喇沁旗| 泸水县| 岫岩| 东乌珠穆沁旗| 恭城| 酉阳| 璧山县| 盐池县| 理塘县| 盖州市| 西昌市| 青田县| 花莲市| 咸阳市| 瑞金市| 离岛区| 凯里市| 宁夏| 康定县| 宣城市| 左权县| 宁津县| 屏山县| 克东县| 阿拉尔市| 博湖县| 大城县| 庆安县| 南和县| 辛集市| 手游| 平远县| 仙居县| 都安| 保靖县| 武川县|