jasonk has asked for the wisdom of the Perl Monks concerning the following question:
I've been fighting this chunk of code for days, and I think there probably exists a much simpler solution, but I can't see the forest for the trees. :)
I have some data that looks like this:
my @data = ( [ bless({'X' => '2289290.30567314', 'Y' => '507448.506452598'}, +'Point'), bless({'X' => '2289282.72943502', 'Y' => '507410.155776398'}, +'Point'), bless({'X' => '2289149', 'Y' => '507002.0625'}, 'Point'), bless({'X' => '2289055.31527948', 'Y' => '506731.474946477'}, +'Point'), ], [ bless({'X' => '2289563.40820205', 'Y' => '512818.142015762'}, +'Point'), bless({'X' => '2289542.5', 'Y' => '512756.0625'}, 'Point'), bless({'X' => '2289188.03442014', 'Y' => '510403.174475172'}, +'Point'), ], [ bless({'X' => '2287498.5', 'Y' => '502968.75'}, 'Point'), bless({'X' => '2287529.54227622', 'Y' => '502752.130034798'}, +'Point'), bless({'X' => '2287571.41650288', 'Y' => '502527.884072168'}, +'Point'), ], [ bless({'X' => '2288410.26157533', 'Y' => '505497.135963316'}, +'Point'), bless({'X' => '2288373.62815415', 'Y' => '505449.841277223'}, +'Point'), bless({'X' => '2287631.5', 'Y' => '503533.9375'}, 'Point'), bless({'X' => '2287593', 'Y' => '503471'}, 'Point'), ], [ bless({'X' => '2289188.03442014', 'Y' => '510403.174475172'}, +'Point'), bless({'X' => '2289106.00673453', 'Y' => '509893.307668147'}, +'Point') ], [ bless({'X' => '2288451.69937281', 'Y' => '505554.395479093'}, +'Point'), bless({'X' => '2288410.26157533', 'Y' => '505497.135963316'}, +'Point') ], [ bless({'X' => '2289106.00673453', 'Y' => '509893.307668147'}, +'Point'), bless({'X' => '2289299.27685392', 'Y' => '507531.897507685'}, +'Point'), bless({'X' => '2289290.30567314', 'Y' => '507448.506452598'}, +'Point'), ], [ bless({'X' => '2287593', 'Y' => '503471'}, 'Point'), bless({'X' => '2287498.5', 'Y' => '502968.75'}, 'Point'), ], [ bless({'X' => '2288891.13335288', 'Y' => '506310.118972175'}, +'Point'), bless({'X' => '2288451.69937281', 'Y' => '505554.395479093'}, +'Point'), ], [ bless({'X' => '2289055.31527948', 'Y' => '506731.474946477'}, +'Point'), bless({'X' => '2289012.5', 'Y' => '506607.8125'}, 'Point'), bless({'X' => '2288907', 'Y' => '506360.3125'}, 'Point'), bless({'X' => '2288891.13335288', 'Y' => '506310.118972175'}, +'Point'), ] );
Each of the array refs contains a series of points (latitude/longitude), that represents a chunk of a road. I am trying to find the refs that have beginning/ending points in common, and combine them, basically turning all the chunks into one long stretch of road. This is the code that I have now:
sub merge { my @allparts = @_; my @complete = @{shift(@allparts)}; ALLPARTS: while(@allparts) { for(0 .. $#allparts) { my @thispart = @{$allparts[$_]}; if($thispart[0] == $complete[$#complete]) { shift(@thispart); # remove part, so we don't double po +ints @complete = (@complete,@thispart); splice(@allparts,$_,1); next ALLPARTS; } if($complete[0] == $thispart[$#thispart]) { shift(@complete); # remove part @complete = (@thispart,@complete); splice(@allparts,$_,1); next ALLPARTS; } } die "Found pieces that don't fit!\n".Dumper(@allparts); } return @complete; }
This works as long as the parts can all be combined to form one large road, if the road consists of more than one unconnected section, this doesn't work. (To demonstrate this, call splice(@data,7,1) before calling merge(), to remove one part of the road). Ideally I'd like to have this function condense the data down to as few parts as possible, but because this code is so ugly, everything I've attempted has rapidly become unreadable and given me a headache. So if anybody has any ideas either how to accomplish my end goal, or a simpler way to implement the current merge() sub which would make it easier for me to expand, I'd appreciate any input.
For testing purposes, here is a simplified implementation o the Point package, so the overloading in the merge() function will work:
package Point; use overload '==' => sub { return ($_[0]->{X} == $_[1]->{X} && $_[0]->{Y} == $_[1]->{Y}); };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AoA data merging
by rjray (Chaplain) on Mar 10, 2003 at 23:50 UTC | |
by jasonk (Parson) on Mar 11, 2003 at 02:49 UTC | |
by rjray (Chaplain) on Mar 11, 2003 at 23:45 UTC | |
by jasonk (Parson) on Mar 11, 2003 at 23:50 UTC | |
|
Re: AoA data merging
by Jaap (Curate) on Mar 10, 2003 at 20:49 UTC | |
by jasonk (Parson) on Mar 10, 2003 at 21:17 UTC | |
|
Re: AoA data merging
by graff (Chancellor) on Mar 11, 2003 at 06:24 UTC | |
by jasonk (Parson) on Mar 11, 2003 at 14:37 UTC | |
|
Re: AoA data merging
by zengargoyle (Deacon) on Mar 11, 2003 at 06:47 UTC | |
|
Re: AoA data merging
by Enlil (Parson) on Mar 11, 2003 at 10:06 UTC | |
|
Re: AoA data merging
by BrowserUk (Patriarch) on Mar 11, 2003 at 09:10 UTC | |
|
Re: AoA data merging (final answer)
by jasonk (Parson) on Mar 11, 2003 at 23:46 UTC |