1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
public JsonResult findByPolygon(SpatialDTO spatialDTO) {
List<List<Double>> pointList = spatialDTO.getPointList();
SpecificFindDTO findDTO = spatialDTO.getFindDTO();
Pageable pageable = genericService.getPageable(findDTO);
Double leftLon = pointList.get(0).get(0);
Double rightLon = pointList.get(2).get(0);
Double bottomLat = pointList.get(0).get(1);
Double upperLat = pointList.get(2).get(1);
String curQueryField = findDTO.getCurQueryField();
if (curQueryField == null || curQueryField.equals("")){
curQueryField = "name";
}
List<MapClassification> mapClassifications = buildClassifications(findDTO.getMapCLSId());
List<MapItem> mapItemList = null;
List<GeoJsonPolygon> queryPolygon = getQueryPolygon(leftLon, rightLon, bottomLat, upperLat);
if (queryPolygon.size() == 1){
mapItemList = mapItemDao.findBySearchTextAndPolygonAndPageable(
curQueryField, findDTO.getSearchText(), queryPolygon.get(0), mapClassifications, pageable);
} else {
mapItemList = mapItemDao.findBySearchTextAndPolygonAndPageable(
curQueryField, findDTO.getSearchText(), new GeoJsonMultiPolygon(queryPolygon), mapClassifications, pageable);
}
return ResultUtils.success(mapItemList);
}
//得到查询的多边形范围
private List<GeoJsonPolygon> getQueryPolygon(double leftLon, double rightLon,double bottomLat,double upperLat){
List<GeoJsonPolygon> polygons = new ArrayList<>();
// 1.先把框选范围移到左边经度大于-180的情况
while (leftLon < -180){
leftLon += 360;
rightLon += 360;
}
// 2.如果右边经度此时小于180的话
// 接着判断经度跨度是否小于180,
// 如果小于的话直接通过GeoJsonPolygon查找
// 如果大于的话就把box从中间切开,通过GeoJsonMultiPolygon进行查找
if (rightLon < 180){
polygons.addAll(getQueryPolygon_standard(leftLon,rightLon,bottomLat,upperLat));
}
// 3.如果右边经度此时大于等于180的话
// 就要把 >=180 的范围从180°经线切开,形成两个box进行查找
// 分别是
// (leftLon, 179.9)
// (-179.9, rightLon-360)
// 由于这两个box肯定是符合第二个情况的,所以接下来这两个box分别进行第二步的讨论就行
else {
polygons.addAll(getQueryPolygon_standard(leftLon,179.9,bottomLat,upperLat));
polygons.addAll(getQueryPolygon_standard(-179.9,rightLon-360,bottomLat,upperLat));
}
return polygons;
}
//上面的第二个步骤是要多次调用的,单独抽出来
private List<GeoJsonPolygon> getQueryPolygon_standard(Double leftLon, Double rightLon,Double bottomLat,Double upperLat){
List<GeoJsonPolygon> polygons = new ArrayList<>();
if (rightLon - leftLon < 180){
polygons.add(new GeoJsonPolygon(
new Point(leftLon,bottomLat),new Point(leftLon,upperLat),
new Point(rightLon,upperLat),new Point(rightLon,bottomLat),
new Point(leftLon,bottomLat)));
}else {
double middleLon = (rightLon + leftLon) / 2;
polygons.add(new GeoJsonPolygon(new Point(leftLon,bottomLat),new Point(leftLon,upperLat),
new Point(middleLon,upperLat),new Point(middleLon,bottomLat),new Point(leftLon,bottomLat)));
polygons.add(new GeoJsonPolygon(
new Point(middleLon,bottomLat),new Point(middleLon,upperLat),
new Point(rightLon,upperLat),new Point(rightLon,bottomLat),new Point(middleLon,bottomLat)));
}
return polygons;
}
|