1

I am trying to create a GIS app using Java, Spring and Hibernate.

I send a JSON from Postman like this:

{
    "pointName":"any location",
    "geom":"POINT(-71.060316 48.432044), 3857"
}

The JSON is received by the Controller Class and sent to a Converter Class where the information that arrives as String is converted to geometry with the following method:

public static Geometry wktToGeometry(String wellKnownText) 
              throws ParseException {

                return new WKTReader().read(wellKnownText);
            }

But once the information is processed by this method, 3857 dissapears and I have the following error when I try to save in the DB:

org.postgresql.util.PSQLException: ERROR: Geometry SRID (0) does not match column SRID (3857)

This is because the reference system has been removed in the wktToGeometry method, and it is interpretated as reference system 0.

How should I format the JSON?

  • What is the set SRID for your postgres database? – Mapperz May 16 at 18:31
  • The SRID is 3857 – Carlos FTG May 16 at 18:34
  • 2
    Your coordinate looks to be in a geographic CS, not Web Mercator (unless you're mapping Null Island), and WKT doesn't actually include the SRID at the end (that's EWKT), so you've got multiple issues. – Vince May 16 at 19:07
  • Which wkt reader are you using? How do you write to the database? – Ian Turton May 16 at 19:15
  • I am using " com.vividsolutions.jts.io.WKTReader.WKTReader()" I write to the DB with save method of Spring´s crud repository – Carlos FTG May 19 at 10:19
1

At the end I have sorted it out. In the converter class, where the info from the json is setted to the objet, I have done like this:

public static GeometryEntity convertToGeometryEntity(NonGeometryEntity nonGeometry) throws ParseException {

    GeometryEntity geometryEntity = new GeometryEntity();

        if(nonGeometry.getUserId() != null) {
        geometryEntity.setUserId(Long.parseLong(nonGeometry.getUserId()));
        }

        geometryEntity.setPointName(nonGeometry.getPointName());
        geometryEntity.setGeom(wktToGeometry(nonGeometry.getGeom()));

        geometryEntity.getGeom().setSRID(3857);

        return geometryEntity;

    }

I have setted the SRID property straight away on the converter, and the info is saved ok in the db:

2019-05-19 20:34:57.553 DEBUG 6908 --- [nio-8081-exec-3] org.hibernate.SQL                        : insert into geometries (geom, point_name, user_id) values (?, ?, ?)

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.