Java: Convert lat/lon from EPSG:4236 to EPSG: 3857

丶灬走出姿态 提交于 2019-12-06 08:19:02

问题


How would I convert my lat lon to EPSG 3857 projection using geotools or another java library? I'm having trouble finding the proper methods to use. I know OpenLayers (javascript) can do it easily, but I don't see a clear path to getting these coordinates transformed.

I would like to see this transformation
source lon, lat: -71.017942,  42.366662    
destination lon, lat: -71 1.25820, 42 22.0932

So I have created my CRS

final CoordinateReferenceSystem source = CRS.decode( "EPSG:4236" );
final CoordinateReferenceSystem dest = CRS.decode("EPSG:3857");

final MathTransform transform = CRS.findMathTransform(source, dest);

But creating geometries doesn't seem straight forward with the points, as they are requiring a geometry factory or something..

I'm new to geo tools and geospatial data, thanks for any direction.


回答1:


Here's a solution for you:

CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4236");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
Point point = geometryFactory.createPoint(new Coordinate(lon, lat));
Point targetPoint = (Point) JTS.transform(point, transform);


来源:https://stackoverflow.com/questions/15605247/java-convert-lat-lon-from-epsg4236-to-epsg-3857

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!