实体类中有Point、Polygon数据该如何处理?
第一步:实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper=false)
public class Site implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(name = "point_address")
private Point pointAddress;
@Column(name = "area")
private Polygon area;
}
第二步:将数据返回给前端展示的时候不能直接返回,需要转换成geojson格式
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.youcon.riverbackend.pojo.po.Site;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.geojson.GeoJsonWriter;
@ApiModel("站点信息VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SiteVo {
private Long id;
@ApiModelProperty(value = "点")
private String pointAddressJson;
@ApiModelProperty(value = "面")
private String areaJson;
public static SiteVo of(Site site) throws JsonProcessingException {
if(site == null){
return null;
}
SiteVo siteVo = new SiteVo();
BeanUtil.copyProperties(site, siteVo);
siteVo.setPointAddressJson(pointToGeoJson(site.getPointAddress()));
siteVo.setAreaJson(polygonToGeoJson(site.getArea()));
return siteVo;
}
public static String pointToGeoJson(Point point) {
// 创建一个 GeoJSON 对象并设置它的类型为 Point
JSONObject geoJsonObject = new JSONObject();
geoJsonObject.put("type", "Point");
// 按顺序键入坐标值并在 GeoJSON 对象中设置几何体
JSONArray coordinatesArray = new JSONArray();
coordinatesArray.add(point.getX());
coordinatesArray.add(point.getY());
geoJsonObject.put("coordinates", coordinatesArray);
// 将 GeoJSON 格式化为字符串并返回它
return geoJsonObject.toJSONString();
}
public static String polygonToGeoJson(Polygon polygon) {
GeoJsonWriter geoJsonWriter = new GeoJsonWriter();
String geoJson = geoJsonWriter.write(polygon);
return geoJson;
}
}
第三步:这种数据该如何写入数据库,先写一个工具类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.*;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class GeoJsonParser {
private final ObjectMapper objectMapper = new ObjectMapper();
public Geometry parseGeoJSON(String geoJson) throws Exception {
return objectMapper.readValue(geoJson, Geometry.class);
}
/**
* geoJson 转 Polygon
* @param geoJson
* @return
* @throws Exception
*/
public Polygon parsePolygon(String geoJson) throws Exception {
GeometryJSON gJson = new GeometryJSON();
return (Polygon) gJson.read(geoJson);
}
/**
* geoJson 转Point
* @param geoJson
* @return
* @throws JsonProcessingException
*/
public Point parsePoint(String geoJson) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(geoJson, Map.class);
GeometryFactory geometryFactory = new GeometryFactory();
Coordinate coordinate = new Coordinate(((List<Double>)map.get("coordinate")).get(0), ((List<Double>)map.get("coordinate")).get(1));
return geometryFactory.createPoint(coordinate);
}
}
第四步:写入数据库,前端给的数据应该string类型的geojson字符串
@ApiOperation(value = "保存站点信息")
@PostMapping
public SiteVo save(@RequestBody @Validated SiteForm siteForm) throws Exception {
//String geoJson = "{ \"type\": \"Point\", \"coordinate\": [116.381541, 39.922501] }";
Point point = geoJsonParser.parsePoint(siteForm.getPointAddress());
//String geoJson2 = "{\"type\":\"Polygon\",\"coordinates\":[[[116.384109,39.917943],[116.386766,39.917943],[116.386766,39.920598],[116.384109,39.920598],[116.384109,39.917943]]] }";
Polygon area = geoJsonParser.parsePolygon(siteForm.getArea());
Site site = new Site();
site.setPointAddress(point);
site.setArea(area);
return SiteVo.of(siteComponent.save(site));
}
其中SiteForm的结构
@ApiModel("站点信息(表单)VO")
@Data
public class SiteForm {
@ApiModelProperty(value = "点经纬度", required = true)
@NotEmpty(message = "点经纬度不能为空")
private String pointAddress;
@ApiModelProperty(value = "面经纬度", required = true)
@NotEmpty(message = "面经纬度不能为空")
private String area;
}
其中需要用到的依赖
implementation("org.geotools:gt-geojson:27.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.0")
implementation("org.locationtech.jts:jts-core:1.18.2")
implementation("org.locationtech.jts.io:jts-io-common:1.18.2")
implementation("com.fasterxml.jackson.core:jackson-core:2.15.0")