Skip to content

Commit

Permalink
Item11187: KML and Vextor Styling working for Clusters
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/OpenLayersPlugin@12897 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulAlexander authored and PaulAlexander committed Oct 27, 2011
1 parent 3e3d377 commit c8d3cbb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 21 deletions.
4 changes: 3 additions & 1 deletion data/Applications/OpenLayers/KMLLayerForm.txt
@@ -1,8 +1,10 @@
%META:TOPICINFO{author="BaseUserMapping_333" date="1319517998" format="1.1" version="1"}%
%META:TOPICINFO{author="BaseUserMapping_333" comment="save topic" date="1319673534" format="1.1" reprev="3" version="3"}%
| *Name* | *Type* | *Size* | *Values* | *Tooltips* | *Attributes* |
| Name | text | | | {String} | |
| URL | text | | | {String} Base URL | |
| ExtractAttributes | text | | | {Boolean} Extract attributes from KML. | |
| ExtractStyles | text | | | {Boolean} Extract point styles and load external graphics (is any) from KML. Set to =on= if graphics exist locally. | |
| Clustering | text | | | {Boolean} Cluster 1 or more points within a 20 pixel circumference. | |
| IsVisible | text | | | | |

%META:FORM{name="LayerTypeForm"}%
Expand Down
100 changes: 80 additions & 20 deletions lib/Foswiki/Plugins/OpenLayersPlugin.pm
Expand Up @@ -187,15 +187,58 @@ sub typehandler_kml {
my ($layerweb, $layertopic) = ($layerObject->web(), $layerObject->topic());
my @fields = $layerObject->find('FIELD');
my %data;
my @returnString;

my $isBaseLayer = $data{IsBaseLayer};
$isBaseLayer = 'false' unless defined $isBaseLayer;
$isBaseLayer = ($isBaseLayer eq 'true')?'true':'false';
my @returnString;
my $strategy;
my $style;

foreach my $field (@fields) {
$data{$field->{name}} = $field->{value};
}

#my $extractStyles = $data{ExtractStyles};
my $extractStyles = Foswiki::Func::isTrue($data{ExtractStyles}, 0) ? 'true' : 'false';
#'off' unless defined $extractStyles;
#$extractStyles = ($extractStyles eq 'on')?'true':'false';

#my $extractAttributes = $data{ExtractAttributes};
#$extractAttributes = 'on' unless defined $extractAttributes;
#$extractAttributes = ($extractAttributes eq 'on')?'true':'false';
my $extractAttributes = Foswiki::Func::isTrue($data{ExtractAttributes}, 0) ? 'true' : 'false';

#my $isBaseLayer = $data{IsBaseLayer};
#$isBaseLayer = 'false' unless defined $isBaseLayer;
#$isBaseLayer = ($isBaseLayer eq 'true')?'true':'false';
my $isBaseLayer = Foswiki::Func::isTrue($data{IsBaseLayer}, 0) ? 'true' : 'false';

my $clustering = $data{Clustering};
# my $distance;
# my $threshold;
$clustering = 'on' unless defined $clustering;
# $clustering = ($clustering eq 'on')?'true':'false';

if ($clustering && $clustering =~ /^(\d+),\s*(\d+)$/) {
my $distance = $1;
my $threshold = $2;
$strategy = ", new OpenLayers.Strategy.Cluster({distance: $distance, threshold: $threshold})";
$clustering = 'true';
} elsif (Foswiki::Func::isTrue($clustering, 1)) {
$clustering = 'true';
$strategy = ", new OpenLayers.Strategy.Cluster()";
} else {
$clustering = 'false';
}

if ($clustering eq 'true') {
$style=<<"HERE";
//Create a style map object and set the 'default' intent to the
var vector_style_map$layerweb$layertopic = new OpenLayers.StyleMap({
'default': style
});
//Add the style map to the vector layer threshold, distance
kmllayer$layerweb$layertopic.styleMap = vector_style_map$layerweb$layertopic;
HERE
}

if ($data{URL} and $data{URL} =~ /\w/) {
if (not $data{URL} =~ /^(\/|$Foswiki::cfg{LinkProtocolPattern})/) {
my ($dataweb, $datatopic) = Foswiki::Func::normalizeWebTopicName($layerweb, $data{URL});
Expand All @@ -208,17 +251,18 @@ sub typehandler_kml {
push @returnString, <<"HERE";
var kmllayer$layerweb$layertopic = new OpenLayers.Layer.Vector(
"$data{Name}",
{ strategies: [new OpenLayers.Strategy.Fixed(), new OpenLayers.Strategy.Cluster()],
{ strategies: [new OpenLayers.Strategy.Fixed() $strategy],
protocol: new OpenLayers.Protocol.HTTP({
url: "$data{URL}",
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: false
extractStyles: $extractStyles,
extractAttributes: $extractAttributes
})
})
});
map.addLayers([kmllayer$layerweb$layertopic]);
$style
HERE

my $returnString = "\n".join("\n", @returnString)."\n";
Expand All @@ -232,6 +276,8 @@ sub typehandler_vector {
my ($layerweb, $layertopic) = ($layerObject->web(), $layerObject->topic());
my @fields = $layerObject->find('FIELD');
my %data;
my $strategy;
my $style;
my @returnString;
my $proxy = '../Applications/OpenLayers'.'.Proxy?skin=text;'.'url=';

Expand All @@ -242,6 +288,23 @@ sub typehandler_vector {
foreach my $field (@fields) {
$data{$field->{name}} = $field->{value};
}

my $clustering = $data{Clustering};
$clustering = 'on' unless defined $clustering;
$clustering = ($clustering eq 'on')?'true':'false';

if ($clustering eq 'true') {
$strategy = ", new OpenLayers.Strategy.Cluster()";
$style=<<"HERE";
//Clustering = $clustering Create a style map object and set the 'default' intent to the
var wikilayer_style_map$layerweb$layertopic = new OpenLayers.StyleMap({
'default': style
});
//Add the style map to the vector layer
wikilayer$layerweb$layertopic.styleMap = wikilayer_style_map$layerweb$layertopic;
HERE
}

if ($data{URL} and $data{URL} =~ /\w/) {
if (not $data{URL} =~ /^(\/|$Foswiki::cfg{LinkProtocolPattern})/) {
my ($dataweb, $datatopic) = Foswiki::Func::normalizeWebTopicName($layerweb, $data{URL});
Expand All @@ -250,22 +313,19 @@ sub typehandler_vector {
} else {
return "<span class='foswikiAlert'>[[$layerweb.$layertopic]] does not contain a URL</span>";
}


push @returnString, <<"HERE";
OpenLayers.ProxyHost = '$proxy';
//OpenLayers.ProxyHost = '$proxy';
var wikilayer$layerweb$layertopic = new OpenLayers.Layer.Vector('$data{Name}',{
protocol: new OpenLayers.Protocol.HTTP({
url: '$data{URL}',
format: new OpenLayers.Format.GeoJSON({})
}),
isBaseLayer:$isBaseLayer,
style: style,
strategies: [new OpenLayers.Strategy.Fixed()]
},{isBaseLayer:$isBaseLayer});
strategies: [new OpenLayers.Strategy.Fixed()$strategy]
});
map.addLayers([wikilayer$layerweb$layertopic]);
$style
HERE

my $returnString = "\n".join("\n", @returnString)."\n";
Expand Down Expand Up @@ -429,7 +489,7 @@ sub _OPENLAYERSMAP {

push @scriptVariable, <<"HERE";
var mapOptions = { maxResolution: 45/512, numZoomLevels: 11, fractionalZoom: true};
var map = new OpenLayers.Map(mapOptions);
map = new OpenLayers.Map(mapOptions);
HERE

my $mapLayerSwitcher = $params->{layerswitcher};
Expand Down Expand Up @@ -486,8 +546,6 @@ HERE
# my $isGoogleLayer;
my $mapLayers = $params->{layers};


push @scriptVariable, @layerScripts;
my $mapDiv='';
my $mapElement = $params->{mapelement};
if (!defined $mapElement) {
Expand All @@ -496,7 +554,7 @@ HERE
}

push @scriptVariable, <<"HERE";
var style = new OpenLayers.Style({
var style = new OpenLayers.Style({
pointRadius: "\${radius}",
fillColor: "#ffcc66",
fillOpacity: 0.8,
Expand All @@ -523,6 +581,8 @@ HERE
});
HERE

push @scriptVariable, @layerScripts;

# push @scriptVariable, <<"HERE";
# var style = new OpenLayers.Style({
# pointRadius: "\${radius}",
Expand Down Expand Up @@ -596,7 +656,7 @@ HERE
}


my $scriptVariable = "<script type='text/javascript'>function init() { \n".join("\n", @scriptVariable)."}\n</script>";
my $scriptVariable = "<script type='text/javascript'>var map;\nfunction init() { \n".join("\n", @scriptVariable)."}\n</script>";
Foswiki::Func::addToZone(
"script",
"OPENLAYERSPLUGIN::OPENLAYERSMAP::$mapElement",
Expand Down

0 comments on commit c8d3cbb

Please sign in to comment.