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) {
332 <?xml version="1.0" encoding="UTF-8"?>
333 <kml xmlns="http://www.opengis.net/kml/2.2">
335 <name>Simple placemark</name>
336 <description>Attached to the ground. Intelligently places itself
337 at the height of the underlying terrain.</description>
339 <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
347 <Style id="sh_sunny_copy69">
351 <href>http://waypoints.google.com/mapfiles/kml/shapes/sunny.png</href>
353 <hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
356 <color>ff00aaff</color>
364 my $writer = XML::Writer->new( OUTPUT => \$output,
365 DATA_MODE => 1, ENCODING => 'UTF-8');
367 $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
369 # first pass: get the icons
370 foreach my $name (keys %{$waypoints{$map}}) {
371 my %options = %{$waypoints{$map}{$name}};
372 $writer->startTag("Style", id => $options{tag});
373 $writer->startTag("IconStyle");
374 $writer->startTag("Icon");
375 $writer->startTag("href");
376 $writer->characters($options{icon});
383 foreach my $name (keys %{$waypoints{$map}}) {
384 my %options = %{$waypoints{$map}{$name}};
385 $writer->startTag("Placemark");
386 $writer->startTag("name");
387 $writer->characters($name);
389 $writer->startTag("styleUrl");
390 $writer->characters('#' . $options{tag});
392 #$writer->emptyTag('atom:link', href => $options{href});
393 # to make it easier for us as the atom:link parameter is
394 # hard to access from javascript
395 $writer->startTag('href');
396 $writer->characters($options{href});
398 $writer->startTag("description");
399 $writer->characters($options{desc});
401 $writer->startTag("Point");
402 $writer->startTag("coordinates");
403 $writer->characters($options{lon} . "," . $options{lat});
410 foreach my $linestring (@{$linestrings{$map}}) {
411 $writer->startTag("Placemark");
412 $writer->startTag("name");
413 $writer->characters("linestring " . $i++);
415 $writer->startTag("LineString");
416 $writer->startTag("coordinates");
418 foreach my $coord (@{$linestring}) {
419 $str .= join(',', @{$coord}) . " \n";
421 $writer->characters($str);
429 writefile("pois.kml", $config{destdir} . "/$map", $output);
434 my %waypoints = %{$_[0]};
435 foreach my $map (keys %waypoints) {
436 my $poisf = "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n";
437 foreach my $name (keys %{$waypoints{$map}}) {
438 my %options = %{$waypoints{$map}{$name}};
440 $options{'lat'} . "\t" .
441 $options{'lon'} . "\t" .
443 $options{'desc'} . '<br /><a href="' . $options{'page'} . '">' . $name . "</a>\t" .
444 $options{'icon'} . "\n";
447 writefile("pois.txt", $config{destdir} . "/$map", $poisf);
451 # pipe some data through the HTML scrubber
453 # code taken from the meta.pm plugin
455 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
456 return IkiWiki::Plugin::htmlscrubber::sanitize(
457 content => shift, page => shift, destpage => shift);
464 # taken from toggle.pm
468 if ($params{content}=~m!<div[^>]*id="mapdiv-[^"]*"[^>]*>!g) {
469 if (! ($params{content}=~s!</body>!include_javascript($params{page})."</body>"!em)) {
470 # no <body> tag, probably in preview mode
471 $params{content}=$params{content} . include_javascript($params{page});
474 return $params{content};
477 sub preferred_format() {
478 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
479 $config{'osm_format'} = 'KML';
481 my @spl = split(/, */, $config{'osm_format'});
486 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
487 $config{'osm_format'} = 'KML';
489 map { $_ => 1 } split(/, */, $config{'osm_format'});
492 sub include_javascript ($) {
496 if (exists $pagestate{$page}{'osm'}) {
497 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
498 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) {
499 $loader .= map_setup_code($map, $name, %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}});
504 return embed_map_code($page) . "<script type=\"text/javascript\" charset=\"utf-8\">$loader</script>";
514 return unless defined $cgi->param('do') &&
515 $cgi->param("do") eq "osm";
517 IkiWiki::loadindex();
519 IkiWiki::decode_cgi_utf8($cgi);
521 my $map = $cgi->param('map');
522 if (!defined $map || $map !~ /^[a-z]*$/) {
523 error("invalid map parameter");
526 print "Content-Type: text/html\r\n";
528 print "<html><body>";
529 print "<div id=\"mapdiv-$map\"></div>";
530 print embed_map_code();
531 print "<script type=\"text/javascript\" charset=\"utf-8\">";
532 print map_setup_code($map, $map,
533 lat => "urlParams['lat']",
534 lon => "urlParams['lon']",
535 zoom => "urlParams['zoom']",
540 print "</body></html>";
545 sub embed_map_code(;$) {
547 return '<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript" charset="utf-8"></script>'.
548 '<script src="'.urlto("ikiwiki/osm.js", $page).
549 '" type="text/javascript" charset="utf-8"></script>'."\n";
552 sub map_setup_code($;@) {
560 $options{'format'} = preferred_format();
562 my %formats = get_formats();
563 if ($formats{'GeoJSON'}) {
564 $options{'jsonurl'} = urlto($map."/pois.json");
566 if ($formats{'CSV'}) {
567 $options{'csvurl'} = urlto($map."/pois.txt");
569 if ($formats{'KML'}) {
570 $options{'kmlurl'} = urlto($map."/pois.kml");
573 return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";