2

in the openlayers we can simple transform EPSG:900913 to EPSG:4326
I'm look for a java lib can do that.
here I found this, http://www.jhlabs.com/java/maps/proj/index.html

but the document is in c++
I don't know how to use it.


If anyone konw that,
please post a simple code

Tomas
  • 57,621
  • 49
  • 238
  • 373

6 Answers6

4

I understand that this is from almost 8 years ago, but maybe this can help another intrepid traveller.

We had to move away from GeoTools because it's LGPL, which is not allowed by our legal folk.

I just moved our code to use proj4j (https://trac.osgeo.org/proj4j/). It doesn't look like it's being actively developed, but it works for our simple needs. Also, the license is Apache 2.0, which is much more permissive.

It's available via Maven, so that makes it easy: http://search.maven.org/#artifactdetails%7Corg.osgeo%7Cproj4j%7C0.1.0%7Cjar.

It doesn't directly support EPSG:900913, since it's not really the official standard. It does support EPSG:3857, which is the same thing.

Here's a snippet doing what you're looking for:

public Point2D.Double transform(Point2D.Double point, String sourceCRS, String targetCRS) {
    Point2D.Double destPosition = new Point2D.Double();

    CRSFactory factory = new CRSFactory();
    CoordinateReferenceSystem srcCrs = factory.createFromName(sourceCRS); // Use "EPSG:3857" here instead of 900913.
    CoordinateReferenceSystem destCrs = factory.createFromName(targetCRS); // Use "EPSG:4326 here.
    CoordinateTransform transform = new CoordinateTransformFactory().createTransform(srcCrs, destCrs);

    ProjCoordinate srcCoord = new ProjCoordinate(point.getX(), point.getY());
    ProjCoordinate destCoord = new ProjCoordinate();
    transform.transform(srcCoord, destCoord);
    destPosition.setLocation(destCoord.x, destCoord.y);

    return destPosition;
}
Jamie
  • 1,754
  • 16
  • 34
2

Jerry Huxtable's delightful Globe Applet on the page you cited is indeed written in Java, as seen in the download. The class com.jhlabs.map.proj.ProjectionFactory contains a method named fromPROJ4Specification(), which returns a com.jhlabs.map.proj.Projection. You can use the EPSG:900913 parameters specified on the OpenLayers site to create the desired projection.

900913:
+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0
+x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs

You should also look at OpenMap.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Another option is to use the OpenSource GIS Java library GeoTools:

http://geotools.org/

Details on the projection classes here:

http://geotools.org/javadocs/org/geotools/referencing/operation/projection/MapProjection.html

Projection definitions in many different formats for all projections can be downloaded from:

http://www.spatialreference.org/

E.g. http://www.spatialreference.org/ref/epsg/4326/

geographika
  • 6,458
  • 4
  • 38
  • 56
2

Geotools is probably the best library to use for this. Taking a look at their CRS tutorial, it looks trivial to transform from one coordinate system to another using:

CoordinateReferenceSystem dataCRS = schema.getCoordinateReferenceSystem();
CoordinateReferenceSystem worldCRS = map.getCoordinateReferenceSystem();
boolean lenient = true; // allow for some error due to different datums
MathTransform transform = CRS.findMathTransform(dataCRS, worldCRS, lenient);

Your CRS references can be retrieved using:

CRS.decode("EPSG:4326")

Per the javadoc.

disrvptor
  • 1,592
  • 12
  • 23
1

Also interesting: Proj4j

Proj4J is a Java library to transform point coordinates from one geographic coordinate system to another, including datum transformations. The core of this library is a port of the PROJ.4 C library.

pxp
  • 87
  • 1
  • 10
  • Some code would have been nice about how to use it. – mico Dec 15 '15 at 12:19
  • Have a look at this [example](https://trac.osgeo.org/proj4j/browser/trunk/src/test/java/org/osgeo/proj4j/ExampleTest.java). For a simple howto in javascript, look here: [proj4js](http://proj4js.org/) – pxp Jan 07 '16 at 19:12
0

There seems to be a new Apache project, which may be a more up to date alternative for the open source projects mentioned above:

http://sis.apache.org/

It presents itself as an alternative for proj4:

https://sis.apache.org/book/en/developer-guide.html

keesp
  • 301
  • 2
  • 13