Ejemplo n.º 1
0
    def test_layer(self):
        import httplib2
        from c2cgeoportal.views.entry import Entry
        from c2cgeoportal.models import Layer, LayerGroup, Theme

        request = testing.DummyRequest()
        request.headers['Host'] = host
        request.static_url = lambda url: '/dummy/static/' + url
        request.route_url = lambda url: '/dummy/route/' + url
        request.registry.settings = {
            'project': 'test_layer',
        }
        entry = Entry(request)

        self.assertEqual(entry._group(LayerGroup(), [], [], None), (None, [], False))

        layer = Layer()
        layer.id = 20
        layer.name = 'test internal WMS'
        layer.metadataURL = "http://example.com/tiwms"
        layer.isChecked = True
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.style = "my-style"
        layer.kml = "tiwms.kml"
        layer.legend = True
        layer.legendRule = "rule"
        layer.legendImage = "legend:static/tiwms-legend.png"
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.disclaimer = "Camptocamp"
        layer.identifierAttributeField = "name"
        layer.geoTable = "tiwms"
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': 'test internal WMS',
            'metadataURL': 'http://example.com/tiwms',
            'isChecked': True,
            'icon': '/dummy/route/mapserverproxy?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&LAYER=test internal WMS&FORMAT=image/png&TRANSPARENT=TRUE&RULE=rule',
            'type': u'internal WMS',
            'imageType': 'image/png',
            'style': 'my-style',
            'kml': '/dummy/static/test_layer:static/tiwms.kml',
            'legend': True,
            'legendImage': '/dummy/static/legend:static/tiwms-legend.png',
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'disclaimer': 'Camptocamp',
            'identifierAttribute': 'name',
            'editable': True,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test external WMS'
        layer.isChecked = False
        layer.icon = "tewms.png"
        layer.layerType = "external WMS"
        layer.url = "http://example.com"
        layer.imageType = "image/jpeg"
        layer.isSingleTile = True
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': 'test external WMS',
            'icon': '/dummy/static/test_layer:static/tewms.png',
            'isChecked': False,
            'type': u'external WMS',
            'url': 'http://example.com',
            'imageType': 'image/jpeg',
            'isSingleTile': True,
            'legend': False,
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.style = 'wmts-style'
        layer.dimensions = '{"DATE": "1012"}'
        layer.matrixSet = "swissgrid"
        layer.wmsUrl = 'http://example.com/'
        layer.wmsLayers = 'test'
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': 'test WMTS',
            'isChecked': False,
            'type': 'WMTS',
            'url': 'http://example.com/WMTS-Capabilities.xml',
            'style': 'wmts-style',
            'dimensions': {u'DATE': u'1012'},
            'matrixSet': 'swissgrid',
            'wmsUrl': 'http://example.com/',
            'wmsLayers': 'test',
            'legend': False,
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsUrl = 'http://example.com/'
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': 'test WMTS',
            'isChecked': False,
            'type': 'WMTS',
            'url': 'http://example.com/WMTS-Capabilities.xml',
            'wmsUrl': 'http://example.com/',
            'wmsLayers': 'test WMTS',
            'legend': False,
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'test'
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': 'test WMTS',
            'isChecked': False,
            'type': 'WMTS',
            'url': 'http://example.com/WMTS-Capabilities.xml',
            'wmsUrl': '/dummy/route/mapserverproxy',
            'wmsLayers': 'test',
            'queryLayers': [],
            'legend': False,
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test no 2D'
        layer.isChecked = False
        layer.layerType = "no 2D"
        layer.legend = False
        layer.metadataURL = 'http://example.com/wmsfeatures.metadata'
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None), ({
            'id': 20,
            'name': u'test no 2D',
            'isChecked': False,
            'type': u'no 2D',
            'legend': False,
            'metadataURL': u'http://example.com/wmsfeatures.metadata',
            'public': True,
        }, []))

        curdir = os.path.dirname(os.path.abspath(__file__))
        mapfile = os.path.join(curdir, 'c2cgeoportal_test.map')

        mapfile = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'c2cgeoportal_test.map'
        )
        params = (
            ('map', mapfile),
            ('SERVICE', 'WMS'),
            ('VERSION', '1.1.1'),
            ('REQUEST', 'GetCapabilities'),
        )
        mapserv = "%s?map=%s&" % (mapserv_url, mapfile)
        url = mapserv + '&'.join(['='.join(p) for p in params])
        http = httplib2.Http()
        h = {'Host': host}
        resp, xml =http.request(url, method='GET', headers=h)

        wms = WebMapService(None, xml=xml)
        wms_layers = list(wms.contents)

        layer = Layer()
        layer.id = 20
        layer.name = 'test_wmsfeaturesgroup'
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.isChecked = False
        layer.legend = False
        layer.public = True
        self.assertEqual(entry._layer(layer, wms_layers, wms), ({
            'id': 20,
            'name': u'test_wmsfeaturesgroup',
            'type': u'internal WMS',
            'isChecked': False,
            'legend': False,
            'imageType': u'image/png',
            'minResolutionHint': 1.76,
            'maxResolutionHint': 8.8200000000000003,
            'public': True,
            'queryable': 0,
            'metadataUrls': [{
                'url': 'http://example.com/wmsfeatures.metadata',
                'type': 'TC211',
                'format': 'text/plain',
            }],
            'childLayers': [{
                'name': u'test_wmsfeatures',
                'minResolutionHint': 1.76,
                'maxResolutionHint': 8.8200000000000003,
                'queryable': 1,
            }],
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'test_wmsfeatures'
        layer.legend = False
        layer.public = True
        self.assertEqual(entry._layer(layer, wms_layers, wms), ({
            'id': 20,
            'name': 'test WMTS',
            'isChecked': False,
            'type': 'WMTS',
            'url': 'http://example.com/WMTS-Capabilities.xml',
            'wmsUrl': '/dummy/route/mapserverproxy',
            'wmsLayers': 'test_wmsfeatures',
            'queryLayers': [{
                'name': 'test_wmsfeatures',
                'minResolutionHint': 1.76,
                'maxResolutionHint': 8.8200000000000003
            }],
            'legend': False,
            'public': True,
        }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'foo'
        layer.queryLayers = 'test_wmsfeatures'
        layer.legend = False
        layer.public = True
        self.assertEqual(entry._layer(layer, wms_layers, wms), ({
            'id': 20,
            'name': 'test WMTS',
            'isChecked': False,
            'type': 'WMTS',
            'url': 'http://example.com/WMTS-Capabilities.xml',
            'wmsUrl': '/dummy/route/mapserverproxy',
            'wmsLayers': 'foo',
            'queryLayers': [{
                'name': 'test_wmsfeatures',
                'minResolutionHint': 1.76,
                'maxResolutionHint': 8.8200000000000003
            }],
            'legend': False,
            'public': True,
        }, []))

        group1 = LayerGroup()
        group1.name = 'block'
        group2 = LayerGroup()
        group2.name = 'node'
        group2.metadataURL = 'http://example.com/group.metadata'
        layer = Layer()
        layer.id = 20
        layer.name = 'test layer in group'
        layer.isChecked = False
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.legend = False
        layer.public = True
        group1.children = [group2]
        group2.children = [layer]
        self.assertEqual(entry._group(group1, [layer], [], None), ({
            'isExpanded': False,
            'isInternalWMS': True,
            'name': u'block',
            'isBaseLayer': False,
            'children': [{
                'isExpanded': False,
                'isInternalWMS': True,
                'name': u'node',
                'isBaseLayer': False,
                'metadataURL': 'http://example.com/group.metadata',
                'children': [{
                    'name': u'test layer in group',
                    'id': 20,
                    'isChecked': False,
                    'type': u'internal WMS',
                    'legend': False,
                    'imageType': u'image/png',
                    'public': True,
                }]
            }]
        }, [], False))

        group1 = LayerGroup()
        group1.isInternalWMS = True
        group2 = LayerGroup()
        group2.isInternalWMS = False
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group1 = LayerGroup()
        group1.isInternalWMS = False
        group2 = LayerGroup()
        group2.isInternalWMS = True
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'internal WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'external WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'WMTS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'no 2D'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'internal WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'external WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'WMTS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'no 2D'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)
Ejemplo n.º 2
0
    def test_layer(self):
        import httplib2
        from c2cgeoportal.views.entry import Entry
        from c2cgeoportal.models import Layer, LayerGroup
        from c2cgeoportal.lib.wmstparsing import TimeInformation

        request = self._create_request_obj()
        request.static_url = lambda name: '/dummy/static/' + name
        request.route_url = lambda name: '/dummy/route/' + name
        request.registry.settings['project'] = 'test_layer'
        entry = Entry(request)

        self.assertEqual(
            entry._group(LayerGroup(), [], [], None, TimeInformation()),
            (None, [], False))

        layer = Layer()
        layer.id = 20
        layer.name = 'test internal WMS'
        layer.metadataURL = "http://example.com/tiwms"
        layer.isChecked = True
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.style = "my-style"
        layer.kml = "tiwms.kml"
        layer.legend = True
        layer.legendRule = "rule"
        layer.legendImage = "legend:static/tiwms-legend.png"
        layer.isLegendExpanded = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.disclaimer = "Camptocamp"
        layer.identifierAttributeField = "name"
        layer.geoTable = "tiwms"
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None, TimeInformation(
        )), ({
            'id': 20,
            'name': 'test internal WMS',
            'metadataURL': 'http://example.com/tiwms',
            'isChecked': True,
            'icon': '/dummy/route/mapserverproxy?SERVICE=WMS&VERSION=1.1.1&'
            'REQUEST=GetLegendGraphic&LAYER=test internal WMS&FORMAT=image/png&TRANSPARENT=TRUE&RULE=rule',
            'type': u'internal WMS',
            'imageType': 'image/png',
            'style': 'my-style',
            'kml': '/dummy/static/test_layer:static/tiwms.kml',
            'legend': True,
            'legendImage': '/dummy/static/legend:static/tiwms-legend.png',
            'isLegendExpanded': False,
            'minResolutionHint': 10,
            'maxResolutionHint': 1000,
            'disclaimer': 'Camptocamp',
            'identifierAttribute': 'name',
            'editable': True,
            'public': True,
        }, ['The layer test internal WMS is not defined in WMS capabilities']))

        layer = Layer()
        layer.id = 20
        layer.name = 'test external WMS'
        layer.isChecked = False
        layer.icon = "tewms.png"
        layer.layerType = "external WMS"
        layer.url = "http://example.com"
        layer.imageType = "image/jpeg"
        layer.isSingleTile = True
        layer.legend = False
        layer.isLegendExpanded = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(
            layer, [], None, TimeInformation), ({
                'id': 20,
                'name': 'test external WMS',
                'icon': '/dummy/static/test_layer:static/tewms.png',
                'isChecked': False,
                'type': u'external WMS',
                'url': 'http://example.com',
                'imageType': 'image/jpeg',
                'isSingleTile': True,
                'legend': False,
                'isLegendExpanded': False,
                'minResolutionHint': 10,
                'maxResolutionHint': 1000,
                'public': True,
            }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.style = 'wmts-style'
        layer.dimensions = '{"DATE": "1012"}'
        layer.matrixSet = "swissgrid"
        layer.wmsUrl = 'http://example.com/'
        layer.wmsLayers = 'test'
        layer.legend = False
        layer.isLegendExpanded = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None, TimeInformation()),
                         ({
                             'id': 20,
                             'name': 'test WMTS',
                             'isChecked': False,
                             'type': 'WMTS',
                             'url': 'http://example.com/WMTS-Capabilities.xml',
                             'style': 'wmts-style',
                             'dimensions': {
                                 u'DATE': u'1012'
                             },
                             'matrixSet': 'swissgrid',
                             'wmsUrl': 'http://example.com/',
                             'wmsLayers': 'test',
                             'legend': False,
                             'isLegendExpanded': False,
                             'minResolutionHint': 10,
                             'maxResolutionHint': 1000,
                             'public': True,
                         }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsUrl = 'http://example.com/'
        layer.legend = False
        layer.isLegendExpanded = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None, TimeInformation()),
                         ({
                             'id': 20,
                             'name': 'test WMTS',
                             'isChecked': False,
                             'type': 'WMTS',
                             'url': 'http://example.com/WMTS-Capabilities.xml',
                             'wmsUrl': 'http://example.com/',
                             'wmsLayers': 'test WMTS',
                             'legend': False,
                             'isLegendExpanded': False,
                             'minResolutionHint': 10,
                             'maxResolutionHint': 1000,
                             'public': True,
                         }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'test'
        layer.legend = False
        layer.isLegendExpanded = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(entry._layer(layer, [], None, TimeInformation()),
                         ({
                             'id': 20,
                             'name': 'test WMTS',
                             'isChecked': False,
                             'type': 'WMTS',
                             'url': 'http://example.com/WMTS-Capabilities.xml',
                             'wmsUrl': '/dummy/route/mapserverproxy',
                             'wmsLayers': 'test',
                             'queryLayers': [],
                             'legend': False,
                             'isLegendExpanded': False,
                             'minResolutionHint': 10,
                             'maxResolutionHint': 1000,
                             'public': True,
                         }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test no 2D'
        layer.isChecked = False
        layer.layerType = "no 2D"
        layer.legend = False
        layer.isLegendExpanded = False
        layer.metadataURL = 'http://example.com/wmsfeatures.metadata'
        layer.public = True
        self.assertEqual(entry._layer(
            layer, [], None, TimeInformation()), ({
                'id': 20,
                'name': u'test no 2D',
                'isChecked': False,
                'type': u'no 2D',
                'legend': False,
                'isLegendExpanded': False,
                'metadataURL': u'http://example.com/wmsfeatures.metadata',
                'public': True,
            }, []))

        curdir = os.path.dirname(os.path.abspath(__file__))
        mapfile = os.path.join(curdir, 'c2cgeoportal_test.map')

        mapfile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               'c2cgeoportal_test.map')
        params = (
            ('map', mapfile),
            ('SERVICE', 'WMS'),
            ('VERSION', '1.1.1'),
            ('REQUEST', 'GetCapabilities'),
        )
        mapserv = "%s?map=%s&" % (mapserv_url, mapfile)
        url = mapserv + '&'.join(['='.join(p) for p in params])
        http = httplib2.Http()
        h = {'Host': host}
        resp, xml = http.request(url, method='GET', headers=h)

        wms = WebMapService(None, xml=xml)
        wms_layers = list(wms.contents)

        layer = Layer()
        layer.id = 20
        layer.name = 'test_wmsfeaturesgroup'
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.isChecked = False
        layer.legend = False
        layer.isLegendExpanded = False
        layer.public = True
        self.assertEqual(
            entry._layer(layer, wms_layers, wms, TimeInformation()), ({
                'id':
                20,
                'name':
                u'test_wmsfeaturesgroup',
                'type':
                u'internal WMS',
                'isChecked':
                False,
                'legend':
                False,
                'isLegendExpanded':
                False,
                'imageType':
                u'image/png',
                'minResolutionHint':
                1.76,
                'maxResolutionHint':
                8.8200000000000003,
                'public':
                True,
                'queryable':
                0,
                'metadataUrls': [{
                    'url': 'http://example.com/wmsfeatures.metadata',
                    'type': 'TC211',
                    'format': 'text/plain',
                }],
                'childLayers': [{
                    'name': u'test_wmsfeatures',
                    'minResolutionHint': 1.76,
                    'maxResolutionHint': 8.8200000000000003,
                    'queryable': 1,
                }],
            }, []))

        layer_t1 = Layer()
        layer_t1.id = 20
        layer_t1.name = 'test_wmstime'
        layer_t1.layerType = "internal WMS"
        layer_t1.imageType = "image/png"
        layer_t1.isChecked = False
        layer_t1.legend = False
        layer_t1.isLegendExpanded = False
        layer_t1.public = True
        layer_t1.timeMode = 'single'
        time = TimeInformation()
        entry._layer(layer_t1, wms_layers, wms, time)
        self.assertEqual(
            time.to_dict(), {
                'resolution': 'year',
                'interval': (1, 0, 0, 0),
                'maxValue': '2010-01-01T00:00:00Z',
                'minValue': '2000-01-01T00:00:00Z',
                'mode': 'single',
            })

        layer_t2 = Layer()
        layer_t2.id = 30
        layer_t2.name = 'test_wmstime2'
        layer_t2.layerType = "internal WMS"
        layer_t2.imageType = "image/png"
        layer_t2.isChecked = False
        layer_t2.legend = False
        layer_t2.isLegendExpanded = False
        layer_t2.public = True
        layer_t2.timeMode = 'single'
        time = TimeInformation()
        entry._layer(layer_t2, wms_layers, wms, time)
        self.assertEqual(
            time.to_dict(), {
                'resolution': 'year',
                'interval': (1, 0, 0, 0),
                'maxValue': '2020-01-01T00:00:00Z',
                'minValue': '2015-01-01T00:00:00Z',
                'mode': 'single',
            })

        group = LayerGroup()
        group.name = 'time'
        group.children = [layer_t1, layer_t2]
        time = TimeInformation()
        entry._group(group, [layer_t1, layer_t2], wms_layers, wms, time)
        self.assertEqual(
            time.to_dict(), {
                'resolution': 'year',
                'interval': (1, 0, 0, 0),
                'maxValue': '2020-01-01T00:00:00Z',
                'minValue': '2000-01-01T00:00:00Z',
                'mode': 'single',
            })

        layer = Layer()
        layer.id = 20
        layer.name = 'test_wmstimegroup'
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.isChecked = False
        layer.legend = False
        layer.isLegendExpanded = False
        layer.public = True
        layer.timeMode = 'single'
        time = TimeInformation()
        entry._layer(layer, wms_layers, wms, time)
        self.assertEqual(
            time.to_dict(), {
                'resolution': 'year',
                'interval': (1, 0, 0, 0),
                'maxValue': '2020-01-01T00:00:00Z',
                'minValue': '2000-01-01T00:00:00Z',
                'mode': 'single',
            })

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'test_wmsfeatures'
        layer.legend = False
        layer.isLegendExpanded = False
        layer.public = True
        self.assertEqual(
            entry._layer(layer, wms_layers, wms, TimeInformation()), ({
                'id':
                20,
                'name':
                'test WMTS',
                'isChecked':
                False,
                'type':
                'WMTS',
                'url':
                'http://example.com/WMTS-Capabilities.xml',
                'wmsUrl':
                '/dummy/route/mapserverproxy',
                'wmsLayers':
                'test_wmsfeatures',
                'queryLayers': [{
                    'name': 'test_wmsfeatures',
                    'minResolutionHint': 1.76,
                    'maxResolutionHint': 8.8200000000000003
                }],
                'legend':
                False,
                'isLegendExpanded':
                False,
                'public':
                True,
            }, []))

        layer = Layer()
        layer.id = 20
        layer.name = 'test WMTS'
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = 'foo'
        layer.queryLayers = 'test_wmsfeatures'
        layer.legend = False
        layer.isLegendExpanded = False
        layer.public = True
        self.assertEqual(
            entry._layer(layer, wms_layers, wms, TimeInformation()), ({
                'id':
                20,
                'name':
                'test WMTS',
                'isChecked':
                False,
                'type':
                'WMTS',
                'url':
                'http://example.com/WMTS-Capabilities.xml',
                'wmsUrl':
                '/dummy/route/mapserverproxy',
                'wmsLayers':
                'foo',
                'queryLayers': [{
                    'name': 'test_wmsfeatures',
                    'minResolutionHint': 1.76,
                    'maxResolutionHint': 8.8200000000000003
                }],
                'legend':
                False,
                'isLegendExpanded':
                False,
                'public':
                True,
            }, []))

        group1 = LayerGroup()
        group1.name = 'block'
        group2 = LayerGroup()
        group2.name = 'node'
        group2.metadataURL = 'http://example.com/group.metadata'
        layer = Layer()
        layer.id = 20
        layer.name = 'test layer in group'
        layer.isChecked = False
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.legend = False
        layer.isLegendExpanded = False
        layer.public = True
        group1.children = [group2]
        group2.children = [layer]
        self.assertEqual(
            entry._group(group1, [layer], [], None, TimeInformation()), ({
                'isExpanded':
                False,
                'isInternalWMS':
                True,
                'name':
                u'block',
                'isBaseLayer':
                False,
                'children': [{
                    'isExpanded':
                    False,
                    'isInternalWMS':
                    True,
                    'name':
                    u'node',
                    'isBaseLayer':
                    False,
                    'metadataURL':
                    'http://example.com/group.metadata',
                    'children': [{
                        'name': u'test layer in group',
                        'id': 20,
                        'isChecked': False,
                        'type': u'internal WMS',
                        'legend': False,
                        'isLegendExpanded': False,
                        'imageType': u'image/png',
                        'public': True,
                    }]
                }]
            }, [
                'The layer test layer in group is not defined in WMS capabilities'
            ], False))

        group1 = LayerGroup()
        group1.isInternalWMS = True
        group2 = LayerGroup()
        group2.isInternalWMS = False
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None, TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group1 = LayerGroup()
        group1.isInternalWMS = False
        group2 = LayerGroup()
        group2.isInternalWMS = True
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None, TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'internal WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertEqual(len(errors), 1)
        self.assertEqual(errors[0],
                         "The layer  is not defined in WMS capabilities")
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'external WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'WMTS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = 'no 2D'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'internal WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'external WMS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'WMTS'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = 'no 2D'
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None,
                                       TimeInformation())
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)
Ejemplo n.º 3
0
    def test_layer(self):
        import httplib2
        from c2cgeoportal.views.entry import Entry
        from c2cgeoportal.models import Layer, LayerGroup, Theme

        request = testing.DummyRequest()
        request.headers["Host"] = host
        request.static_url = lambda url: "/dummy/static/" + url
        request.route_url = lambda url: "/dummy/route/" + url
        request.registry.settings = {"project": "test_layer"}
        entry = Entry(request)

        self.assertEqual(entry._group(LayerGroup(), [], [], None), (None, [], False))

        layer = Layer()
        layer.id = 20
        layer.name = "test internal WMS"
        layer.metadataURL = "http://example.com/tiwms"
        layer.isChecked = True
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.style = "my-style"
        layer.kml = "tiwms.kml"
        layer.legend = True
        layer.legendRule = "rule"
        layer.legendImage = "legend:static/tiwms-legend.png"
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.disclaimer = "Camptocamp"
        layer.identifierAttributeField = "name"
        layer.geoTable = "tiwms"
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": "test internal WMS",
                    "metadataURL": "http://example.com/tiwms",
                    "isChecked": True,
                    "icon": "/dummy/route/mapserverproxy?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&LAYER=test internal WMS&FORMAT=image/png&TRANSPARENT=TRUE&RULE=rule",
                    "type": u"internal WMS",
                    "imageType": "image/png",
                    "style": "my-style",
                    "kml": "/dummy/static/test_layer:static/tiwms.kml",
                    "legend": True,
                    "legendImage": "/dummy/static/legend:static/tiwms-legend.png",
                    "minResolutionHint": 10,
                    "maxResolutionHint": 1000,
                    "disclaimer": "Camptocamp",
                    "identifierAttribute": "name",
                    "editable": True,
                    "public": True,
                },
                [],
            ),
        )

        layer = Layer()
        layer.id = 20
        layer.name = "test external WMS"
        layer.isChecked = False
        layer.icon = "tewms.png"
        layer.layerType = "external WMS"
        layer.url = "http://example.com"
        layer.imageType = "image/jpeg"
        layer.isSingleTile = True
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": "test external WMS",
                    "icon": "/dummy/static/test_layer:static/tewms.png",
                    "isChecked": False,
                    "type": u"external WMS",
                    "url": "http://example.com",
                    "imageType": "image/jpeg",
                    "isSingleTile": True,
                    "legend": False,
                    "minResolutionHint": 10,
                    "maxResolutionHint": 1000,
                    "public": True,
                },
                [],
            ),
        )

        layer = Layer()
        layer.id = 20
        layer.name = "test WMTS"
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.style = "wmts-style"
        layer.dimensions = '{"DATE": "1012"}'
        layer.matrixSet = "swissgrid"
        layer.wmsUrl = "http://example.com/"
        layer.wmsLayers = "test"
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": "test WMTS",
                    "isChecked": False,
                    "type": "WMTS",
                    "url": "http://example.com/WMTS-Capabilities.xml",
                    "style": "wmts-style",
                    "dimensions": {u"DATE": u"1012"},
                    "matrixSet": "swissgrid",
                    "wmsUrl": "http://example.com/",
                    "wmsLayers": "test",
                    "legend": False,
                    "minResolutionHint": 10,
                    "maxResolutionHint": 1000,
                    "public": True,
                },
                [],
            ),
        )

        layer = Layer()
        layer.id = 20
        layer.name = "test WMTS"
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsUrl = "http://example.com/"
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": "test WMTS",
                    "isChecked": False,
                    "type": "WMTS",
                    "url": "http://example.com/WMTS-Capabilities.xml",
                    "wmsUrl": "http://example.com/",
                    "wmsLayers": "test WMTS",
                    "legend": False,
                    "minResolutionHint": 10,
                    "maxResolutionHint": 1000,
                    "public": True,
                },
                [],
            ),
        )

        layer = Layer()
        layer.id = 20
        layer.name = "test WMTS"
        layer.isChecked = False
        layer.layerType = "WMTS"
        layer.url = "http://example.com/WMTS-Capabilities.xml"
        layer.wmsLayers = "test"
        layer.legend = False
        layer.minResolution = 10
        layer.maxResolution = 1000
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": "test WMTS",
                    "isChecked": False,
                    "type": "WMTS",
                    "url": "http://example.com/WMTS-Capabilities.xml",
                    "wmsUrl": "/dummy/route/mapserverproxy",
                    "wmsLayers": "test",
                    "legend": False,
                    "minResolutionHint": 10,
                    "maxResolutionHint": 1000,
                    "public": True,
                },
                [],
            ),
        )

        layer = Layer()
        layer.id = 20
        layer.name = "test no 2D"
        layer.isChecked = False
        layer.layerType = "no 2D"
        layer.legend = False
        layer.metadataURL = "http://example.com/wmsfeatures.metadata"
        layer.public = True
        self.assertEqual(
            entry._layer(layer, [], None),
            (
                {
                    "id": 20,
                    "name": u"test no 2D",
                    "isChecked": False,
                    "type": u"no 2D",
                    "legend": False,
                    "metadataURL": u"http://example.com/wmsfeatures.metadata",
                    "public": True,
                },
                [],
            ),
        )

        curdir = os.path.dirname(os.path.abspath(__file__))
        mapfile = os.path.join(curdir, "c2cgeoportal_test.map")

        mapfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), "c2cgeoportal_test.map")
        params = (("map", mapfile), ("SERVICE", "WMS"), ("VERSION", "1.1.1"), ("REQUEST", "GetCapabilities"))
        mapserv = "%s?map=%s&" % (mapserv_url, mapfile)
        url = mapserv + "&".join(["=".join(p) for p in params])
        http = httplib2.Http()
        h = {"Host": host}
        resp, xml = http.request(url, method="GET", headers=h)

        wms = WebMapService(None, xml=xml)
        wms_layers = list(wms.contents)
        layer = Layer()
        layer.id = 20
        layer.name = "test_wmsfeaturesgroup"
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.isChecked = False
        layer.legend = False
        layer.public = True
        self.assertEqual(
            entry._layer(layer, wms_layers, wms),
            (
                {
                    "id": 20,
                    "name": u"test_wmsfeaturesgroup",
                    "type": u"internal WMS",
                    "isChecked": False,
                    "legend": False,
                    "imageType": u"image/png",
                    "minResolutionHint": 1.76,
                    "maxResolutionHint": 8.8200000000000003,
                    "public": True,
                    "queryable": 0,
                    "metadataUrls": [
                        {"url": "http://example.com/wmsfeatures.metadata", "type": "TC211", "format": "text/plain"}
                    ],
                    "childLayers": [
                        {
                            "name": u"test_wmsfeatures",
                            "minResolutionHint": 1.76,
                            "maxResolutionHint": 8.8200000000000003,
                            "queryable": 1,
                        }
                    ],
                },
                [],
            ),
        )

        group1 = LayerGroup()
        group1.name = "block"
        group2 = LayerGroup()
        group2.name = "node"
        group2.metadataURL = "http://example.com/group.metadata"
        layer = Layer()
        layer.id = 20
        layer.name = "test layer in group"
        layer.isChecked = False
        layer.layerType = "internal WMS"
        layer.imageType = "image/png"
        layer.legend = False
        layer.public = True
        group1.children = [group2]
        group2.children = [layer]
        self.assertEqual(
            entry._group(group1, [layer], [], None),
            (
                {
                    "isExpanded": False,
                    "isInternalWMS": True,
                    "name": u"block",
                    "isBaseLayer": False,
                    "children": [
                        {
                            "isExpanded": False,
                            "isInternalWMS": True,
                            "name": u"node",
                            "isBaseLayer": False,
                            "metadataURL": "http://example.com/group.metadata",
                            "children": [
                                {
                                    "name": u"test layer in group",
                                    "id": 20,
                                    "isChecked": False,
                                    "type": u"internal WMS",
                                    "legend": False,
                                    "imageType": u"image/png",
                                    "public": True,
                                }
                            ],
                        }
                    ],
                },
                [],
                False,
            ),
        )

        group1 = LayerGroup()
        group1.isInternalWMS = True
        group2 = LayerGroup()
        group2.isInternalWMS = False
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group1 = LayerGroup()
        group1.isInternalWMS = False
        group2 = LayerGroup()
        group2.isInternalWMS = True
        group1.children = [group2]
        _, errors, stop = entry._group(group1, [], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = "internal WMS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = "external WMS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = "WMTS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = True
        layer = Layer()
        layer.layerType = "no 2D"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = "internal WMS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertTrue(len(errors) > 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = "external WMS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = "WMTS"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)

        group = LayerGroup()
        group.isInternalWMS = False
        layer = Layer()
        layer.layerType = "no 2D"
        group.children = [layer]
        _, errors, stop = entry._group(group, [layer], [], None)
        self.assertEqual(len(errors), 0)
        self.assertFalse(stop)