Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sumo-user] Export SUMO net as shapefile (or KML)

here is my python code, it is not written in an objected oriented manner or as professional as your tools, but it should do the job. If you could help me a bit, I try to push it into geomhelper functions. 
Thanks again for the support.

Best,
Sasan

On Thu, Oct 6, 2022 at 2:04 PM Jakob Erdmann <namdre.sumo@xxxxxxxxx> wrote:
set option --internal  (which translates to sumolib.net.readNet(file, withInternal=True)

Am Do., 6. Okt. 2022 um 13:55 Uhr schrieb Sasan Amini <aminissn@xxxxxxxxx>:
Thanks for the tip. It was actually easy to generate the polygons. I will share my code as soon as I am finished to fully generate all elements of the network. 
One last question: somehow I cannot read the shape of the connections (internal Links) of the network using sumolib. Am I missing something? Below you can ee that junctions and internal links are missing.
sumoNetPoly.png

On Wed, Oct 5, 2022 at 10:24 PM Jakob Erdmann <namdre.sumo@xxxxxxxxx> wrote:
No. Though it shouldn't be too hard to build this with the help of sumolib/geomhelper function move2side
Feel free to do add a new option for this.
Alternatively, you could export the lane width and use this to style the lines in QGIS.

Am Mi., 5. Okt. 2022 um 20:50 Uhr schrieb Sasan Amini <aminissn@xxxxxxxxx>:
Thanks. just one follow-up question: is it possible to export lanes (and edges) as polygons instead of linear objects i.e. their centerline? 

On Wed, Oct 5, 2022 at 12:48 PM Jakob Erdmann <namdre.sumo@xxxxxxxxx> wrote:

Am Mi., 5. Okt. 2022 um 12:00 Uhr schrieb Sasan Amini <aminissn@xxxxxxxxx>:
Dear all,
is there a way to export *.net.xml files as shapefile (or kml) files to visualize them in QGIS? I know there is not such a tool yet, but now ´with the OSG support for 3D visualization, I thought maybe with a little fix it would be possible. Any tips for a workaround are very much appreciated. 

Best,
Sasan
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user
# -*- coding: utf-8 -*-
"""
Created on Wed Oct  5 21:49:14 2022

@author: Sasan Amini
"""
def net2polygon(sumoNetPath:str='./sumoNet.net.xml',shapefile:str=True,geojson:str=True):

    import geopandas as gpd
    from shapely.geometry import Polygon
    from sumolib import geomhelper
    import sumolib
    import matplotlib.pyplot as plt
    # read sumo network
    sumoNet = sumolib.net.readNet(sumoNetPath,withInternal=True)
    # read the required features from lanes e.g. lane width, allowed vehicles or speed limit etc. 
    laneId = []
    laneShape = []
    laneWidth = []
    laneAllow = []
    laneSpeed = []
    
    for edge in sumoNet.getEdges():
        for lane in edge.getLanes():
            if lane.getLength()>1 or lane.getLength()==-1: #exclude very short lanes/edges but include internal links with length of -1
                laneId.append(lane.getID())
                laneShape.append(lane.getShape())
                laneWidth.append(lane.getWidth())
                laneAllow.append(list(lane.getPermissions()))
                laneSpeed.append(edge.getSpeed())
    #
    polyLaneShape = {}
    for i in range(len(laneId)):
        geom_list = []
        tmp_coord1 = []
        tmp_coord2 = []
        tmp_geom1 = geomhelper.move2side(laneShape[i],laneWidth[i]/2)
        tmp_geom2 = geomhelper.move2side(laneShape[i],-laneWidth[i]/2)
        for k in tmp_geom1:
            tmp_coord1.append(sumoNet.convertXY2LonLat(k[0],k[1]))
        for k in tmp_geom2:
            tmp_coord2.append(sumoNet.convertXY2LonLat(k[0],k[1]))
        geom_list.append(tmp_coord1)
        geom_list.append(tmp_coord2[::-1])
        polyLaneShape[laneId[i]] = Polygon([item for sublist in geom_list for item in sublist])
        
    # read junctions
    polyNodeShape = {}
    nodeId = []
    nodeType = []
    for node in sumoNet.getNodes():
        nodeId.append(node.getID())
        nodeType.append(node.getType())
    for i in range(len(nodeId)):
        if len(sumoNet.getNode(nodeId[i]).getShape())>2:
            tmp_coord1 = []
            geom_list = []
            tmp_geom1 = sumoNet.getNode(nodeId[i]).getShape()
            for k in tmp_geom1:
                tmp_coord1.append(sumoNet.convertXY2LonLat(k[0],k[1]))
            geom_list.append(tmp_coord1)
        polyNodeShape[laneId[i]] = Polygon([item for sublist in geom_list for item in sublist])
    # convert to geopadas DataFrame
    df_node = gpd.GeoDataFrame({'nodeId':nodeId,'nodeType':nodeType,'geometry':polyNodeShape.values()},crs="EPSG:4326")
    df_lane = gpd.GeoDataFrame({'laneId':laneId,'laneWidth':laneWidth,'maxSpeed':laneSpeed,'geometry':polyLaneShape.values()},crs="EPSG:4326")
    # save the output to file
    if shapefile:
        df_node.to_file('./sumoNetNodes')
        df_lane.to_file('./sumoNetLanes')
    if geojson:
        df_node_json = df_node.to_json()
        df_lane_json = df_lane.to_json()
        with open('sumoNetNodes.geojson', 'w') as outfile:
            outfile.write(df_node_json)
        with open('sumoNetLanes.geojson', 'w') as outfile:
            outfile.write(df_lane_json)
    else:
        print('no output was asked! please set one of the outputs to True')
        return False
    # optional 
    fig = plt.figure(figsize=(50,50))
    for p in polyLaneShape.keys():
        x,y = Polygon(polyLaneShape[p]).exterior.xy
        plt.plot(x,y);
    plt.savefig('./sumoNetPolyLane.png');
    return True 

Back to the top