package com.xiaopeng.map.mangodb.service.impl;
|
|
import com.mongodb.client.FindIterable;
|
import com.mongodb.client.MongoCollection;
|
import com.mongodb.client.model.Filters;
|
import com.xiaopeng.api.model.map.XpGeoFencingPolygon;
|
import com.xiaopeng.map.mangodb.model.GeoPoint;
|
import com.xiaopeng.map.mangodb.service.XpGeoFencingPolygonService;
|
import com.xiaopeng.map.utils.mangoUtils.MongoGeoFenceUtil;
|
import org.bson.Document;
|
import org.bson.conversions.Bson;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
|
@Service
|
public class XpGeoFencingPolygonServiceImpl implements XpGeoFencingPolygonService {
|
|
@Autowired
|
private MongoTemplate mongoTemplate;
|
|
@Override
|
public int create(XpGeoFencingPolygon xpGeoFencingPolygon) {
|
mongoTemplate.insert(xpGeoFencingPolygon);
|
return 1;
|
}
|
|
@Override
|
public XpGeoFencingPolygon findById(String id) {
|
return mongoTemplate.findById(id, XpGeoFencingPolygon.class);
|
}
|
|
@Override
|
public List<XpGeoFencingPolygon> findAll() {
|
return mongoTemplate.findAll(XpGeoFencingPolygon.class);
|
}
|
|
|
|
/**
|
* 方法3:查找包含指定点的所有围栏(支持自定义地理字段名称)
|
*/
|
public List<String> findFencesContainingPoint(GeoPoint point, String collectionName, String geoFieldName) {
|
MongoCollection<Document> collection = mongoTemplate.getCollection(collectionName);
|
List<String> fenceIds = new ArrayList<>();
|
// 查询所有包含该点的围栏
|
Bson filter = Filters.geoIntersects(geoFieldName, MongoGeoFenceUtil.toGeoJsonPoint(point.getLongitude(), point.getLatitude()));
|
FindIterable<Document> documents = collection.find(filter);
|
for (Document document : documents) {
|
fenceIds.add(document.getString("fenceId"));
|
}
|
|
return fenceIds;
|
}
|
|
|
/**
|
* 查找指定父围栏ID的禁行区域点位
|
* @param parentId 父围栏ID
|
* @param parentIdField 父围栏ID字段名
|
* @param collectionName 集合名称
|
* @return 禁行区域点位列表
|
*/
|
public List<Document> findPointsByParentId(String parentId, String parentIdField, String collectionName) {
|
MongoCollection<Document> collection = mongoTemplate.getCollection(collectionName);
|
List<Document> points = new ArrayList<>();
|
|
// 查询父围栏ID等于指定值的所有文档
|
Bson filter = Filters.eq(parentIdField, parentId);
|
|
collection.find(filter).forEach(doc -> {
|
points.add(doc);
|
});
|
|
return points;
|
}
|
|
|
|
/**
|
* 根据经纬度点位判断是否在指定的电子围栏中
|
* @param longitude 经度
|
* @param latitude 纬度
|
* @param fenceId 围栏ID
|
* @param collectionName 集合名称
|
* @return 是否在围栏中 true在 false不在
|
*/
|
public boolean isInFence(double longitude, double latitude, String fenceId, String collectionName) {
|
if (fenceId == null || fenceId.trim().isEmpty()) {
|
return false;
|
}
|
MongoCollection<Document> collection = mongoTemplate.getCollection(collectionName);
|
Bson fenceIdFilter = Filters.or(Filters.eq("fenceId", fenceId), Filters.eq("parentId", fenceId));
|
Bson geoFilter = Filters.geoIntersects("points", MongoGeoFenceUtil.toGeoJsonPoint(longitude, latitude));
|
Bson filter = Filters.and(fenceIdFilter, geoFilter);
|
return collection.find(filter).limit(1).first() != null;
|
}
|
|
|
}
|