21 package org.matsim.core.network.algorithms;
23 import java.io.FileInputStream;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.TreeMap;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.geotools.api.data.SimpleFeatureSource;
32 import org.geotools.api.feature.simple.SimpleFeature;
33 import org.geotools.data.shapefile.dbf.DbaseFileReader;
34 import org.geotools.data.simple.SimpleFeatureIterator;
114 log.info(
"init " + this.getClass().getName() +
" module...");
170 log.info(
"running " + this.getClass().getName() +
" module...");
173 TreeMap<String, TreeMap<Integer,Id<Link>>> mSequences =
new TreeMap<>();
175 try (FileInputStream fis =
new FileInputStream(this.mpDbfFileName)) {
178 int mpIdNameIndex = -1;
179 int mpSeqNrNameIndex = -1;
180 int mpTrpelIDNameIndex = -1;
181 for (
int i=0; i<r.getHeader().getNumFields(); i++) {
182 if (r.getHeader().getFieldName(i).equals(MP_ID_NAME)) { mpIdNameIndex = i; }
183 if (r.getHeader().getFieldName(i).equals(MP_SEQNR_NAME)) { mpSeqNrNameIndex = i; }
184 if (r.getHeader().getFieldName(i).equals(MP_TRPELID_NAME)) { mpTrpelIDNameIndex = i; }
186 if (mpIdNameIndex < 0) {
throw new NoSuchFieldException(
"Field name '"+MP_ID_NAME+
"' not found."); }
187 if (mpSeqNrNameIndex < 0) {
throw new NoSuchFieldException(
"Field name '"+MP_SEQNR_NAME+
"' not found."); }
188 if (mpTrpelIDNameIndex < 0) {
throw new NoSuchFieldException(
"Field name '"+MP_TRPELID_NAME+
"' not found."); }
189 log.trace(
" FieldName-->Index:");
190 log.trace(
" "+MP_ID_NAME+
"-->"+mpIdNameIndex);
191 log.trace(
" "+MP_SEQNR_NAME+
"-->"+mpSeqNrNameIndex);
192 log.trace(
" "+MP_TRPELID_NAME+
"-->"+mpTrpelIDNameIndex);
196 log.info(
" parsing meneuver paths dbf file...");
197 while (r.hasNext()) {
198 Object[] entries = r.readEntry();
199 String mpId = entries[mpIdNameIndex].toString();
200 int mpSeqNr = Integer.parseInt(entries[mpSeqNrNameIndex].toString());
202 TreeMap<Integer,Id<Link>> mSequence = mSequences.get(mpId);
203 if (mSequence == null) {
204 mSequence =
new TreeMap<>();
205 mSequences.put(mpId, mSequence);
207 if (mSequence.put(mpSeqNr,linkId) != null) {
209 throw new IllegalArgumentException(MP_ID_NAME+
"="+mpId+
": "+MP_SEQNR_NAME+
" "+mpSeqNr+
" already exists.");
212 log.info(
" "+mSequences.size()+
" maneuvers sequences stored.");
219 log.info(
" parsing meneuver shape file...");
220 TreeMap<Id<Node>, ArrayList<Tuple<String, Integer>>> maneuvers =
new TreeMap<>();
222 SimpleFeatureIterator fIt = fs.getFeatures().features();
223 while (fIt.hasNext()) {
224 SimpleFeature f = fIt.next();
225 int featType = Integer.parseInt(f.getAttribute(MN_FEATTYP_NAME).toString());
226 if ((featType == 2103) || (featType == 2102) || (featType == 2101)) {
229 ArrayList<Tuple<String, Integer>> ms = maneuvers.get(nodeId);
231 ms =
new ArrayList<>();
235 maneuvers.put(nodeId, ms);
237 else if ((featType == 9401) || (featType == 2104)) {
241 throw new IllegalArgumentException(
"mnId="+f.getAttribute(MN_ID_NAME)+
": "+MN_FEATTYP_NAME+
"="+featType+
" not known.");
245 log.info(
" "+maneuvers.size()+
" nodes with maneuvers stored.");
250 log.info(
" expand nodes according to the given manveuvers...");
251 int nodesIgnoredCnt = 0;
252 int nodesAssignedCnt = 0;
253 int maneuverIgnoredCnt = 0;
254 int maneuverAssignedCnt = 0;
255 int virtualNodesCnt = 0;
256 int virtualLinksCnt = 0;
259 if (network.getNodes().get(nodeId) == null) {
260 log.trace(
" nodeid="+nodeId+
": maneuvers exist for that node but node is missing. Ignoring and proceeding anyway...");
264 Node n = network.getNodes().get(nodeId);
267 TreeMap<Id<Link>, TreeMap<Id<Link>, Boolean>> mmatrix =
new TreeMap<>();
269 ArrayList<Tuple<String, Integer>> ms = entry.getValue();
272 TreeMap<Integer,Id<Link>> mSequence = mSequences.get(m.getFirst());
273 if (mSequence == null) {
throw new Exception(
"nodeid="+nodeId+
"; mnId="+m.getFirst()+
": no maneuver sequence given."); }
274 if (mSequence.size() < 2) {
throw new Exception(
"nodeid="+nodeId+
"; mnId="+m.getFirst()+
": mSequenceSize="+mSequence.size()+
" not alowed!"); }
276 Id<Link> firstLinkid = mSequence.values().iterator().next();
278 for (
Id<Link> otherLinkId : mSequence.values()) {
281 if (inLink == null) {
285 if (outLink == null) {
288 if ((inLink != null) && (outLink != null)) {
290 if (m.getSecond() == 2102) {
293 TreeMap<Id<Link>, Boolean> outLinkMap = mmatrix.get(inLink.
getId());
294 if (outLinkMap == null) {
295 outLinkMap =
new TreeMap<>();
297 outLinkMap.put(outLink.
getId(), Boolean.TRUE);
298 mmatrix.put(inLink.
getId(),outLinkMap);
303 TreeMap<Id<Link>,Boolean> outLinkMap = mmatrix.get(inLink.
getId());
304 if (outLinkMap == null) {
305 outLinkMap =
new TreeMap<>();
307 outLinkMap.put(outLink.
getId(), Boolean.FALSE);
308 mmatrix.put(inLink.
getId(),outLinkMap);
310 maneuverAssignedCnt++;
312 else { maneuverIgnoredCnt++; }
316 for (TreeMap<
Id<Link>, Boolean> fromLinkEntry : mmatrix.values()) {
318 boolean hasRestrictedManeuver =
false;
319 for (Boolean b : fromLinkEntry.values()) {
320 if (b) { hasRestrictedManeuver =
true; }
324 if (!fromLinkEntry.containsKey(toLinkId)) {
325 fromLinkEntry.put(toLinkId, !hasRestrictedManeuver);
331 if (!mmatrix.containsKey(fromLinkId)) {
332 mmatrix.put(fromLinkId,
new TreeMap<
Id<Link>, Boolean>());
334 mmatrix.get(fromLinkId).put(toLinkId, Boolean.TRUE);
339 if (this.removeUTurns) {
341 String str1 = fromLinkId.toString().substring(0,fromLinkId.toString().length()-2);
343 String str2 = toLinkId.toString().substring(0,toLinkId.toString().length()-2);
344 if (str1.equals(str2)) {
345 mmatrix.get(fromLinkId).put(toLinkId, Boolean.FALSE);
351 ArrayList<TurnInfo> turns =
new ArrayList<>();
352 for (Map.Entry<
Id<Link>, TreeMap<
Id<Link>, Boolean>> fromLinkEntry : mmatrix.entrySet()) {
353 Id<Link> fromLinkId = fromLinkEntry.getKey();
354 for (Map.Entry<
Id<Link>, Boolean> toLinkEntry : fromLinkEntry.getValue().entrySet()) {
355 if (toLinkEntry.getValue()) {
356 turns.add(
new TurnInfo(fromLinkId, toLinkEntry.getKey()));
362 virtualNodesCnt += t.
getFirst().size();
367 log.info(
" "+nodesAssignedCnt+
" nodes expanded.");
368 log.info(
" "+maneuverAssignedCnt+
" maneuvers assigned.");
369 log.info(
" "+virtualNodesCnt+
" new nodes created.");
370 log.info(
" "+virtualLinksCnt+
" new links created.");
371 log.info(
" "+nodesIgnoredCnt+
" nodes with given maneuvers (2103, 2102 or 2101) ignored.");
372 log.info(
" "+maneuverIgnoredCnt+
" maneuvers ignored (while node was found).");
387 System.out.println(prefix+
"configuration of "+this.getClass().getName()+
":");
388 System.out.println(prefix+
" options:");
389 System.out.println(prefix+
" removeUTurns: "+removeUTurns);
390 System.out.println(prefix+
" expansionRadius: "+expansionRadius);
391 System.out.println(prefix+
" linkSeparation: "+linkSeparation);
392 System.out.println(prefix+
" maneuver shape:");
393 System.out.println(prefix+
" mnShpFileName: "+mnShpFileName);
394 System.out.println(prefix+
" MN_ID_NAME: "+MN_ID_NAME);
395 System.out.println(prefix+
" MN_FEATTYP_NAME: "+MN_FEATTYP_NAME);
396 System.out.println(prefix+
" MN_JNCTID_NAME: "+MN_JNCTID_NAME);
397 System.out.println(prefix+
" maneuver path dbf:");
398 System.out.println(prefix+
" mpDbfFileName: "+mpDbfFileName);
399 System.out.println(prefix+
" MP_ID_NAME: "+MP_ID_NAME);
400 System.out.println(prefix+
" MP_SEQNR_NAME: "+MP_SEQNR_NAME);
401 System.out.println(prefix+
" MP_TRPELID_NAME: "+MP_TRPELID_NAME);
402 System.out.println(prefix+
"done.");
NetworkTeleatlasAddManeuverRestrictions(final String mnShpFileName, final String mpDbfFileName)
static final String MN_FEATTYP_NAME
final String mnShpFileName
Map< Id< Link >, ? extends Link > getInLinks()
void run(final Network network)
static final Charset CHARSET_WINDOWS_ISO88591
static final String MP_TRPELID_NAME
final Tuple< List< Node >, List< Link > > expandNode(final Id< Node > nodeId, final List< TurnInfo > turns)
void run2(final Network network)
static< T > Id< T > create(final long key, final Class< T > type)
final void printInfo(final String prefix)
static SimpleFeatureSource readDataFile(final String filename)
static final String MN_JNCTID_NAME
static final String MP_SEQNR_NAME
static final String MP_ID_NAME
Map< Id< Link >, ? extends Link > getOutLinks()
final String mpDbfFileName
static final String MN_ID_NAME