java实现将WGS84坐标经纬度转为sumo仿真路网XML文件中lane里的shape的UTM+50
时间: 2024-06-12 07:07:51 浏览: 207
以下是一个Java实现将WGS84坐标经纬度转为sumo仿真路网XML文件中lane里的shape的UTM 50的示例代码:
```
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.io.WKTWriter;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.opengis.referencing.operation.TransformFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class WGS84toUTM50 {
public static CoordinateReferenceSystem wgs84crs;
public static CoordinateReferenceSystem utm50crs;
public static MathTransform wgs84ToUtm50Transform;
public static MathTransform utm50ToWgs84Transform;
static {
try {
wgs84crs = org.geotools.referencing.crs.DefaultGeographicCRS.WGS84;
utm50crs = org.geotools.referencing.crs.DefaultProjectedCRS.WGS84_UTM(50, true);
wgs84ToUtm50Transform = TransformFactory
.finder()
.findMathTransform(wgs84crs, utm50crs, true);
utm50ToWgs84Transform = TransformFactory
.finder()
.findMathTransform(utm50crs, wgs84crs, true);
} catch (NoSuchAuthorityCodeException | FactoryException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
File file = new File("input.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Roads.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Roads roads = (Roads) jaxbUnmarshaller.unmarshal(file);
for (Road road : roads.getRoad()) {
for (Lane lane : road.getLane()) {
List<Point> points = parsePoints(lane.getShape());
List<Point> convertedPoints = convertPoints(points);
String convertedShape = buildShape(convertedPoints);
lane.setShape(convertedShape);
}
}
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(roads, new FileOutputStream("output.xml"));
} catch (JAXBException | FileNotFoundException e) {
e.printStackTrace();
}
}
public static List<Point> parsePoints(String shape) {
List<Point> points = new ArrayList<>();
try {
WKTReader reader = new WKTReader(new GeometryFactory(new PrecisionModel(), 4326));
Point point;
for (String s : shape.split(" ")) {
point = (Point) reader.read("POINT (" + s.replace(",", " ") + ")");
points.add(point);
}
} catch (ParseException e) {
e.printStackTrace();
}
return points;
}
public static List<Point> convertPoints(List<Point> points) {
List<Point> convertedPoints = new ArrayList<>();
for (Point point : points) {
try {
Point utmPoint = (Point) JTS.transform(point, wgs84ToUtm50Transform);
convertedPoints.add(utmPoint);
} catch (TransformException e) {
e.printStackTrace();
}
}
return convertedPoints;
}
public static String buildShape(List<Point> points) {
StringBuilder builder = new StringBuilder();
WKTWriter writer = new WKTWriter();
for (Point point : points) {
Coordinate coordinate = point.getCoordinate();
builder.append(coordinate.x).append(",").append(coordinate.y).append(" ");
}
return builder.toString().trim();
}
}
```
这个程序的主要工作是:
1. 读取输入的XML文件,将每个lane的shape字段解析成一组WGS84坐标点;
2. 将这些WGS84坐标点转换为UTM 50坐标点;
3. 将这些UTM 50坐标点重新组成一个字符串,作为新的lane的shape字段;
4. 将修改后的XML文件写出到磁盘上。
注意,在这个程序中,我们使用了一些外部库:
1. JTS:用于解析和处理几何形状;
2. GeoTools:用于处理坐标系和坐标转换。
如果您使用的是Maven构建工具,可以将以下依赖项添加到您的pom.xml文件中:
```
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>22.1</version>
</dependency>
```
阅读全文