Skip to content

Commit

Permalink
improving net parsing speed with lxml #12
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Apr 4, 2024
1 parent 3a210d3 commit 297f0d2
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions tools/sumolib/net/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
from collections import defaultdict
from itertools import chain

try:
import lxml.etree
HAVE_LXML = True
except ImportError:
HAVE_LXML = False

import sumolib
from . import lane, edge, netshiftadaptor, node, connection, roundabout # noqa
from .connection import Connection
Expand Down Expand Up @@ -939,10 +945,21 @@ def readNet(filename, **others):
'withFoes' : import right-of-way information (default True)
'withInternal' : import internal edges and lanes (default False)
'withPedestrianConnections' : import connections between sidewalks, crossings (default False)
'lxml' : set to False to use the xml.sax parser instead of the lxml parser
"""
netreader = NetReader(**others)
try:
parse(gzip.open(filename), netreader)
source = gzip.open(filename)
source.read(10)
source.seek(0)
except IOError:
parse(filename, netreader)
source = filename
if HAVE_LXML and others.get("lxml", True):
for event, v in lxml.etree.iterparse(source, events=("start", "end")):
if event == "start":
netreader.startElement(v.tag, v.attrib)
elif event == "end":
netreader.endElement(v.tag)
else:
parse(source, netreader)
return netreader.getNet()

0 comments on commit 297f0d2

Please sign in to comment.