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

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.
主站蜘蛛池模板: 侯马市| 梁山县| 东海县| 开封县| 黑山县| 通海县| 丹巴县| 泊头市| 上饶县| 华阴市| 苏州市| 嘉善县| 堆龙德庆县| 合川市| 南宫市| 漠河县| 云安县| 贵南县| 唐河县| 泸溪县| 甘洛县| 青海省| 湟中县| 东方市| 浦北县| 灌阳县| 饶阳县| 科尔| 中卫市| 天峨县| 双城市| 东城区| 合江县| 邳州市| 陇西县| 扶沟县| 大港区| 中方县| 东乌珠穆沁旗| 深水埗区| 安顺市|