Skip to content

CWN at slashed bannered routes #528

Open

Description

@yakra

from #420:

https://forum.travelmapping.net/index.php?topic=4015.msg22719#msg22719
nc.us015;US15/401Bus;;;LABEL_SELFREF;
There are a lot of these cases in HighwayData. The idea is, it's (US15/401)(Bus) rather than (US15)/(401Bus).
It makes sense to continue to allow them, and not force a bunch of conversions to US15Bus/401Bus

bool Waypoint::banner_after_slash(const char* slash)
{ // return whether colocated with a bannered route of same designation whose banner
// is contained in this waypoint's label after a slash, before underscore if exists
if (colocated)
for (Waypoint* w : *colocated)
if (w != this && w->route->banner.size() && w->route->route == route->route && strdstr(slash+1, w->route->banner.data(), '_'))
return 1;
return 0;
}

def banner_after_slash(self, postslash):
"""return whether colocated with a bannered route of same designation whose banner
is contained in this waypoint's label after a slash, before underscore if exists"""
if self.colocated is not None:
for w in self.colocated:
if w != self and len(w.route.banner) and w.route.route == self.route.route and w.route.banner in postslash:
return True
return False

These functions were tailor-made for the LABEL_SELFREF datacheck & can't be used for CWN purposes without modification.
route->route / self.route.route can be replaced with a reference passed into the function.
As for how we'd wanna tie this into the CWN routine itself, I haven't thought that through yet.

Useful in 3+_intersection cases. Others?

NH11TrkWCl@NH12/103_E&NH12@NH12TrkWCl_N&NH12TrkWCl@NH12_N&NH103@NH11/12TrkWCl
NH11TrkWCl@NH120_N&NH12TrkECl@NH120_N&NH103TrkCla@NH120_N&NH120@NH11/12Trk

could become

NH11TrkWCl/NH12_N/NH12TrkWCl_N/NH103_E
NH11TrkWCl/NH12TrkECl/NH103TrkCla/NH120_N

Maybe. Or something. Haven't fully thought it through yet.

Activity

  1. added
    CWN
    on May 1, 2022
  2. yakra commented on May 1, 2022

    @yakra
    ContributorAuthor

    route->route / self.route.route can be replaced with a reference passed into the function.

    Not quite.

    • Datacheck iterates through *colocated.
    • CWN wants to iterate through ap_coloc.
  3. yakra commented on May 2, 2022

    @yakra
    ContributorAuthor

    Here's where I'm at with this...

    I'll link to the existing version of the 3+_intersection code like this so GitHub doesn't paste the whole thing in. This'll already be a long post (apologies in advance)...
    To start off, let's convert from a traditional C-style for loop to a C++11 Range-based for loop, for brevity & readability.
    This gives us an easier basis of comparison for the in-dev version I'll paste in next...

    if (ap_coloc.size() > 2)
    {	bool match;
    	std::vector<std::string> suffixes(ap_coloc.size());
    	for (Waypoint* check : ap_coloc)
    	{	match = 0;
    		for (unsigned int other = 0; other < ap_coloc.size(); other++)
    		{	if (ap_coloc[other] == check) continue;
    			std::string other_no_abbrev = ap_coloc[other]->route->name_no_abbrev();
    			if ( !strncmp(check->label.data(), other_no_abbrev.data(), other_no_abbrev.size()) )
    			{	// should check here for false matches, like
    				// NY50/67 would match startswith NY5
    				match = 1;
    				const char* suffix = strchr(check->label.data(), '_');
    				if (suffix
    				// we confirmed above that other_no_abbrev matches, so skip past it
    				// we need only match the suffix with or without the abbrev
    				 && (!strcmp(check->label.data()+other_no_abbrev.size(), suffix)
    				  || check->label.data()+other_no_abbrev.size() == ap_coloc[other]->route->abbrev + suffix
    				   ))	suffixes[other] = suffix;
    			}
    		}
    		if (!match) break;
    	}
    	if (match)
    	// ... nothing different here
    }

    In addition to the main purpose of this issue as noted in the OP, let's also take care of this longstanding comment while making changes:

    # should check here for false matches, like
    # NY50/67 would match startswith NY5

    Here's what I'm working with now:

    bool match;
    std::vector<const char*> suffixes(ap_coloc.size(), "");
    for (Waypoint* check : ap_coloc)
    {	match = 0;
    	for (unsigned int other = 0; other < ap_coloc.size(); other++)
    	{	if (ap_coloc[other] == check) continue;
    		const char* endmatch = ap_coloc[other]->route->label_match(check->label.data());
    		if (endmatch)
    		{	match = 1;
    			const char* suffix = strchr(endmatch, '_');
    			if (suffix && endmatch == suffix)
    			  suffixes[other] = suffix;
    		}
    	}
    	if (!match) break;
    }

    Now let's examine the changes in detail.

    std::vector<std::string> suffixes(ap_coloc.size());
    /*   ->   */
    std::vector<const char*> suffixes(ap_coloc.size(), "");

    ^This is just a minor efficiency tweak; no change in output. No need to create new string objects when pointers into the existing char array work fine.

    std::string other_no_abbrev is gonzo, replaced with a new function that goes through a route's .list name components (route, banner, abbrev) in finer detail. This new function is called here:

    const char* endmatch = ap_coloc[other]->route->label_match(check->label.data());

    Here's how it works under the hood:

    const char* Route::label_match(const char* ptr)
    {	// Determine whether a specified string matches as a waypoint label for this route.
    	// Don't require banner to immediately follow route; allow it to be after a slash
    	// (but not an _) for cases of different-numbered routes both with the same banner.
    	// Returns null pointer if no match, otherwise a pointer to the next character
    	// in the string after the match. This can be '_', '(', '/', or the null terminator.
    	if ( strncmp(ptr, route.data(), route.size()) ) return 0;
    	ptr += route.size();
    	if (banner.size())
    	{	if (*ptr == 0 || *ptr == '_' || *ptr == '(') return 0;
    		if (*ptr == '/')
    		{	if (!(ptr = strdstr(ptr+2, banner.data(), '_')) ) return 0;
    		} else	if ( strncmp(ptr, banner.data(), banner.size()) ) return 0;
    		ptr += banner.size();
    	}
    	if (!*ptr || *ptr == '_' || *ptr == '(' || *ptr == '/') return ptr;
    	if ( strncmp(ptr, abbrev.data(), abbrev.size()) ) return 0;
    	ptr += abbrev.size();
    	return (!*ptr || *ptr == '_' || *ptr == '(' || *ptr == '/') ? ptr : 0;
    }

    (strdstr is defined here.)

    And now, suffixes:

    const char* suffix = strchr(check->label.data(), '_');
    /*   ->   */
    const char* suffix = strchr(endmatch, '_');

    We don't need to start our search before endmatch. We know there won't be any underscores before endmatch in our string, because if there were, either

    • that pointer would have been returned as our value of endmatch instead, or
    • Route::label_match would have failed to match some part of the .list name, returned 0, and we'd not even be inside this if block.

    Nota bene: this all depends on list names not containing underscores anywhere. I've confirmed via grepping system CSVs that none do, but maybe this could be something to watch out for?

    None of the suffix changes alter our output. That all happens as a result of Route::label_match.

    For suffix comparisons, let's take the comment out and just look at the "before" conditional:

    if (suffix
     && (!strcmp(check->label.data()+other_no_abbrev.size(), suffix)
      || check->label.data()+other_no_abbrev.size() == ap_coloc[other]->route->abbrev + suffix
       ))	suffixes[other] = suffix;

    other_no_abbrev is gone now, of course. check->label.data()+other_no_abbrev.size() is our endmatch value, as endmatch points to the next char after the matching text. So let's replace it & clean up the formatting:

    if (suffix && (!strcmp(endmatch, suffix) || endmatch == ap_coloc[other]->route->abbrev + suffix))
      suffixes[other] = suffix;

    Recall that suffix = strchr(endmatch, '_'); this means that if suffix is anything other than endmatch itself, strcmp will return a non-match. Thus this can be simplified to:

    if (suffix && (endmatch == suffix || endmatch == ap_coloc[other]->route->abbrev + suffix))
      suffixes[other] = suffix;

    Now let's turn our attention to || endmatch == ap_coloc[other]->route->abbrev + suffix.
    If the abbrev matches after the route and banner, Route::label_match returns an endmatch value pointing to the next char after the abbrev in our label. So we're already past ap_coloc[other]->route->abbrev, and only need to worry about matching suffix. Thus we can simplify to:

    if (suffix && endmatch == suffix)
      suffixes[other] = suffix;

    ...which is what's in the big WIP code block above.
    And yes, this could be further simplified -- if (*endmatch == '_') suffixes[other] = endmatch;, without defining suffix or calling strchr at all. But let's stop short of doing that for now. I'll explain why in a future post.
    @jteresco, does everything make sense so far? Any questions?


    Results so far:

    147 changes to label simplification. That's a small enough amount to review manually. The diffs all fall into 3 categories:

    • 3+_intersection -> Keep_failsafe, due to the checks for false matches.
      • Mismatched banners or abbrevs. A number of examples in Japan.
        • Sometimes a mismatch is just a case sensitivity glitch, like F2SMa vs F2sMa or MEX140CaC vs MEX140Cac.
      • City suffixes that should more properly be after an underscore, e.g. MI35Esc_N.
        Six of "NY9A", half a dozen of "NY9Ams"... :)
        290604c290604
        < 3+_intersection: NC32@NC45Ply_N&NC45@NC32Ply_N&US64@NC45_S -> NC32/NC45_S/US64
        ---
        >   Keep_failsafe: NC32@NC45Ply_N&NC45@NC32Ply_N&US64@NC45_S
        NC45Ply & NC32Ply were false matches for NC45 & NC32.
        298551,298554c298551,298553
        < 3+_intersection: US90@US98Bro_E&US98@US90Bro_W&US98TrkMob@US98Bro_E -> US90/US98/US98TrkMob
        < 3+_intersection: US90@US98Bro_W&US98@US90Bro_E&US98TrkMob@US98Bro_W -> US90/US98/US98TrkMob
        < Appended region: US90/US98/US98TrkMob|AL
        < 3+_intersection: US90@US98Mob_W&US98@US90Mob_W&US98TrkMob@US98_E -> US90/US98_E/US98TrkMob
        ---
        >   Keep_failsafe: US90@US98Bro_E&US98@US90Bro_W&US98TrkMob@US98Bro_E
        >   Keep_failsafe: US90@US98Bro_W&US98@US90Bro_E&US98TrkMob@US98Bro_W
        >   Keep_failsafe: US90@US98Mob_W&US98@US90Mob_W&US98TrkMob@US98_E
        Keeping the failsafe names here means we don't need append a region. A good thing for disambiguation IMO.
    • 3+_intersection cases that gain some more suffixes.
      Sometimes, a route that didn't match as an other before will match now, due to its banner being located after a slash in a waypoint label.
      Or, a route that only matched a suffixless label will now match a suffixed one.
      262637c262637
      < 3+_intersection: E78@SS73/73Var_E&SS73San@SS73Var&SS73VarPal@SS73_E -> E78/SS73San_E/SS73VarPal
      ---
      > 3+_intersection: E78@SS73/73Var_E&SS73San@SS73Var&SS73VarPal@SS73_E -> E78/SS73San_E/SS73VarPal_E
      Before: Route SS73VarPal matches only label SS73Var. Gets no suffix.
      After: Route SS73VarPal matches label SS73/73Var_E. Gets _E suffix.
      290209,290210c290209,290210
      < 3+_intersection: US15@US15Bus/29Bus&US15BusCul@US15_N&US29@US15/29Bus_N&US29BusCul@US29_N -> US15_N/US15BusCul/US29_N/US29BusCul
      < 3+_intersection: US15@US29_S&US15BusCul@US15_S&US29@US15/15Bus_S -> US15_S/US15BusCul/US29_S
      ---
      > 3+_intersection: US15@US15Bus/29Bus&US15BusCul@US15_N&US29@US15/29Bus_N&US29BusCul@US29_N -> US15_N/US15BusCul_N/US29_N/US29BusCul
      > 3+_intersection: US15@US29_S&US15BusCul@US15_S&US29@US15/15Bus_S -> US15_S/US15BusCul_S/US29_S
      Top: Only match @ US15Bus/29Bus sans suffix -> 2nd match @ US15/29Bus_N
      Bottom: No match for US15BusCul as other -> match @ US15/15Bus_S
    • Keep_failsafe -> 3+_intersection, due to a banner being detected after a slash
      281019c281019
      <   Keep_failsafe: US52@US52/281Byp&US52BypJam@US52_W&US281@US52/281Byp&US281BypJam@US281_N
      ---
      > 3+_intersection: US52@US52/281Byp&US52BypJam@US52_W&US281@US52/281Byp&US281BypJam@US281_N -> US52_W/US52BypJam/US281_N/US281BypJam
      Label US52/281Byp becomes a match for route US52BypJam.
      290455c290455
      <   Keep_failsafe: US13BusSuf@US58/460Bus_W&US58BusSuf@US13/460Bus_S&US460BusSuf@US13/58Bus_S&VA10@US460Bus_E&VA32@US460Bus_E
      ---
      > 3+_intersection: US13BusSuf@US58/460Bus_W&US58BusSuf@US13/460Bus_S&US460BusSuf@US13/58Bus_S&VA10@US460Bus_E&VA32@US460Bus_E -> US13BusSuf_S/US58BusSuf_W/US460BusSuf_E/VA10/VA32
      US58/460Bus_W becomes a match for US58BusSuf
      US13/460Bus_S becomes a match for US13BusSuf
      US13/58Bus_S becomes a match for US13BusSuf
      I saw a lot of benefit in VA, the Carolinas, and GA. Especially in GA, where there's lots of multiplexing and lots of high-colocation locations. Six of the 12 longest graph labels are there. Of these, 5 now get simplified via 3+_intersection, including the longest graph label: GA12TrkMad@US129/441_S&GA24@US129/441Byp_S&GA24BypMad@GA24_S&US129@US129/441Byp_S&US129BypMad@US129_S&US278TrkMad@US129/441_S&US441@US129/441Byp_S&US441BypMad@US441_S -> GA12TrkMad/GA24_S/GA24BypMad/US129_S/US129BypMad_S/US278TrkMad/US441_S/US441BypMad 166 -> 82 bytes. That's a win!
    • If we want to, I could bash together a shell script (pun intended) to see how many of each category there are.
  4. yakra commented on May 3, 2022

    @yakra
    ContributorAuthor

    The next proposed change:

    if (suffix && endmatch == suffix)
      suffixes[other] = suffix;

    ...which is what's in the big WIP code block above.
    And yes, this could be further simplified -- if (*endmatch == '_') suffixes[other] = endmatch;, without defining suffix or calling strchr at all. But let's stop short of doing that for now. I'll explain why in a future post.

    Instead, let's do if (suffix) suffixes[other] = suffix;
    How does this change the outcome of the conditional? When does suffix exist but not equal endmatch?

    Possible values for *endmatch:

    • \0: End of string; no suffix possible. No difference in the conditional.
    • _: suffix == endmatch. No difference in the conditional.
    • (: This indicates a Rte(Exit) type label.
      There should be a colocated point with just the exit number as its label. Meaning, we don't meet the "each label starts with some route number" criterion for simplfication.
      Unless that point is magically missing (e.g., NMP). In which case, well... GIGO? 😀
      Even if this does happen, a Rte(Exit)_Suf type label indicates a loop route or a US19TrkPit type scenario. In which unlikely case adding a suffix isn't much worse than existing cases like N264_W/R23/R23 or LA29/LA95/LA1174_S.
    • /: This is where things are likely to change, where a suffix exists but doesn't start right at endmatch.
      We're no longer worried about false matches (e.g. "NY50/67".startswith("NY5")), as those are already excluded via Route::label_match. We know our label matches the other route.
      The only reason suffix is not right at endmatch is because there's a 2nd route in our waypoint label, after the matching text, after the slash, before the suffix.
      We should be able to safely say the suffix applies to both routes. So let's include it!

    If the purpose of suffixes is to reduce the chance of conflicting names, this helps with that:

    diff _r/logs/waypointsimplification.log _s/logs/waypointsimplification.log | grep -c '^<'
    1000
    diff _r/logs/waypointsimplification.log _s/logs/waypointsimplification.log | grep -c '^>'
    992

    The 8-line difference can be accounted for by:

    diff _r/logs/waypointsimplification.log _s/logs/waypointsimplification.log | grep '^< Appended'
    < Appended region: L361/L382/L390|DEU-NW
    < Appended region: B50Bit/B51/B257/E29|DEU-RP
    < Appended region: G0601/G6/G7|CHN-NM
    < Appended region: NC24/NC27/US15/US501|NC
    < Appended region: US11/US41/US64/US76|TN
    < Appended region: GA3/GA8/US19/US41/US78/US278|GA
    < Appended region: MO6/MO11/US63|MO
    < Appended region: US16/US16TrkHil/US385/US385TrkHil|SD

    What do we get now instead?

    oldsimple=$(diff _r/logs/waypointsimplification.log _s/logs/waypointsimplification.log | grep '^< Appended' | sed -r 's~.+ (.+)\|.+~\1~')
    failsafe=$(for r in $oldsimple; do grep $r _r/logs/waypointsimplification.log | grep -v Appended | cut -f2 -d' '; done)
    for r in $failsafe; do grep "$r" _s/logs/waypointsimplification.log; done
    3+_intersection: L361@L382/L390_N&L382@L361/L390_N&L390@L361/L382_N -> L361_N/L382_N/L390
    3+_intersection: L361@L382/L390_S&L382@L361/L390_S&L390@L361/L382_S -> L361_S/L382_S/L390
    3+_intersection: B50Bit@B51/B257_N&B51@B50/B257_N&B257@B50/B51_W&E29@B50/B257_N -> B50Bit_N/B51_N/B257/E29
    3+_intersection: B50Bit@B51/B257_S&B51@B50/B257_S&B257@B50/B51_E&E29@B51/B257 -> B50Bit_E/B51_S/B257/E29
    3+_intersection: G0601@G6/G7_E&G6@G7/G0601&G7@G6/G0601 -> G0601/G6_E/G7
    Exit/Intersection: G0601@G6/G7_W&G6@G0601&G7@G0601 -> G0601/G6/G7
    3+_intersection: NC24@US15/501_N&NC27@US15/501_N&US15@NC24/27_E&US501@NC24/27_E -> NC24_E/NC27/US15_N/US501
    3+_intersection: NC24@US15/501_S&NC27@US15/501_S&US15@NC24/27_W&US501@NC24/27_W -> NC24_W/NC27/US15_S/US501
    3+_intersection: US11@US41/76_N&US41@US11/64_E&US64@US41/76_N&US76@US11/64_E -> US11_E/US41_N/US64/US76
    3+_intersection: US11@US41/76_S&US41@US11/64_W&US64@US41/76_S&US76@US11/64_W -> US11_W/US41_S/US64/US76
    3+_intersection: GA3@US78/278_E&GA8@US19/41_S&US19@US78/278_E&US41@US78/278_E&US78@US19/41_S&US278@US19/41_S -> GA3/GA8/US19_S/US41/US78_E/US278
    3+_intersection: GA3@US78/278_W&GA8@US19/41_N&US19@US78/278_W&US41@US78/278_W&US78@US19/41_N&US278@US19/41_N -> GA3/GA8/US19_N/US41/US78_W/US278
    3+_intersection: MO6@US63/11_N&MO11@US63/6_N&US63@MO6/11_N -> MO6_N/MO11/US63_N
    3+_intersection: MO6@US63/11_S&MO11@US63/6_S&US63@MO6/11_S -> MO6_S/MO11/US63_S
    3+_intersection: US16@US16Trk/385Trk_N&US16TrkHil@US16/385_N&US385@US16Trk/385Trk_N&US385TrkHil@US16/385_N -> US16_N/US16TrkHil_N/US385/US385TrkHil
    3+_intersection: US16@US16Trk/385Trk_S&US16TrkHil@US16/385_S&US385@US16Trk/385Trk_S&US385TrkHil@US16/385_S -> US16_S/US16TrkHil_S/US385/US385TrkHil
    

    Less ambiguity for the win!
    That one Exit/Intersection case in the mix is interesting. G6 doesn't get a suffix there.
    I don't suggest 3+_intersection be moved before Exit/Intersection though. That yields another Appended region.

    The only differences in waypointsimplification.log are:

    • Suffixes added where we didn't have them before, e.g.
      < 3+_intersection: MRSja@SR259/265_Ska&SR259@MR_Ska&SR265@MR_Ska -> MRSja_Ska/SR259/SR265
      < 3+_intersection: MRSja@SR259/265_Tra&SR259@MR_Tra&SR265@MR_Tra -> MRSja_Tra/SR259/SR265
      > 3+_intersection: MRSja@SR259/265_Ska&SR259@MR_Ska&SR265@MR_Ska -> MRSja_Ska/SR259_Ska/SR265
      > 3+_intersection: MRSja@SR259/265_Tra&SR259@MR_Tra&SR265@MR_Tra -> MRSja_Tra/SR259_Tra/SR265
    • Suffixes overwritten by other suffixes later in the colocation list (this is normal and already happens) that were ignored before, e.g.
      < 3+_intersection: A38Der@A61_N&A61@A38_N&B6179@A38/A61_Alf -> A38Der_N/A61_N/B6179
      < 3+_intersection: A38Der@A61_S&A61@A38_S&B6179@A38/A61_Bre -> A38Der_S/A61_S/B6179
      > 3+_intersection: A38Der@A61_N&A61@A38_N&B6179@A38/A61_Alf -> A38Der_Alf/A61_N/B6179
      > 3+_intersection: A38Der@A61_S&A61@A38_S&B6179@A38/A61_Bre -> A38Der_Bre/A61_S/B6179
  5. yakra commented on May 4, 2022

    @yakra
    ContributorAuthor

    Route::label_match:
    if (banner.size()) -> if (banner.size() && banner[0] != '-')
    for French departmental "banners"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CWNdata qualityenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions