【问题标题】:How to generate GeoJson on Spring boot?如何在 Spring Boot 上生成 GeoJson?
【发布时间】:2018-12-03 12:40:16
【问题描述】:

我正在尝试生成一个 GeoJson 以在 LineString 类型上发送 Google Maps (Google API)。我正在使用 Spring Boot。

现在,我可以生成一个 Json,但我找不到“转换”或“转换”该 Json 的方法。

我使用 Model 类从我的查询中发送数据(我使用 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)。

【问题讨论】:

    标签: java mysql json spring


    【解决方案1】:

    最后,我决定选择“精选集”

    解决方案

    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 2020-05-19
      • 2019-11-15
      • 2018-08-14
      • 2019-12-30
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多