-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
5,164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@file __init__.py | ||
@author Michael Behrisch | ||
@date 2011-06-23 | ||
@version $Id: __init__.py 13845 2013-05-02 13:53:19Z dkrajzew $ | ||
Python interface to SUMO especially for parsing xml input and output files. | ||
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ | ||
Copyright (C) 2011-2013 DLR (http://www.dlr.de/) and contributors | ||
All rights reserved | ||
""" | ||
import net, shapes, output | ||
|
||
import os, subprocess | ||
from xml.sax import parseString, handler | ||
from optparse import OptionParser, OptionGroup, Option | ||
|
||
class ConfigurationReader(handler.ContentHandler): | ||
"""Reads a configuration template, storing the options in an OptionParser""" | ||
|
||
def __init__(self, optParse, groups, options): | ||
self._opts = optParse | ||
self._groups = groups | ||
self._options = options | ||
self._group = self._opts | ||
|
||
def startElement(self, name, attrs): | ||
if len(attrs) == 0: | ||
self._group = OptionGroup(self._opts, name) | ||
if self._group != self._opts and self._groups and self._group.title not in self._groups: | ||
return | ||
if "type" in attrs and name != "help": | ||
if self._options and name not in self._options: | ||
return | ||
help = attrs.get("help", "") | ||
option = Option("--" + name, help=help) | ||
if attrs["type"] == "BOOL": | ||
option = Option("--" + name, action="store_true", default=False, help=help) | ||
elif attrs["type"] in ["FLOAT", "TIME"]: | ||
option.type = "float" | ||
if attrs["value"]: | ||
option.default = float(attrs["value"]) | ||
elif attrs["type"] == "INT": | ||
option.type = "int" | ||
if attrs["value"]: | ||
option.default = int(attrs["value"]) | ||
else: | ||
option.default = attrs["value"] | ||
self._group.add_option(option) | ||
|
||
def endElement(self, name): | ||
if self._group != self._opts and name == self._group.title: | ||
self._opts.add_option_group(self._group) | ||
self._group = self._opts | ||
|
||
|
||
def pullOptions(executable, optParse, groups=None, options=None): | ||
output = subprocess.Popen([executable, "--save-template", "-"], stdout=subprocess.PIPE).communicate()[0] | ||
parseString(output, ConfigurationReader(optParse, groups, options)) | ||
|
||
def saveConfiguration(executable, options, filename): | ||
options.save_configuration = filename | ||
call(executable, options) | ||
|
||
def call(executable, options): | ||
optParser = OptionParser() | ||
pullOptions(executable, optParser) | ||
cmd = [executable] | ||
for option, value in options.__dict__.iteritems(): | ||
o = "--" + option.replace("_", "-") | ||
opt = optParser.get_option(o) | ||
if opt is not None and value is not None and opt.default != value: | ||
cmd.append(o) | ||
if opt.action != "store_true": | ||
cmd.append(str(value)) | ||
return subprocess.call(cmd) | ||
|
||
def exeExists(binary): | ||
if os.name == "nt" and binary[-4:] != ".exe": | ||
binary += ".exe" | ||
return os.path.exists(binary) | ||
|
||
def checkBinary(name, bindir=None): | ||
"""Checks for the given binary in the places, defined by the environment variables SUMO_HOME and SUMO_BINDIR.""" | ||
if name == "sumo-gui": | ||
envName = "GUISIM_BINARY" | ||
else: | ||
envName = name.upper() + "_BINARY" | ||
env = os.environ | ||
join = os.path.join | ||
if envName in env and exeExists(env.get(envName)): | ||
return env.get(envName) | ||
if bindir is not None: | ||
binary = join(bindir, name) | ||
if exeExists(binary): | ||
return binary | ||
if "SUMO_BINDIR" in env: | ||
binary = join(env.get("SUMO_BINDIR"), name) | ||
if exeExists(binary): | ||
return binary | ||
if "SUMO_HOME" in env: | ||
binary = join(env.get("SUMO_HOME"), "bin", name) | ||
if exeExists(binary): | ||
return binary | ||
binary = os.path.abspath(join(os.path.dirname(__file__), '..', '..', 'bin', name)) | ||
if exeExists(binary): | ||
return binary | ||
return name | ||
|
||
class _Running: | ||
""" | ||
A generator of running, numerical IDs | ||
Should be enhanced by: | ||
- a member method for returning the size | ||
- a member iterator over the stored ids | ||
""" | ||
def __init__(self): | ||
"""Contructor""" | ||
self._m = {} | ||
|
||
def g(self, id): | ||
""" | ||
If the given id is known, the numerical representation is returned, | ||
otherwise a new running number is assigned to the id and returned""" | ||
if id not in self._m: | ||
self._m[id] = len(self._m) | ||
return len(self._m)-1 | ||
return self._m[id] | ||
|
||
def k(self, id): | ||
""" | ||
Returns whether the given id is known.""" | ||
return id in self._m | ||
|
||
def d(self, id): | ||
""" | ||
Removed the element.""" | ||
del self._m[id] | ||
|
||
|
||
|
||
def _intTime(tStr): | ||
""" | ||
Converts a time given as a string containing a float into an integer representation. | ||
""" | ||
return int(float(tStr)) | ||
|
||
|
||
def _laneID2edgeID(laneID): | ||
return laneID[:laneID.rfind("_")] |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This file is placed here by pip to indicate the source was put | ||
here by pip. | ||
|
||
Once this package is successfully installed this source code will be | ||
deleted (unless you remove this file). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
@file color.py | ||
@author Daniel Krajzewicz | ||
@date 2012-12-04 | ||
@version $Id: color.py 13811 2013-05-01 20:31:43Z behrisch $ | ||
Library for reading and encoding of colors. | ||
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ | ||
Copyright (C) 2010-2013 DLR (http://www.dlr.de/) and contributors | ||
All rights reserved | ||
""" | ||
|
||
from xml.sax import handler, parse | ||
|
||
|
||
class RGBAColor: | ||
def __init__(self, r, g, b, a=None): | ||
self.r = r | ||
self.g = g | ||
self.b = b | ||
self.a = a | ||
def toXML(self): | ||
if self.a!=None: | ||
return "%s,%s,%s,%s" % (self.r, self.g, self.b, self.a) | ||
else: | ||
return "%s,%s,%s" % (self.r, self.g, self.b) | ||
def decodeXML(c): | ||
return [float(x) for x in c.split(",")] | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
@file geomhelper.py | ||
@author Daniel Krajzewicz | ||
@date 2013-02-25 | ||
@version $Id: geomhelper.py 13886 2013-05-06 10:18:57Z namdre $ | ||
Some helper functions for geometrical computations. | ||
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ | ||
Copyright (C) 2008-2013 DLR (http://www.dlr.de/) and contributors | ||
All rights reserved | ||
""" | ||
import math | ||
|
||
INVALID_DISTANCE = -1 | ||
|
||
def distance(p1, p2): | ||
dx = p1[0]-p2[0] | ||
dy = p1[1]-p2[1] | ||
return math.sqrt(dx*dx + dy*dy) | ||
|
||
|
||
def lineOffsetWithMinimumDistanceToPoint(point, line_start, line_end, perpendicular=False): | ||
"""Return the offset from line (line_start, line_end) where the distance to | ||
point is minimal""" | ||
p = point | ||
p1 = line_start | ||
p2 = line_end | ||
l = distance(p1, p2) | ||
u = (((p[0] - p1[0]) * (p2[0] - p1[0])) + ((p[1] - p1[1]) * (p2[1] - p1[1]))) / (l * l) | ||
if u < 0.0 or u > 1: | ||
if perpendicular: | ||
return INVALID_DISTANCE | ||
if u < 0: | ||
return 0 | ||
return l | ||
return u * l | ||
|
||
|
||
def polygonOffsetWithMinimumDistanceToPoint(point, polygon): | ||
"""Return the offset from the polygon start where the distance to point is minimal""" | ||
p = point | ||
s = polygon | ||
o = 0 | ||
for i in range(0, len(s)-1): | ||
q = lineOffsetWithMinimumDistanceToPoint(p, s[i], s[i+1], True) | ||
if q!=-1: | ||
return o+q | ||
o = o + distance(s[i], s[i+1]) | ||
return -1 | ||
|
||
|
||
def distancePointToLine(point, line_start, line_end, perpendicular=False): | ||
"""Return the minimum distance between point and the line (line_start, line_end)""" | ||
p1 = line_start | ||
p2 = line_end | ||
u = lineOffsetWithMinimumDistanceToPoint(point, line_start, line_end, perpendicular) | ||
if u == INVALID_DISTANCE: | ||
return INVALID_DISTANCE | ||
intersection = (p1[0] + u*(p2[0]-p1[0]), p1[1] + u*(p2[1]-p1[1])) | ||
return distance(point, intersection) | ||
|
||
|
||
def distancePointToPolygon(point, polygon, perpendicular=True): | ||
"""Return the minimum distance between point and polygon""" | ||
p = point | ||
s = polygon | ||
minDist = None | ||
for i in range(0, len(s)-1): | ||
dist = distancePointToLine(p, s[i], s[i+1], perpendicular) | ||
if dist != INVALID_DISTANCE: | ||
if minDist is None or dist < minDist: | ||
minDist = dist | ||
if minDist is not None: | ||
return minDist | ||
else: | ||
return INVALID_DISTANCE | ||
|
||
|
Oops, something went wrong.