mongodb存储geoJson原创
2020-07-09 09:40:29 2点赞 文章源自JAVA秀-https://www.javaxiu.com/19237.html
文章源自JAVA秀-https://www.javaxiu.com/19237.html
文章源自JAVA秀-https://www.javaxiu.com/19237.html
码龄6年 文章源自JAVA秀-https://www.javaxiu.com/19237.html
关注文章源自JAVA秀-https://www.javaxiu.com/19237.html
在业务需求中,需要对地理坐标进行存储,mongodb是一种很好的选择,因为它提供了许多关于地理位置的方法,但是它提供的GeoJson却是一个接口,当然也提供了对应的点、线、面的实现类,不过在业务需求中,往往需要前端传一个GeoJson数据格式,后端做一个点、线、面的适配,已达到简单使用,简单存储的功能;文章源自JAVA秀-https://www.javaxiu.com/19237.html
基于上面的场景,写了这篇文章,下面就贴代码了(关于maven及mongdb的配置下面就不贴了,只贴核心代码)文章源自JAVA秀-https://www.javaxiu.com/19237.html
1.首先自定义一个GeoJson类文章源自JAVA秀-https://www.javaxiu.com/19237.html
@DatapublicclassCustomGeoJsonimplementsGeoJson, Serializable {privatestaticfinallong serialVersionUID =1;private String type;private Iterable<?> coordinates;}
2.添加mongdb的convert转换器,读写用的 1)CustomReadGeoJsonConverter文章源自JAVA秀-https://www.javaxiu.com/19237.html
@ConfigurationpublicclassCustomReadGeoJsonConverterimplementsConverter<Document, CustomGeoJson>{@Overridepublic CustomGeoJson convert(Document document){ CustomGeoJson geoJson =newCustomGeoJson(); 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/19237.html
@ConfigurationpublicclassCustomWriteGeoJsonConverterimplementsConverter<CustomGeoJson, Document>{@Overridepublic Document convert(CustomGeoJson geoJson){ Document document =newDocument(); document.put(GeoJsonConstant.TYPE, geoJson.getType()); document.put(GeoJsonConstant.COORDINATES, geoJson.getCoordinates());return document;}}
3.geoJson数据的键名称文章源自JAVA秀-https://www.javaxiu.com/19237.html
publicfinalclassGeoJsonConstant{/** * type(类型) */publicstaticfinal String TYPE ="type";/** * coordinates(坐标位置) */publicstaticfinal String COORDINATES ="coordinates";}
4.geoJson的type文章源自JAVA秀-https://www.javaxiu.com/19237.html
publicfinalclassGeoJsonTypeConstant{/** * 点 */publicstaticfinal String POINT ="Point";/** * 线 */publicstaticfinal String LINESTRING ="LineString";/** * 面 */publicstaticfinal String POLYGON ="Polygon";}
@Data@Document(collection ="map_data")publicclassMapData{@Idprivate String id;private CustomGeoJson geometry;}
6.Config文章源自JAVA秀-https://www.javaxiu.com/19237.html
@ConfigurationpublicclassConfig{@Autowiredprivate CustomReadGeoJsonConverter customReadGeoJsonConverter;@Autowiredprivate CustomWriteGeoJsonConverter customWriteGeoJsonConverter;@Beanpublic MongoCustomConversions customConversions(){ List<Converter<?,?>> converterList =newArrayList<>(); converterList.add(customReadGeoJsonConverter); converterList.add(customWriteGeoJsonConverter);returnnewMongoCustomConversions(converterList);}}
7.TestController文章源自JAVA秀-https://www.javaxiu.com/19237.html
@RestController@ApipublicclassTestController{@Autowiredprivate MongoTemplate mongoTemplate;@PostMapping("test")public MapData test(@RequestBody MapData mapData){return mongoTemplate.insert(mapData);}}
8.测试效果图 swagger入参文章源自JAVA秀-https://www.javaxiu.com/19237.html
9.mongdb 存储数据的值文章源自JAVA秀-https://www.javaxiu.com/19237.html
以上就是GeoJson存储地理位置的实现;文章源自JAVA秀-https://www.javaxiu.com/19237.html
有疑问或者有更好的实现方式可以讨论,本文属于原创,转载需指明出处 QQ交流群: 132312549文章源自JAVA秀-https://www.javaxiu.com/19237.html
打开CSDN,阅读体验更佳文章源自JAVA秀-https://www.javaxiu.com/19237.html
将点,线,面数据集里面的几何对象转换成GeoJson格式存储在记事本(.txt)中。 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
MongoDB 位置信息存储及查询,批量插入或更新位置信息,查询位置点是否在指定区域内(圆形区域、矩形区域、多边形区域)。 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
相关推荐文章源自JAVA秀-https://www.javaxiu.com/19237.html
语法: $ mongoimport -d TheBaseName -c TheCollectionName --file xxx.jsonNote 1. json文件,每条数据间不需要逗号,隔开,以确保mongodb把它当作一条记录。 2. 路径可写成: - D:/xx.json - D:\\xx.json 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
在业务需求中,需要对地理坐标进行存储,mongodb是一种很好的选择,因为它提供了许多关于地理位置的方法,但是它提供的GeoJson却是一个接口,当然也提供了对应的点、线、面的实现类,不过在业务需求中,往往需要前端传一个GeoJson数据格式,后端做一个点、线、面的适配,已达到简单使用,简单存储的功能;基于上面的场景,写了这篇文章,下面就贴代码了(关于maven及mongdb的配置下面就不贴了,只... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
mongodb 3.x存储二进制数据并不是以base64的方式,虽然在mongo客户端的查询结果以base64方式显示,请放心使用。下面来分析存储文件的存储内容。base64编码数据会增长1/3成为顾虑。 首先看mongo的c-driver对Binary类型数据的相关定义。注意下面提到了JS,要清楚官方提供给我们的mongo-client是使用JS语言的。 下面我将会存储一张图片,图... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
随着2.3> MongoDB对位置数据处理和查询变得更加有用。 MongoDB将文档存储为BSON,因此每个文档都具有所有文档字段,这显然可能导致比传统RMDBS更大的数据库。我曾经将折线和多边形存储为一系列索引点,另外一个字段表示每一行的顺序(我正在这样做,以确保一致性,因为我使用JavaScript,所以点并不总是以正确的顺序存储)。这是这样的:polyline: {[point: [0... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
我正在使用GeoJSON存储我想要稍后通过邻近查询的位置坐标,我的架构如下所示:'use strict';var mongoose = require('mongoose'),Schema = mongoose.Schema;var BranchSchema = new Schema({parentId: {type: Schema.Types.ObjectId,required: 'The ID... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
目录 1、存入地理数据 GeoJSON数据存入 1、Ponit 点数据 2、LineString 线数据(多段线) 3、 Polygon 多边形数据 4、MultiPoint多点、MultiLineString多线、MultiPolygon多多边形 5、GeometryCollection 几何集合 6、全国区县行政区划入库示例 2、创建地理索引 2.1、2dsphere索引 2.2、2d索... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
地理空间数据 在 mongodb 中, 可以将地理空间数据存储为 geojson 对象或旧坐标对。 geojson 对象 要在类似地球的球体上计算几何图形, 请将位置数据存储为 geojson 对象。 若要指定 geojson 数据, 请使用嵌入文档: 一个名为类型的字段, 指定 geojson 对象类型和 一个名为坐标的字段, 指定对象的坐标。 如果指定纬度和经度坐标, 请先列出经度... 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html
最近进行人脸识别,要在mongodb储存人脸照片,是个numpy数组,普通的储存是不行的,要用到bson和pickle库: 首先,获得了一个编码了人脸信息的numpy数组 my_nparray,然后我要将其存入mongodb中,这里我用的是pymongo让python与mongodb交互,储存代码如下: ''' from bson.binary import Binary import 浏览器打开文章源自JAVA秀-https://www.javaxiu.com/19237.html

评论