简体   繁体   中英

How to generate GeoJson on Spring boot?

I'm trying to generate a GeoJson to send a Google Maps (Google API) on LineString type. I´m using Spring Boot.

Right now, I can generate a Json, but I can't find the way to "convert" or "transform" that Json.

I use a Model class to send the data from my query (I use MySQL). So, my idea its pass the point of lecture (the scanner name) with the respective coordinates.

This is my model:

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 + "]";
    }


}

This is my Query to generate my Consult and 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;

}

This is the way than I use to call my Query from my Controller.

@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);
    }

My actual Json generate its like:

[{"nombreEscaneo":"SALIDAS ALBACETE","latitud":"39.018922","longitud":"-1.875926"},{"nombreEscaneo":"SALIDAS PLATAFORMA BAILEN","latitud":"38.085772","longitud":"-3.773147"}, ....

So, I want to return a GeoJson to send that GeoJson to a Google API, but I can´t find a way to do that, I already search a lot and I just find a way but with Mongo DB (and I'm using MySQL).

In the end,i decided the choice "Feature Collection"

SOLUTION

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM