java中使用mongo的地理信息支持

144 阅读2分钟

java中使用mongo的地理信息支持

使用的依赖

直接使用的spring-boot提供的mongodb的starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

映射数据的实体类

这个如果直接使用依赖中提供的 Point Polygon等类,会出现返回数据重复问题

所以需要自己写一个符合业务又符合mongo查询需求的实体类映射数据

点类型的实体类:

public class PointData extends EntityView {
    private Long data_id;
    private String data_type;
    private LocationData point;

    public Long getData_id() {
        return data_id;
    }

    public void setData_id(Long data_id) {
        this.data_id = data_id;
    }

    public String getData_type() {
        return data_type;
    }

    public void setData_type(String data_type) {
        this.data_type = data_type;
    }

    public LocationData getPoint() {
        return point;
    }

    public void setPoint(LocationData point) {
        this.point = point;
    }
}

子类:

public class LocationData{
    private String type;
    private List<Double> coordinates;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Double> getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(List<Double> coordinates) {
        this.coordinates = coordinates;
    }
}

多边形类型的实体类:

public class PolygonData extends EntityView {
    private Long data_id;
    private String data_type;
    private LocationListData polygon;

    public Long getData_id() {
        return data_id;
    }

    public void setData_id(Long data_id) {
        this.data_id = data_id;
    }

    public String getData_type() {
        return data_type;
    }

    public void setData_type(String data_type) {
        this.data_type = data_type;
    }

    public LocationListData getPolygon() {
        return polygon;
    }

    public void setPolygon(LocationListData polygon) {
        this.polygon = polygon;
    }
}

子类:

public class LocationListData {
    private String type;
    private List<List<List<Double>>> coordinates;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<List<List<Double>>> getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(List<List<List<Double>>> coordinates) {
        this.coordinates = coordinates;
    }
}

因为mongo查询点是否在多边形内时,入参必须是:[[[],[],],[]]

需要包三次数组,要不然查询条件会报错,

这样写就可以使用id获取多边形点位数据,然后入参

相关的写入,查询方法

注入mongo

@Autowired
private MongoTemplate mongoTemplate;

写入点数据:

@PostMapping("insertpointdata")
public ReturnForm insertPointData(@RequestBody JSONObject res) {
    String data_type = res.getString("data_type");
    List<Double> point = res.getObject("point", List.class);
    String msg = GlobalDefine.SYS_OPERATESUCCESS_MESSAGE;
    PointData pointData = new PointData();
    Long dataId = SFlakeUtils.getUUID();
    pointData.setData_id(dataId);
    pointData.setData_type(data_type);
    LocationData locationData = new LocationData();
    locationData.setType("Point");
    locationData.setCoordinates(point);
    pointData.setPoint(locationData);
    mongoTemplate.insert(pointData,"point_data");
    return new ReturnForm(msg);
}

请求:

###
POST http://localhost:10086/log/insertpointdata
Content-Type: application/json

{
  "data_type": "bjxx",
  "point": [116.407148,39.938209]
}

写入多边形:

@PostMapping("insertpolygon")
public ReturnForm insertPolygon(@RequestBody JSONObject res) {
    String msg = GlobalDefine.SYS_OPERATESUCCESS_MESSAGE;
    String data_type = res.getString("data_type");
    List<List<Double>> point = res.getObject("points", List.class);
    List<List<List<Double>>> coordinates = new ArrayList<>();
    coordinates.add(point);
    LocationListData locationListData = new LocationListData();
    locationListData.setType("Polygon");
    locationListData.setCoordinates(coordinates);
    PolygonData polygonData = new PolygonData();
    polygonData.setData_id(SFlakeUtils.getUUID());
    polygonData.setData_type(data_type);
    polygonData.setPolygon(locationListData);
    mongoTemplate.insert(polygonData,"polygon_data");
    return new ReturnForm(msg);
}

请求:

###
POST http://localhost:10086/log/insertpolygon
Content-Type: application/json

{
  "data_type": "wgxx",
  "points": [
    [
      116.396373,
      39.940489
    ],
    [
      116.408346,
      39.940769
    ],
    [
      116.408668,
      39.93353
    ],
    [
      116.39648,
      39.933595
    ],
    [
      116.396373,
      39.940489
    ]
  ]
}

查询点是否在多边形内:

@PostMapping("getpointlistinpolygon")
public ReturnForm getPointListInPolygon(@RequestBody JSONObject req) {
    Long data_id = req.getLong("data_id");
    String data_type = req.getString("data_type");
    Query query = new Query();
    if (NumberUtils.isLong(data_id)) {
        query.addCriteria(Criteria.where("data_id").is(data_id));
    }
    String msg = GlobalDefine.SYS_OPERATESUCCESS_MESSAGE;
    ReturnForm returnForm = new ReturnForm();
    ArrayList point_data = logQueryDAO.getArrayList(query, PolygonData.class, "polygon_data");
    PolygonData polygonData = (PolygonData) point_data.get(0);
    BasicDBList dbList = new BasicDBList();
    if (StringUtils.isNotBlank(data_type)) {
        dbList.add(new BasicDBObject(
            "data_type",data_type));
    }
    dbList.add(new BasicDBObject("point", new BasicDBObject(
        "$geoWithin", new BasicDBObject(
            "$geometry", new BasicDBObject(
                "type", polygonData.getPolygon().getType()).append(
                "coordinates", polygonData.getPolygon().getCoordinates())))));
    BasicDBObject basicDBObject = new BasicDBObject(
        "$and",dbList);
    BasicQuery basicQuery = new BasicQuery(basicDBObject.toString());
    List<PointData> dataList = mongoTemplate.find(basicQuery, PointData.class, "point_data");
    if (ListUtils.isNotBlank(dataList)) {
        returnForm.setData(dataList);
    }
    returnForm.setMessage(msg);
    return returnForm;
}

请求:

###
POST http://localhost:10086/log/getpointlistinpolygon
Content-Type: application/json

{
  "data_id": "605385126969610240",
  "data_type": "bjxx"
}

mongo中的数据示例

point:

// 1
{
    "_id": ObjectId("60d3113006da9150c6f4d317"),
    "data_id": NumberLong("605149007128563712"),
    "data_type": "jgxx",
    "point": {
        "type": "Point",
        "coordinates": [
            116.401523,
            39.939305
        ]
    },
    "_class": "com.shizc.log.pojo.PointData"
}

// 2
{
    "_id": ObjectId("60d312b42d7d3c139a028e4d"),
    "data_id": NumberLong("605150635990716416"),
    "data_type": "jgxx",
    "point": {
        "type": "Point",
        "coordinates": [
            116.396373,
            39.940489
        ]
    },
    "_class": "com.shizc.log.pojo.PointData"
}

多边形:

// 1
{
    "_id": ObjectId("60d3ed17607d5a649bb5f474"),
    "data_id": NumberLong("605385126969610240"),
    "data_type": "wgxx",
    "polygon": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    116.401866,
                    39.940851
                ],
                [
                    116.408346,
                    39.940769
                ],
                [
                    116.408668,
                    39.93353
                ],
                [
                    116.402124,
                    39.93348
                ],
                [
                    116.401866,
                    39.940851
                ]
            ]
        ]
    },
    "_class": "com.shizc.log.pojo.PolygonData"
}

// 2
{
    "_id": ObjectId("60d3ed50607d5a649bb5f475"),
    "data_id": NumberLong("605385368188227584"),
    "data_type": "wgxx",
    "polygon": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    116.396373,
                    39.940489
                ],
                [
                    116.408346,
                    39.940769
                ],
                [
                    116.408668,
                    39.93353
                ],
                [
                    116.39648,
                    39.933595
                ],
                [
                    116.396373,
                    39.940489
                ]
            ]
        ]
    },
    "_class": "com.shizc.log.pojo.PolygonData"
}