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 $fullscreen = defined($params{'fullscreen'}); # sanitized here
75 my ($width, $height, $float);
82 $height = scrub($params{'height'} || "300px", $page, $dest); # sanitized here
83 $width = scrub($params{'width'} || "500px", $page, $dest); # sanitized here
84 $float = (defined($params{'right'}) && 'right') || (defined($params{'left'}) && 'left'); # sanitized here
86 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
89 $map = $params{'map'} || $page;
92 $map = $params{'map'} || 'map';
94 $map = scrub($map, $page, $dest); # sanitized here
95 my $name = scrub($params{'name'} || $map, $page, $dest);
97 if (defined($lon) || defined($lat) || defined($loc)) {
98 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
101 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
104 $pagestate{$page}{'osm'}{$map}{'displays'}{$name} = {
109 fullscreen => $fullscreen,
110 editable => defined($params{'editable'}),
115 return "<div id=\"mapdiv-$name\"></div>";
118 sub process_waypoint {
120 my $loc = $params{'loc'}; # sanitized below
121 my $lat = $params{'lat'}; # sanitized below
122 my $lon = $params{'lon'}; # sanitized below
123 my $page = $params{'page'}; # not sanitized?
124 my $dest = $params{'destpage'}; # not sanitized?
125 my $hidden = defined($params{'hidden'}); # sanitized here
126 my ($p) = $page =~ /(?:^|\/)([^\/]+)\/?$/; # shorter page name
127 my $name = scrub($params{'name'} || $p, $page, $dest); # sanitized here
128 my $desc = scrub($params{'desc'} || '', $page, $dest); # sanitized here
129 my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below
130 my $icon = $config{'osm_default_icon'} || "ikiwiki/images/osm.png"; # sanitized: we trust $config
131 my $map = scrub($params{'map'} || 'map', $page, $dest); # sanitized here
132 my $alt = $config{'osm_alt'} ? "alt=\"$config{'osm_alt'}\"" : ''; # sanitized: we trust $config
133 if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) {
137 ($lon, $lat) = scrub_lonlat($loc, $lon, $lat);
138 if (!defined($lat) || !defined($lon)) {
139 error("Must specify lat and lon");
142 my $tag = $params{'tag'};
143 foreach my $t (keys %{$typedlinks{$page}{'tag'}}) {
144 if ($icon = get_tag_icon($t)) {
148 $t =~ s!/$config{'tagbase'}/!!;
149 if ($icon = get_tag_icon($t)) {
154 $icon = urlto($icon, $dest, 1);
155 $tag = '' unless $tag;
156 if ($page eq $dest) {
157 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
158 $config{'osm_format'} = 'KML';
160 my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'});
161 if ($formats{'GeoJSON'}) {
162 will_render($page,$config{destdir} . "/$map/pois.json");
164 if ($formats{'CSV'}) {
165 will_render($page,$config{destdir} . "/$map/pois.txt");
167 if ($formats{'KML'}) {
168 will_render($page,$config{destdir} . "/$map/pois.kml");
171 my $href = IkiWiki::cgiurl(
178 if (defined($destsources{htmlpage($map)})) {
179 $href = urlto($map,$page) . "?lat=$lat&lon=$lon&zoom=$zoom";
180 $href =~ s!&!&!g;
182 $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name} = {
189 # how to link back to the page from the map, not to be
190 # confused with the URL of the map itself sent to the
192 href => urlto($page,$map),
195 if (defined($params{'embed'})) {
196 $params{'href'} = $href; # propagate down to embeded
197 $output .= preprocess(%params);
200 $output .= "<a href=\"$href\"><img class=\"img\" src=\"$icon\" $alt /></a>";
205 # get the icon from the given tag
206 sub get_tag_icon($) {
208 # look for an icon attached to the tag
209 my $attached = $tag . '/' . $config{'osm_tag_default_icon'};
210 if (srcfile($attached)) {
218 sub scrub_lonlat($$$) {
219 my ($loc, $lon, $lat) = @_;
221 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*$/) {
230 if ($lat =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([NS])?\s*$/) {
231 $lat = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
232 if (($1 eq '-') || (($7//'') eq 'S')) {
241 if ($lon =~ /^(\-?)(\d+)(?:(\.\d*)°?|(?:°|\s)\s*(\d+)(?:(\.\d*)\'?|(?:\'|\s)\s*(\d+(?:\.\d*)?\"?)|\'?)|°?)\s*([EW])?$/) {
242 $lon = $2 + ($3//0) + ((($4//0) + (($5//0) + (($6//0)/60.)))/60.);
243 if (($1 eq '-') || (($7//'') eq 'W')) {
251 if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
252 error("Location out of range");
259 my %linestrings = ();
261 foreach my $page (keys %pagestate) {
262 if (exists $pagestate{$page}{'osm'}) {
263 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
264 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
265 debug("found waypoint $name");
266 $waypoints{$map}{$name} = $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name};
272 foreach my $page (keys %pagestate) {
273 if (exists $pagestate{$page}{'osm'}) {
274 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
275 # examine the links on this page
276 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'waypoints'}}) {
277 if (exists $links{$page}) {
278 foreach my $otherpage (@{$links{$page}}) {
279 if (exists $waypoints{$map}{$otherpage}) {
280 push(@{$linestrings{$map}}, [
281 [ $waypoints{$map}{$name}{'lon'}, $waypoints{$map}{$name}{'lat'} ],
282 [ $waypoints{$map}{$otherpage}{'lon'}, $waypoints{$map}{$otherpage}{'lat'} ]
289 # clear the state, it will be regenerated on the next parse
290 # the idea here is to clear up removed waypoints...
291 $pagestate{$page}{'osm'} = ();
295 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
296 $config{'osm_format'} = 'KML';
298 my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'});
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) {
342 <?xml version="1.0" encoding="UTF-8"?>
343 <kml xmlns="http://www.opengis.net/kml/2.2">
345 <name>Simple placemark</name>
346 <description>Attached to the ground. Intelligently places itself
347 at the height of the underlying terrain.</description>
349 <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
357 <Style id="sh_sunny_copy69">
361 <href>http://waypoints.google.com/mapfiles/kml/shapes/sunny.png</href>
363 <hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
366 <color>ff00aaff</color>
374 my $writer = XML::Writer->new( OUTPUT => \$output,
375 DATA_MODE => 1, ENCODING => 'UTF-8');
377 $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
379 # first pass: get the icons
380 foreach my $name (keys %{$waypoints{$map}}) {
381 my %options = %{$waypoints{$map}{$name}};
382 $writer->startTag("Style", id => $options{tag});
383 $writer->startTag("IconStyle");
384 $writer->startTag("Icon");
385 $writer->startTag("href");
386 $writer->characters($options{icon});
393 foreach my $name (keys %{$waypoints{$map}}) {
394 my %options = %{$waypoints{$map}{$name}};
395 $writer->startTag("Placemark");
396 $writer->startTag("name");
397 $writer->characters($name);
399 $writer->startTag("styleUrl");
400 $writer->characters('#' . $options{tag});
402 #$writer->emptyTag('atom:link', href => $options{href});
403 # to make it easier for us as the atom:link parameter is
404 # hard to access from javascript
405 $writer->startTag('href');
406 $writer->characters($options{href});
408 $writer->startTag("description");
409 $writer->characters($options{desc});
411 $writer->startTag("Point");
412 $writer->startTag("coordinates");
413 $writer->characters($options{lon} . "," . $options{lat});
420 foreach my $linestring (@{$linestrings{$map}}) {
421 $writer->startTag("Placemark");
422 $writer->startTag("name");
423 $writer->characters("linestring " . $i++);
425 $writer->startTag("LineString");
426 $writer->startTag("coordinates");
428 foreach my $coord (@{$linestring}) {
429 $str .= join(',', @{$coord}) . " \n";
431 $writer->characters($str);
439 writefile("pois.kmp", $config{destdir} . "/$map", $output);
444 my %waypoints = %{$_[0]};
445 foreach my $map (keys %waypoints) {
446 my $poisf = "lat\tlon\ttitle\tdescription\ticon\ticonSize\ticonOffset\n";
447 foreach my $name (keys %{$waypoints{$map}}) {
448 my %options = %{$waypoints{$map}{$name}};
450 $options{'lat'} . "\t" .
451 $options{'lon'} . "\t" .
453 $options{'desc'} . '<br /><a href="' . $options{'page'} . '">' . $name . "</a>\t" .
454 $options{'icon'} . "\n";
457 writefile("pois.txt", $config{destdir} . "/$map", $poisf);
461 # pipe some data through the HTML scrubber
463 # code taken from the meta.pm plugin
465 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
466 return IkiWiki::Plugin::htmlscrubber::sanitize(
467 content => shift, page => shift, destpage => shift);
474 # taken from toggle.pm
478 if ($params{content}=~m!<div[^>]*id="mapdiv-[^"]*"[^>]*>!g) {
479 if (! ($params{content}=~s!</body>!include_javascript($params{page})."</body>"!em)) {
480 # no <body> tag, probably in preview mode
481 $params{content}=$params{content} . include_javascript($params{page});
484 return $params{content};
487 sub prefered_format() {
488 if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
489 $config{'osm_format'} = 'KML';
491 my @spl = split(/, */, $config{'osm_format'});
495 sub include_javascript ($) {
501 if (exists $pagestate{$page}{'osm'}) {
502 foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
503 foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) {
504 my %options = %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}};
505 $options{'map'} = $map;
506 $options{'format'} = prefered_format();
507 $loader .= "mapsetup(\"mapdiv-$name\", " . to_json(\%options) . ");\n";
512 return embed_map_code($page) . "<script type=\"text/javascript\" charset=\"utf-8\">$loader</script>";
522 return unless defined $cgi->param('do') &&
523 $cgi->param("do") eq "osm";
525 IkiWiki::decode_cgi_utf8($cgi);
527 my $map = $cgi->param('map');
528 if (!defined $map || $map !~ /^[a-z]*$/) {
529 error("invalid map parameter");
532 print "Content-Type: text/html\r\n";
534 print "<html><body>";
535 print "<div id=\"mapdiv-$map\"></div>";
536 print embed_map_code();
537 print "<script type=\"text/javascript\" charset=\"utf-8\">mapsetup( 'mapdiv-$map', { 'map': '$map', 'lat': urlParams['lat'], 'lon': urlParams['lon'], 'zoom': urlParams['zoom'], 'fullscreen': 1, 'editable': 1, 'format': '" . prefered_format() . "'});</script>";
538 print "</body></html>";
543 sub embed_map_code(;$) {
545 return '<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript" charset="utf-8"></script>'.
546 '<script src="'.urlto("ikiwiki/osm.js", $page).
547 '" type="text/javascript" charset="utf-8"></script>'."\n";