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