f7999cac355df54e7e88ac34c29ab2a17001152f
[osmrrze.git] / cgis / osm-map.pl
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 # The file from which we get the Template for Openlayers?
9 $leaflettemplate = '/var/www/index.leaflet.html';
10
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;
20 my $mlon = -999.0;
21 my $mlat = -999.0;
22 my $zoom = -1;
23 my $baselayer = undef;
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 }
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 }
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 }
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 }
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 }
64 $mytemplate = $openlayerstemplate; # Openlayers
65 if ($0 =~ m/-leaflet/) {
66   $mytemplate = $leaflettemplate; # leaflet
67 }
68 my $OLF;
69 unless (open($OLF, '<' . $mytemplate)) {
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   }
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;/;
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
89     my $rest = '';
90     if (defined($baselayer)) {
91       $rest .= '&maptype=' . $baselayer;
92     }
93     if (($mlon != -999.0) && ($mlat != -999.0)) {
94       $rest .= "&markers=$mlat,$mlon,ol-marker";
95     }
96     $ll =~ s!<img src="(.*?)/staticmaplite\?.*?size=([^"&]+).*?"!<img src="$1/staticmaplite?center=$lat,$lon&zoom=$zoom&size=$2$rest"!;
97   }
98   print($ll);
99 }
100 close($OLF);
This page took 0.049898 seconds and 2 git commands to generate.