Guest User

James Michael DuPont

a guest
Jan 29th, 2010
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. use warnings;
  2. use strict;
  3. my $chunksize=10000;
  4.  
  5.  
  6. my $count=1;
  7. sub begin
  8. {
  9. warn "OUT_${count}.osm";
  10. open OUT , ">OUT_${count}.osm";
  11. $count++;
  12. print OUT '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  13. print OUT '<osm version="0.6" generator="JOSM">' . "\n";
  14. }
  15.  
  16. sub end
  17. {
  18. print OUT '</osm>' . "\n";
  19. close OUT;
  20. }
  21.  
  22. my $id=1;
  23.  
  24. my %hide;
  25. my %need; # what nodes are needed
  26.  
  27. begin;
  28. my $interesting=0;
  29. my @data;
  30. sub emit
  31. {
  32.  
  33. my $data=join ("\n", @data);
  34. if (($data =~ /\<way/) && ($interesting))
  35. {
  36. $id++;
  37.  
  38. # emit all the needed points before the way
  39. foreach my $k (keys %need)
  40. {
  41. if ($hide{$k})
  42. {
  43. print OUT $hide{$k}; #emit the referenced nodes first
  44. # delete $hide{$k}; it could be referenced twice!
  45. }
  46. }
  47.  
  48.  
  49. print OUT $data;
  50. }
  51. else
  52. {
  53.  
  54. if ($data =~ /^\<node/)
  55. {
  56. if ($data =~ /id='(-\d+)'/)
  57. {
  58. my $nid= $1;
  59. if ($data =~ /lon='(\d+\.\d+)'/)
  60. {
  61. my $lon = $1;
  62. $lon += 0.00016;
  63.  
  64. if ($data =~ s/lon='(\d+\.\d+)'/lon='$lon'/g)
  65. {
  66. # warn "changed $data";
  67. }
  68.  
  69. }
  70. elsif ($data =~ /lon='0'/)
  71. {
  72. }
  73. else
  74. {
  75. die "Bad $data";
  76. }
  77.  
  78. $hide{$nid}=$data;
  79. }
  80. }
  81.  
  82. }
  83. @data=();
  84. %need=();
  85.  
  86. my $rem =$id % $chunksize;
  87. if ($rem==0)
  88. {
  89. warn "$id and $rem $_";
  90. $id++; # make sure it does not loop
  91. end;
  92. begin;
  93. }
  94. $interesting=0;
  95. }
  96.  
  97. while (<>)
  98. {
  99. chomp;
  100.  
  101. if (/\<\?xml/)
  102. {
  103. }
  104. elsif (/\<osm/)
  105. {
  106. }
  107. elsif (/\/osm/)
  108. {
  109. }
  110. elsif (/\<node/)
  111. {
  112. push @data, $_;
  113. }
  114. elsif (/\<way/)
  115. {
  116. push @data, $_;
  117. }
  118. elsif (/\<nd ref='(-\d+)'/)
  119. {
  120. $need{$1}++;
  121. push @data, $_;
  122. # print OUT $_;
  123. }
  124. elsif (/\lat=/)
  125. {
  126. push @data, $_;
  127. # print OUT $_;
  128. }
  129. elsif (/\<tag/)
  130. {
  131. push @data, $_;
  132. if (/k='ac:layer_name' v='BOUNDARIES.OTHER'/)
  133. {
  134. $interesting=1;
  135. }
  136.  
  137. # print OUT $_;
  138. }
  139. elsif (/\<\/node/)
  140. {
  141. push @data, $_;
  142. emit;
  143. }
  144. elsif (/\<\/way/)
  145. {
  146. push @data, $_;
  147. emit;
  148. }
  149. elsif (/^\s*$/)
  150. {
  151.  
  152. }
  153. else
  154. {
  155. warn "$_";
  156. }
  157. }
  158.  
  159.  
  160. end;
  161.  
Advertisement
Add Comment
Please, Sign In to add comment