support more layers, also support leaflet permalinks (so the noscript-replacement...
[osmrrze.git] / cgis / osm-map.pl
CommitLineData
188cb5f8 1#!/usr/bin/perl -w
2
3# OSM Map CGI - creating a HTML page that starts in the right place
4
5# The file from which we get the Template for Openlayers?
6$openlayerstemplate = '/var/www/index.openlayers.html';
7
25c0fde3 8# The file from which we get the Template for Openlayers?
9$leaflettemplate = '/var/www/index.leaflet.html';
10
188cb5f8 11# Your should not need to change anything below this line
12# ----------------------------------------------------------------------------
13
14use CGI qw(:standard);
15use CGI qw/escape unescape escapeHTML unescapeHTML/;
16
17print("Content-type: text/html\n\n");
18my $lon = -999.0;
19my $lat = -999.0;
f83d1746 20my $mlon = -999.0;
21my $mlat = -999.0;
188cb5f8 22my $zoom = -1;
f01e50b1 23my $baselayer = undef;
188cb5f8 24if (defined(param('lon'))) {
25 $lon = param('lon');
26 unless ($lon =~ m/^-{0,1}\d+\.\d+$/) { $lon = -999.0; }
27 if (($lon < -180.0) || ($lon > 180.0)) { $lon = -999.0; }
28}
29if (defined(param('lat'))) {
30 $lat = param('lat');
31 unless ($lat =~ m/^-{0,1}\d+\.\d+$/) { $lat = -999.0; }
32 if (($lat < -90.0) || ($lat > 90.0)) { $lat = -999.0; }
33}
f83d1746 34if (defined(param('mlon'))) {
35 $mlon = param('mlon');
36 unless ($mlon =~ m/^-{0,1}\d+\.\d+$/) { $mlon = -999.0; }
37 if (($mlon < -180.0) || ($mlon > 180.0)) { $mlon = -999.0; }
38}
39if (defined(param('mlat'))) {
40 $mlat = param('mlat');
41 unless ($mlat =~ m/^-{0,1}\d+\.\d+$/) { $mlat = -999.0; }
42 if (($mlat < -90.0) || ($mlat > 90.0)) { $mlat = -999.0; }
43}
188cb5f8 44if (defined(param('zoom'))) {
45 $zoom = param('zoom');
46 unless ($zoom =~ m/^\d+$/) { $zoom = -1; }
47 if (($zoom < 0) || ($zoom > 30)) { $lat = -1; }
48}
f01e50b1 49if (defined(param('layers'))) {
898873d3
UO
50 # Mapping for Openlayers Permalinks
51 my @layerlist = ('osmde', 'osmorg', 'osmorglowzoom', 'luftbilderl');
f01e50b1 52 my $layers = param('layers');
53 my $i;
54 for ($i = 0; $i < int(@layerlist); $i++) {
55 if (substr($layers, $i, 1) eq 'B') { # This is our baselayer
56 $baselayer = $layerlist[$i];
57 }
58 }
59}
898873d3
UO
60if (defined(param('layer'))) {
61 # Mapping for Leaflet Permalinks
62 my $layers = param('layer');
63 if ($layers eq 'RRZE tileserver osm.de style tiles') { $baselayer = 'osmde'; }
64 if ($layers eq 'RRZE tileserver osm.org style tiles') { $baselayer = 'osmorg'; }
65 if ($layers eq 'RRZE tileserver osm.org style with different lowzoom tiles') { $baselayer = 'osmorglowzoom'; }
66 if ($layers eq 'Luftbild Erlangen') { $baselayer = 'luftbilderl'; }
67}
f83d1746 68# If mlon/mlat is defined but lon/lat isn't, use mlat/mlon as center pos.
69if (($lon == -999.0) && ($lat == -999.0)) {
70 if ($mlat != -999.0) { $lat = $mlat; }
71 if ($mlon != -999.0) { $lon = $mlon; }
72}
25c0fde3 73$mytemplate = $openlayerstemplate; # Openlayers
74if ($0 =~ m/-leaflet/) {
75 $mytemplate = $leaflettemplate; # leaflet
76}
188cb5f8 77my $OLF;
25c0fde3 78unless (open($OLF, '<' . $mytemplate)) {
188cb5f8 79 print("Sorry, failed to read my map template.\n"); exit(0);
80}
81while ($ll = <$OLF>) {
82 # var lon = 11.028847;
83 # var lat = 49.573836;
84 # var zoom = 16;
85 if ($lon >= -180.0) {
86 $ll =~ s/var\s+lon\s*=\s*-{0,1}\d+\.\d+;/var lon = $lon;/;
87 }
88 if ($lat >= -90.0) {
89 $ll =~ s/var\s+lat\s*=\s*-{0,1}\d+\.\d+;/var lat = $lat;/;
90 }
f83d1746 91 # In any case, because the script understands the "invalid" value!
92 $ll =~ s/var\s+mlon\s*=\s*-{0,1}\d+\.\d+;/var mlon = $mlon;/;
93 $ll =~ s/var\s+mlat\s*=\s*-{0,1}\d+\.\d+;/var mlat = $mlat;/;
188cb5f8 94 if ($zoom >= 0) {
95 $ll =~ s/var\s+zoom\s*=\s*\d+;/var zoom = $zoom;/;
96 }
97 if (($lon >= -180.0) && ($lat >= -180.0) && ($zoom >= 0)) { # All values required in that case
f01e50b1 98 my $rest = '';
99 if (defined($baselayer)) {
100 $rest .= '&maptype=' . $baselayer;
101 }
f83d1746 102 if (($mlon != -999.0) && ($mlat != -999.0)) {
103 $rest .= "&markers=$mlat,$mlon,ol-marker";
104 }
f01e50b1 105 $ll =~ s!<img src="(.*?)/staticmaplite\?.*?size=([^"&]+).*?"!<img src="$1/staticmaplite?center=$lat,$lon&zoom=$zoom&size=$2$rest"!;
188cb5f8 106 }
107 print($ll);
108}
109close($OLF);
This page took 0.062315 seconds and 4 git commands to generate.