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號