1

So I have a GeoJSON with EPSG:3068 and I would like to convert the coordinates to WGS84(EPSG:4326). I could also convert them before I write them to GeoJSON. I could not find an algorithm for the conversion. I prefer Java, but any other scripts would be ok as well.

My code looks like this:

public static void convert() throws NoSuchAuthorityCodeException, FactoryException, IOException, MismatchedDimensionException, TransformException{ // CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3068"); // CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

String SoldnerBerlinWKT = "PROJCS[\"DHDN / Soldner Berlin\","
           + "GEOGCS[\"DHDN\",DATUM[\"Deutsches_Hauptdreiecksnetz\","
           + "SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",\"7004\"]],"
           + "AUTHORITY[\"EPSG\",\"6314\"]]," 
           + "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
           + "UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],"
           + "AUTHORITY[\"EPSG\",\"4314\"]]," 
           + "UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],"
           + "PROJECTION[\"Cassini_Soldner\"],PARAMETER[\"latitude_of_origin\",52.41864827777778],"
           + "PARAMETER[\"central_meridian\",13.62720366666667],PARAMETER[\"false_easting\",40000],"
           + "PARAMETER[\"false_northing\",10000],"
           + "AUTHORITY[\"EPSG\",\"3068\"],AXIS[\"y\",EAST],AXIS[\"x\",NORTH]]";
CoordinateReferenceSystem sourceCRS = CRS.parseWKT(SoldnerBerlinWKT);
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); //line 74
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
GeometryJSON jsonReader = new GeometryJSON();
Geometry sourceGeometry = jsonReader.read("C:\\Users\\Selphi\\Desktop\\test.geojson");
Geometry targetGeometry = JTS.transform(sourceGeometry, transform); 
System.out.println(sourceGeometry.toString());
System.out.println(targetGeometry.toString());

}

and I get this error (maybe some jars missing?):

Nov 13, 2014 9:50:49 AM org.geotools.referencing.factory.epsg.ThreadedPostgreSQLEpsgFactory isAvailable
WARNUNG: Unavailable authority factory: European Petroleum Survey Group
org.opengis.referencing.FactoryException: Failed to connect to the EPSG database.
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.createBackingStore(ThreadedEpsgFactory.java:441)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.getBackingStore(DeferredAuthorityFactory.java:133)
    at org.geotools.referencing.factory.BufferedAuthorityFactory.isAvailable(BufferedAuthorityFactory.java:235)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.isAvailable(DeferredAuthorityFactory.java:119)
    at org.geotools.factory.FactoryRegistry.isAvailable(FactoryRegistry.java:667)
    at org.geotools.factory.FactoryRegistry.isAcceptable(FactoryRegistry.java:501)
    at org.geotools.factory.FactoryRegistry$1.filter(FactoryRegistry.java:192)
    at javax.imageio.spi.FilterIterator.advance(Unknown Source)
    at javax.imageio.spi.FilterIterator.<init>(Unknown Source)
    at javax.imageio.spi.ServiceRegistry.getServiceProviders(Unknown Source)
    at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:197)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactories(ReferencingFactoryFinder.java:180)
    at org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactories(ReferencingFactoryFinder.java:455)
    at org.geotools.referencing.DefaultAuthorityFactory.getBackingFactory(DefaultAuthorityFactory.java:88)
    at org.geotools.referencing.DefaultAuthorityFactory.<init>(DefaultAuthorityFactory.java:69)
    at org.geotools.referencing.CRS.getAuthorityFactory(CRS.java:262)
    at org.geotools.referencing.CRS.decode(CRS.java:519)
    at org.geotools.referencing.CRS.decode(CRS.java:447)
    at Test.convert(Test.java:74)
    at Test.main(Test.java:53)
Caused by: org.postgresql.util.PSQLException: Connection refused. Check the correctness of host name and port number and check whether the data base server accepts TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:87)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:70)
    at org.geotools.referencing.factory.epsg.DirectEpsgFactory.getConnection(DirectEpsgFactory.java:3196)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.createBackingStore(ThreadedEpsgFactory.java:436)
    ... 19 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76)
    ... 31 more

Nov 13, 2014 9:50:49 AM org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory createDataSource
INFORMATION: Building new data source for org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory
Exception in thread "main" java.lang.NoClassDefFoundError: org/hsqldb/jdbc/jdbcDataSource
    at org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory.createDataSource(ThreadedHsqlEpsgFactory.java:216)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.createBackingStore0(ThreadedEpsgFactory.java:386)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.createBackingStore(ThreadedEpsgFactory.java:434)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.getBackingStore(DeferredAuthorityFactory.java:133)
    at org.geotools.referencing.factory.BufferedAuthorityFactory.isAvailable(BufferedAuthorityFactory.java:235)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.isAvailable(DeferredAuthorityFactory.java:119)
    at org.geotools.factory.FactoryRegistry.isAvailable(FactoryRegistry.java:667)
    at org.geotools.factory.FactoryRegistry.isAcceptable(FactoryRegistry.java:501)
    at org.geotools.factory.FactoryRegistry$1.filter(FactoryRegistry.java:192)
    at javax.imageio.spi.FilterIterator.advance(Unknown Source)
    at javax.imageio.spi.FilterIterator.<init>(Unknown Source)
    at javax.imageio.spi.ServiceRegistry.getServiceProviders(Unknown Source)
    at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:197)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactories(ReferencingFactoryFinder.java:180)
    at org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactories(ReferencingFactoryFinder.java:455)
    at org.geotools.referencing.DefaultAuthorityFactory.getBackingFactory(DefaultAuthorityFactory.java:88)
    at org.geotools.referencing.DefaultAuthorityFactory.<init>(DefaultAuthorityFactory.java:69)
    at org.geotools.referencing.CRS.getAuthorityFactory(CRS.java:262)
    at org.geotools.referencing.CRS.decode(CRS.java:519)
    at org.geotools.referencing.CRS.decode(CRS.java:447)
    at Test.convert(Test.java:74)
    at Test.main(Test.java:53)
Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbc.jdbcDataSource
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 22 more
5
  • It may be good to know that official support for CRS is going to be removed from the GeoJSON tools.ietf.org/html/draft-butler-geojson-04.
    – user30184
    Nov 12, 2014 at 9:54
  • Ah thanks! That shouldn't be a problem, I could switch to GML easily
    – Selphiron
    Nov 12, 2014 at 10:21
  • EPSG:3068 seems to have axis in northing-easting order but I guess that you know that already and can handle the case when converting coordinates to/from GML3 and other formats.
    – user30184
    Nov 12, 2014 at 10:29
  • Which is line 74?
    – BradHards
    Nov 13, 2014 at 8:35
  • Updated the code. I removed the wrong Mathtransform transform line and updated the error message. Oh and line 74 is CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); (also commented in the code)
    – Selphiron
    Nov 13, 2014 at 8:57

1 Answer 1

3

I'd suggest not writing this yourself, but instead using one of the existing coordinate transform libraries, such as proj4j or the CRS part of GeoTools. From GeoTools (with some JTS help):

import com.vividsolutions.jts.geom.Geometry;

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.geojson.geom.GeometryJSON;

String SoldnerBerlinWKT = "PROJCS[\"DHDN / Soldner Berlin\","
   + "GEOGCS[\"DHDN\",DATUM[\"Deutsches_Hauptdreiecksnetz\","
   + "SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",\"7004\"]],"
   + "AUTHORITY[\"EPSG\",\"6314\"]]," +
   + "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
   + "UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],"
   + "AUTHORITY[\"EPSG\",\"4314\"]]," +
   + "UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],"
   + "PROJECTION[\"Cassini_Soldner\"],PARAMETER[\"latitude_of_origin\",52.41864827777778],"
   + "PARAMETER[\"central_meridian\",13.62720366666667],PARAMETER[\"false_easting\",40000],"
   + "PARAMETER[\"false_northing\",10000],"
   + "AUTHORITY[\"EPSG\",\"3068\"],AXIS[\"y\",EAST],AXIS[\"x\",NORTH]]";

CoordinateReferenceSystem sourceCRS = CRS.parseWKT(SoldnerBerlinWKT);
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
GeometryJSON jsonReader = new GeometryJSON();

Geometry sourceGeometry = jsonReader.read(inputStreamWithJSONdata);
// you could also get the sourceGeometry using  the read(Object) method,
// which takes a File, or a String containing a filename, or a Reader
// instance.

Geometry targetGeometry = JTS.transform(sourceGeometry, transform);

If you'd like another language, see http://trac.osgeo.org/proj/ or one of the other variants linked off the metacrs page.

9
  • thanks for your answer. I tried your code but I don't know what sourceGeometry is. It says envelope but do you have an example for that? I read this (docs.geotools.org/stable/javadocs/org/opengis/geometry/…) but too many ?s in my head
    – Selphiron
    Nov 12, 2014 at 8:43
  • Its the geometry you want to convert. It depends on the format you have the GeoJSON data in, but you could use something like the GeometryJSON::read() method. I'll add that to the example.
    – BradHards
    Nov 12, 2014 at 8:50
  • I tried but eclipse says: "Type mismatch: cannot convert from com.vividsolutions.jts.geom.Geometry to org.opengis.geometry.Geometry and suggests to make a typecast to Geometry at jsonreader.read("C:\\Users..."); If I do than there is an error at JTS.transform "The method transform(Envelope, MathTransform) in the type JTS is not applicable for the arguments (Geometry, MathTransform)"
    – Selphiron
    Nov 12, 2014 at 10:19
  • oh and I did not import anything from vividsolution
    – Selphiron
    Nov 12, 2014 at 10:25
  • Geometry should be com.vividsolutions.jts.geom.Geometry, per the javadoc for JTS class (docs.geotools.org/stable/javadocs/org/geotools/geometry/jts/…). I've updated the example again - I thought that part was obvious, sorry for the confusion.
    – BradHards
    Nov 12, 2014 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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