1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2016-2023 German Aerospace Center (DLR) and others.
# SUMOPy module
# Copyright (C) 2012-2021 University of Bologna - DICAM
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file scenario.py
# @author Joerg Schweizer
# @date 2012
import numpy as np
from coremodules.misc import shapeformat
from coremodules.simulation import simulation
from coremodules.demand import demand
from coremodules.landuse import landuse
from coremodules.network import network
from agilepy.lib_base.processes import Process
import agilepy.lib_base.arrayman as am
import agilepy.lib_base.classman as cm
import os
import sys
if __name__ == '__main__':
try:
FILEDIR = os.path.dirname(os.path.abspath(__file__))
except:
FILEDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.append(os.path.join(FILEDIR, "..", ".."))
# this is default scenario directory
DIRPATH_SCENARIO = os.path.join(os.path.expanduser("~"), 'Sumo')
# os.path.join(os.getcwd(),'testscenario')
def load_scenario(filepath, logger=None):
scen = cm.load_obj(filepath, parent=None)
# scenario.set_workdirpath(os.path.dirname(filepath))
# this will set rootname and workdir
if scen is not None:
scen.set_filepath(filepath)
# print 'load_scenario', scen.get_rootfilepath(),'workdirpath',scen.workdirpath
if logger is not None:
scen.set_logger(logger)
return scen
class ScenarioCreator(Process):
def __init__(self, rootname='myscenario', name_scenario='My Scenario', workdirpath=None, description='', logger=None):
# init process
self._init_common('scenariocreator', name='New Scenario', logger=logger)
if workdirpath is None:
#workdirpath = os.getcwd()
workdirpath = os.path.expanduser("~")
attrsman = self.get_attrsman()
self.rootname = attrsman.add(cm.AttrConf('rootname', rootname,
groupnames=['options'],
perm='rw',
name='Shortname',
info='Short name for scenario. This string is used as rootname for all files produced by this scenario. Please avoid special charracters, whitespace, accents etc. ASCII is recommented in order to remain compatible between operating systems.',
))
self.name_scenario = attrsman.add(cm.AttrConf('name_scenario', name_scenario,
groupnames=['options'],
perm='rw',
name='Name',
info='Scenario name, used for documentation purposes only.',
))
self.description = attrsman.add(cm.AttrConf('description', description,
groupnames=['options'],
perm='rw',
name='Description',
info='Short, free description of Scenario.',
))
self.workdirpath = attrsman.add(cm.AttrConf('workdirpath', workdirpath,
groupnames=['options'],
perm='rw',
name='Workdir',
metatype='dirpath',
info='Working directory for this scenario.',
))
def do(self):
# print 'do',self.newident
self._scenario = Scenario(self.rootname,
name_scenario=self.name_scenario,
description=self.description,
parent=None,
workdirpath=self.workdirpath,
logger=self.get_logger(),
)
return True
def get_scenario(self):
return self._scenario
# class OxScenariocreator(ScenarioCreator):
# def __init__(self, **kwargs):
##
# ScenarioCreator.__init__(self,**kwargs)
##
# nodeshapefilepath, edgeshapefilepath, polyshapefilepath,
## attrsman = self.get_attrsman()
# self.nodeshapefilepath = attrsman.add(
# cm.AttrConf('nodeshapefilepath',kwargs.get('nodeshapefilepath',''),
## groupnames = ['options'],
# perm='rw',
## name = 'Nodes shapefile',
## wildcards = 'Shape file (*.shp)|*.shp;*.SHP',
## metatype = 'filepaths',
## info = """Shapefile path for nodes.""",
# ))
##
# self.edgeshapefilepath = attrsman.add(
# cm.AttrConf('edgeshapefilepath',kwargs.get('edgeshapefilepath',''),
## groupnames = ['options'],
# perm='rw',
## name = 'Edge shapefile',
## wildcards = 'Shape file (*.shp)|*.shp;*.SHP',
## metatype = 'filepaths',
## info = """Shapefile path for edges.""",
# ))
##
# self.polyshapefilepath = attrsman.add(
# cm.AttrConf('polyshapefilepath',kwargs.get('polyshapefilepath',''),
## groupnames = ['options'],
# perm='rw',
## name = 'Poly shapefile',
## wildcards = 'Shape file (*.shp)|*.shp;*.SHP',
## metatype = 'filepaths',
## info = """Shapefile path for polygons.""",
# ))
##
##
# def do(self):
# print 'do',self.newident
# if ScenarioCreator.do(self):
##
## scenario = self.get_scenario()
# return shapeformat.OxImporter(scenario,
## self.nodeshapefilepath, self.edgeshapefilepath, self.polyshapefilepath,
## ident = 'oximporter',
## name = 'OSMnx importer',
## info = 'Import of network imported with the help of osmnx.',
## logger =self.get_logger()
# ).do()
# else:
# return False
##
class Scenario(cm.BaseObjman):
def __init__(self, rootname, name_scenario='myscenario',
description='', parent=None,
workdirpath=None, **kwargs):
self._init_objman(ident='scenario', parent=parent,
name='Scenario', info='Main scenario instance.',
version=0.2,
**kwargs)
attrsman = self.set_attrsman(cm.Attrsman(self))
if workdirpath is not None:
# create a directory if path is given, but does not exist
if not os.path.isdir(workdirpath):
os.mkdir(workdirpath)
else:
workdirpath = os.getcwd()
#workdirpath = os.path.expanduser("~")
self.name_scenario = attrsman.add(cm.AttrConf('name_scenario', name_scenario,
groupnames=['options'],
perm='rw',
name='Name',
info='Scenario name, used for documentation purposes only.',
))
self.description = attrsman.add(cm.AttrConf('description', description,
groupnames=['options'],
perm='rw',
name='Description',
info='Short, free description of Scenario.',
))
self.rootname = attrsman.add(cm.AttrConf('rootname', rootname,
groupnames=['options'],
perm='r',
is_save=True,
name='Shortname',
info='Short name for scenario. This string is defined when saving the scenario. It is used as rootname for all files produced by this scenario. Please avoid special charracters, whitespace, accents etc. ASCII is recommented in order to remain compatible between operating systems.',
))
self.workdirpath = attrsman.add(cm.AttrConf('workdirpath', workdirpath,
groupnames=['options'],
perm='r',
is_save=True,
name='Workdir',
metatype='dirpath',
info='Working directory for this scenario and can be changed when saving the scenario. Please avoid special charracters, whitespace, accents etc. ASCII is recommented in order to remain compatible between operating systems.',
))
self.net = attrsman.add(cm.ObjConf(network.Network(self)))
self.landuse = attrsman.add(cm.ObjConf(landuse.Landuse(self, self.net)))
self.demand = attrsman.add(cm.ObjConf(demand.Demand(self)))
# if self.get_version()<0.2:
# self.delete('simulation')
self._init_attributes()
def _init_attributes(self):
print('Scenario._init_attributes') # ,dir(self)
attrsman = self.get_attrsman()
self.simulation = attrsman.add(cm.ObjConf(simulation.Simulation(self)))
self.set_version(0.2)
# print ' finish Scenario._init_attributes'
def set_filepath(self, filepath):
"""
A new filepath will set the shortname and workdir.
"""
names = os.path.basename(filepath).split('.')
dirname = os.path.dirname(filepath)
if len(names) >= 3:
rootname = '.'.join(names[:-2])
elif len(names) <= 2:
rootname = names[0]
self.set_workdirpath(dirname)
self.set_rootfilename(rootname)
def save(self, filepath=None):
if filepath is None:
filepath = self.get_rootfilepath()+'.obj'
self.set_filepath(filepath)
cm.save_obj(self, filepath, is_not_save_parent=False)
return filepath
def get_workdirpath(self):
return self.workdirpath
def set_workdirpath(self, workdirpath):
self.workdirpath = workdirpath
def set_rootfilename(self, filename):
self.rootname = filename
def get_rootfilename(self):
"""
Centralized definition of filename bases.
"""
return self.rootname
def get_rootfilepath(self):
return os.path.join(self.get_workdirpath(), self.get_rootfilename())
def import_xml(self, is_clean_nodes=False):
"""
Try to import xml-type files into scenario.
"""
# print 'import_xml'
netfilepath = self.net.get_filepath()
if os.path.isfile(netfilepath):
# convert and import edg,nod,con,tll...
self.net.import_netxml(filepath=netfilepath, rootname=self.get_rootfilename())
else:
# import edg,nod,con,tll...
self.net.import_xml(self.get_rootfilename(), self.get_workdirpath(), is_clean_nodes=is_clean_nodes)
self.landuse.import_polyxml(self.get_rootfilename(), self.get_workdirpath())
try:
self.demand.import_xml(self.get_rootfilename(), self.get_workdirpath())
except:
print('WARNING: import of demand data failed. Please check for inconsistency with trip/route and network edge IDs.')
def update_netoffset(self, deltaoffset):
"""
Called when network offset has changed.
Children may need to adjust theur coordinates.
"""
self.landuse.update_netoffset(deltaoffset)
self.demand.update_netoffset(deltaoffset)
if __name__ == '__main__':
###############################################################################
# print 'sys.path',sys.path
from agilepy.lib_wx.objpanel import objbrowser
from agilepy.lib_base.logger import Logger
if len(sys.argv) == 3:
rootname = sys.argv[1]
dirpath = sys.argv[2]
else:
rootname = 'facsp2'
dirpath = os.path.join(os.path.dirname(__file__), '..', 'network', 'testnet')
scenario = Scenario(rootname, workdirpath=dirpath, logger=Logger())
# net.import_nodes(os.path.join('test','facsp2.nod.xml'))
# net.import_edges(os.path.join('test','facsp2.edg.xml'))
objbrowser(scenario)
|