netconvert xodr 转net.xml格式
使用 netconvert 命令转换
netconvert --opendrive-files caoyang.xodr -o caoyang.net.xml
使用工具类将caoyang.net.xml 转 caoyang.geojson
package com.ys.test.netxml_to_geojson;import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.json.*;public class NetXmlToGeoJson {public static void main(String[] args) throws IOException {final String netFile = "D:\\A_tool\\sumo-win64-1.20.0\\A-file\\xodr-demo\\caoyang.net.xml";final String shpFile = "D:\\A_tool\\sumo-win64-1.20.0\\A-file\\xodr-demo\\caoyang.geojson";try {netxmlToGeojson(netFile, shpFile);System.out.println("Conversion completed successfully.");} catch (Exception e) {e.printStackTrace();}}public static void netxmlToGeojson(String netFile, String outputFile) throws Exception {// Parse the XML fileDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(new File(netFile));doc.getDocumentElement().normalize();// Initialize GeoJSON structureJSONArray featuresArray = new JSONArray();// Get all edgesNodeList edgeList = doc.getElementsByTagName("edge");for (int i = 0; i < edgeList.getLength(); i++) {Node edgeNode = edgeList.item(i);if (edgeNode.getNodeType() == Node.ELEMENT_NODE) {Element edge = (Element) edgeNode;// Extract coordinates from lanesJSONArray coordinates = new JSONArray();NodeList laneList = edge.getElementsByTagName("lane");for (int j = 0; j < laneList.getLength(); j++) {Node laneNode = laneList.item(j);if (laneNode.getNodeType() == Node.ELEMENT_NODE) {Element lane = (Element) laneNode;String shape = lane.getAttribute("shape");String[] points = shape.split(" ");for (String point : points) {String[] coords = point.split(",");double lon = Double.parseDouble(coords[0]);double lat = Double.parseDouble(coords[1]);coordinates.put(new JSONArray().put(lon).put(lat));}}}// Create feature objectJSONObject properties = new JSONObject().put("id", edge.getAttribute("id")).put("name", edge.getAttribute("name"));JSONObject geometry = new JSONObject().put("type", "LineString").put("coordinates", coordinates);JSONObject feature = new JSONObject().put("type", "Feature").put("geometry", geometry).put("properties", properties);featuresArray.put(feature);}}// Write to GeoJSON fileJSONObject geojson = new JSONObject().put("type", "FeatureCollection").put("features", featuresArray);try (FileWriter file = new FileWriter(outputFile)) {file.write(geojson.toString(4)); // Pretty print with indent of 4 spaces}}
}
OSGeo4W安装gdal库流程
参考:https://blog.csdn.net/brucehaoLee/article/details/140928493
ogr2ogr工具 .geojson转 .shp
ogr2ogr 是 GDAL(Geospatial Data Abstraction Library)的一部分,用于转换矢量地理空间数据格式。GDAL 是一个开源的库,提供了读写栅格和矢量地理空间数据格式的能力。ogr2ogr 专门处理矢量数据(如 Shapefile、GeoJSON、PostGIS 等),而 gdal_translate 和 gdalwarp 则主要用于栅格数据。
命令
ogr2ogr -f "ESRI Shapefile" output.shp caoyang.geojson