欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot + mongodb 通過經(jīng)緯度坐標(biāo)匹配平面區(qū)域的方法

 更新時(shí)間:2021年10月29日 10:57:42   作者:失策狗  
這篇文章主要介紹了springboot + mongodb 通過經(jīng)緯度坐標(biāo)匹配平面區(qū)域的方法,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

java api 自帶的mongodb實(shí)體無法滿足環(huán)狀多邊形的區(qū)域匹配(大概是我沒用對(duì)方法可能)所以我們要自定義一個(gè)空間坐標(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)類型插入實(shí)體
其中的coordinates 可自定義插入point

/**
 * @author cy
 */
@Data
public class CustomGeoJson implements GeoJson, Serializable {
    private String type;

    private Iterable<?> coordinates;

}

在我們定義的mongodb實(shí)體中加入我們自定義的類型

/**
 * @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點(diǎn)集合(這里不固定格式參照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;
}

只是一種方法,還不完美歡迎評(píng)論指教

到此這篇關(guān)于springboot + mongodb 通過經(jīng)緯度坐標(biāo)匹配平面區(qū)域的方法的文章就介紹到這了,更多相關(guān)springboot mongodb 經(jīng)緯度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論