20 package org.matsim.core.scenario;
22 import com.google.inject.Inject;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
52 import java.io.UncheckedIOException;
76 class ScenarioLoaderImpl {
78 private static final Logger log = LogManager.getLogger(ScenarioLoaderImpl.class);
80 private final Config config;
82 private final MutableScenario scenario;
84 private Map<Class<?>, AttributeConverter<?>> attributeConverters = Collections.emptyMap();
87 public void setAttributeConverters(Map<Class<?>, AttributeConverter<?>> attributeConverters) {
88 log.debug(
"setting "+attributeConverters );
89 this.attributeConverters = attributeConverters;
92 ScenarioLoaderImpl(Config config) {
94 this.scenario = (MutableScenario) ScenarioUtils.createScenario(
this.config);
97 ScenarioLoaderImpl(Scenario scenario) {
98 this.scenario = (MutableScenario) scenario;
99 this.config = this.scenario.getConfig();
108 Scenario loadScenario() {
115 this.loadActivityFacilities();
116 this.loadPopulation();
117 this.loadHouseholds();
119 this.loadTransitVehicles();
120 if (this.config.vehicles().getVehiclesFile()!=null ) {
121 this.loadVehicles() ;
123 if (this.config.network().getLaneDefinitionsFile()!=null ) {
126 return this.scenario;
132 private void loadNetwork() {
133 if ((this.config.network() != null) && (this.config.network().getInputFile() != null)) {
134 URL networkUrl = this.config.network().getInputFileURL(this.config.getContext());
135 log.info(
"loading network from " + networkUrl);
136 String inputCRS = config.network().getInputCRS();
138 MatsimNetworkReader reader =
139 new MatsimNetworkReader(
141 config.global().getCoordinateSystem(),
142 this.scenario.getNetwork());
143 reader.putAttributeConverters( attributeConverters );
144 reader.parse(networkUrl);
146 if ((this.config.network().getChangeEventsInputFile()!= null) && this.config.network().isTimeVariantNetwork()) {
147 log.info(
"loading network change events from " + this.config.network().getChangeEventsInputFileUrl(this.config.getContext()).getFile());
148 Network network = this.scenario.getNetwork();
149 List<NetworkChangeEvent> changeEvents =
new ArrayList<>() ;
150 NetworkChangeEventsParser parser =
new NetworkChangeEventsParser(network,changeEvents);
151 parser.parse(this.config.network().getChangeEventsInputFileUrl(config.getContext()));
152 NetworkUtils.setNetworkChangeEvents(network,changeEvents);
157 private void loadActivityFacilities() {
158 if ((this.config.facilities() != null) && (this.config.facilities().getInputFile() != null)) {
159 URL facilitiesFileName = this.config.facilities().getInputFileURL(config.getContext());
160 log.info(
"loading facilities from " + facilitiesFileName);
162 final String inputCRS = config.facilities().getInputCRS();
163 final String internalCRS = config.global().getCoordinateSystem();
165 MatsimFacilitiesReader reader =
new MatsimFacilitiesReader(inputCRS, internalCRS, this.scenario.getActivityFacilities());
166 reader.putAttributeConverters(attributeConverters);
167 reader.parse(facilitiesFileName);
169 log.info(
"loaded " + this.scenario.getActivityFacilities().getFacilities().size() +
" facilities from " + facilitiesFileName);
172 log.info(
"no facilities file set in config, therefore not loading any facilities. This is not a problem except if you are using facilities");
174 if ((this.config.facilities() != null) && (this.config.facilities().getInputFacilitiesAttributesFile() != null)) {
175 if ( !this.config.facilities().isInsistingOnUsingDeprecatedFacilitiesAttributeFile() ) {
176 throw new RuntimeException(FacilitiesConfigGroup.FACILITIES_ATTRIBUTES_DEPRECATION_MESSAGE) ;
178 URL facilitiesAttributesURL = ConfigGroup.getInputFileURL(this.config.getContext(), this.config.facilities().getInputFacilitiesAttributesFile());
179 log.info(
"loading facility attributes from " + facilitiesAttributesURL);
180 parseObjectAttributesToAttributable(
181 facilitiesAttributesURL,
182 scenario.getActivityFacilities().getFacilities().values(),
183 "facilityAttributes not empty after going through all facilities, meaning that it contains material for facilityIDs that " +
184 "are not in the container. This is not necessarily a bug so we will continue, but note that such material " +
185 "will no longer be contained in the output_* files.");
188 log.info(
"no facility-attributes file set in config, not loading any facility attributes");
193 private void loadPopulation() {
194 if ((this.config.plans() != null) && (this.config.plans().getInputFile() != null)) {
195 URL populationFileName = this.config.plans().getInputFileURL(this.config.getContext());
196 log.info(
"loading population from " + populationFileName);
198 final String targetCRS = config.global().getCoordinateSystem();
199 final String internalCRS = config.global().getCoordinateSystem();
201 final PopulationReader reader =
new PopulationReader(targetCRS, internalCRS, this.scenario);
202 reader.putAttributeConverters( attributeConverters );
203 reader.parse( populationFileName );
205 PopulationUtils.printPlansCount(this.scenario.getPopulation()) ;
208 log.info(
"no population file set in config, not able to load population");
211 if ((this.config.plans() != null) && (this.config.plans().getInputPersonAttributeFile() != null)) {
212 URL personAttributesURL = this.config.plans().getInputPersonAttributeFileURL(this.config.getContext());
213 log.info(
"loading person attributes from " + personAttributesURL);
214 parseObjectAttributesToAttributable(
216 scenario.getPopulation().getPersons().values(),
217 "personAttributes not empty after going through all persons, meaning that it contains material for personIDs that " +
218 "are not in the population. This is not necessarily a bug so we will continue, but note that such material " +
219 "will no longer be contained in the output_* files. (We have this happening in particular when the same personAttributes " +
220 "file is used for the 10pct and the 1pct scenario. The material that is still there will follow. kai, jun'19" 223 final String outputDirectory = this.config.controller().getOutputDirectory();
239 if ( !this.config.plans().isInsistingOnUsingDeprecatedPersonAttributeFile() ) {
244 log.info(
"no person-attributes file set in config, not loading any person attributes");
248 private void loadHouseholds() {
249 if ( (this.config.households() != null) && (this.config.households().getInputFile() != null) ) {
250 URL householdsFile = this.config.households().getInputFileURL(this.config.getContext());
251 log.info(
"loading households from " + householdsFile);
252 HouseholdsReaderV10 reader =
new HouseholdsReaderV10(this.scenario.getHouseholds());
253 reader.putAttributeConverters(this.attributeConverters);
254 reader.parse(householdsFile);
255 log.info(
"households loaded.");
258 log.info(
"no households file set in config, not loading households");
260 if ((this.config.households() != null)) {
261 final String fn = this.config.households().getInputHouseholdAttributesFile();
263 if (!this.config.households().isInsistingOnUsingDeprecatedHouseholdsAttributeFile()) {
264 throw new RuntimeException(HouseholdsConfigGroup.HOUSEHOLD_ATTRIBUTES_DEPRECATION_MESSAGE);
267 URL householdAttributesFileName = ConfigGroup.getInputFileURL(this.config.getContext(), fn ) ;
268 log.info(
"loading household attributes from " + householdAttributesFileName);
269 parseObjectAttributesToAttributable(
270 householdAttributesFileName,
271 this.scenario.getHouseholds().getHouseholds().values(),
272 "householdAttributes not empty after going through all households, meaning that it contains material for householdIDs that " +
273 "are not in the container. This is not necessarily a bug so we will continue, but note that such material " +
274 "will no longer be contained in the output_* files.");
278 log.info(
"no household-attributes file set in config, not loading any household attributes");
282 private void loadTransit() throws UncheckedIOException {
284 if ( this.config.transit().getTransitScheduleFile() != null ) {
285 URL transitScheduleFile = this.config.transit().getTransitScheduleFileURL(this.config.getContext());
286 final String inputCRS = config.transit().getInputScheduleCRS();
287 final String internalCRS = config.global().getCoordinateSystem();
289 new TransitScheduleReader( inputCRS, internalCRS, this.scenario).readURL(transitScheduleFile );
292 log.info(
"no transit schedule file set in config, not loading any transit schedule");
295 if ( this.config.transit().getTransitLinesAttributesFile() != null ) {
296 if (!this.config.transit().isInsistingOnUsingDeprecatedAttributeFiles()) {
297 throw new RuntimeException(TransitConfigGroup.TRANSIT_ATTRIBUTES_DEPRECATION_MESSAGE);
300 URL transitLinesAttributesFileName = IOUtils.extendUrl(this.config.getContext(), this.config.transit().getTransitLinesAttributesFile());
301 log.info(
"loading transit lines attributes from " + transitLinesAttributesFileName);
302 parseObjectAttributesToAttributable(
303 transitLinesAttributesFileName,
304 this.scenario.getTransitSchedule().getTransitLines().values(),
305 "transit lines attributes not empty after going through all lines, meaning that it contains material for line IDs that " +
306 "are not in the container. This is not necessarily a bug so we will continue, but note that such material " +
307 "will no longer be contained in the output_* files.");
310 if ( this.config.transit().getTransitStopsAttributesFile() != null ) {
311 if (!this.config.transit().isInsistingOnUsingDeprecatedAttributeFiles()) {
312 throw new RuntimeException(TransitConfigGroup.TRANSIT_ATTRIBUTES_DEPRECATION_MESSAGE);
315 URL transitStopsAttributesURL = IOUtils.extendUrl(this.config.getContext(), this.config.transit().getTransitStopsAttributesFile());
316 log.info(
"loading transit stop facilities attributes from " + transitStopsAttributesURL);
317 parseObjectAttributesToAttributable(
318 transitStopsAttributesURL,
319 this.scenario.getTransitSchedule().getFacilities().values(),
320 "transit stops attributes not empty after going through all stops, meaning that it contains material for stop IDs that " +
321 "are not in the container. This is not necessarily a bug so we will continue, but note that such material " +
322 "will no longer be contained in the output_* files.");
326 private void loadTransitVehicles() throws UncheckedIOException {
327 final String vehiclesFile = this.config.transit().getVehiclesFile();
328 if ( vehiclesFile != null ) {
329 log.info(
"loading transit vehicles from " + vehiclesFile);
330 new MatsimVehicleReader(this.scenario.getTransitVehicles()).readURL(this.config.transit().getVehiclesFileURL(this.config.getContext() ) );
333 log.info(
"no transit vehicles file set in config, not loading any transit vehicles");
336 private void loadVehicles() throws UncheckedIOException {
337 final String vehiclesFile = this.config.vehicles().getVehiclesFile();
338 if ( vehiclesFile != null ) {
339 log.info(
"loading vehicles from " + vehiclesFile );
340 new MatsimVehicleReader(this.scenario.getVehicles()).readURL(IOUtils.extendUrl(
this.config.getContext(), vehiclesFile ) );
343 log.info(
"no vehicles file set in config, not loading any vehicles");
347 private void loadLanes() {
348 String filename = this.config.network().getLaneDefinitionsFile();
349 if (filename != null){
350 LanesReader reader =
new LanesReader(this.scenario);
351 reader.readURL( ConfigGroup.getInputFileURL(
this.config.getContext(), filename ) );
354 log.info(
"no lanes file set in config, not loading any lanes");
358 private <T extends Identifiable<?> & Attributable>
void parseObjectAttributesToAttributable(
362 final ObjectAttributes attributes =
new ObjectAttributes();
363 ObjectAttributesXmlReader reader =
new ObjectAttributesXmlReader(attributes);
364 reader.putAttributeConverters( attributeConverters );
367 for( T facility : attributables ) {
368 Collection<String> keys = ObjectAttributesUtils.getAllAttributeNames( attributes, facility.getId().toString() );
369 for( String key : keys ){
370 Object value = attributes.getAttribute( facility.getId().toString(), key );
371 facility.getAttributes().putAttribute( key, value ) ;
373 attributes.removeAllAttributes( facility.getId().toString() );
378 if ( !attributes.toString().equals(
"" ) ) {
379 log.warn( message ) ;
380 log.warn(
"showing the first 1000 characters from the remaining personAttributes ...") ;
381 log.warn( attributes.toString().substring( 0, Math.min(attributes.toString().length(), 1000 ) ) );
static final String PERSON_ATTRIBUTES_DEPRECATION_MESSAGE