From 8fb42f42f495095ca32a569401df21f089f0e40c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Genevi=C3=A8ve=20Bastien?= Date: Mon, 6 Aug 2012 23:00:31 -0400 Subject: [PATCH 1/9] OSM plugin: new config option to specify the url to fetch maps from --- IkiWiki/Plugin/osm.pm | 13 +++++++++++++ underlays/osm/ikiwiki/osm.js | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 764767525..21d1dce3f 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -67,6 +67,13 @@ sub getsetup () { safe => 0, rebuild => 1, }, + osm_map_url => { + type => "string", + example => "/tiles/\${z}/\${x}/\${y}.png", + description => "Url to get map tiles from (if none specified, uses the openstreetmap server, see http://wiki.openstreetmap.org/wiki/Creating_your_own_tiles for more info on serving your own tiles)", + safe => 0, + rebuild => 1, + }, } @@ -540,6 +547,8 @@ sub map_setup_code($;@) { my $name=shift; my %options=@_; + my $mapurl = $config{osm_map_url}; + eval q{use JSON}; error $@ if $@; @@ -556,6 +565,10 @@ sub map_setup_code($;@) { $options{'kmlurl'} = urlto($map."/pois.kml"); } + if ($mapurl) { + $options{'mapurl'} = $mapurl; + } + return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");"; } diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index d7e3d53f4..644d4b573 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -37,8 +37,13 @@ function mapsetup(divname, options) { numZoomLevels: 18 }); + if (options.mapurl) { + var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl, {numZoomLevels: 19, isBaseLayer: true}); + map.addLayer(newLayer); + } else { + map.addLayer(new OpenLayers.Layer.OSM()); + } - map.addLayer(new OpenLayers.Layer.OSM()); if (options.format == 'CSV') { pois = new OpenLayers.Layer.Text( "CSV", { location: options.csvurl, From b57cad3131218c7d1a027a60c71ede74f0f14829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 9 Aug 2012 00:40:17 -0400 Subject: [PATCH 2/9] osm: be explicit about projection of data --- underlays/osm/ikiwiki/osm.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 644d4b573..388d625f1 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -47,7 +47,7 @@ function mapsetup(divname, options) { if (options.format == 'CSV') { pois = new OpenLayers.Layer.Text( "CSV", { location: options.csvurl, - projection: map.displayProjection + projection: new OpenLayers.Projection("EPSG:4326") }); } else if (options.format == 'GeoJSON') { pois = new OpenLayers.Layer.Vector("GeoJSON", { @@ -55,7 +55,8 @@ function mapsetup(divname, options) { url: options.jsonurl, format: new OpenLayers.Format.GeoJSON() }), - strategies: [new OpenLayers.Strategy.Fixed()] + strategies: [new OpenLayers.Strategy.Fixed()], + projection: new OpenLayers.Projection("EPSG:4326") }); } else { pois = new OpenLayers.Layer.Vector("KML", { @@ -66,7 +67,9 @@ function mapsetup(divname, options) { extractAttributes: true }) }), - strategies: [new OpenLayers.Strategy.Fixed()]}); + strategies: [new OpenLayers.Strategy.Fixed()], + projection: new OpenLayers.Projection("EPSG:4326") + }); } map.addLayer(pois); select = new OpenLayers.Control.SelectFeature(pois); From acc4d7d7d65ed9519fa9b5fca7c878f3e9a31451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 9 Aug 2012 01:07:20 -0400 Subject: [PATCH 3/9] osm: add optional google maps support for google maps to work, an API key needs to be added to the configuration --- IkiWiki/Plugin/osm.pm | 16 ++++++++++++++-- underlays/osm/ikiwiki/osm.js | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 21d1dce3f..d86dbd66e 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -74,7 +74,13 @@ sub getsetup () { safe => 0, rebuild => 1, }, - + osm_google_apikey => { + type => "string", + example => "", + description => "Google maps API key, Google layer not used if missing, see https://code.google.com/apis/console/ to get an API key", + safe => 1, + rebuild => 1, + }, } sub register_rendered_files { @@ -144,6 +150,7 @@ sub preprocess { lat => $lat, lon => $lon, href => $href, + google_apikey => $config{'osm_google_apikey'}, }; return "
"; } @@ -527,6 +534,7 @@ sub cgi($) { zoom => "urlParams['zoom']", fullscreen => 1, editable => 1, + google_apikey => $config{'osm_google_apikey'}, ); print ""; print ""; @@ -537,9 +545,13 @@ sub cgi($) { sub embed_map_code(;$) { my $page=shift; my $olurl = $config{osm_openlayers_url} || "http://www.openlayers.org/api/OpenLayers.js"; - return ''. + my $code = ''."\n". ''."\n"; + if ($config{'osm_google_apikey'}) { + $code .= ''; + } + return $code; } sub map_setup_code($;@) { diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 388d625f1..9269bd899 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -34,6 +34,10 @@ function mapsetup(divname, options) { new OpenLayers.Control.Permalink(permalink) ], displayProjection: new OpenLayers.Projection("EPSG:4326"), + maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), + projection: "EPSG:900913", + units: "m", + maxResolution: 156543.0339, numZoomLevels: 18 }); @@ -44,6 +48,17 @@ function mapsetup(divname, options) { map.addLayer(new OpenLayers.Layer.OSM()); } + // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html + if (options.google_apikey && options.google_apikey != 'null') { + googleLayer = new OpenLayers.Layer.Google( + "Google Hybrid", + {type: G_HYBRID_MAP, + 'sphericalMercator': true, + 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), + projection: new OpenLayers.Projection("EPSG:3857")} + ); + map.addLayer(googleLayer); + } if (options.format == 'CSV') { pois = new OpenLayers.Layer.Text( "CSV", { location: options.csvurl, From ac775ef8e72170102f8db94dc56c6add861234ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Sat, 11 Aug 2012 11:14:23 -0400 Subject: [PATCH 4/9] extend default zoom to 19, OSM's maximum, simplify code --- underlays/osm/ikiwiki/osm.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 9269bd899..c41aeeb1f 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -38,11 +38,11 @@ function mapsetup(divname, options) { projection: "EPSG:900913", units: "m", maxResolution: 156543.0339, - numZoomLevels: 18 + numZoomLevels: 19 }); if (options.mapurl) { - var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl, {numZoomLevels: 19, isBaseLayer: true}); + var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl); map.addLayer(newLayer); } else { map.addLayer(new OpenLayers.Layer.OSM()); From 2fb93db1afea846447cfe8ce723af80e2aa25882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Sat, 11 Aug 2012 11:15:31 -0400 Subject: [PATCH 5/9] simplify local tile code, uniform titles --- underlays/osm/ikiwiki/osm.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index c41aeeb1f..4f53b1dc8 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -42,10 +42,9 @@ function mapsetup(divname, options) { }); if (options.mapurl) { - var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl); - map.addLayer(newLayer); + map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Local)", options.mapurl)); } else { - map.addLayer(new OpenLayers.Layer.OSM()); + map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)")); } // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html From 081fd3863698a273eb468cceec84cc0cffb10261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Fri, 24 Aug 2012 15:55:28 -0400 Subject: [PATCH 6/9] make layers completely customizable --- IkiWiki/Plugin/osm.pm | 17 +++++++++--- underlays/osm/ikiwiki/osm.js | 51 +++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index d86dbd66e..4c50fec53 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -67,10 +67,19 @@ sub getsetup () { safe => 0, rebuild => 1, }, - osm_map_url => { + osm_layers => { type => "string", - example => "/tiles/\${z}/\${x}/\${y}.png", - description => "Url to get map tiles from (if none specified, uses the openstreetmap server, see http://wiki.openstreetmap.org/wiki/Creating_your_own_tiles for more info on serving your own tiles)", + example => { OSM => 1, + Google => 'Hybrid', + }, + description => "Layers to use in the map. If the value is 1, use the default for the map, otherwise the argument is a URL (for OSM layers, e.g. http://a.tile.stamen.com/toner/\${z}/\${x}/\${y}.png) or a type option for Google maps (Normal, Satellite, Hybrid or Physical).", + safe => 0, + rebuild => 1, + }, + osm_layers_order => { + type => "string", + example => { 'OSM', 'Google' }, + description => "Display order for the layers. The first layer is the default layer, must match exactly the left side of the osm_layers hash.", safe => 0, rebuild => 1, }, @@ -580,6 +589,8 @@ sub map_setup_code($;@) { if ($mapurl) { $options{'mapurl'} = $mapurl; } + $options{'layers'} = $config{osm_layers}; + $options{'layers_order'} = $config{osm_layers_order}; return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");"; } diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 4f53b1dc8..9547ee29c 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -41,23 +41,44 @@ function mapsetup(divname, options) { numZoomLevels: 19 }); - if (options.mapurl) { - map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Local)", options.mapurl)); - } else { - map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)")); + for (x in options.layers_order) { + layer = options.layers_order[x]; + console.log("setting up layer: " + layer + " with argument : " + options.layers[layer]); + if (layer.indexOf("Google") >= 0) { + if (options.google_apikey && options.google_apikey != 'null') { + var gtype = G_NORMAL_MAP; + var gtext = ""; + if (options.layers[layer] == "Satellite") { + gtype = G_SATELLITE_MAP; + } else if (options.layers[layer] == "Hybrid") { + gtype = G_HYBRID_MAP // the normal map overlaying the satellite photographs + } else if (options.layers[layer] == "Physical") { + gtype = G_PHYSICAL_MAP // terrain information + } + // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html + googleLayer = new OpenLayers.Layer.Google( + "Google " + options.layers[layer], + {type: gtype, + 'sphericalMercator': true, + 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), + projection: new OpenLayers.Projection("EPSG:3857")} + ); + map.addLayer(googleLayer); + } else { + console.log("no API key defined for Google layer, skipping"); + } + } else { // OSM + if (options.layers[layer] != 1) { + l = options.layers[layer]; + fqdn = l.split("/")[2].split(".") + text = fqdn[fqdn.length-2] + map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (" + text + ")", l)); + } else { + map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)")); + } + } } - // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html - if (options.google_apikey && options.google_apikey != 'null') { - googleLayer = new OpenLayers.Layer.Google( - "Google Hybrid", - {type: G_HYBRID_MAP, - 'sphericalMercator': true, - 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), - projection: new OpenLayers.Projection("EPSG:3857")} - ); - map.addLayer(googleLayer); - } if (options.format == 'CSV') { pois = new OpenLayers.Layer.Text( "CSV", { location: options.csvurl, From 69467738ed3e066f8ac8ff13329173c3f2660cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Fri, 24 Aug 2012 17:29:41 -0400 Subject: [PATCH 7/9] openlayers: use the provided layer name and put the source in parenthesis --- underlays/osm/ikiwiki/osm.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 9547ee29c..05ab793eb 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -70,11 +70,11 @@ function mapsetup(divname, options) { } else { // OSM if (options.layers[layer] != 1) { l = options.layers[layer]; - fqdn = l.split("/")[2].split(".") - text = fqdn[fqdn.length-2] - map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (" + text + ")", l)); + fqdn = l.split("/")[2].split("."); + text = fqdn[fqdn.length-2] + "." + fqdn[fqdn.length-1]; + map.addLayer(new OpenLayers.Layer.OSM(layer + " (" + text + ")", l)); } else { - map.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)")); + map.addLayer(new OpenLayers.Layer.OSM(layer + " (Mapnik)")); } } } From c7070ddd48232f43cbc4d48b399aad9636084364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Fri, 24 Aug 2012 17:31:31 -0400 Subject: [PATCH 8/9] put google layers in parenthesis --- underlays/osm/ikiwiki/osm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 05ab793eb..8b2170706 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -57,7 +57,7 @@ function mapsetup(divname, options) { } // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html googleLayer = new OpenLayers.Layer.Google( - "Google " + options.layers[layer], + "Google (" + options.layers[layer] + ")", {type: gtype, 'sphericalMercator': true, 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), From 636e04a13a96f11c67d82aebfb4ee9fd51b61110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Sat, 25 Aug 2012 08:53:30 -0400 Subject: [PATCH 9/9] make layers an array this simplifies the code, make the configuration more intuitive, at the cost of making the labels on the layers automatically generated and therefore less customizable --- IkiWiki/Plugin/osm.pm | 14 ++------------ underlays/osm/ikiwiki/osm.js | 29 ++++++++++++----------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 4c50fec53..2c555f372 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -69,17 +69,8 @@ sub getsetup () { }, osm_layers => { type => "string", - example => { OSM => 1, - Google => 'Hybrid', - }, - description => "Layers to use in the map. If the value is 1, use the default for the map, otherwise the argument is a URL (for OSM layers, e.g. http://a.tile.stamen.com/toner/\${z}/\${x}/\${y}.png) or a type option for Google maps (Normal, Satellite, Hybrid or Physical).", - safe => 0, - rebuild => 1, - }, - osm_layers_order => { - type => "string", - example => { 'OSM', 'Google' }, - description => "Display order for the layers. The first layer is the default layer, must match exactly the left side of the osm_layers hash.", + example => { 'OSM', 'GoogleSattelite' }, + description => "Layers to use in the map. Can be either the 'OSM' string or a type option for Google maps (GoogleNormal, GoogleSatellite, GoogleHybrid or GooglePhysical). It can also be an arbitrary URL in a syntax acceptable for OpenLayers.Layer.OSM.url parameter.", safe => 0, rebuild => 1, }, @@ -590,7 +581,6 @@ sub map_setup_code($;@) { $options{'mapurl'} = $mapurl; } $options{'layers'} = $config{osm_layers}; - $options{'layers_order'} = $config{osm_layers_order}; return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");"; } diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 8b2170706..5ba3223ad 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -41,23 +41,22 @@ function mapsetup(divname, options) { numZoomLevels: 19 }); - for (x in options.layers_order) { - layer = options.layers_order[x]; - console.log("setting up layer: " + layer + " with argument : " + options.layers[layer]); + for (x in options.layers) { + layer = options.layers[x]; + console.log("setting up layer: " + layer); if (layer.indexOf("Google") >= 0) { if (options.google_apikey && options.google_apikey != 'null') { var gtype = G_NORMAL_MAP; - var gtext = ""; - if (options.layers[layer] == "Satellite") { + if (layer.indexOf("Satellite") >= 0) { gtype = G_SATELLITE_MAP; - } else if (options.layers[layer] == "Hybrid") { + } else if (layer.indexOf("Hybrid") >= 0) { gtype = G_HYBRID_MAP // the normal map overlaying the satellite photographs - } else if (options.layers[layer] == "Physical") { + } else if (layer.indexOf("Physical") >= 0) { gtype = G_PHYSICAL_MAP // terrain information } // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html googleLayer = new OpenLayers.Layer.Google( - "Google (" + options.layers[layer] + ")", + layer, {type: gtype, 'sphericalMercator': true, 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), @@ -67,15 +66,11 @@ function mapsetup(divname, options) { } else { console.log("no API key defined for Google layer, skipping"); } - } else { // OSM - if (options.layers[layer] != 1) { - l = options.layers[layer]; - fqdn = l.split("/")[2].split("."); - text = fqdn[fqdn.length-2] + "." + fqdn[fqdn.length-1]; - map.addLayer(new OpenLayers.Layer.OSM(layer + " (" + text + ")", l)); - } else { - map.addLayer(new OpenLayers.Layer.OSM(layer + " (Mapnik)")); - } + } else if (layer == 'OSM') { // OSM default layer + map.addLayer(new OpenLayers.Layer.OSM("OSM (Mapnik)")); + } else { // assumed to be a URL + text = layer.match(/([^.\/]*\.[^.\/]*(\/[^\$]*)?)\/.*$/i) // take the first two parts of the FQDN and everything before the first $ + map.addLayer(new OpenLayers.Layer.OSM("OSM (" + text[1] + ")", layer)); } }