/** * 规则多边形 环的中心点在polygon的左下方 * @throws ParseException */ @Test public void test6() throws ParseException { Polygon polygon1 = (Polygon) wktReader.read("POLYGON ((5400 -700, 6000 -700, 5999.939 -2100, 6000 -2100, 5400 -2100, 5400 -1700, 5320 -1700, 5320 -700, 5400 -700))"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5670.419486717734 -1382.5970582185353) } @Test public void test61() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("LinearRing (5400 -700, 6000 -700, 5999.939 -2100, 6000 -2100, 5400 -2100, 5400 -1700, 5320 -1700, 5320 -700, 5400 -700)"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5667.686915788274 -1392.3180693769175) } /** * 线段中心点 * @throws ParseException */ @Test public void test10() throws ParseException { LineString polygon1 = (LineString) wktReader.read("LINESTRING (7129.693356 10376.111416, 5297.476759 11041.826775)"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (6213.5850575 10708.9690955) } @Test public void test11() throws ParseException { LineString polygon1 = (LineString) wktReader.read("LINEARRING (7129.693356 10376.111416, 5297.476759 11041.826775)"); log.info(polygon1); log.info(polygon1.getCentroid());//Points of LinearRing do not form a closed linestring } /** * TEST 矩形 获取中心点 * Polygon和LinearRing 的结果一致 * @throws ParseException */ @Test public void test12() throws ParseException { Polygon polygon1 = (Polygon) wktReader.read("POLYGON ((5490 10950, 6240 10950, 6240 10510, 5490 10510, 5490 10950))"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5865 10730) } @Test public void test13() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("LINEARRING (5490 10950, 6240 10950, 6240 10510, 5490 10510, 5490 10950)"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5865 10730) } /** * TEST 不规则四边形 获取中心点 * 结论 Polygon和LinearRing 的结果不一致 环的中心点在polygon的右上方 * @throws ParseException */ @Test public void test121() throws ParseException { Polygon polygon1 = (Polygon) wktReader.read("POLYGON ((5650 10820, 6073 10765, 5950 10630, 5725 10533, 5650 10820))"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5829.589743589743 10697.393162393162) } @Test public void test131() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("LINEARRING (5650 10820, 6073 10765, 5950 10630, 5725 10533, 5650 10820)"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5835.344664155982 10702.602098682733) } /** * test 不规则多边形 获取中心点 *结论 Polygon和LinearRing 的结果不一致 环的中心点在polygon的右下方 * @throws ParseException */ @Test public void test7() throws ParseException { Polygon polygon1 = (Polygon) wktReader.read("POLYGON ((4640 -1240, 5320 -940, 5460 -1150, 5630 -1330, 5700 -1510, 5220 -1530, 5330 -1650, 4770 -1630, 4770 -1450, 4640 -1240))"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5154.716638904312 -1335.9652045160096) } @Test public void test9() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("LinearRing (4640 -1240, 5320 -940, 5460 -1150, 5630 -1330, 5700 -1510, 5220 -1530, 5330 -1650, 4770 -1630, 4770 -1450, 4640 -1240)"); log.info(polygon1); log.info(polygon1.getCentroid());//POINT (5171.656664667675 -1360.5231087790598) } @Test public void test8() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("POLYGON ((4640 -1240, 5320 -940, 5460 -1150, 5630 -1330, 5700 -1510, 5220 -1530, 5330 -1650, 4770 -1630, 4770 -1450, 4640 -1240))"); log.info(polygon1);//java.lang.ClassCastException: org.locationtech.jts.geom.Polygon cannot be cast to org.locationtech.jts.geom.LinearRing log.info(polygon1.getCentroid()); } @Test public void test100() throws ParseException { LinearRing polygon1 = (LinearRing) wktReader.read("LinearRing ((640 -1240, 5320 -940, 5460 -1150, 5630 -1330, 5700 -1510, 5220 -1530, 5330 -1650, 4770 -1630, 4770 -1450, 4640 -1240))"); log.info(polygon1);//org.locationtech.jts.io.ParseException: Expected number but found '(' (line 1) log.info(polygon1.getCentroid()); }
04-07
482
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-31
5784
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)