本文主要是介绍mongodb GIS做电子围栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路
- mongodb本身适合存地理数据,自带了函数
- 先将电子围栏存入mongodb中
- 每次点位是否在电子围栏外时向mongodb发送空间查询
资料:
GeoJson数据详见:http://geojson.io/
SpringBoot+mongodb实现代码:
/*** 电子围栏*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "geographicalRegion")
public class GeographicalRegion {@Idprivate String id;/*** 车辆唯一编码*/private String bikeCode;/*** 电子围栏名称*/private String name;/*** 点位*/private GeoJsonPolygon geometry;/*** 创建时间*/private String createDate;}
service:
public boolean findIntersectiveTest(String truckCode, double[] point) throws IOException {GeoJsonPoint geoJson=new GeoJsonPoint(point[0],point[1]);Set<String> truckCodes=new HashSet<String>();truckCodes.add(truckCode);return this.isInRegion(geoJson, truckCodes);}//查找目标区域是否在指定的几个区域内,locations的set中保存指定的区域id//有时数据库中保存的geojson地理信息数据集比较大,整个对象返回往往会消耗很大的io和带宽,所以我们在这里指定仅返回区域的_id,并且只要判断 目标geojson是指定的一个区域的子集就返回结果,降低数据库的处理开销public boolean isInRegion(GeoJsonPoint point, Set locations){Criteria geoCriteria=Criteria.where("geometry").intersects(point);\Criteria[] orArr=new Criteria[locations.size()];Iterator<Integer> iterator=locations.iterator();int i=0;while(iterator.hasNext()){orArr[i]=new Criteria("truckCode").is(iterator.next());i++;}Criteria orCriteria=new Criteria().orOperator(orArr);Criteria andCriteria=new Criteria().andOperator(geoCriteria,orCriteria);DBObject field=new BasicDBObject();field.put("id",true);Query query=new BasicQuery(new BasicDBObject().toString(), field.toString());query.addCriteria(andCriteria);GeographicalRegion region=mongoOperations.findOne(query, GeographicalRegion.class);if(region==null){return false;}return true;}
controller:
@RequestMapping("/check")public R check(String pointJson, String truckCode){String[] points = pointJson.split(",");double[] point = {Double.valueOf(points[0]), Double.valueOf(points[1])};
// double[] point = {1.0, 2.0};boolean check = false;try {check = geographicalRegionService.findIntersectiveTest(truckCode, point);} catch (IOException e) {e.printStackTrace();}return R.ok().put("status", check);}
这篇关于mongodb GIS做电子围栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!