Java:从EPSG转换lat / lon:4236到EPSG:3857 [英] Java: Convert lat/lon from EPSG:4236 to EPSG: 3857
问题描述
如何使用geotools或其他java库将我的lat lon转换为EPSG 3857投影?我找不到合适的使用方法。我知道OpenLayers(javascript)可以轻松完成,但我没有看到一条明确的路径来改变这些坐标。
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
所以我创建了我的CRS
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.
推荐答案
以下是您的解决方案:
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);
这篇关于Java:从EPSG转换lat / lon:4236到EPSG:3857的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!