springboot + mongodb 通過經(jīng)緯度坐標(biāo)匹配平面區(qū)域的方法
java api 自帶的mongodb實體無法滿足環(huán)狀多邊形的區(qū)域匹配(大概是我沒用對方法可能)所以我們要自定義一個空間坐標(biāo)類型
廢話不多說 上代碼
/** * * @author cy */ @Configuration @ReadingConverter public class CustomReadGeoJsonConverter implements Converter<Document, CustomGeoJson> { @Override public CustomGeoJson convert(Document document) { CustomGeoJson geoJson = new CustomGeoJson(); geoJson.setType(document.get(GeoJsonConstant.TYPE, String.class)); geoJson.setCoordinates(document.get(GeoJsonConstant.COORDINATES, Iterable.class)); return geoJson; } }
@Configuration public class Config { @Autowired private CustomReadGeoJsonConverter customReadGeoJsonConverter; @Bean public MongoCustomConversions customConversions() { List<Converter<?, ?>> converterList = new ArrayList<>(); converterList.add(customReadGeoJsonConverter); return new MongoCustomConversions(converterList); } }
自定義的空間坐標(biāo)類型插入實體
其中的coordinates 可自定義插入point
/** * @author cy */ @Data public class CustomGeoJson implements GeoJson, Serializable { private String type; private Iterable<?> coordinates; }
在我們定義的mongodb實體中加入我們自定義的類型
/** * @author cy * @since 2021-10-20 */ @Data @Document(collection = "demo_mdb") public class DemoMdb implements Serializable { private String id; @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) private CustomGeoJson customGeoJson; }
插入數(shù)據(jù)
public void saveData() { //這里自定義point點集合(這里不固定格式參照mongdb官方文檔) List<List<Point>> pointList = new ArrayList<>(); DemoMdb db=new DemoMdb(); //自行查看需要的類型 db.setType("***"); db.setCoordinates(pointList); //mongoTemplate自行引入不做贅述 mongoTemplate.insert(db, DemoMdb .class); }
查詢數(shù)據(jù)
/** ** 經(jīng)度x緯度y **/ public List<DemoMdb> findData(String x, String y) { Query query = new Query(Criteria.where("customGeoJson"). intersects(new GeoJsonPoint(Double.valueOf(x), Double.valueOf(y)))); List<DemoMdb> dbList = mongoTemplate.find(query, DemoMdb.class); return dbList; }
只是一種方法,還不完美歡迎評論指教
到此這篇關(guān)于springboot + mongodb 通過經(jīng)緯度坐標(biāo)匹配平面區(qū)域的方法的文章就介紹到這了,更多相關(guān)springboot mongodb 經(jīng)緯度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Window環(huán)境下配置Mongodb數(shù)據(jù)庫
這篇文章介紹了Window環(huán)境下配置Mongodb數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07MongoDB使用小結(jié):一些不常見的經(jīng)驗分享
最近一年忙碌于數(shù)據(jù)處理相關(guān)的工作,跟MongoDB打交道極多,以下為實踐過程中的Q&A,后續(xù)會不定期更新補充2017-03-03mongoDB 多重數(shù)組查詢(AngularJS綁定顯示 nodejs)
這篇文章主要介紹了mongoDB 多重數(shù)組查詢(AngularJS綁定顯示 nodejs),需要的朋友可以參考下2017-06-06Mongodb?刪除文檔Delete與Remove的區(qū)別解析
這篇文章主要介紹了Mongodb?刪除文檔Delete與Remove的區(qū)別,要從集合中刪除所有文檔,請將空過濾器文檔傳遞{}給該?db.collection.deleteMany()方法,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08Windows系統(tǒng)下安裝MongoDB并內(nèi)網(wǎng)穿透遠(yuǎn)程連接
這篇文章主要給大家介紹了關(guān)于Windows系統(tǒng)下安裝MongoDB并內(nèi)網(wǎng)穿透遠(yuǎn)程連接的相關(guān)資料,文中通過圖文將步驟介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MongoDB具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03mongodb權(quán)限設(shè)置之添加管理員、普通用戶的方法
這篇文章主要介紹了mongodb添加管理員、普通用戶的方法,同時介紹了mongodb開啟權(quán)限認(rèn)證后PHP客戶端的兩種連接方法,需要的朋友可以參考下2014-06-06