2 # Copyright 2011 Blars Blarson
3 # Released under GPL version 2
5 package IkiWiki::Plugin::osm;
13 hook(type => "getsetup", id => "osm", call => \&getsetup);
14 hook(type => "format", id => "osm", call => \&format);
15 hook(type => "preprocess", id => "osm", call => \&preprocess);
16 hook(type => "preprocess", id => "waypoint", call => \&process_waypoint);
17 hook(type => "savestate", id => "waypoint", call => \&savestate);
18 hook(type => "cgi", id => "osm", call => \&cgi);
26 section => "special-purpose",
31 description => "the default zoom when you click on the map link",
37 example => "ikiwiki/images/osm.png",
38 description => "the icon shown on links and on the main map",
45 description => "the alt tag of links, defaults to empty",
52 description => "the output format for waypoints, can be KML, GeoJSON or CSV (one or many, comma-separated)",
56 osm_tag_default_icon => {
58 example => "icon.png",
59 description => "the icon attached to a tag, displayed on the map for tagged pages",
65 sub register_rendered_files {
71 my %formats = get_formats();
72 if ($formats{'GeoJSON'}) {
73 will_render($page, "$map/pois.json");
75 if ($formats{'CSV'}) {
76 will_render($page, "$map/pois.txt");
78 if ($formats{'KML'}) {
79 will_render($page, "$map/pois.kml");
86 my $page = $params{page};
87 my $dest = $params{destpage};
88 my $loc = $params{loc}; # sanitized below
89 my $lat = $params{lat}; # sanitized below
90 my $lon = $params{lon}; # sanitized below
91 my $href = $params{href};
93 my ($width, $height, $float);
94 $height = scrub($params{'height'} || "300px", $page, $dest); # sanitized here
95 $width = scrub($params{'width'} || "500px", $page, $dest); # sanitized here
96 $float = (defined($params{'right'}) && 'right') || (defined($params{'left'}) && 'left'); # sanitized here
98 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
100 $map = $params{'map'} || 'map';
102 $map = scrub($map, $page, $dest); # sanitized here
103 my $name = scrub($params{'name'} || $map, $page, $dest);
105 if (defined($lon) || defined($lat) || defined($loc)) {
106 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
109 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
113 if (! defined $href || ! length $href) {
114 $href=IkiWiki::cgiurl(
120 register_rendered_files($map, $page, $dest);
122 $pagestate{$page}{'osm'}{$map}{'displays'}{$name} = {
128 editable => defined($params{'editable'}),
133 return "<div id=\"mapdiv-$name\"></div>";
136 sub process_waypoint {
138 my $loc = $params{'loc'}; # sanitized below
139 my $lat = $params{'lat'}; # sanitized below
140 my $lon = $params{'lon'}; # sanitized below
141 my $page = $params{'page'}; # not sanitized?
142 my $dest = $params{'destpage'}; # not sanitized?
143 my $hidden = defined($params{'hidden'}); # sanitized here
144 my ($p) = $page =~ /(?:^|\/)([^\/]+)\/?$/; # shorter page name
145 my $name = scrub($params{'name'} || $p, $page, $dest); # sanitized here
146 my $desc = scrub($params{'desc'} || '', $page, $dest); # sanitized here
147 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
148 my $icon = $config{'osm_default_icon'} || "ikiwiki/images/osm.png"; # sanitized: we trust $config
149 my $map = scrub($params{'map'} || 'map', $page, $dest); # sanitized here
150 my $alt = $config{'osm_alt'} ? "alt=\"$config{'osm_alt'}\"" : ''; # sanitized: we trust $config
151 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
155 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
156 if (!defined($lat) || !defined($lon)) {
157 error("Must specify lat and lon");
160 my $tag = $params{'tag'};
161 foreach my $t (keys %{$typedlinks{$page}{'tag'}}) {
162 if ($icon = get_tag_icon($t)) {
166 $t =~ s!/$config{'tagbase'}/!!;
167 if ($icon = get_tag_icon($t)) {
172 $icon = urlto($icon, $dest, 1);
173 $tag = '' unless $tag;
174 register_rendered_files($map, $page, $dest);
175 $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name} = {
182 # How to link back to the page from the map, not to be
183 # confused with the URL of the map itself sent to the
184 # embeded map below. Note: used in generated KML etc file,
185 # so must be absolute.
186 href => urlto($page),
189 my $mapurl = IkiWiki::cgiurl(
197 if (defined($params{'embed'})) {
198 $output .= preprocess(%params,
203 $output .= "<a href=\"$mapurl\"><img class=\"img\" src=\"$icon\" $alt /></a>";
208 # get the icon from the given tag
209 sub get_tag_icon($) {
211 # look for an icon attached to the tag
212 my $attached = $tag . '/' . $config{'osm_tag_default_icon'};
213 if (srcfile($attached)) {
221 sub scrub_lonlat($$$) {
222 my ($loc, $lon, $lat) = @_;
224 if ($loc =~ /^\s*(\-?\d+(?:\.\d*°?|(?:°?|\s)\s*\d+(?:\.\d*\'?|(?:\'|\s)\s*\d+(?:\.\d*)?\"?|\'?)°?)[NS]?)\s*\,?\;?\s*(\-?\d+(?:\.\d*°?|(?:°?|\s)\s*\d+(?:\.\d*\'?|(?:\'|\s)\s*\d+(?:\.\d*)?\"?|\'?)°?)[EW]?)\s*$/) {
233 if ($lat =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([NS])?\s*$/) {
234 $lat = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
235 if (($1 eq '-') || (($7//'') eq 'S')) {
244 if ($lon =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([EW])?$/) {
245 $lon = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
246 if (($1 eq '-') || (($7//'') eq 'W')) {
254 if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
255 error("Location out of range");
262 my %linestrings = ();
264 foreach my $page (keys %pagestate) {
265 if (exists $pagestate{$page}{'osm'}) {
266 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
267 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
268 debug("found waypoint $name");
269 $waypoints{$map}{$name} = $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name};
275 foreach my $page (keys %pagestate) {
276 if (exists $pagestate{$page}{'osm'}) {
277 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
278 # examine the links on this page
279 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
280 if (exists $links{$page}) {
281 foreach my $otherpage (@{$links{$page}}) {
282 if (exists $waypoints{$map}{$otherpage}) {
283 push(@{$linestrings{$map}}, [
284 [ $waypoints{$map}{$name}{'lon'}, $waypoints{$map}{$name}{'lat'} ],
285 [ $waypoints{$map}{$otherpage}{'lon'}, $waypoints{$map}{$otherpage}{'lat'} ]
292 # clear the state, it will be regenerated on the next parse
293 # the idea here is to clear up removed waypoints...
294 $pagestate{$page}{'osm'} = ();
298 my %formats = get_formats();
299 if ($formats{'GeoJSON'}) {
300 writejson(\%waypoints, \%linestrings);
302 if ($formats{'CSV'}) {
303 writecsvs(\%waypoints, \%linestrings);
305 if ($formats{'KML'}) {
306 writekml(\%waypoints, \%linestrings);
311 my %waypoints = %{$_[0]};
312 my %linestrings = %{$_[1]};
315 foreach my $map (keys %waypoints) {
316 my %geojson = ( "type" => "FeatureCollection", "features" => []);
317 foreach my $name (keys %{$waypoints{$map}}) {
318 my %marker = ( "type" => "Feature",
319 "geometry" => { "type" => "Point", "coordinates" => [ $waypoints{$map}{$name}{'lon'}, $waypoints{$map}{$name}{'lat'} ] },
320 "properties" => $waypoints{$map}{$name} );
321 push @{$geojson{'features'}}, \%marker;
323 foreach my $linestring (@{$linestrings{$map}}) {
324 my %json = ( "type" => "Feature",
325 "geometry" => { "type" => "LineString", "coordinates" => $linestring });
326 push @{$geojson{'features'}}, \%json;
328 writefile("pois.json", $config{destdir} . "/$map", to_json(\%geojson));
333 my %waypoints = %{$_[0]};
334 my %linestrings = %{$_[1]};
335 eval q{use XML::Writer};
337 foreach my $map (keys %waypoints) {
339 my $writer = XML::Writer->new( OUTPUT => \$output,
340 DATA_MODE => 1, ENCODING => 'UTF-8');
342 $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
344 # first pass: get the icons
345 foreach my $name (keys %{$waypoints{$map}}) {
346 my %options = %{$waypoints{$map}{$name}};
347 $writer->startTag("Style", id => $options{tag});
348 $writer->startTag("IconStyle");
349 $writer->startTag("Icon");
350 $writer->startTag("href");
351 $writer->characters($options{icon});
358 foreach my $name (keys %{$waypoints{$map}}) {
359 my %options = %{$waypoints{$map}{$name}};
360 $writer->startTag("Placemark");
361 $writer->startTag("name");
362 $writer->characters($name);
364 $writer->startTag("styleUrl");
365 $writer->characters('#' . $options{tag});
367 #$writer->emptyTag('atom:link', href => $options{href});
368 # to make it easier for us as the atom:link parameter is
369 # hard to access from javascript
370 $writer->startTag('href');
371 $writer->characters($options{href});
373 $writer->startTag("description");
374 $writer->characters($options{desc});
376 $writer->startTag("Point");
377 $writer->startTag("coordinates");
378 $writer->characters($options{lon} . "," . $options{lat});
385 foreach my $linestring (@{$linestrings{$map}}) {
386 $writer->startTag("Placemark");
387 $writer->startTag("name");
388 $writer->characters("linestring " . $i++);
390 $writer->startTag("LineString");
391 $writer->startTag("coordinates");
393 foreach my $coord (@{$linestring}) {
394 $str .= join(',', @{$coord}) . " \n";
396 $writer->characters($str);
404 writefile("pois.kml", $config{destdir} . "/$map", $output);
409 my %waypoints = %{$_[0]};
410 foreach my $map (keys %waypoints) {
411 my $poisf = "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n";
412 foreach my $name (keys %{$waypoints{$map}}) {
413 my %options = %{$waypoints{$map}{$name}};
415 $options{'lat'} . "\t" .
416 $options{'lon'} . "\t" .
418 $options{'desc'} . '<br /><a href="' . $options{'page'} . '">' . $name . "</a>\t" .
419 $options{'icon'} . "\n";
422 writefile("pois.txt", $config{destdir} . "/$map", $poisf);
426 # pipe some data through the HTML scrubber
428 # code taken from the meta.pm plugin
430 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
431 return IkiWiki::Plugin::htmlscrubber::sanitize(
432 content => shift, page => shift, destpage => shift);
439 # taken from toggle.pm
443 if ($params{content}=~m!<div[^>]*id="mapdiv-[^"]*"[^>]*>!g) {
444 if (! ($params{content}=~s!</body>!include_javascript($params{page})."</body>"!em)) {
445 # no <body> tag, probably in preview mode
446 $params{content}=$params{content} . include_javascript($params{page});
449 return $params{content};
452 sub preferred_format() {
453 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
454 $config{'osm_format'} = 'KML';
456 my @spl = split(/, */, $config{'osm_format'});
461 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
462 $config{'osm_format'} = 'KML';
464 map { $_ => 1 } split(/, */, $config{'osm_format'});
467 sub include_javascript ($) {
471 if (exists $pagestate{$page}{'osm'}) {
472 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
473 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) {
474 $loader .= map_setup_code($map, $name, %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}});
479 return embed_map_code($page) . "<script type=\"text/javascript\" charset=\"utf-8\">$loader</script>";
489 return unless defined $cgi->param('do') &&
490 $cgi->param("do") eq "osm";
492 IkiWiki::loadindex();
494 IkiWiki::decode_cgi_utf8($cgi);
496 my $map = $cgi->param('map');
497 if (!defined $map || $map !~ /^[a-z]*$/) {
498 error("invalid map parameter");
501 print "Content-Type: text/html\r\n";
503 print "<html><body>";
504 print "<div id=\"mapdiv-$map\"></div>";
505 print embed_map_code();
506 print "<script type=\"text/javascript\" charset=\"utf-8\">";
507 print map_setup_code($map, $map,
508 lat => "urlParams['lat']",
509 lon => "urlParams['lon']",
510 zoom => "urlParams['zoom']",
515 print "</body></html>";
520 sub embed_map_code(;$) {
522 return '<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript" charset="utf-8"></script>'.
523 '<script src="'.urlto("ikiwiki/osm.js", $page).
524 '" type="text/javascript" charset="utf-8"></script>'."\n";
527 sub map_setup_code($;@) {
535 $options{'format'} = preferred_format();
537 my %formats = get_formats();
538 if ($formats{'GeoJSON'}) {
539 $options{'jsonurl'} = urlto($map."/pois.json");
541 if ($formats{'CSV'}) {
542 $options{'csvurl'} = urlto($map."/pois.txt");
544 if ($formats{'KML'}) {
545 $options{'kmlurl'} = urlto($map."/pois.kml");
548 return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";