mongodb存储geoJson
文章源自JAVA秀-https://www.javaxiu.com/19231.html beanprocesser文章源自JAVA秀-https://www.javaxiu.com/19231.html 简书作者文章源自JAVA秀-https://www.javaxiu.com/19231.html
2020-01-02 11:14打开App文章源自JAVA秀-https://www.javaxiu.com/19231.html
在业务需求中,需要对地理坐标进行存储,mongodb是一种很好的选择,因为它提供了许多关于地理位置的方法,但是它提供的GeoJson却是一个接口,当然也提供了对应的点、线、面的实现类,不过在业务需求中,往往需要前端传一个GeoJson数据格式,后端做一个点、线、面的适配,已达到简单使用,简单存储的功能;文章源自JAVA秀-https://www.javaxiu.com/19231.html
基于上面的场景,写了这篇文章,下面就贴代码了(关于maven及mongdb的配置下面就不贴了,只贴核心代码)文章源自JAVA秀-https://www.javaxiu.com/19231.html
1.首先自定义一个GeoJson类文章源自JAVA秀-https://www.javaxiu.com/19231.html
@Data public class CustomGeoJson implements GeoJson { private String type; private Iterable<?> coordinates; }
2.添加mongdb的convert转换器,读写用的 1)CustomReadGeoJsonConverter文章源自JAVA秀-https://www.javaxiu.com/19231.html
@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; } }
2)CustomWriteGeoJsonConverter文章源自JAVA秀-https://www.javaxiu.com/19231.html
@WritingConverter public class CustomWriteGeoJsonConverter implements Converter<CustomGeoJson, Document> { @Override public Document convert(CustomGeoJson geoJson) { Document document = new Document(); document.put(GeoJsonConstant.TYPE, geoJson.getType()); document.put(GeoJsonConstant.COORDINATES, geoJson.getCoordinates()); return document; } }
3.geoJson数据的键名称文章源自JAVA秀-https://www.javaxiu.com/19231.html
public final class GeoJsonConstant { /** * type(类型) */ public static final String TYPE = "type"; /** * coordinates(坐标位置) */ public static final String COORDINATES = "coordinates"; }
4.geoJson的type文章源自JAVA秀-https://www.javaxiu.com/19231.html
public final class GeoJsonTypeConstant { /** * 点 */ public static final String POINT = "Point"; /** * 线 */ public static final String LINESTRING = "LineString"; /** * 面 */ public static final String POLYGON = "Polygon"; }
- 实体类
@Data @Document(collection = "map_data") public class MapData { @Id private String id; private CustomGeoJson geometry; }
6.TestController文章源自JAVA秀-https://www.javaxiu.com/19231.html
@RestController @Api public class TestController { @Autowired private MongoTemplate mongoTemplate; @PostMapping("test") public MapData test(@RequestBody MapData mapData){ return mongoTemplate.insert(mapData); } }
7.测试效果图 swagger入参文章源自JAVA秀-https://www.javaxiu.com/19231.html
企业微信截图_08821638-8255-4b6f-92aa-3a85efa2c2c9.png文章源自JAVA秀-https://www.javaxiu.com/19231.html
8.mongdb 存储数据的值文章源自JAVA秀-https://www.javaxiu.com/19231.html
企业微信截图_0ee06576-194f-4ad1-bf60-cd320312afb0.png文章源自JAVA秀-https://www.javaxiu.com/19231.html
以上就是GeoJson存储地理位置的实现;文章源自JAVA秀-https://www.javaxiu.com/19231.html
有疑问或者有更好的实现方式可以讨论,本文属于原创,转载需指明出处文章源自JAVA秀-https://www.javaxiu.com/19231.html
© 著作权归作者所有,转载或内容合作请联系作者 文章源自JAVA秀-https://www.javaxiu.com/19231.html

评论