From 917cdb7ade640c1e88f26d99acb42cff55a4c6fc Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 18 Mar 2012 17:07:01 -0400 Subject: [PATCH] multiple osm fixes * fix will_render calls to pass proper relative filenames * fix urls to kml etc files to not assume wiki's top is at / * avoid building the javascript to display the map in two different ways between the cgi and on-page maps * refactor duplicate code --- IkiWiki/Plugin/osm.pm | 68 +++++++++++++++++++++++++----------- underlays/osm/ikiwiki/osm.js | 6 ++-- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 41202b4ef..661f72bab 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -154,18 +154,15 @@ sub process_waypoint { $icon = urlto($icon, $dest, 1); $tag = '' unless $tag; if ($page eq $dest) { - if (!defined($config{'osm_format'}) || !$config{'osm_format'}) { - $config{'osm_format'} = 'KML'; - } - my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'}); + my %formats = get_formats(); if ($formats{'GeoJSON'}) { - will_render($page,$config{destdir} . "/$map/pois.json"); + will_render($page, "$map/pois.json"); } if ($formats{'CSV'}) { - will_render($page,$config{destdir} . "/$map/pois.txt"); + will_render($page, "$map/pois.txt"); } if ($formats{'KML'}) { - will_render($page,$config{destdir} . "/$map/pois.kml"); + will_render($page, "$map/pois.kml"); } } my $href = IkiWiki::cgiurl( @@ -292,10 +289,7 @@ sub savestate { } } - if (!defined($config{'osm_format'}) || !$config{'osm_format'}) { - $config{'osm_format'} = 'KML'; - } - my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'}); + my %formats = get_formats(); if ($formats{'GeoJSON'}) { writejson(\%waypoints, \%linestrings); } @@ -436,7 +430,7 @@ Sample style: $writer->endTag(); $writer->end(); - writefile("pois.kmp", $config{destdir} . "/$map", $output); + writefile("pois.kml", $config{destdir} . "/$map", $output); } } @@ -484,7 +478,7 @@ sub format (@) { return $params{content}; } -sub prefered_format() { +sub preferred_format() { if (!defined($config{'osm_format'}) || !$config{'osm_format'}) { $config{'osm_format'} = 'KML'; } @@ -492,19 +486,21 @@ sub prefered_format() { return shift @spl; } +sub get_formats() { + if (!defined($config{'osm_format'}) || !$config{'osm_format'}) { + $config{'osm_format'} = 'KML'; + } + map { $_ => 1 } split(/, */, $config{'osm_format'}); +} + sub include_javascript ($) { my $page=shift; my $loader; - eval q{use JSON}; - error $@ if $@; if (exists $pagestate{$page}{'osm'}) { foreach my $map (keys %{$pagestate{$page}{'osm'}}) { foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) { - my %options = %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}}; - $options{'map'} = $map; - $options{'format'} = prefered_format(); - $loader .= "mapsetup(\"mapdiv-$name\", " . to_json(\%options) . ");\n"; + $loader .= map_setup_code($map, $name, %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}}); } } } @@ -536,7 +532,15 @@ sub cgi($) { print ""; print "
"; print embed_map_code(); - print ""; + print ""; print ""; exit 0; @@ -549,4 +553,28 @@ sub embed_map_code(;$) { '" type="text/javascript" charset="utf-8">'."\n"; } +sub map_setup_code($;@) { + my $map=shift; + my $name=shift; + my %options=@_; + + eval q{use JSON}; + error $@ if $@; + + $options{'format'} = preferred_format(); + + my %formats = get_formats(); + if ($formats{'GeoJSON'}) { + $options{'jsonurl'} = urlto($map."/pois.json"); + } + if ($formats{'CSV'}) { + $options{'csvurl'} = urlto($map."/pois.txt"); + } + if ($formats{'KML'}) { + $options{'kmlurl'} = urlto($map."/pois.kml"); + } + + return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");"; +} + 1; diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 7994c62fc..d7e3d53f4 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -41,13 +41,13 @@ function mapsetup(divname, options) { map.addLayer(new OpenLayers.Layer.OSM()); if (options.format == 'CSV') { pois = new OpenLayers.Layer.Text( "CSV", - { location:"/" + options.map + "/pois.txt", + { location: options.csvurl, projection: map.displayProjection }); } else if (options.format == 'GeoJSON') { pois = new OpenLayers.Layer.Vector("GeoJSON", { protocol: new OpenLayers.Protocol.HTTP({ - url: "/" + options.map + "/pois.json", + url: options.jsonurl, format: new OpenLayers.Format.GeoJSON() }), strategies: [new OpenLayers.Strategy.Fixed()] @@ -55,7 +55,7 @@ function mapsetup(divname, options) { } else { pois = new OpenLayers.Layer.Vector("KML", { protocol: new OpenLayers.Protocol.HTTP({ - url: "/" + options.map + "/pois.kml", + url: options.kmlurl, format: new OpenLayers.Format.KML({ extractStyles: true, extractAttributes: true -- 2.26.2