Do you really use the buffer in PostGIS?

buffer-Graphical buffer analysis, one of the most basic spatial analysis in GIS.

There are many tools for implementing buffers, such as truf.js on the front end, ArcGISserver on the server side, ArcMap on the desktop side, and PosrGIS on the database side.

But recently, when using PostGIS to analyze the point buffer, the result is an ellipse.

image-20201109210112613

Why is it an ellipse, shouldn't it be a perfect circle?

In order to clarify this problem, I went to study the principle of buffer.

There are two methods to build the buffer: European methods and geodesic methods .

  1. The European method is to calculate the buffer on a two-dimensional flat map. This two-dimensional flat map is a map obtained after the earth is projected. The projection process will cause the map to be deformed. The European method calculates the buffer based on the deformed map.
  2. The geodesic method is calculated on a three-dimensional ellipsoid. The three-dimensional ellipsoid is a sphere that is very close to the shape of the earth. The geodesic method is based on the surface of the sphere for buffer calculation, and then the calculation result is transformed by projection and displayed on the map. .

The difference between the two results is that in the European method, the calculation result of the point buffer is a perfect circle at all times, but when the result is placed in the real world, there will be errors. The size of the error depends on the projection method, the location of the buffer, and the distance of the buffer. Take the Gaode map as an example, it uses the Mercator projection. Under this projection, the equatorial area has the smallest deformation, and the higher the north and south poles are. In latitude regions, the greater the deformation, the most obvious is Greenland. Its area is only about 1/4 of the area of ​​mainland China, but on the map, it is larger than China.

image-20201111125821281

There is no error in the calculation result of the geodesic method, but to display it on a two-dimensional map, map projection must be performed, and the projection will cause distortion.

What if you want the results to be error-free and show no distortion?

Use a three-dimensional map.

A three-dimensional map does not need to undergo projection transformation like a two-dimensional map, and without projection transformation, there will be no deformation.

In the following figure, the left side is a two-dimensional map, and the right side is a three-dimensional map. It can be clearly seen that in high latitude areas, the left side has been deformed, but the right side is not.

image-20201109204728345

After understanding the principle of the buffer, look back at the problem that appeared at the beginning.

This is how my sql code is written in postGIS. According to the official document of postGIS , this should belong to the European method.

image-20201112141538745

The effect of buffering 500 meters is like this

[External link image transfer failed, the source site may have an anti-hotlinking mechanism, it is recommended to save the image and upload it directly (img-0nv5XIg8-1605169096527)(http://blogimage.gisarmory.xyz/20201112122638.png)]

Then I wrote a geodesic method, note the difference between the red box and above the input v_inGeomvariables, the default is geometrytype cast to it geographylater, postGIS will use the geodesic methods.

image-20201112141932669

The effect of buffering 500 meters is like this

image-20201110184415635

Two simultaneous display

image-20201110184657159

The question is obvious, why the result of the geodesic method is round, and the result of the European method is elliptical? This is not in line with the principles learned earlier.

Shouldn’t the European method be a perfect circle, and the geodesic method should be an ellipse?

I used truf.js to make a 500-meter buffer, and the buffering result was superimposed on the above graphics, the effect is like this (the small circle inside is the result of truf.js buffering)

image-20201110185227560

I fell into deep thinking.

Checking the official documentation of truf.js , there is only one buffering method, and there is no specific description of which.

I feel that this of truf.js is the correct European method. What the hell is the ellipse on it?

It seems that you need to find an authoritative one to calibrate it. Use the buffer interface of the arcgis server to try and see what the effect is.

Code

image-20201110190244861

The effect is as follows, the large circle is the geodesic method, the small circle is the European method

image-20201110190339808

In this way, the European method in truf.js, the geodesic method in postGIS is correct, but the European method is problematic.

Then study the European method in postGIS.

When calling the buffer interface of the arcgis server, I noticed that three coordinate-related parameters were passed in the interface, the coordinates of the inSRinput graphics, the coordinates of the outSRoutput graphics, and the coordinates bufferSRused for buffering.

image-20201110191005317

Benchmark postGIS

wgs84The coordinates passed in and returned are also the same , so what coordinates are used for buffering?

Oh~ oh~ I see

The reason for the ellipse is that when geojson is converted to geometric figures (see the figure below), the St_geomfromgeojson function returns the geometrytype. When buffering, the ST_Bufferfunction receives the geometrytype and chooses to use the European method for buffering, but the data in geojson is spherical coordinates. For the longitude and latitude data, the radius of the buffer is also passed in in radians. It is not surprising that the final result is an ellipse when calculated in the flat map algorithm of the European method using spherical coordinates and distance in radians.

image-20201112141538745

Hmm~ It makes sense.

Try to rotate the coordinates. The red box in the figure below shows the process of coordinate conversion. At the same time, because the projection coordinate calculation is used, the distance parameter of the buffer can be directly used in meters, and there is no need to convert it to radians.

image-20201112150016107

Try again, the big circle is the geodesic method, and the small circle is the European method, haha, perfect!

image-20201110192011776

Finally, verify the accuracy and measure the distance to see which one is accurate.

Obviously, in the figure below, the distance of 500 meters is consistent with the boundary of the big circle in the figure above, that is, the geodesic method is more accurate.

image-20201112151837047

Little question:

Why the circle buffered by the geodesic method in the example is still a perfect circle, isn't it deformed?

Answer: The main reason is that the buffer distance in the example is only 500 meters, which is too small. It is also Beijing, if it is buffered for more than 1,000 kilometers, obvious deformation can be seen.

to sum up:

  1. There are two ways to construct the buffer, the European method and the geodesic method
  2. The European method is to calculate the buffer on the flat map after the projection deformation. The advantage is that the algorithm is simple and efficient, but the disadvantage is that the result has an error. The size of the error depends on the projection method, the buffer position and the buffer distance.
  3. The geodesic method is to perform buffer calculation on a three-dimensional ellipsoid. The advantage is that the result is accurate and is not affected by projection deformation. The disadvantage is that the algorithm is complex, and the efficiency may be affected when the amount of data is large.
  4. truf.js only supports European methods.
  5. ArcGIS server supports two construction methods.
  6. postGIS supports two construction methods. The default is the European method. In the European method, if the parameters are latitude and longitude coordinates, you need to convert the latitude and longitude coordinates to projected coordinates before calculating, otherwise the buffered result will be an ellipse. After the parameter type is geometryforcibly converted to geography, postGIS will use the geodesic method for buffer calculation.

Examples, source code

This example is the example used in the article. It can be accessed online and the code can be seen using the browser developer tools.

postGIS buffer example

This function script contains the European method and geodesic method mentioned in the article. The incoming and return are in geojson format, the buffer radius unit is meter, and the buffering method is controlled by the type. Direct execution will create the function.

Buffer function script in postGIS

Reference documents

http://www.postgis.net/docs/ST_Buffer.html

https://postgis.net/docs/using_postgis_dbmanagement.html#Geography_Basics

http://turfjs.org/docs/#buffer

https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/analysis-toolbox/how-buffer-analysis-works.htm

http://server.arcgisonline.com/arcgis/sdk/rest/index.html#//02ss000000nq000000

http://server.arcgisonline.com/arcgis/sdk/rest/index.html#/Buffer/02ss0000003z000000/

https://developers.arcgis.com/javascript/latest/sample-code/ge-geodesicbuffer/index.html


Original address: http://gisarmory.xyz/blog/index.html?blog=postGISbuffer

Pay attention to the " GIS Weapon Library " public account and get more high-quality GIS articles in the first time.

This article uses the  Creative Commons Attribution-Non-Commercial Use-Share 4.0 International License Agreement to license the same way  . Welcome to reprint, use, and republish, but be sure to keep the article's signature "GIS Weapon Library" (including link:   http://gisarmory.xyz/blog/ ), and it must not be used for commercial purposes. The modified works based on this article must be the same License issuance.

Guess you like

Origin blog.csdn.net/gisarmory/article/details/109646712