举例:问题围栏
MULTIPOLYGON (((114.0905685 32.1120567, 114.0905685 32.112957, 114.0905685 32.1138535, 114.0905685 32.1147537, 114.0905685 32.115654, 114.0905685 32.1165543, 114.0905685 32.1174507, 114.0905685 32.118351, 114.0905685 32.1192513, 114.0905685 32.1201515, 114.0905685 32.121048, 114.0914612 32.121048, 114.0914612 32.1212578, 114.0918121 32.121048, 114.0923615 32.1207199, 114.0932617 32.1201782, 114.0933151 32.1201515, 114.094162 32.1196404, 114.0948181 32.1192513, 114.0950623 32.1191025, 114.0959625 32.1185646, 114.0963135 32.118351, 114.0967636 32.118084, 114.0968628 32.118042, 114.0977631 32.1176682, 114.0982666 32.1174507, 114.0986557 32.1172905, 114.099556 32.1169128, 114.1004105 32.1165543, 114.1004562 32.1165352, 114.1013565 32.1161575, 114.1022568 32.1157799, 114.1025543 32.115654, 114.1022568 32.115654, 114.1013565 32.115654, 114.1004562 32.115654, 114.099556 32.115654, 114.099556 32.1165543, 114.0986557 32.1165543, 114.0986557 32.115654, 114.0977631 32.115654, 114.0968628 32.115654, 114.0968628 32.1147537, 114.0977631 32.1147537, 114.0986557 32.1147537, 114.099556 32.1147537, 114.099556 32.1138535, 114.1004562 32.1138535, 114.1004562 32.112957, 114.1013565 32.112957, 114.1013565 32.1120567, 114.1022568 32.1120567, 114.1022568 32.1111565, 114.103157 32.1111565, 114.103157 32.1102562, 114.1040573 32.1102562, 114.1040573 32.1093597, 114.103157 32.1093597, 114.103157 32.1084595, 114.1040573 32.1084595, 114.1040573 32.1075592, 114.103157 32.1075592, 114.103157 32.1066589, 114.1040573 32.1066589, 114.1049576 32.1066589, 114.1049576 32.1057625, 114.1049576 32.1048622, 114.1058502 32.1048622, 114.1058502 32.1039619, 114.1067505 32.1039619, 114.1067505 32.1030617, 114.1076508 32.1030617, 114.108551 32.1030617, 114.108551 32.1021652, 114.1094513 32.1021652, 114.1103516 32.1021652, 114.1103516 32.101265, 114.1103516 32.1003647, 114.1103516 32.0994644, 114.1103516 32.098568, 114.1103516 32.0976677, 114.1112518 32.0976677, 114.1112518 32.0967674, 114.1103516 32.0967674, 114.1094513 32.0967674, 114.108551 32.0967674, 114.108551 32.0958672, 114.108551 32.0949707, 114.108551 32.0940704, 114.1076508 32.0940704, 114.1076508 32.0949707, 114.1067505 32.0949707, 114.1067505 32.0958672, 114.1058502 32.0958672, 114.1058502 32.0949707, 114.1049576 32.0949707, 114.1040573 32.0949707, 114.1040573 32.0940704, 114.103157 32.0940704, 114.1022568 32.0940704, 114.1022568 32.0949707, 114.1022568 32.0958672, 114.1022568 32.0967674, 114.1013565 32.0967674, 114.1013565 32.0976677, 114.1004562 32.0976677, 114.1004562 32.098568, 114.099556 32.098568, 114.099556 32.0994644, 114.0986557 32.0994644, 114.0986557 32.1003647, 114.0977631 32.1003647, 114.0977631 32.101265, 114.0977631 32.1021652, 114.0986557 32.1021652, 114.0986557 32.1030617, 114.099556 32.1030617, 114.1004562 32.1030617, 114.1004562 32.1039619, 114.099556 32.1039619, 114.099556 32.1048622, 114.1004562 32.1048622, 114.1004562 32.1057625, 114.1004562 32.1066589, 114.1004562 32.1075592, 114.099556 32.1075592, 114.099556 32.1066589, 114.0986557 32.1066589, 114.0986557 32.1075592, 114.0977631 32.1075592, 114.0977631 32.1084595, 114.0968628 32.1084595, 114.0968628 32.1093597, 114.0959625 32.1093597, 114.0950623 32.1093597, 114.0950623 32.1102562, 114.094162 32.1102562, 114.094162 32.1111565, 114.0932617 32.1111565, 114.0932617 32.1102562, 114.0923615 32.1102562, 114.0923615 32.1111565, 114.0914612 32.1111565, 114.0914612 32.1120567, 114.0905685 32.1120567)), ((114.0752768 32.1282442, 114.0752768 32.1291435, 114.0752768 32.1300428, 114.0752768 32.1309422, 114.0761761 32.1309422, 114.0761761 32.1318415, 114.0761761 32.1327408, 114.0761761 32.1336401, 114.0761761 32.133748, 114.0763593 32.1336401, 114.0770754 32.1332181, 114.0778854 32.1327408, 114.0779748 32.1326881, 114.0788741 32.1321582, 114.0794116 32.1318415, 114.079594 32.131734, 114.0797734 32.1315036, 114.0802106 32.1309422, 114.0806727 32.1303487, 114.0809109 32.1300428, 114.081237 32.129624, 114.0815721 32.1293581, 114.0818426 32.1291435, 114.0824714 32.1286446, 114.082976 32.1282442, 114.0833707 32.1279311, 114.0841095 32.1273449, 114.08427 32.1272175, 114.084858 32.126751, 114.0851693 32.1264867, 114.0852177 32.1264456, 114.0860687 32.1257232, 114.0862772 32.1255462, 114.086968 32.1249597, 114.0873365 32.1246469, 114.0878673 32.1241962, 114.0883958 32.1237476, 114.0887666 32.1234328, 114.0894551 32.1228483, 114.0896659 32.1226693, 114.090108 32.122294, 114.0904915 32.121949, 114.0905653 32.1218826, 114.090855 32.121622, 114.0914646 32.121257, 114.0914646 32.1210496, 114.0905653 32.1210496, 114.0905653 32.1201503, 114.0905653 32.119251, 114.0905653 32.1183517, 114.0905653 32.1174523, 114.0896659 32.1174523, 114.0896659 32.1183517, 114.0896659 32.119251, 114.0887666 32.119251, 114.0887666 32.1201503, 114.0887666 32.1210496, 114.0878673 32.1210496, 114.0878673 32.121949, 114.0878673 32.1228483, 114.0878673 32.1237476, 114.086968 32.1237476, 114.0860687 32.1237476, 114.0860687 32.1246469, 114.0851693 32.1246469, 114.08427 32.1246469, 114.0833707 32.1246469, 114.0833707 32.1255462, 114.0824714 32.1255462, 114.0815721 32.1255462, 114.0806727 32.1255462, 114.0806727 32.1264456, 114.0797734 32.1264456, 114.0788741 32.1264456, 114.0788741 32.1273449, 114.0779748 32.1273449, 114.0770754 32.1273449, 114.0761761 32.1273449, 114.0761761 32.1282442, 114.0752768 32.1282442)))
如图:下面的围栏存在自相交问题
java代码在进行集合运算时会出现以下异常信息
Exception in thread "main" org.locationtech.jts.geom.TopologyException: side location conflict [ (114.0914612, 32.1210496, NaN) ]
上面的围栏,小数点后存在7位小数。精度过高。将其缩减为6为小数。
java代码
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.precision.GeometryPrecisionReducer;
public class Test{
public static GeometryPrecisionReducer mergeReducer =
new GeometryPrecisionReducer(new PrecisionModel(1000000));
public static void main(String[] args) throws ParseException {
String polyline ="MULTIPOLYGON (((114.0905685 32.1120567, 114.0905685 32.112957, 114.0905685 32.1138535, 114.0905685 32.1147537, 114.0905685 32.115654, 114.0905685 32.1165543, 114.0905685 32.1174507, 114.0905685 32.118351, 114.0905685 32.1192513, 114.0905685 32.1201515, 114.0905685 32.121048, 114.0914612 32.121048, 114.0914612 32.1212578, 114.0918121 32.121048, 114.0923615 32.1207199, 114.0932617 32.1201782, 114.0933151 32.1201515, 114.094162 32.1196404, 114.0948181 32.1192513, 114.0950623 32.1191025, 114.0959625 32.1185646, 114.0963135 32.118351, 114.0967636 32.118084, 114.0968628 32.118042, 114.0977631 32.1176682, 114.0982666 32.1174507, 114.0986557 32.1172905, 114.099556 32.1169128, 114.1004105 32.1165543, 114.1004562 32.1165352, 114.1013565 32.1161575, 114.1022568 32.1157799, 114.1025543 32.115654, 114.1022568 32.115654, 114.1013565 32.115654, 114.1004562 32.115654, 114.099556 32.115654, 114.099556 32.1165543, 114.0986557 32.1165543, 114.0986557 32.115654, 114.0977631 32.115654, 114.0968628 32.115654, 114.0968628 32.1147537, 114.0977631 32.1147537, 114.0986557 32.1147537, 114.099556 32.1147537, 114.099556 32.1138535, 114.1004562 32.1138535, 114.1004562 32.112957, 114.1013565 32.112957, 114.1013565 32.1120567, 114.1022568 32.1120567, 114.1022568 32.1111565, 114.103157 32.1111565, 114.103157 32.1102562, 114.1040573 32.1102562, 114.1040573 32.1093597, 114.103157 32.1093597, 114.103157 32.1084595, 114.1040573 32.1084595, 114.1040573 32.1075592, 114.103157 32.1075592, 114.103157 32.1066589, 114.1040573 32.1066589, 114.1049576 32.1066589, 114.1049576 32.1057625, 114.1049576 32.1048622, 114.1058502 32.1048622, 114.1058502 32.1039619, 114.1067505 32.1039619, 114.1067505 32.1030617, 114.1076508 32.1030617, 114.108551 32.1030617, 114.108551 32.1021652, 114.1094513 32.1021652, 114.1103516 32.1021652, 114.1103516 32.101265, 114.1103516 32.1003647, 114.1103516 32.0994644, 114.1103516 32.098568, 114.1103516 32.0976677, 114.1112518 32.0976677, 114.1112518 32.0967674, 114.1103516 32.0967674, 114.1094513 32.0967674, 114.108551 32.0967674, 114.108551 32.0958672, 114.108551 32.0949707, 114.108551 32.0940704, 114.1076508 32.0940704, 114.1076508 32.0949707, 114.1067505 32.0949707, 114.1067505 32.0958672, 114.1058502 32.0958672, 114.1058502 32.0949707, 114.1049576 32.0949707, 114.1040573 32.0949707, 114.1040573 32.0940704, 114.103157 32.0940704, 114.1022568 32.0940704, 114.1022568 32.0949707, 114.1022568 32.0958672, 114.1022568 32.0967674, 114.1013565 32.0967674, 114.1013565 32.0976677, 114.1004562 32.0976677, 114.1004562 32.098568, 114.099556 32.098568, 114.099556 32.0994644, 114.0986557 32.0994644, 114.0986557 32.1003647, 114.0977631 32.1003647, 114.0977631 32.101265, 114.0977631 32.1021652, 114.0986557 32.1021652, 114.0986557 32.1030617, 114.099556 32.1030617, 114.1004562 32.1030617, 114.1004562 32.1039619, 114.099556 32.1039619, 114.099556 32.1048622, 114.1004562 32.1048622, 114.1004562 32.1057625, 114.1004562 32.1066589, 114.1004562 32.1075592, 114.099556 32.1075592, 114.099556 32.1066589, 114.0986557 32.1066589, 114.0986557 32.1075592, 114.0977631 32.1075592, 114.0977631 32.1084595, 114.0968628 32.1084595, 114.0968628 32.1093597, 114.0959625 32.1093597, 114.0950623 32.1093597, 114.0950623 32.1102562, 114.094162 32.1102562, 114.094162 32.1111565, 114.0932617 32.1111565, 114.0932617 32.1102562, 114.0923615 32.1102562, 114.0923615 32.1111565, 114.0914612 32.1111565, 114.0914612 32.1120567, 114.0905685 32.1120567)), ((114.0752768 32.1282442, 114.0752768 32.1291435, 114.0752768 32.1300428, 114.0752768 32.1309422, 114.0761761 32.1309422, 114.0761761 32.1318415, 114.0761761 32.1327408, 114.0761761 32.1336401, 114.0761761 32.133748, 114.0763593 32.1336401, 114.0770754 32.1332181, 114.0778854 32.1327408, 114.0779748 32.1326881, 114.0788741 32.1321582, 114.0794116 32.1318415, 114.079594 32.131734, 114.0797734 32.1315036, 114.0802106 32.1309422, 114.0806727 32.1303487, 114.0809109 32.1300428, 114.081237 32.129624, 114.0815721 32.1293581, 114.0818426 32.1291435, 114.0824714 32.1286446, 114.082976 32.1282442, 114.0833707 32.1279311, 114.0841095 32.1273449, 114.08427 32.1272175, 114.084858 32.126751, 114.0851693 32.1264867, 114.0852177 32.1264456, 114.0860687 32.1257232, 114.0862772 32.1255462, 114.086968 32.1249597, 114.0873365 32.1246469, 114.0878673 32.1241962, 114.0883958 32.1237476, 114.0887666 32.1234328, 114.0894551 32.1228483, 114.0896659 32.1226693, 114.090108 32.122294, 114.0904915 32.121949, 114.0905653 32.1218826, 114.090855 32.121622, 114.0914646 32.121257, 114.0914646 32.1210496, 114.0905653 32.1210496, 114.0905653 32.1201503, 114.0905653 32.119251, 114.0905653 32.1183517, 114.0905653 32.1174523, 114.0896659 32.1174523, 114.0896659 32.1183517, 114.0896659 32.119251, 114.0887666 32.119251, 114.0887666 32.1201503, 114.0887666 32.1210496, 114.0878673 32.1210496, 114.0878673 32.121949, 114.0878673 32.1228483, 114.0878673 32.1237476, 114.086968 32.1237476, 114.0860687 32.1237476, 114.0860687 32.1246469, 114.0851693 32.1246469, 114.08427 32.1246469, 114.0833707 32.1246469, 114.0833707 32.1255462, 114.0824714 32.1255462, 114.0815721 32.1255462, 114.0806727 32.1255462, 114.0806727 32.1264456, 114.0797734 32.1264456, 114.0788741 32.1264456, 114.0788741 32.1273449, 114.0779748 32.1273449, 114.0770754 32.1273449, 114.0761761 32.1273449, 114.0761761 32.1282442, 114.0752768 32.1282442)))";
Geometry geometry = GeometryUtil.readGeometry(polyline);
System.out.println(geometry.isValid());
Geometry reduce = mergeReducer.reduce(geometry);
System.out.println(reduce);
System.out.println(reduce.isValid());
}
}
精度缩小到6位小数后,围栏数据
POLYGON ((114.075277 32.128244, 114.075277 32.129143, 114.075277 32.130043, 114.075277 32.130942, 114.076176 32.130942, 114.076176 32.131842, 114.076176 32.132741, 114.076176 32.13364, 114.076176 32.133748, 114.076359 32.13364, 114.077075 32.133218, 114.077885 32.132741, 114.077975 32.132688, 114.078874 32.132158, 114.079412 32.131842, 114.079594 32.131734, 114.079773 32.131504, 114.080211 32.130942, 114.080673 32.130349, 114.080911 32.130043, 114.081237 32.129624, 114.081572 32.129358, 114.081843 32.129143, 114.082471 32.128645, 114.082976 32.128244, 114.083371 32.127931, 114.08411 32.127345, 114.08427 32.127218, 114.084858 32.126751, 114.085169 32.126487, 114.085218 32.126446, 114.086069 32.125723, 114.086277 32.125546, 114.086968 32.12496, 114.087337 32.124647, 114.087867 32.124196, 114.088396 32.123748, 114.088767 32.123433, 114.089455 32.122848, 114.089666 32.122669, 114.090108 32.122294, 114.090492 32.121949, 114.090565 32.121883, 114.090855 32.121622, 114.091465 32.121257, 114.091465 32.121256, 114.091812 32.121048, 114.092362 32.12072, 114.093262 32.120178, 114.093315 32.120152, 114.094162 32.11964, 114.094818 32.119251, 114.095062 32.119102, 114.095963 32.118565, 114.096314 32.118351, 114.096764 32.118084, 114.096863 32.118042, 114.097763 32.117668, 114.098267 32.117451, 114.098656 32.117291, 114.099556 32.116913, 114.100411 32.116554, 114.100456 32.116535, 114.101357 32.116158, 114.102257 32.11578, 114.102554 32.115654, 114.102257 32.115654, 114.101357 32.115654, 114.100456 32.115654, 114.099556 32.115654, 114.099556 32.116554, 114.098656 32.116554, 114.098656 32.115654, 114.097763 32.115654, 114.096863 32.115654, 114.096863 32.114754, 114.097763 32.114754, 114.098656 32.114754, 114.099556 32.114754, 114.099556 32.113853, 114.100456 32.113853, 114.100456 32.112957, 114.101357 32.112957, 114.101357 32.112057, 114.102257 32.112057, 114.102257 32.111157, 114.103157 32.111157, 114.103157 32.110256, 114.104057 32.110256, 114.104057 32.10936, 114.103157 32.10936, 114.103157 32.10846, 114.104057 32.10846, 114.104057 32.107559, 114.103157 32.107559, 114.103157 32.106659, 114.104057 32.106659, 114.104958 32.106659, 114.104958 32.105762, 114.104958 32.104862, 114.10585 32.104862, 114.10585 32.103962, 114.106751 32.103962, 114.106751 32.103062, 114.107651 32.103062, 114.108551 32.103062, 114.108551 32.102165, 114.109451 32.102165, 114.110352 32.102165, 114.110352 32.101265, 114.110352 32.100365, 114.110352 32.099464, 114.110352 32.098568, 114.110352 32.097668, 114.111252 32.097668, 114.111252 32.096767, 114.110352 32.096767, 114.109451 32.096767, 114.108551 32.096767, 114.108551 32.095867, 114.108551 32.094971, 114.108551 32.09407, 114.107651 32.09407, 114.107651 32.094971, 114.106751 32.094971, 114.106751 32.095867, 114.10585 32.095867, 114.10585 32.094971, 114.104958 32.094971, 114.104057 32.094971, 114.104057 32.09407, 114.103157 32.09407, 114.102257 32.09407, 114.102257 32.094971, 114.102257 32.095867, 114.102257 32.096767, 114.101357 32.096767, 114.101357 32.097668, 114.100456 32.097668, 114.100456 32.098568, 114.099556 32.098568, 114.099556 32.099464, 114.098656 32.099464, 114.098656 32.100365, 114.097763 32.100365, 114.097763 32.101265, 114.097763 32.102165, 114.098656 32.102165, 114.098656 32.103062, 114.099556 32.103062, 114.100456 32.103062, 114.100456 32.103962, 114.099556 32.103962, 114.099556 32.104862, 114.100456 32.104862, 114.100456 32.105762, 114.100456 32.106659, 114.100456 32.107559, 114.099556 32.107559, 114.099556 32.106659, 114.098656 32.106659, 114.098656 32.107559, 114.097763 32.107559, 114.097763 32.10846, 114.096863 32.10846, 114.096863 32.10936, 114.095963 32.10936, 114.095062 32.10936, 114.095062 32.110256, 114.094162 32.110256, 114.094162 32.111157, 114.093262 32.111157, 114.093262 32.110256, 114.092362 32.110256, 114.092362 32.111157, 114.091461 32.111157, 114.091461 32.112057, 114.090569 32.112057, 114.090569 32.112957, 114.090569 32.113853, 114.090569 32.114754, 114.090569 32.115654, 114.090569 32.116554, 114.090569 32.117451, 114.090569 32.118351, 114.090569 32.119251, 114.090569 32.120152, 114.090569 32.121048, 114.091461 32.121048, 114.091461 32.12105, 114.090565 32.12105, 114.090565 32.12015, 114.090565 32.119251, 114.090565 32.118352, 114.090565 32.117452, 114.089666 32.117452, 114.089666 32.118352, 114.089666 32.119251, 114.088767 32.119251, 114.088767 32.12015, 114.088767 32.12105, 114.087867 32.12105, 114.087867 32.121949, 114.087867 32.122848, 114.087867 32.123748, 114.086968 32.123748, 114.086069 32.123748, 114.086069 32.124647, 114.085169 32.124647, 114.08427 32.124647, 114.083371 32.124647, 114.083371 32.125546, 114.082471 32.125546, 114.081572 32.125546, 114.080673 32.125546, 114.080673 32.126446, 114.079773 32.126446, 114.078874 32.126446, 114.078874 32.127345, 114.077975 32.127345, 114.077075 32.127345, 114.076176 32.127345, 114.076176 32.128244, 114.075277 32.128244))