The barycenter of a polyline is obtained by finding the midpoints of each line segment and then forming their weighted average using the segment lengths as weights.
For example, consider the "L" shape delineated by the points (0,0), (6,0), (6,12). There are two segments: one of length 6 with midpoint at ( (0+0)/2, (0+6)/2 ) = (3,0) and another of length 12 with midpoint at ( (6+6)/2, (0+12)/2 ) = (6,6). Their length-weighted average coordinates are therefore (x,y) with
x = (6*3 + 12*6) / (6+12) = 5, y = (6*0 + 12*6) / (6+12) = 4.
This differs from the barycenter of the three vertices, which is ( (0+6+6)/3, (0+0+12)/3 ) = (4,4).
(Edit As another example, consider the figure in the question, which although square in shape, is represented as a pentagon determined by the sequence of points (0,0), (1/2,0), (1,0), (1,1), (0,1). The five sides have lengths 1/2, 1/2, 1, 1, 1 and midpoints (1/4,0), (3/4,0), (1,1/2), (1/2,1), and (0,1/2), respectively. Their weighted average therefore equals
[(1/2)*(1/4, 0) + (1/2)*(3/4, 0) + (1)*(1, 1/2) + (1)*(1/2, 1) + (1)*(0, 1/2)] / (1/2+1/2+1+1+1)
= (2/4, 2/4) = (0.5, 0.5)
as one would hope, even though the barycenter of the vertices alone (computed as in #2 above) is (0.5, 0.4).)
The barycenter of a polygon can be obtained by triangulation to decompose it into triangles. The barycenter of a triangle-qua-polygon coincides with the barycenter of its vertices. The area-weighted average of these barycenters is the polygon's barycenter. Triangle areas are readily computed in terms of their vertex coordinates (e.g., in terms of the wedge product of two of the sides). For an illustration of such area calculations, including how to exploit signed (positive or negative) areas, see the section on "Area" at my (old) course notes page.
(Edit Consider the polygon depicted in the question for example. We could triangulate it with triangles ((0,0), (1/2,0), (0,1)) on the left, ((0,1), (1/2,0), (1,1)) in the middle, and ((1,1), (1,0), (1/2,0)) on the right. Their areas are 1/4, 1/2, 1/4 respectively and their barycenters--obtained by averaging their vertices--are (1/6,1/3), (1/2,2/3), and (5/6,1/3), respectively. The area-weighted average of these barycenters equals
[(1/4)*(1/6,1/3) + (1/2)*(1/2,2/3) + (1/4)*(5/6,1/3)] / (1/4 + 1/2 + 1/4)
= (12/24, 6/12)
= (0.5, 0.5)
as it should, despite the presence of that fifth vertex along the bottom edge.)
Best Answer
Sorry for reviving an old thread, the same question came up in the context of the geopackage specification so I thought I might as well answer it here too. The reason you need all these empty geometry types is because the ISO SQL/MM part 3 specification requires them (not explicitly, but indirectly).
As an example
ST_Intersection
needs to be able to return a 'there is no intersection' value. The type of the returned value (whatST_GeometryType
will return when called on the value) depends on the argument types.ST_Intersection(ST_GeomFromText('POINT (1 1)'), ST_GeomFromText('POINT (0 0)')
for instance is specified in section 5.1.22 as returning 'an empty set of type point'. So this needs to return a validST_Geometry
value,ST_IsEmpty
should returntrue
for it andST_GeometryType
should returnST_Point
.So what should you get when you execute
ST_AsText
andST_AsBinary
on that returned value? For WKT the only correct answer I think isPOINT EMPTY
. For WKB this is poorly defined. WKB does not provide an equivalent of WKT'sEMPTY
and points don't have a natural empty representation. This is why in GeoPackage there's an empty header bit and it's required to useNaN
as the coordinates of the point. The intention is to avoid people accidentally using empty point sets as a regular point value. NaN works as an absorbing element, so it's pretty obvious when you've made a mistake since the outcome of your calculations is very likely to be NaN as well.Note that in many implementations, like PostGIS for instance, the above example will return
GEOMETRYCOLLECTION EMPTY
instead. That's strictly speaking not compliant with the ISO spec since the type of the return value is not correct.So to answer the question 'why do you have all these different empty types in WKT and WKB'? Because WKT and WKB are, as far as I know, defined in the SQL/MM part 3 specification, that specification defines distinct typed empty sets for each geometry type and as a consequence WKT and WKB need to have a representation for these typed empty sets.