这里用到的第三方库为:proj4j-0.1.1.jar 【可用于GIS开发空间坐标转换、投影】
下载地址:
【这里在“墨水直达”公众号输入:“proj4j”或者“横轴墨卡托投影”即可获取下载链接】
实现步骤:
创建lib文件夹,将上面下载的jar包放入lib文件夹中
然后往 pom.xml文件中添加如下代码
<dependency>
<groupId>io.jeo</groupId>
<artifactId>proj4j</artifactId>
<version>0.1.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/proj4j-0.1.1.jar</systemPath>
</dependency>
//转crs方法
private static CoordinateReferenceSystem createCRS(String proj4Params) {
CRSFactory factory = new CRSFactory();
CoordinateReferenceSystem crs = factory.createFromParameters(null, proj4Params);
return crs;
}
/**
下面是功能代码:
// 定义经纬度坐标-这里我定义三组经纬度坐标,用于生成三个图形,放在一个dxf文件中
//主要考虑你们可能会需要往dxf文件中插入多个图形
List<double[]> coordinatesList = new ArrayList<>();
coordinatesList.add(new double[]{
121.63373239, 31.20239190,
121.63448776, 31.20245508,
121.63450921, 31.20218446,
121.63383956, 31.20214868
});
coordinatesList.add(new double[]{
121.63395210, 31.20174493,
121.63463246, 31.20174859,
121.63464320, 31.20141377,
121.63403248, 31.20139168
});
coordinatesList.add(new double[]{
121.63472884, 31.20233092,
121.63528598, 31.20234849,
121.63525922, 31.20204124,
121.63480387, 31.20194556
});
// 定义投影坐标系和原始坐标系
//这里我说明下我的参数,你们可以自行带入
//坐标类型:经纬度<-->北京54
//转换类型:全参数
//参数模式:默认模式
//投影比例尺:1
//中央经线:121.467160611111
//纬度基线:0
//东偏移(米):0
//北偏移(米):-3457140.589
//DX(米):38.639859
//DY(米):-65.015948
//DZ(米):-49.162919
//RX(米):-2.237467
//RY(米):3.648844
//RZ(米):2.651865
//m[ppm]:-2.9139
//使用校正参数
//X:3
//Y:7.5
//投影高度:0
String proj4Params = "+proj=tmerc +lat_0=0 +lon_0=121.467160611111 +k=1 +x_0=0 +y_0=-3457140.589 +a=6378137 +b=6356752.314 +towgs84=0,0,0,0,0,0,0 +units=m";
CoordinateReferenceSystem projCRS = createCRS(proj4Params);
CoordinateReferenceSystem wgs84CRS = createCRS("+proj=longlat +datum=WGS84 +no_defs");
// 创建坐标转换对象
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
CoordinateTransform transform = ctFactory.createTransform(wgs84CRS, projCRS);
try (PrintWriter writer = new PrintWriter(new FileWriter("D:\\xxxx\\你自己想保存的文件路径\\文件命.dxf"))) {
writer.println("0");
writer.println("SECTION");
writer.println("2");
writer.println("ENTITIES");
for (double[] coordinates : coordinatesList) {
// 开始创建填充区域
List<Point> points = new ArrayList<>();
for (int i = 0; i < coordinates.length; i += 2) {
double longitude = coordinates[i];
double latitude = coordinates[i + 1];
ProjCoordinate srcCoord = new ProjCoordinate(longitude, latitude);
ProjCoordinate targetCoord = new ProjCoordinate();
// 坐标转换
transform.transform(srcCoord, targetCoord);
points.add(new Point(targetCoord.x, targetCoord.y));
}
writer.println("0");
writer.println("LWPOLYLINE");
writer.println("8");
writer.println("0");
writer.println("90");
writer.println(points.size());
writer.println("70");
writer.println("1");
for (Point point : points) {
writer.println("10");
writer.println(point.x);
writer.println("20");
writer.println(point.y);
}
// 添加闭合属性
writer.println("70");
writer.println("1");
writer.println("0");
writer.println("LWPOLYLINE");
writer.println("8");
writer.println("0");
writer.println("66");
writer.println("1"); // 使填充区域闭合
writer.println("70");
writer.println("1");
// 设置背景颜色属性
writer.println("62");
writer.println("5"); // 设置为颜色代码 5(深蓝色)
writer.println("0");
writer.println("VERTEX");
writer.println("10");
writer.println(points.get(0).x);
writer.println("20");
writer.println(points.get(0).y);
writer.println("0");
writer.println("VERTEX");
writer.println("10");
writer.println(points.get(1).x);
writer.println("20");
writer.println(points.get(1).y);
writer.println("0");
writer.println("VERTEX");
writer.println("10");
writer.println(points.get(2).x);
writer.println("20");
writer.println(points.get(2).y);
writer.println("0");
writer.println("VERTEX");
writer.println("10");
writer.println(points.get(3).x);
writer.println("20");
writer.println(points.get(3).y);
writer.println("0");
writer.println("VERTEX");
writer.println("10");
writer.println(points.get(0).x);
writer.println("20");
writer.println(points.get(0).y);
writer.println("0");
writer.println("SEQEND");
}
writer.println("0");
writer.println("ENDSEC");
writer.println("0");
writer.println("EOF");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("DXF文件已创建。");
最后生成的文件如下:
这里也可以不使用第三方库,可以自己进行计算,如下:
double longitude = 121.0; // 输入经度
double latitude = 31.0; // 输入纬度
// 参数设置,根据你的投影坐标系进行适当调整
double lonOrigin = 121.467160611111; // 中央经线
double xOrigin = 0.0; // X 轴偏移量
double yOrigin = -3457140.589; // Y 轴偏移量
double a = 6378137.0; // 长半轴
double b = 6356752.314; // 短半轴
// 由经纬度计算转换后的投影坐标
double lonDiff = longitude - lonOrigin;
double x = xOrigin + a * Math.toRadians(lonDiff) * Math.cos(Math.toRadians(latitude));
double y = yOrigin + a * Math.toRadians(latitude);
System.out.println("投影坐标:X=" + x + ", Y=" + y);