Buffer

A spatial database allows you to store spatial data, but you can also perform operations on the data and get different geometries back. The most common of these operations would be a buffer. You have a table of points, but using ST_Buffer(), you can have the database return a polygon around the point with a specified radius. The following code shows you how:

cursor.execute("SELECT ST_AsText(ST_Buffer(a.location,25.00,'quad_segs=2')) from pieces a WHERE a.code='101'")

cursor.fetchall()

The previous code grabs a record from the table where the art code field equals 101, and selects a buffer with a radius of 25 around the location. The result will be a polygon, which is shown as follows:

When using geography, if the buffer is large, falls between two UTM zones, or crosses the dateline, it may behave unexpectedly.

'POLYGON((-106.591563918525 35.1555036055616,-106.591568334295 35.1554595740463,-106.59158312469 35.1554170960907,...,-106.591570047094 35.155547498531,-106.591563918525 35.1555036055616))'

If you load the polygon into shapely using the following code, a Jupyter Notebook will draw the polygon:

from shapely.geometry import Polygon
from shapely.wkt import loads
buff=loads(data[0][0])
buff

The buffer returned from ST_Buffer as a shapely polygon is shown as follows:

 

You can also pass a parameter to ST_Buffer for the number of segments you used to draw a quarter of a circle. If you divide the circle into four quadrants, the quad_segs parameter will draw that many segments in each quadrant. A quad_seg value of 1 will draw a rotated square, which is shown as follows:

Whereas a quad_seg value of 2 would draw an octagon which is shown as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset