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 | |
14 | use CGI qw(:standard); |
15 | use CGI qw/escape unescape escapeHTML unescapeHTML/; |
16 | |
17 | print("Content-type: text/html\n\n"); |
18 | my $lon = -999.0; |
19 | my $lat = -999.0; |
f83d1746 |
20 | my $mlon = -999.0; |
21 | my $mlat = -999.0; |
188cb5f8 |
22 | my $zoom = -1; |
f01e50b1 |
23 | my $baselayer = undef; |
188cb5f8 |
24 | if (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 | } |
29 | if (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 |
34 | if (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 | } |
39 | if (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 |
44 | if (defined(param('zoom'))) { |
45 | $zoom = param('zoom'); |
46 | unless ($zoom =~ m/^\d+$/) { $zoom = -1; } |
47 | if (($zoom < 0) || ($zoom > 30)) { $lat = -1; } |
48 | } |
f01e50b1 |
49 | if (defined(param('layers'))) { |
50 | my @layerlist = ('osmde', 'osmorg'); |
51 | my $layers = param('layers'); |
52 | my $i; |
53 | for ($i = 0; $i < int(@layerlist); $i++) { |
54 | if (substr($layers, $i, 1) eq 'B') { # This is our baselayer |
55 | $baselayer = $layerlist[$i]; |
56 | } |
57 | } |
58 | } |
f83d1746 |
59 | # If mlon/mlat is defined but lon/lat isn't, use mlat/mlon as center pos. |
60 | if (($lon == -999.0) && ($lat == -999.0)) { |
61 | if ($mlat != -999.0) { $lat = $mlat; } |
62 | if ($mlon != -999.0) { $lon = $mlon; } |
63 | } |
25c0fde3 |
64 | $mytemplate = $openlayerstemplate; # Openlayers |
65 | if ($0 =~ m/-leaflet/) { |
66 | $mytemplate = $leaflettemplate; # leaflet |
67 | } |
188cb5f8 |
68 | my $OLF; |
25c0fde3 |
69 | unless (open($OLF, '<' . $mytemplate)) { |
188cb5f8 |
70 | print("Sorry, failed to read my map template.\n"); exit(0); |
71 | } |
72 | while ($ll = <$OLF>) { |
73 | # var lon = 11.028847; |
74 | # var lat = 49.573836; |
75 | # var zoom = 16; |
76 | if ($lon >= -180.0) { |
77 | $ll =~ s/var\s+lon\s*=\s*-{0,1}\d+\.\d+;/var lon = $lon;/; |
78 | } |
79 | if ($lat >= -90.0) { |
80 | $ll =~ s/var\s+lat\s*=\s*-{0,1}\d+\.\d+;/var lat = $lat;/; |
81 | } |
f83d1746 |
82 | # In any case, because the script understands the "invalid" value! |
83 | $ll =~ s/var\s+mlon\s*=\s*-{0,1}\d+\.\d+;/var mlon = $mlon;/; |
84 | $ll =~ s/var\s+mlat\s*=\s*-{0,1}\d+\.\d+;/var mlat = $mlat;/; |
188cb5f8 |
85 | if ($zoom >= 0) { |
86 | $ll =~ s/var\s+zoom\s*=\s*\d+;/var zoom = $zoom;/; |
87 | } |
88 | if (($lon >= -180.0) && ($lat >= -180.0) && ($zoom >= 0)) { # All values required in that case |
f01e50b1 |
89 | my $rest = ''; |
90 | if (defined($baselayer)) { |
91 | $rest .= '&maptype=' . $baselayer; |
92 | } |
f83d1746 |
93 | if (($mlon != -999.0) && ($mlat != -999.0)) { |
94 | $rest .= "&markers=$mlat,$mlon,ol-marker"; |
95 | } |
f01e50b1 |
96 | $ll =~ s!<img src="(.*?)/staticmaplite\?.*?size=([^"&]+).*?"!<img src="$1/staticmaplite?center=$lat,$lon&zoom=$zoom&size=$2$rest"!; |
188cb5f8 |
97 | } |
98 | print($ll); |
99 | } |
100 | close($OLF); |