java - 如何在 Spring Boot 上生成 GeoJson?
问题描述
我正在尝试生成一个 GeoJson 以在LineString
类型上发送一个谷歌地图(谷歌 API)。我正在使用 Spring Boot。
现在,我可以生成一个 Json,但我找不到“转换”或“转换”该 Json 的方法。
我使用模型类从我的查询中发送数据(我使用 MySQL)。所以,我的想法是通过相应的坐标传递演讲点(扫描仪名称)。
这是我的模型:
package com.geologistic.model;
public class PaqueteJson {
private String nombreEscaneo;
private String latitud;
private String longitud;
public String getNombreEscaneo() {
return nombreEscaneo;
}
public void setNombreEscaneo(String nombreEscaneo) {
this.nombreEscaneo = nombreEscaneo;
}
public String getLatitud() {
return latitud;
}
public void setLatitud(String latitud) {
this.latitud = latitud;
}
public String getLongitud() {
return longitud;
}
public void setLongitud(String longitud) {
this.longitud = longitud;
}
public PaqueteJson() {
super();
// TODO Auto-generated constructor stub
}
public PaqueteJson(String nombreEscaneo, String latitud, String longitud) {
super();
this.nombreEscaneo = nombreEscaneo;
this.latitud = latitud;
this.longitud = longitud;
}
@Override
public String toString() {
return "PaqueteJson [nombreEscaneo=" + nombreEscaneo + ", latitud=" + latitud + ", longitud=" + longitud + "]";
}
}
这是生成我的咨询和 Json 的查询。
public List<PaqueteJson> findJson()
{
List<PaqueteJson> paquetes= jdbcTemplate.query("select * from agencia ", new RowMapper<PaqueteJson>() {
public PaqueteJson mapRow (ResultSet rs, int argl) throws SQLException{
PaqueteJson paquete = new PaqueteJson (rs.getString("nombreEscaneo"),rs.getString("latitud"),
rs.getString("longitud"));
return paquete;
}
});
return paquetes;
}
这是我用来从我的控制器调用我的查询的方式。
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<PaqueteJson>> getTimelineProjectCaptions() {
return new ResponseEntity<List<PaqueteJson>>(paqueteService.findJson(), HttpStatus.OK);
}
我的实际 Json 生成如下:
[{"nombreEscaneo":"SALIDAS ALBACETE","latitud":"39.018922","longitud":"-1.875926"},{"nombreEscaneo":"SALIDAS PLATAFORMA BAILEN","latitud":"38.085772","longitud":"-3.773147"}, ....
所以,我想返回一个 GeoJson 以将该 GeoJson 发送到 Google API,但我找不到这样做的方法,我已经搜索了很多,我只是找到了一种方法,但是使用 Mongo DB(我正在使用MySQL)。
解决方案
最后,我决定选择“精选集”
解决方案
JSONObject featureCollection = new JSONObject();
featureCollection.put("type", "FeatureCollection");
JSONObject properties = new JSONObject();
properties.put("name", "ESPG:4326");
JSONObject crs = new JSONObject();
crs.put("type", "name");
crs.put("properties", properties);
featureCollection.put("crs", crs);
JSONArray features = new JSONArray();
// ciclo for
for (PaqueteJson obj : paqueteService.findJson()) {
JSONObject feature = new JSONObject();
feature.put("type", "Feature");
//JSONArray JSONArrayCoord = new JSONArray();
JSONObject geometry = new JSONObject();
JSONObject desc = new JSONObject();
JSONObject newFeature = new JSONObject();
//JSONArrayCoord.put(0, Double.parseDouble(obj.getLongitud()));
//JSONArrayCoord.put(1, Double.parseDouble(obj.getLatitud()));
JSONArray JSONArrayCoord = new JSONArray("[" + obj.getLongitud() + "," + obj.getLatitud() + "]");
geometry.put("type", "Point");
geometry.put("coordinates", JSONArrayCoord);
feature.put("geometry", geometry);
// proper.put("properties", desc);
feature.put("properties", desc);
desc.put("name", obj.getNombreEscaneo());
features.put(feature);
featureCollection.put("features", features);
// System.out.println(featureCollection.toString());
// }
}
System.out.println(featureCollection.toString());
推荐阅读
- c++ - 修改固定大小 tbb 并发向量的元素
- r - 根据关键词提取每个id的日期-R
- arrays - 在使用反应角度形式向表格显示数据时需要帮助
- html - Wordpress Max Mega Menu,想在粘性导航栏上添加单独的类
- reactjs - React - 光标跳到输入的末尾
- javascript - 如何在服务器端渲染中正确使用带有 ant 设计库的 next js?
- python - 将灰度图像转换为蓝色和红色图像
- r - 如何在 geom_bar 的图例中仅显示 3 个标签中的 2 个
- python - 如何使用来自两个目录的图像创建 PrefetchDataset?
- pytorch - 两个一维张量之间的差异