geotools坐标转换
发布时间
阅读量:
阅读量
报错:
org.geotools.referencing.operation.MathTransformProvider
geotools org.postgresql.ds.PGSimpleDataSource
geotools版本我使用的是18.5,使用最新版时我的JTS.transform会提示类型错误,新人不知道怎么改,就使用了旧版的geotools。但跑起来后还会有报错

最开始我将下载的jar包全都添加到了项目中,调试后会报错如上
删除下面的jar包后报错消失

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
public static double[] convert(double lon, double lat)
throws FactoryException, MismatchedDimensionException, TransformException {
Coordinate sourceCoord = new Coordinate(lon, lat);
GeometryFactory geoFactory = new GeometryFactory();
Point sourcePoint = geoFactory.createPoint(sourceCoord);
final String strWKTMercator = "PROJCS[\"World_Mercator\","
+ "GEOGCS[\"GCS_WGS_1984\","
+ "DATUM[\"WGS_1984\","
+ "SPHEROID[\"WGS_1984\",6378137,298.257223563]],"
+ "PRIMEM[\"Greenwich\",0],"
+ "UNIT[\"Degree\",0.017453292519943295]],"
+ "PROJECTION[\"Mercator_1SP\"],"
+ "PARAMETER[\"False_Easting\",0],"
+ "PARAMETER[\"False_Northing\",0],"
+ "PARAMETER[\"Central_Meridian\",0],"
+ "PARAMETER[\"latitude_of_origin\",0],"
+ "UNIT[\"Meter\",1]]";
CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, mercatroCRS);
Point targetPoint = (Point) JTS.transform(sourcePoint, transform);
double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
return targetCoord;
}
public static double[] convert(double lon, double lat, String strWKT)
throws FactoryException, MismatchedDimensionException, TransformException {
Coordinate sourceCoord = new Coordinate(lon, lat);
GeometryFactory geoFactory = new GeometryFactory();
Point sourcePoint = geoFactory.createPoint(sourceCoord);
CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKT);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, mercatroCRS);
Point targetPoint = (Point) JTS.transform(sourcePoint, transform);
double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
return targetCoord;
}
public static void main( String[] args ) throws Exception
{
double longitude = 113.926982;
double latitude = 22.53089;
double[] coordinate = convert(longitude, latitude);
System.out.println("X: " + coordinate[0] + ", Y: " + coordinate[1]);
}
全部评论 (0)
还没有任何评论哟~
