Browse Source

Merge pull request #18 from AnikaBettge/r.in.wms_gdalproxy

r.in.wms: add proxy for GDAL driver
Markus Neteler 6 years ago
parent
commit
39ddb05163
3 changed files with 44 additions and 3 deletions
  1. 22 0
      scripts/r.in.wms/r.in.wms.py
  2. 3 3
      scripts/r.in.wms/wms_base.py
  3. 19 0
      scripts/r.in.wms/wms_gdal_drv.py

+ 22 - 0
scripts/r.in.wms/r.in.wms.py

@@ -145,6 +145,20 @@ This program is free software under the GNU General Public License
 #% guisection: Map style
 #%end
 
+#%option
+#% key: proxy
+#% label: HTTP proxy only GDAL driver (GDAL_HTTP_PROXY)
+#% type: string
+#% description: HTTP proxy
+#%end
+
+#%option
+#% key: proxy_user_pw
+#% label: User and password for HTTP proxy only for GDAL driver (GDAL_HTTP_PROXYUSERPWD). Must be in the form of [user name]:[password].
+#% type: string
+#% description: User and password for HTTP proxy
+#%end
+
 #%option G_OPT_F_BIN_INPUT
 #% key: capfile
 #% required: no
@@ -227,6 +241,14 @@ def main():
         wms.GetCapabilities(options)
     else:
         from wms_base import GRASSImporter
+        # set proxy
+        if options['proxy'] and options['proxy_user_pw']:
+            wms.setProxy(options['proxy'], options['proxy_user_pw'])
+        if options['proxy']:
+            wms.setProxy(options['proxy'])
+            if 'GRASS' in options['driver']:
+                grass.warning(_("The proxy will be ignored by the choosen GRASS driver. It is only used with the GDAL driver."))
+
         options['region'] = GetRegionParams(options['region'])
         fetched_map = wms.GetMap(options, flags)
 

+ 3 - 3
scripts/r.in.wms/wms_base.py

@@ -32,7 +32,7 @@ import grass.script as grass
 from grass.exceptions import CalledModuleError
 
 
-class WMSBase:
+class WMSBase(object):
 
     def __init__(self):
         # these variables are information for destructor
@@ -127,7 +127,7 @@ class WMSBase:
         if 'epsg' in target_crs.keys():
             self.target_epsg = target_crs['epsg']
             if self.source_epsg != self.target_epsg:
-                grass.warning(_("SRS differences: WMS source EPSG %s != location EPSG %s (use srs=%s to adjust)") % 
+                grass.warning(_("SRS differences: WMS source EPSG %s != location EPSG %s (use srs=%s to adjust)") %
                               (self.source_epsg, self.target_epsg, self.target_epsg))
 
         self.proj_srs = grass.read_command('g.proj',
@@ -382,7 +382,7 @@ class WMSBase:
         if self.source_epsg is not None and self.target_epsg is not None \
             and self.source_epsg == self.target_epsg:
             do_reproject = False
-        # TODO: correctly compare source and target crs 
+        # TODO: correctly compare source and target crs
         if do_reproject == True and self.proj_srs == self.proj_location:
             do_reproject = False
         if do_reproject:

+ 19 - 0
scripts/r.in.wms/wms_gdal_drv.py

@@ -35,6 +35,20 @@ class NullDevice():
 
 class WMSGdalDrv(WMSBase):
 
+    def __init__(self):
+        super(WMSGdalDrv, self).__init__()
+        self.proxy = None
+        self.proxy_user_pw = None
+
+    def setProxy(self, proxy, proxy_user_pw=None):
+        """ Set the HTTP proxy and its user and password
+
+        @input proxy HTTP proxy with [IP address]:[port]
+        @input proxy_user_pw with [user name]:[password]
+        """
+        self.proxy = proxy
+        self.proxy_user_pw = proxy_user_pw
+
     def _createXML(self):
         """!Create XML for GDAL WMS driver
 
@@ -127,6 +141,11 @@ class WMSGdalDrv(WMSBase):
         temp_map = self._tempfile()
 
         xml_file = self._createXML()
+
+        if self.proxy:
+            gdal.SetConfigOption('GDAL_HTTP_PROXY', str(self.proxy))
+        if self.proxy_user_pw:
+            gdal.SetConfigOption('GDAL_HTTP_PROXYUSERPWD', str(self.proxy_user_pw))
         wms_dataset = gdal.Open(xml_file, gdal.GA_ReadOnly)
         grass.try_remove(xml_file)
         if wms_dataset is None: