|
|
@ -56,6 +56,12 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.geo.Point; |
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate; |
|
|
|
import org.springframework.data.mongodb.core.geo.GeoJsonPoint; |
|
|
|
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; |
|
|
|
import org.springframework.data.mongodb.core.query.Criteria; |
|
|
|
import org.springframework.data.mongodb.core.query.Query; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
@ -112,6 +118,9 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit |
|
|
|
@Autowired |
|
|
|
private HttpUtil httpUtil; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private MongoTemplate mongoTemplate; |
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SysDeptServiceImpl.class); |
|
|
|
|
|
|
|
@Override |
|
|
@ -1405,7 +1414,7 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit |
|
|
|
JSONArray jsonArray = JSON.parseArray(toResult.get("features").toString()); |
|
|
|
|
|
|
|
jsonArray.forEach(item -> { |
|
|
|
|
|
|
|
List<Point> points = new ArrayList<>(); |
|
|
|
JSONObject feature = JSON.parseObject(item.toString()); |
|
|
|
JSONObject attributes = JSON.parseObject(feature.get("attributes").toString()); |
|
|
|
String gridName = attributes.get("网格名称").toString(); |
|
|
@ -1426,7 +1435,10 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit |
|
|
|
entity.setGridId(gridId); |
|
|
|
|
|
|
|
list.add(entity); |
|
|
|
Point p = new Point(Double.parseDouble(longitude), Double.parseDouble(latitude)); |
|
|
|
points.add(p); |
|
|
|
}); |
|
|
|
saveCoordinateCache(gridName, gridId, points); |
|
|
|
}); |
|
|
|
|
|
|
|
if (!list.isEmpty()) { |
|
|
@ -1436,25 +1448,46 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 地理坐标放到mongodb |
|
|
|
* |
|
|
|
* @param gridName |
|
|
|
* @param gridId |
|
|
|
* @param points |
|
|
|
* @return void |
|
|
|
* @author zhy |
|
|
|
* @date 2022/12/16 17:31 |
|
|
|
*/ |
|
|
|
private void saveCoordinateCache(String gridName, String gridId, List<Point> points) { |
|
|
|
PolyDTO polyDTO = new PolyDTO(); |
|
|
|
polyDTO.setGridId(gridId); |
|
|
|
polyDTO.setGridName(gridName); |
|
|
|
GeoJsonPolygon geoJsonPolygon = new GeoJsonPolygon(points); |
|
|
|
polyDTO.setPolygons(geoJsonPolygon); |
|
|
|
// 我们将多个对象放到同一个容器polygons中,每个对象会生成自己的id,需要注意不要重复保存,否则会产生覆盖的区域
|
|
|
|
// 如果包含了主键,insert会提示主键冲突,而save会进行更新(设置主键需要使用@Id注解,否则自动生成)
|
|
|
|
// insert可以批量处理,效率高,save只能逐个处理
|
|
|
|
mongoTemplate.save(polyDTO, "polygons"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public GisGridDTO getCoordinate(GisFormDTO formDTO) { |
|
|
|
Map<String, String> urlParams = new HashMap<>(); |
|
|
|
String geometryStr = "point(" + formDTO.getLongitude() + "," + formDTO.getLatitude() + ")"; |
|
|
|
urlParams.put("geometry", geometryStr); |
|
|
|
|
|
|
|
String resultStr = httpUtil.getGisStr(urlParams); |
|
|
|
JSONObject toResult = JSON.parseObject(resultStr); |
|
|
|
JSONArray jsonArray = JSON.parseArray(toResult.get("features").toString()); |
|
|
|
Query query = new Query(Criteria.where("polygons"). |
|
|
|
intersects(new GeoJsonPoint(Double.parseDouble(formDTO.getLongitude()), Double.parseDouble(formDTO.getLatitude())))); |
|
|
|
// 如果带了collectionName,MongoTemplate就查询collectionName集合中的记录;
|
|
|
|
// 如果没带collectionName,MongoTemplate就会到缓存上下文mappingContext中根据classname获取集合名,
|
|
|
|
// 如果没有获取到,就会新建一个根据classname生成的集合名(首字母小写),这样就可能和mongo中的集合名不一致,导致查询不到结果。
|
|
|
|
List<PolyDTO> dbList = mongoTemplate.find(query, PolyDTO.class, "polygons"); |
|
|
|
|
|
|
|
if (jsonArray.size() == NumConstant.ZERO) { |
|
|
|
if (dbList.size() == NumConstant.ZERO) { |
|
|
|
throw new RenException("该坐标不在任何网格内,请重新选择"); |
|
|
|
} else if (jsonArray.size() > NumConstant.ONE) { |
|
|
|
} else if (dbList.size() > NumConstant.ONE) { |
|
|
|
throw new RenException("该坐标处于多个网格交界处,请重新选择"); |
|
|
|
} else { |
|
|
|
JSONObject feature = JSON.parseObject(jsonArray.get(0).toString()); |
|
|
|
JSONObject attributes = JSON.parseObject(feature.get("attributes").toString()); |
|
|
|
String gridName = attributes.get("网格名称").toString(); |
|
|
|
String gridId = attributes.get("网格编码").toString(); |
|
|
|
PolyDTO polyDTO = dbList.get(0); |
|
|
|
String gridName = polyDTO.getGridName(); |
|
|
|
String gridId = polyDTO.getGridId(); |
|
|
|
|
|
|
|
GisGridDTO gisGridDTO = gisGridService.getByGisGridId(gridId); |
|
|
|
if (gisGridDTO == null) { |
|
|
|