moonroadgeom

    Planar OpenDRIVE lane geometry and native SVG/JSON CLI

    opendrive
    xodr
    lanes
    geometry
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    11

    #MoonRoadGeom

    MoonBit 平面 OpenDRIVE 车道几何库,附原生 CLI。读取 UTF-8 .xodr,按道路 ID、车道 ID 和参考线里程查询车道中心及内外边界,也可导出带分段标识的 SVG 或 JSON。

    0.1.0 的模块名为 Ag108/moonroadgeom。核心库支持 native 和 JS,文件 CLI 使用 native。支持 OpenDRIVE 1.8 平面子集中的直线、圆弧和分段车道宽度,以及 esmini 的 1.4 直道样例。 格式支持范围

    #环境与运行

    本地与 CI 使用 MoonBit 编译器 v0.10.20+65efd5b93。JS 检查使用 Node.js 22,复跑脚本需要 Python 3.11 或以上。XML 读取使用 Milky2018/xml@0.6.1。

    moon version --all moon run --target native cmd/moonroadgeom -- --help moon run --target native cmd/moonroadgeom -- inspect testdata/straight.xodr moon run --target native cmd/moonroadgeom -- query testdata/straight.xodr 1 -1 10 moon run --target native cmd/moonroadgeom -- export testdata/straight.xodr svg > straight.svg

    直道示例在 s = 10 米处返回右车道中心 (10, -1.75) 和外边界 (10, -3.5)。查询输出 JSON,保留地图局部 x/y 方向。SVG 翻转画布 y 轴以保持地图朝向。

    moonroadgeom inspect FILE moonroadgeom query FILE ROAD_ID LANE_ID S moonroadgeom export FILE svg|json [MAX_STEP [MAX_SAMPLES]]

    导出默认最大步长为 1 米,最多生成 200000 个车道位置。CLI 成功返回 0,输入或计算失败返回 1,参数错误返回 2。诊断写入 stderr。 坐标与采样约定

    #库调用

    其他项目在 moon.pkg 中导入 "Ag108/moonroadgeom" @road。把宿主读取的 UTF-8 字节传给 parse_map,后续查询复用同一个 RoadMap。

    ///|
    fn locate_lane(xodr : Bytes) -> @road.LanePosition raise @road.RoadError {
    let map = @road.parse_map(xodr)
    @road.lane_position(map, "1", -1, 10.0)
    }

    reference_position 查询参考线位姿,sample_lane 生成分段折线,export_svg 和 export_json 导出整张地图。发行包的 MoonBit 示例 随 moon test 编译运行。以下命令打包当前代码,并在独立项目中验证 API 调用。

    python scripts/consumer.py

    #示例与复跑

    场景命令
    直道左右车道moon run --target native cmd/moonroadgeom -- query testdata/straight.xodr 1 -1 10
    正负曲率moon run --target native cmd/moonroadgeom -- query testdata/arcs.xodr right -1 31.41592653589793
    宽度与车道段断点moon run --target native cmd/moonroadgeom -- query testdata/sections.xodr 1 1 20
    esmini 原始地图与 SVGpython scripts/esmini_compare.py --download
    不支持的几何类型moon run --target native cmd/moonroadgeom -- inspect testdata/unsupported.xodr

    python scripts/examples.py python scripts/esmini_compare.py --download python scripts/consumer.py --esmini .artifacts/straight_500m.xodr python scripts/plot_error.py

    esmini 对照使用 v2.59.0 和原始 straight_500m.xodr,下载后校验 SHA-256。SVG、24 个车道位置的误差明细和独立圆弧采样报告写入 .artifacts/。 上游样例与哈希

    #开发检查

    moon fmt --check moon check --target native,js --deny-warn moon test --target native,js --deny-warn python scripts/cli_smoke.py moon info --target native moon fmt

    生成的接口签名见 pkg.generated.mbti。库代码与自建样本采用 Apache-2.0。

    #可运行的 MoonBit 示例

    以下示例构造一条宽 3.5 米的右车道,并检查里程 10 米处的中心和外边界。

    ///|
    test "README: query a straight lane" {
    let source =
    #|<OpenDRIVE>
    #| <header revMajor="1" revMinor="8"/>
    #| <road id="1" length="30" junction="-1">
    #| <planView>
    #| <geometry s="0" x="0" y="0" hdg="0" length="30"><line/></geometry>
    #| </planView>
    #| <lanes>
    #| <laneSection s="0">
    #| <center><lane id="0" type="none"/></center>
    #| <right>
    #| <lane id="-1" type="driving">
    #| <width sOffset="0" a="3.5" b="0" c="0" d="0"/>
    #| </lane>
    #| </right>
    #| </laneSection>
    #| </lanes>
    #| </road>
    #|</OpenDRIVE>
    let map = @moonroadgeom.parse_map(@utf8.encode(source[:]))
    let lane = @moonroadgeom.lane_position(map, "1", -1, 10.0)
    assert_eq(lane.center.x, 10.0)
    assert_eq(lane.center.y, -1.75)
    assert_eq(lane.outer.y, -3.5)
    }

    RoadError

    pub(all) suberror RoadError {
    Failure(Diagnostic)
    } derive(
    Debug
    )

    RoadError::to_repr

    automatically derived

    Diagnostic

    pub struct Diagnostic {
    code : ErrorCode
    message : String
    line : Int?
    column : Int?
    road_id : String?
    lane_id : Int?
    s : Double?
    } derive(
    Debug
    )

    Diagnostic::to_repr

    automatically derived

    ErrorCode

    pub(all) enum ErrorCode {
    XmlSyntax
    MissingField
    InvalidValue
    InvalidLayout
    UnsupportedVersion
    UnsupportedFeature
    LimitExceeded
    RoadNotFound
    LaneNotFound
    InvalidStation
    } derive(Eq,
    Debug
    )

    ErrorCode::equal

    fn ErrorCode::equal(ErrorCode, ErrorCode) -> Bool
    automatically derived

    ErrorCode::not_equal

    fn ErrorCode::not_equal(x : ErrorCode, y : ErrorCode) -> Bool
    automatically derived

    ErrorCode::to_repr

    automatically derived

    LanePosition

    pub struct LanePosition {
    road_id : String
    lane_id : Int
    section_index : Int
    section_start : Double
    s : Double
    center : Point
    inner : Point
    outer : Point
    width : Double
    } derive(
    Debug
    )

    A single cross section of a nonzero lane. Inner is closest to lane zero.

    LanePosition::to_json

    LanePosition::to_repr

    automatically derived

    LaneSpan

    pub struct LaneSpan {
    road_id : String
    lane_id : Int
    section_index : Int
    section_start : Double
    points : Array[LanePosition]
    }

    One continuous interval of a sampled lane. Adjacent spans can each carry a position at their shared station to retain both sides of a breakpoint.

    Limits

    pub struct Limits {
    max_bytes : Int
    max_nodes : Int
    max_depth : Int
    max_string : Int
    max_roads : Int
    max_samples : Int
    }

    Independent limits for XML input and generated lane positions.

    Limits::new

    fn Limits::new(max_bytes? : Int, max_nodes? : Int, max_depth? : Int, max_string? : Int, max_roads? : Int, max_samples? : Int) -> Limits

    Point

    pub struct Point {
    x : Double
    y : Double
    } derive(
    Debug
    )

    A point in map-local Cartesian metres with the original y direction.

    Point::to_json

    Point::to_repr

    automatically derived

    ReferencePosition

    pub struct ReferencePosition {
    road_id : String
    s : Double
    segment_start : Double
    point : Point
    heading : Double
    } derive(
    Debug
    )

    Reference-line pose evaluated directly at a road station.

    ReferencePosition::to_repr

    automatically derived

    RoadMap

    pub struct RoadMap {
    // private fields
    } derive(
    Debug
    )

    A validated planar map. Road ordering follows the input document.

    RoadMap::revision_minor

    fn RoadMap::revision_minor(self : RoadMap) -> Int

    RoadMap::road_ids

    fn RoadMap::road_ids(self : RoadMap) -> Array[String]

    RoadMap::road_length

    fn RoadMap::road_length(self : RoadMap, id : String) -> Double?

    RoadMap::to_repr

    automatically derived

    export_json

    fn export_json(map : RoadMap, max_step? : Double, limits? : Limits) ->
    Json
    raise RoadError

    Serialize sampled geometries to the version 1 map-local JSON format.

    export_svg

    fn export_svg(map : RoadMap, max_step? : Double, limits? : Limits) -> String raise RoadError

    Export planar lane boundaries and centers to SVG. SVG reverses the map y coordinate to preserve the map's visual orientation on screen.

    lane_position

    fn lane_position(map : RoadMap, road_id : String, lane_id : Int, s : Double) -> LanePosition raise RoadError

    Evaluate a lane center and both edges at reference-line station s.

    parse_map

    fn parse_map(data : Bytes, limits? : Limits) -> RoadMap raise RoadError

    Parse a UTF-8 OpenDRIVE 1.8 or 1.4 map in planar mode.

    reference_position

    fn reference_position(map : RoadMap, road_id : String, s : Double) -> ReferencePosition raise RoadError

    Query the reference line. Each internal breakpoint selects the next interval, and the final road station belongs to the last interval.

    sample_lane

    fn sample_lane(map : RoadMap, road_id : String, lane_id : Int, max_step? : Double, limits? : Limits) -> Array[LaneSpan] raise RoadError

    Sample every section of this road where the specified lane exists.