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
浙公网安备 33010602011771号