@@ -39,9 +39,11 @@ public void TestLineCentroid() throws Exception {
3939 BytesWritable bwGeom = stLn .evaluate (new Text ("linestring (0 0, 6 0)" ));
4040 BytesWritable bwCentroid = stCtr .evaluate (bwGeom );
4141 validatePoint (new Point (3 ,0 ), bwCentroid );
42- bwGeom = stLn .evaluate (new Text ("linestring (0 0, 2 4, 6 8 )" ));
42+ bwGeom = stLn .evaluate (new Text ("linestring (0 0, 0 4, 12 4 )" ));
4343 bwCentroid = stCtr .evaluate (bwGeom );
44- validatePoint (new Point (3 ,4 ), bwCentroid );
44+ // L1 = 4, L2 = 12, W1 = 0.25, W2 = 0.75, X = W1 * 0 + W2 * 6, Y = W1 * 2 + W2 * 4
45+ // Or like centroid of multipoint of 1 of (0 2) and 3 of (6 4)
46+ validatePoint (new Point (4.5 , 3.5 ), bwCentroid );
4547 }
4648
4749 @ Test
@@ -51,13 +53,16 @@ public void TestPolygonCentroid() throws Exception {
5153 BytesWritable bwGeom = stPoly .evaluate (new Text ("polygon ((0 0, 0 8, 8 8, 8 0, 0 0))" ));
5254 BytesWritable bwCentroid = stCtr .evaluate (bwGeom );
5355 validatePoint (new Point (4 ,4 ), bwCentroid );
54- bwGeom = stPoly .evaluate (new Text ("polygon ((1 1, 5 1, 3 4))" ));
56+ bwGeom = stPoly .evaluate (new Text ("polygon ((1 1, 5 1, 3 4, 1 1 ))" ));
5557 bwCentroid = stCtr .evaluate (bwGeom );
5658 validatePoint (new Point (3 ,2 ), bwCentroid );
59+ bwGeom = stPoly .evaluate (new Text ("polygon ((14 0, -14 0, -2 24, 2 24, 14 0))" ));
60+ bwCentroid = stCtr .evaluate (bwGeom ); // Cross-checked with ...
61+ validatePoint (new Point (0 ,9 ), bwCentroid ); // ... omnicalculator.com/math/centroid
5762 }
5863
5964 /**
60- * Validates the geometry writable.
65+ * Validates the centroid geometry writable.
6166 *
6267 * @param point
6368 * the represented point location.
@@ -66,13 +71,18 @@ public void TestPolygonCentroid() throws Exception {
6671 */
6772 private static void validatePoint (Point point , BytesWritable geometryAsWritable ) {
6873 ST_X getX = new ST_X ();
69- DoubleWritable xAsWritable = getX .evaluate (geometryAsWritable );
70- assertNotNull ("The x writable must not be null!" , xAsWritable );
71-
7274 ST_Y getY = new ST_Y ();
75+ DoubleWritable xAsWritable = getX .evaluate (geometryAsWritable );
7376 DoubleWritable yAsWritable = getY .evaluate (geometryAsWritable );
74- assertNotNull ("The y writable must not be null!" , yAsWritable );
7577
78+ if (null == xAsWritable || null == yAsWritable ||
79+ Math .abs (point .getX () - xAsWritable .get ()) > Epsilon ||
80+ Math .abs (point .getY () - yAsWritable .get ()) > Epsilon )
81+ System .err .println ("validateCentroid: " + (new ST_AsText ()).evaluate (geometryAsWritable )
82+ + " ~ " + point );
83+
84+ assertNotNull ("The x writable must not be null!" , xAsWritable );
85+ assertNotNull ("The y writable must not be null!" , yAsWritable );
7686 assertEquals ("Longitude is different!" , point .getX (), xAsWritable .get (), Epsilon );
7787 assertEquals ("Latitude is different!" , point .getY (), yAsWritable .get (), Epsilon );
7888 }
0 commit comments