簡體   English   中英

如何在Spring Boot上生成GeoJson?

[英]How to generate GeoJson on Spring boot?

我正在嘗試生成一個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 + "]";
    }


}

這是我的查詢以生成我的Consult和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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM