Spatial Data from Postgis to Java/REST with RestEasy + Jackson

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Spatial Data from Postgis to Java/REST with RestEasy + Jackson

Luciano Borges
Hi all,

I'm developing an application (REST service) that, briefly speaking, takes a route from google maps and saves this route in Postgis using hibernatespatial (LineString geometry).

Now I want to get the polygon from database and display on the map. How to transform this polygon on a GeoJSON to pass to web page via REST Service?

My Entity:

public class Rota {
...
private LineString caminho;

public Rota() {
}

public Rota(Integer id, Usuario usuario, String descricao, LineString lineString) {
    this.id = id;
    this.usuario = usuario;
    this.descricao = descricao;
    this.caminho = lineString;
}

// getters and setters

@Type(type = "org.hibernate.spatial.GeometryType")
public LineString getCaminho() {
    return caminho;
}

public void setCaminho(LineString caminho) {
    this.caminho = caminho;
}

// hashcode and equals method
}

My REST service:

@Path("")
public class RotaService {

@Inject
private RotaBC rotaBC;

@GET
@Path("/rotas/{idrota}")
public Rota show(@PathParam("idrota") String idRota) {
Integer id = null;

if (!idRota.equals("")) {
id = Integer.parseInt(idRota);
}

return rotaBC.show(id);
}

@POST
@Path("/rotas")
public void create(@FormParam("idrota") String idRota, @FormParam("cpfusuario") String cpfUsuario,
@FormParam("descricao") String descricao, @FormParam("geometria[]") String[] pontos) throws ParseException {

Integer id = null;

if (!idRota.equals("")) {
id = Integer.parseInt(idRota);
}

Usuario usuario = new Usuario(cpfUsuario);

GeometryFactory gf = new GeometryFactory();

Coordinate c[] = new Coordinate[pontos.length];
String ap[];
for (int i = 0; i < c.length; i++) {
ap = pontos[i].split(",");
c[i] = new Coordinate(Double.parseDouble(ap[0]), Double.parseDouble(ap[1]));
}

Rota r = new Rota(id, usuario, descricao, gf.createLineString(c));
rotaBC.create(r);

}

}

Thanks,
Luciano


_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: Spatial Data from Postgis to Java/REST with RestEasy + Jackson

Karel Maesen
Administrator
Hi Luciano,

Recent versions of Postgis have a ST_AsGeoJSON function. You could use that function in a native SQL query to get the result directly as a GeoJSON string.

Hope this helps.

Regards,

Karel


On 17 Jan 2013, at 19:32, Luciano Borges wrote:

> Hi all,
>
> I'm developing an application (REST service) that, briefly speaking, takes a route from google maps and saves this route in Postgis using hibernatespatial (LineString geometry).
>
> Now I want to get the polygon from database and display on the map. How to transform this polygon on a GeoJSON to pass to web page via REST Service?
>
> My Entity:
>
> public class Rota {
> ...
> private LineString caminho;
>
> public Rota() {
> }
>
> public Rota(Integer id, Usuario usuario, String descricao, LineString lineString) {
>     this.id = id;
>     this.usuario = usuario;
>     this.descricao = descricao;
>     this.caminho = lineString;
> }
>
> // getters and setters
>
> @Type(type = "org.hibernate.spatial.GeometryType")
> public LineString getCaminho() {
>     return caminho;
> }
>
> public void setCaminho(LineString caminho) {
>     this.caminho = caminho;
> }
>
> // hashcode and equals method
> }
>
> My REST service:
>
> @Path("")
> public class RotaService {
>
> @Inject
> private RotaBC rotaBC;
>
> @GET
> @Path("/rotas/{idrota}")
> public Rota show(@PathParam("idrota") String idRota) {
> Integer id = null;
>
> if (!idRota.equals("")) {
> id = Integer.parseInt(idRota);
> }
>
> return rotaBC.show(id);
> }
>
> @POST
> @Path("/rotas")
> public void create(@FormParam("idrota") String idRota, @FormParam("cpfusuario") String cpfUsuario,
> @FormParam("descricao") String descricao, @FormParam("geometria[]") String[] pontos) throws ParseException {
>
> Integer id = null;
>
> if (!idRota.equals("")) {
> id = Integer.parseInt(idRota);
> }
>
> Usuario usuario = new Usuario(cpfUsuario);
>
> GeometryFactory gf = new GeometryFactory();
>
> Coordinate c[] = new Coordinate[pontos.length];
> String ap[];
> for (int i = 0; i < c.length; i++) {
> ap = pontos[i].split(",");
> c[i] = new Coordinate(Double.parseDouble(ap[0]), Double.parseDouble(ap[1]));
> }
>
> Rota r = new Rota(id, usuario, descricao, gf.createLineString(c));
>
> rotaBC.create(r);
>
> }
>
> }
>
> Thanks,
> Luciano
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users