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",
67 my $page = $params{page};
68 my $dest = $params{destpage};
69 my $loc = $params{loc}; # sanitized below
70 my $lat = $params{lat}; # sanitized below
71 my $lon = $params{lon}; # sanitized below
72 my $href = $params{href};
74 my ($width, $height, $float);
75 $height = scrub($params{'height'} || "300px", $page, $dest); # sanitized here
76 $width = scrub($params{'width'} || "500px", $page, $dest); # sanitized here
77 $float = (defined($params{'right'}) && 'right') || (defined($params{'left'}) && 'left'); # sanitized here
79 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
81 $map = $params{'map'} || 'map';
83 $map = scrub($map, $page, $dest); # sanitized here
84 my $name = scrub($params{'name'} || $map, $page, $dest);
86 if (defined($lon) || defined($lat) || defined($loc)) {
87 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
90 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
94 if (! defined $href || ! length $href) {
95 $href=IkiWiki::cgiurl(
101 $pagestate{$page}{'osm'}{$map}{'displays'}{$name} = {
107 editable => defined($params{'editable'}),
112 return "<div id=\"mapdiv-$name\"></div>";
115 sub process_waypoint {
117 my $loc = $params{'loc'}; # sanitized below
118 my $lat = $params{'lat'}; # sanitized below
119 my $lon = $params{'lon'}; # sanitized below
120 my $page = $params{'page'}; # not sanitized?
121 my $dest = $params{'destpage'}; # not sanitized?
122 my $hidden = defined($params{'hidden'}); # sanitized here
123 my ($p) = $page =~ /(?:^|\/)([^\/]+)\/?$/; # shorter page name
124 my $name = scrub($params{'name'} || $p, $page, $dest); # sanitized here
125 my $desc = scrub($params{'desc'} || '', $page, $dest); # sanitized here
126 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
127 my $icon = $config{'osm_default_icon'} || "ikiwiki/images/osm.png"; # sanitized: we trust $config
128 my $map = scrub($params{'map'} || 'map', $page, $dest); # sanitized here
129 my $alt = $config{'osm_alt'} ? "alt=\"$config{'osm_alt'}\"" : ''; # sanitized: we trust $config
130 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
134 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
135 if (!defined($lat) || !defined($lon)) {
136 error("Must specify lat and lon");
139 my $tag = $params{'tag'};
140 foreach my $t (keys %{$typedlinks{$page}{'tag'}}) {
141 if ($icon = get_tag_icon($t)) {
145 $t =~ s!/$config{'tagbase'}/!!;
146 if ($icon = get_tag_icon($t)) {
151 $icon = urlto($icon, $dest, 1);
152 $tag = '' unless $tag;
153 if ($page eq $dest) {
154 my %formats = get_formats();
155 if ($formats{'GeoJSON'}) {
156 will_render($page, "$map/pois.json");
158 if ($formats{'CSV'}) {
159 will_render($page, "$map/pois.txt");
161 if ($formats{'KML'}) {
162 will_render($page, "$map/pois.kml");
165 $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name} = {
172 # How to link back to the page from the map, not to be
173 # confused with the URL of the map itself sent to the
174 # embeded map below. Note: used in generated KML etc file,
175 # so must be absolute.
176 href => urlto($page),
179 my $mapurl = IkiWiki::cgiurl(
187 if (defined($params{'embed'})) {
188 $output .= preprocess(%params,
193 $output .= "<a href=\"$mapurl\"><img class=\"img\" src=\"$icon\" $alt /></a>";
198 # get the icon from the given tag
199 sub get_tag_icon($) {
201 # look for an icon attached to the tag
202 my $attached = $tag . '/' . $config{'osm_tag_default_icon'};
203 if (srcfile($attached)) {
211 sub scrub_lonlat($$$) {
212 my ($loc, $lon, $lat) = @_;
214 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*$/) {
223 if ($lat =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([NS])?\s*$/) {
224 $lat = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
225 if (($1 eq '-') || (($7//'') eq 'S')) {
234 if ($lon =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([EW])?$/) {
235 $lon = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
236 if (($1 eq '-') || (($7//'') eq 'W')) {
244 if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
245 error("Location out of range");
252 my %linestrings = ();
254 foreach my $page (keys %pagestate) {
255 if (exists $pagestate{$page}{'osm'}) {
256 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
257 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
258 debug("found waypoint $name");
259 $waypoints{$map}{$name} = $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name};
265 foreach my $page (keys %pagestate) {
266 if (exists $pagestate{$page}{'osm'}) {
267 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
268 # examine the links on this page
269 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
270 if (exists $links{$page}) {
271 foreach my $otherpage (@{$links{$page}}) {
272 if (exists $waypoints{$map}{$otherpage}) {
273 push(@{$linestrings{$map}}, [
274 [ $waypoints{$map}{$name}{'lon'}, $waypoints{$map}{$name}{'lat'} ],
275 [ $waypoints{$map}{$otherpage}{'lon'}, $waypoints{$map}{$otherpage}{'lat'} ]
282 # clear the state, it will be regenerated on the next parse
283 # the idea here is to clear up removed waypoints...
284 $pagestate{$page}{'osm'} = ();
288 my %formats = get_formats();
289 if ($formats{'GeoJSON'}) {
290 writejson(\%waypoints, \%linestrings);
292 if ($formats{'CSV'}) {
293 writecsvs(\%waypoints, \%linestrings);
295 if ($formats{'KML'}) {
296 writekml(\%waypoints, \%linestrings);
301 my %waypoints = %{$_[0]};
302 my %linestrings = %{$_[1]};
305 foreach my $map (keys %waypoints) {
306 my %geojson = ( "type" => "FeatureCollection", "features" => []);
307 foreach my $name (keys %{$waypoints{$map}}) {
308 my %marker = ( "type" => "Feature",
309 "geometry" => { "type" => "Point", "coordinates" => [ $waypoints{$map}{$name}{'lon'}, $waypoints{$map}{$name}{'lat'} ] },
310 "properties" => $waypoints{$map}{$name} );
311 push @{$geojson{'features'}}, \%marker;
313 foreach my $linestring (@{$linestrings{$map}}) {
314 my %json = ( "type" => "Feature",
315 "geometry" => { "type" => "LineString", "coordinates" => $linestring });
316 push @{$geojson{'features'}}, \%json;
318 writefile("pois.json", $config{destdir} . "/$map", to_json(\%geojson));
323 my %waypoints = %{$_[0]};
324 my %linestrings = %{$_[1]};
325 eval q{use XML::Writer};
327 foreach my $map (keys %waypoints) {
329 my $writer = XML::Writer->new( OUTPUT => \$output,
330 DATA_MODE => 1, ENCODING => 'UTF-8');
332 $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
334 # first pass: get the icons
335 foreach my $name (keys %{$waypoints{$map}}) {
336 my %options = %{$waypoints{$map}{$name}};
337 $writer->startTag("Style", id => $options{tag});
338 $writer->startTag("IconStyle");
339 $writer->startTag("Icon");
340 $writer->startTag("href");
341 $writer->characters($options{icon});
348 foreach my $name (keys %{$waypoints{$map}}) {
349 my %options = %{$waypoints{$map}{$name}};
350 $writer->startTag("Placemark");
351 $writer->startTag("name");
352 $writer->characters($name);
354 $writer->startTag("styleUrl");
355 $writer->characters('#' . $options{tag});
357 #$writer->emptyTag('atom:link', href => $options{href});
358 # to make it easier for us as the atom:link parameter is
359 # hard to access from javascript
360 $writer->startTag('href');
361 $writer->characters($options{href});
363 $writer->startTag("description");
364 $writer->characters($options{desc});
366 $writer->startTag("Point");
367 $writer->startTag("coordinates");
368 $writer->characters($options{lon} . "," . $options{lat});
375 foreach my $linestring (@{$linestrings{$map}}) {
376 $writer->startTag("Placemark");
377 $writer->startTag("name");
378 $writer->characters("linestring " . $i++);
380 $writer->startTag("LineString");
381 $writer->startTag("coordinates");
383 foreach my $coord (@{$linestring}) {
384 $str .= join(',', @{$coord}) . " \n";
386 $writer->characters($str);
394 writefile("pois.kml", $config{destdir} . "/$map", $output);
399 my %waypoints = %{$_[0]};
400 foreach my $map (keys %waypoints) {
401 my $poisf = "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n";
402 foreach my $name (keys %{$waypoints{$map}}) {
403 my %options = %{$waypoints{$map}{$name}};
405 $options{'lat'} . "\t" .
406 $options{'lon'} . "\t" .
408 $options{'desc'} . '<br /><a href="' . $options{'page'} . '">' . $name . "</a>\t" .
409 $options{'icon'} . "\n";
412 writefile("pois.txt", $config{destdir} . "/$map", $poisf);
416 # pipe some data through the HTML scrubber
418 # code taken from the meta.pm plugin
420 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
421 return IkiWiki::Plugin::htmlscrubber::sanitize(
422 content => shift, page => shift, destpage => shift);
429 # taken from toggle.pm
433 if ($params{content}=~m!<div[^>]*id="mapdiv-[^"]*"[^>]*>!g) {
434 if (! ($params{content}=~s!</body>!include_javascript($params{page})."</body>"!em)) {
435 # no <body> tag, probably in preview mode
436 $params{content}=$params{content} . include_javascript($params{page});
439 return $params{content};
442 sub preferred_format() {
443 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
444 $config{'osm_format'} = 'KML';
446 my @spl = split(/, */, $config{'osm_format'});
451 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
452 $config{'osm_format'} = 'KML';
454 map { $_ => 1 } split(/, */, $config{'osm_format'});
457 sub include_javascript ($) {
461 if (exists $pagestate{$page}{'osm'}) {
462 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
463 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) {
464 $loader .= map_setup_code($map, $name, %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}});
469 return embed_map_code($page) . "<script type=\"text/javascript\" charset=\"utf-8\">$loader</script>";
479 return unless defined $cgi->param('do') &&
480 $cgi->param("do") eq "osm";
482 IkiWiki::loadindex();
484 IkiWiki::decode_cgi_utf8($cgi);
486 my $map = $cgi->param('map');
487 if (!defined $map || $map !~ /^[a-z]*$/) {
488 error("invalid map parameter");
491 print "Content-Type: text/html\r\n";
493 print "<html><body>";
494 print "<div id=\"mapdiv-$map\"></div>";
495 print embed_map_code();
496 print "<script type=\"text/javascript\" charset=\"utf-8\">";
497 print map_setup_code($map, $map,
498 lat => "urlParams['lat']",
499 lon => "urlParams['lon']",
500 zoom => "urlParams['zoom']",
505 print "</body></html>";
510 sub embed_map_code(;$) {
512 return '<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript" charset="utf-8"></script>'.
513 '<script src="'.urlto("ikiwiki/osm.js", $page).
514 '" type="text/javascript" charset="utf-8"></script>'."\n";
517 sub map_setup_code($;@) {
525 $options{'format'} = preferred_format();
527 my %formats = get_formats();
528 if ($formats{'GeoJSON'}) {
529 $options{'jsonurl'} = urlto($map."/pois.json");
531 if ($formats{'CSV'}) {
532 $options{'csvurl'} = urlto($map."/pois.txt");
534 if ($formats{'KML'}) {
535 $options{'kmlurl'} = urlto($map."/pois.kml");
538 return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";