liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
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
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
    }
 
 
}