0

I have some problem with the representation of the coordinates. I need to convert the coordinates from "EPSG: 32633" to "EPSG: 4326", and then create geometries in PostGIS. For the conversion (from java) I use GeoTools.

double xc = 586073.93799999997;
double yc = 4644834.9100000001;
Coordinate c = new Coordinate(xc, yc);
Point p = gf.createPoint(c);
CoordinateReferenceSystem utmCrs = CRS.decode("EPSG:32633");
CoordinateReferenceSystem tCrs = CRS.decode("EPSG:4326");
MathTransform mathTransform = CRS.findMathTransform(utmCrs, tCrs, false);
Point p1 = (Point) JTS.transform(p, mathTransform);
System.out.println(p1.getCoordinate());

with the output:

(40.271915834122524, 17.83405351767443, NaN)

However, this representation is wrong for PostGIS.

create table coordinate(
id int  primary key,
tipo varchar(15)
);
SELECT AddGeometryColumn('coordinate','luogo','4326','POINT',2);
INSERT INTO coordinate(id, tipo, luogo) VALUES(1,'calcare', ST_GeomFromText('POINT(40.271915834122524, 17.83405351767443)', 4326));

returns

ERROR: parse error - invalid geometry HINT: "POINT(40.271915834122524, " <-- parse error at position 26 within geometr

how can I solve my problem?

... the wkt of GeoTools is the same as that contained in spatial_ref_sys:

crs = CRS.decode("EPSG:4326");
wkt = crs.toWKT();
System.out.println("\n\nwkt for EPSG:4326");

wkt for EPSG:4326
GEOGCS["WGS 84", 
  DATUM["World Geodetic System 1984", 
    SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], 
    AUTHORITY["EPSG","6326"]], 
  PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic latitude", NORTH], 
  AXIS["Geodetic longitude", EAST], 
  AUTHORITY["EPSG","4326"]]
0

You need to provide WKT (i.e., no comma). To easily get this out, use toText():

System.out.println(p1.toText());

or directly specify X and Y values, which is useful if you want to eventually parameterise the inserts:

INSERT INTO coordinate(id, tipo, luogo)
VALUES(1,'calcare', ST_SetSRID(ST_MakePoint(40.271915834122524, 17.83405351767443), 4326));

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.