4

I'm using JTS (http://www.vividsolutions.com/jts/JTSHome.htm) and need to get a JSON representation of a Point :

Hibernate beam:

@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name="the_geom")
private Point theGeom;

Table:

CREATE TABLE cities
(
  id_city serial NOT NULL,
  name text,
  the_geom geometry,
  id_user integer,
  CONSTRAINT cities_pkey PRIMARY KEY (id_city),
  CONSTRAINT fk2de48dcf3c0b0090 FOREIGN KEY (id_user)
      REFERENCES users (id_user) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

I need something like:

jsonString = someClass.convertToJson( myBeam.getTheGeom() )
2
  • the JTS home page is actually at tsusiatsoftware.net - it's now at version 1.12 - is that what you are using? – Ian Turton Nov 14 '13 at 12:14
  • Well, sounds strange a project having 2 diferents home pages. I'm in 1.8, but how can the new version helps me in this question? – Magno C Nov 14 '13 at 12:26
4

From @Mike Toews' tip:

@Entity
@Table(name="cities") 
public class City {

    @Type(type = "org.hibernate.spatial.GeometryType")
    @Column(name="the_geom")
    private Point theGeom;

    public String asJson() {
        GeometryJSON g = new GeometryJSON();
        return g.toString(theGeom) ; 
    }
}

You will need at least these JARs:

gt-api-10.0.jar
gt-epsg-extension-10.0.jar
gt-epsg-wkt-10.0.jar
gt-geojson-10.2.jar
gt-geometry-10.0.jar
gt-metadata-10.0.jar
gt-opengis-10.0.jar
gt-referencing-10.0.jar
jts-1.8.jar
jtsio-1.8.jar

and some few others I can't remember now, because I don't really need GeoTools.

1
  • You will not need the Hibernate stuff, of course. – Magno C Nov 18 '13 at 13:44
1
+50

Use a SELECT statement with ST_AsGeoJSON to let the server return the data as GeoJSON text.

On a related note, it looks like GeoTools has extended JTS to have a GeometryJSON class for the feature you are looking for within a pure Java environment.

2
  • I know ST_AsGeoJSON and is not what I need. Looks like GeoTools is more close. – Magno C Nov 18 '13 at 10:33
  • 2
    By leading me to right direction, I will reward you, but you don't realy give a clear answer, so I will accept mine. – Magno C Nov 18 '13 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.