sumo——相关地图文件格式转换
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 file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(netFile));
doc.getDocumentElement().normalize();
// Initialize GeoJSON structure
JSONArray featuresArray = new JSONArray();
// Get all edges
NodeList 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 lanes
JSONArray 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 object
JSONObject 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 file
JSONObject 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
OSGeo4W 安装 ogr2osm
OSGeo4W 没有直接提供 ogr2osm 包,你需要手动安装它。你可以通过 Python 的 pip 工具来安装 ogr2osm。
打开 OSGeo4W Shell:你可以在开始菜单中找到并启动 OSGeo4W Shell。这将为你提供一个已经配置好环境变量的命令行界面。
更新 pip(如果需要):
python3 -m pip install --upgrade pip
安装 ogr2osm
python3 -m pip install ogr2osm
验证安装
为了确保一切正常工作,你可以尝试运行 ogr2osm 来查看帮助信息:
ogr2osm --help
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)