예제 #1
0
    def testLinks(self):
        l = QgsLayerMetadata.Link()
        l.name = 'Trashbat'
        l.type = 'fashion'
        l.description = 'registered in the cook islands!'
        l.url = 'http://trashbat.co.uk'
        l.format = 'whois'
        l.mimeType = 'text/string'
        l.size = '112'
        self.assertEqual(l.name, 'Trashbat')
        self.assertEqual(l.type, 'fashion')
        self.assertEqual(l.description, 'registered in the cook islands!')
        self.assertEqual(l.url, 'http://trashbat.co.uk')
        self.assertEqual(l.format, 'whois')
        self.assertEqual(l.mimeType, 'text/string')
        self.assertEqual(l.size, '112')

        m = QgsLayerMetadata()
        l2 = QgsLayerMetadata.Link(l)
        l2.name = 'Trashbat2'

        m.setLinks([l, l2])
        self.assertEqual(m.links()[0].name, 'Trashbat')
        self.assertEqual(m.links()[1].name, 'Trashbat2')

        # add link
        l3 = QgsLayerMetadata.Link(l)
        l3.name = 'Trashbat3'
        m.addLink(l3)
        self.assertEqual(len(m.links()), 3)
        self.assertEqual(m.links()[2].name, 'Trashbat3')
예제 #2
0
    def testSaveReadFromLayer(self):
        """
        Test saving and reading metadata from a layer
        """
        vl = QgsVectorLayer('Point', 'test', 'memory')
        self.assertTrue(vl.isValid())

        # save metadata to layer
        m = self.createTestMetadata()
        m.saveToLayer(vl)

        # read back from layer and check result
        m2 = QgsLayerMetadata()
        m2.readFromLayer(vl)
        self.checkExpectedMetadata(m2)
예제 #3
0
    def testSaveReadFromXml(self):
        """
        Test saving and reading metadata from a XML.
        """

        # save metadata to XML
        m = self.createTestMetadata()

        doc = QDomDocument("testdoc")
        elem = doc.createElement("metadata")
        self.assertTrue(m.writeMetadataXml(elem, doc))

        # read back from XML and check result
        m2 = QgsLayerMetadata()
        m2.readMetadataXml(elem)
        self.checkExpectedMetadata(m2)
예제 #4
0
    def testContact(self):
        c = QgsLayerMetadata.Contact()
        c.name = 'Prince Gristle'
        c.organization = 'Bergen co'
        c.position = 'prince'
        c.voice = '1500 515 555'
        c.fax = 'who the f*** still uses fax?'
        c.email = '*****@*****.**'
        c.role = 'person to blame when all goes wrong'
        a = QgsLayerMetadata.Address()
        a.type = 'postal'
        a2 = QgsLayerMetadata.Address()
        a2.type = 'street'
        c.addresses = [a, a2]
        self.assertEqual(c.name, 'Prince Gristle')
        self.assertEqual(c.organization, 'Bergen co')
        self.assertEqual(c.position, 'prince')
        self.assertEqual(c.voice, '1500 515 555')
        self.assertEqual(c.fax, 'who the f*** still uses fax?')
        self.assertEqual(c.email, '*****@*****.**')
        self.assertEqual(c.role, 'person to blame when all goes wrong')
        self.assertEqual(c.addresses[0].type, 'postal')
        self.assertEqual(c.addresses[1].type, 'street')

        m = QgsLayerMetadata()
        c2 = QgsLayerMetadata.Contact(c)
        c2.name = 'Bridgette'

        m.setContacts([c, c2])
        self.assertEqual(m.contacts()[0].name, 'Prince Gristle')
        self.assertEqual(m.contacts()[1].name, 'Bridgette')

        # add contact
        c3 = QgsLayerMetadata.Contact(c)
        c3.name = 'Princess Poppy'
        m.addContact(c3)
        self.assertEqual(len(m.contacts()), 3)
        self.assertEqual(m.contacts()[2].name, 'Princess Poppy')
예제 #5
0
    def testExtent(self):
        e = QgsLayerMetadata.Extent()
        se = QgsLayerMetadata.SpatialExtent()
        se.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        se.bounds = QgsBox3d(1, 2, 3, 4, 5, 6)
        e.setSpatialExtents([se])
        e.setTemporalExtents([QgsDateTimeRange(QDateTime(QDate(2017, 1, 3), QTime(11, 34, 56)), QDateTime(QDate(2018, 1, 3), QTime(12, 35, 57)))])

        m = QgsLayerMetadata()
        m.setExtent(e)

        extents = m.extent().spatialExtents()
        self.assertEqual(extents[0].extentCrs.authid(), 'EPSG:3111')
        self.assertEqual(extents[0].bounds.xMinimum(), 1.0)
        self.assertEqual(extents[0].bounds.yMinimum(), 2.0)
        self.assertEqual(extents[0].bounds.zMinimum(), 3.0)
        self.assertEqual(extents[0].bounds.xMaximum(), 4.0)
        self.assertEqual(extents[0].bounds.yMaximum(), 5.0)
        self.assertEqual(extents[0].bounds.zMaximum(), 6.0)
        self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2017, 1, 3), QTime(11, 34, 56)))
        self.assertEqual(m.extent().temporalExtents()[0].end(), QDateTime(QDate(2018, 1, 3), QTime(12, 35, 57)))
예제 #6
0
    def testKeywords(self):
        m = QgsLayerMetadata()

        m.setKeywords({'gmd:topicCategory': ['natural']})
        self.assertEqual(m.keywords(), {'gmd:topicCategory': ['natural']})
        self.assertEqual(m.categories(), ['natural'])
        self.assertTrue(m.removeKeywords('gmd:topicCategory'))

        m.setKeywords({'vocab a': ['keyword a', 'other a'],
                       'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywordVocabularies(), ['vocab a', 'vocab b'])
        self.assertEqual(m.keywords('vocab a'), ['keyword a', 'other a'])
        self.assertEqual(m.keywords('vocab b'), ['keyword b', 'other b'])
        self.assertEqual(m.keywords('not valid'), [])

        m.addKeywords('vocab c', ['keyword c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['keyword c']})
        # replace existing using addKeywords
        m.addKeywords('vocab c', ['c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['c']})
        # replace existing using setKeywords
        m.setKeywords({'x': ['x'], 'y': ['y']})
        self.assertEqual(m.keywords(), {'x': ['x'],
                                        'y': ['y']})
예제 #7
0
    def testGettersSetters(self):
        m = QgsLayerMetadata()

        m.setIdentifier('identifier')
        self.assertEqual(m.identifier(), 'identifier')

        m.setParentIdentifier('parent identifier')
        self.assertEqual(m.parentIdentifier(), 'parent identifier')

        m.setLanguage('en-us')
        self.assertEqual(m.language(), 'en-us')

        m.setType('type')
        self.assertEqual(m.type(), 'type')

        m.setTitle('title')
        self.assertEqual(m.title(), 'title')

        m.setCategories(['category'])
        self.assertEqual(m.categories(), ['category'])

        m.setAbstract('abstract')
        self.assertEqual(m.abstract(), 'abstract')

        m.setFees('fees')
        self.assertEqual(m.fees(), 'fees')

        m.setConstraints([
            QgsLayerMetadata.Constraint('constraint a'),
            QgsLayerMetadata.Constraint('constraint b')
        ])
        m.addConstraint(QgsLayerMetadata.Constraint('constraint c'))
        self.assertEqual(m.constraints()[0].constraint, 'constraint a')
        self.assertEqual(m.constraints()[1].constraint, 'constraint b')
        self.assertEqual(m.constraints()[2].constraint, 'constraint c')

        m.setRights(['right a', 'right b'])
        self.assertEqual(m.rights(), ['right a', 'right b'])

        m.setLicenses(['l a', 'l b'])
        self.assertEqual(m.licenses(), ['l a', 'l b'])

        m.setHistory(['loaded into QGIS'])
        self.assertEqual(m.history(), ['loaded into QGIS'])
        m.setHistory(['accidentally deleted some features'])
        self.assertEqual(m.history(), ['accidentally deleted some features'])
        m.addHistoryItem('panicked and deleted more')
        self.assertEqual(m.history(), [
            'accidentally deleted some features', 'panicked and deleted more'
        ])

        m.setEncoding('encoding')
        self.assertEqual(m.encoding(), 'encoding')

        m.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3111))
        self.assertEqual(m.crs().authid(), 'EPSG:3111')
예제 #8
0
    def testCombine(self):
        m1 = QgsLayerMetadata()
        m2 = QgsLayerMetadata()

        # should be retained
        m1.setIdentifier('i1')
        m1.combine(m2)
        self.assertEqual(m1.identifier(), 'i1')

        # should be overwritten
        m1.setIdentifier(None)
        m2.setIdentifier('i2')
        m1.combine(m2)
        self.assertEqual(m1.identifier(), 'i2')

        # should be overwritten
        m1.setIdentifier('i1')
        m2.setIdentifier('i2')
        m1.combine(m2)
        self.assertEqual(m1.identifier(), 'i2')

        m1.setParentIdentifier('pi1')
        m2.setParentIdentifier(None)
        m1.combine(m2)
        self.assertEqual(m1.parentIdentifier(), 'pi1')

        m1.setParentIdentifier(None)
        m2.setParentIdentifier('pi2')
        m1.combine(m2)
        self.assertEqual(m1.parentIdentifier(), 'pi2')

        m1.setLanguage('l1')
        m2.setLanguage(None)
        m1.combine(m2)
        self.assertEqual(m1.language(), 'l1')

        m1.setLanguage(None)
        m2.setLanguage('l2')
        m1.combine(m2)
        self.assertEqual(m1.language(), 'l2')

        m1.setType('ty1')
        m2.setType(None)
        m1.combine(m2)
        self.assertEqual(m1.type(), 'ty1')

        m1.setType(None)
        m2.setType('ty2')
        m1.combine(m2)
        self.assertEqual(m1.type(), 'ty2')

        m1.setTitle('t1')
        m2.setTitle(None)
        m1.combine(m2)
        self.assertEqual(m1.title(), 't1')

        m1.setTitle(None)
        m2.setTitle('t2')
        m1.combine(m2)
        self.assertEqual(m1.title(), 't2')

        m1.setAbstract('a1')
        m2.setAbstract(None)
        m1.combine(m2)
        self.assertEqual(m1.abstract(), 'a1')

        m1.setAbstract(None)
        m2.setAbstract('a2')
        m1.combine(m2)
        self.assertEqual(m1.abstract(), 'a2')

        m1.setHistory(['h1', 'hh1'])
        m2.setHistory([])
        m1.combine(m2)
        self.assertEqual(m1.history(), ['h1', 'hh1'])

        m1.setHistory([])
        m2.setHistory(['h2', 'hh2'])
        m1.combine(m2)
        self.assertEqual(m1.history(), ['h2', 'hh2'])

        m1.setKeywords({'words': ['k1', 'kk1']})
        m2.setKeywords({})
        m1.combine(m2)
        self.assertEqual(m1.keywords(), {'words': ['k1', 'kk1']})

        m1.setKeywords({})
        m2.setKeywords({'words': ['k2', 'kk2']})
        m1.combine(m2)
        self.assertEqual(m1.keywords(), {'words': ['k2', 'kk2']})

        m1.setContacts(
            [QgsLayerMetadata.Contact('c1'),
             QgsLayerMetadata.Contact('cc1')])
        m2.setContacts([])
        m1.combine(m2)
        self.assertEqual(
            m1.contacts(),
            [QgsLayerMetadata.Contact('c1'),
             QgsLayerMetadata.Contact('cc1')])

        m1.setContacts([])
        m2.setContacts(
            [QgsLayerMetadata.Contact('c2'),
             QgsLayerMetadata.Contact('cc2')])
        m1.combine(m2)
        self.assertEqual(
            m1.contacts(),
            [QgsLayerMetadata.Contact('c2'),
             QgsLayerMetadata.Contact('cc2')])

        m1.setLinks(
            [QgsLayerMetadata.Link('l1'),
             QgsLayerMetadata.Link('ll1')])
        m2.setLinks([])
        m1.combine(m2)
        self.assertEqual(
            m1.links(),
            [QgsLayerMetadata.Link('l1'),
             QgsLayerMetadata.Link('ll1')])

        m1.setLinks([])
        m2.setLinks(
            [QgsLayerMetadata.Link('l2'),
             QgsLayerMetadata.Link('ll2')])
        m1.combine(m2)
        self.assertEqual(
            m1.links(),
            [QgsLayerMetadata.Link('l2'),
             QgsLayerMetadata.Link('ll2')])

        m1.setFees('f1')
        m2.setFees(None)
        m1.combine(m2)
        self.assertEqual(m1.fees(), 'f1')

        m1.setFees(None)
        m2.setFees('f2')
        m1.combine(m2)
        self.assertEqual(m1.fees(), 'f2')

        m1.setConstraints([
            QgsLayerMetadata.Constraint('c1'),
            QgsLayerMetadata.Constraint('cc1')
        ])
        m2.setConstraints([])
        m1.combine(m2)
        self.assertEqual(m1.constraints(), [
            QgsLayerMetadata.Constraint('c1'),
            QgsLayerMetadata.Constraint('cc1')
        ])

        m1.setConstraints([])
        m2.setConstraints([
            QgsLayerMetadata.Constraint('c2'),
            QgsLayerMetadata.Constraint('cc2')
        ])
        m1.combine(m2)
        self.assertEqual(m1.constraints(), [
            QgsLayerMetadata.Constraint('c2'),
            QgsLayerMetadata.Constraint('cc2')
        ])

        m1.setRights(['r1', 'rr1'])
        m2.setRights([])
        m1.combine(m2)
        self.assertEqual(m1.rights(), ['r1', 'rr1'])

        m1.setRights([])
        m2.setRights(['r2', 'rr2'])
        m1.combine(m2)
        self.assertEqual(m1.rights(), ['r2', 'rr2'])

        m1.setLicenses(['li1', 'lli1'])
        m2.setLicenses([])
        m1.combine(m2)
        self.assertEqual(m1.licenses(), ['li1', 'lli1'])

        m1.setLicenses([])
        m2.setLicenses(['li2', 'lli2'])
        m1.combine(m2)
        self.assertEqual(m1.licenses(), ['li2', 'lli2'])

        m1.setEncoding('e1')
        m2.setEncoding(None)
        m1.combine(m2)
        self.assertEqual(m1.encoding(), 'e1')

        m1.setEncoding(None)
        m2.setEncoding('e2')
        m1.combine(m2)
        self.assertEqual(m1.encoding(), 'e2')

        m1.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
        m2.setCrs(QgsCoordinateReferenceSystem())
        m1.combine(m2)
        self.assertEqual(m1.crs().authid(), 'EPSG:3111')

        m1.setCrs(QgsCoordinateReferenceSystem())
        m2.setCrs(QgsCoordinateReferenceSystem('EPSG:3113'))
        m1.combine(m2)
        self.assertEqual(m1.crs().authid(), 'EPSG:3113')

        s = QgsLayerMetadata.SpatialExtent()
        s.bounds = QgsBox3d(1, 2, 3, 4, 5, 6)
        m1.extent().setSpatialExtents([s])
        m2.extent().setSpatialExtents([])
        m1.combine(m2)
        self.assertEqual(m1.extent().spatialExtents()[0].bounds,
                         QgsBox3d(1, 2, 3, 4, 5, 6))

        s.bounds = QgsBox3d(11, 12, 13, 14, 15, 16)
        m1.extent().setSpatialExtents([])
        m2.extent().setSpatialExtents([s])
        m1.combine(m2)
        self.assertEqual(m1.extent().spatialExtents()[0].bounds,
                         QgsBox3d(11, 12, 13, 14, 15, 16))

        s = QgsDateTimeRange(QDateTime(2020, 1, 1, 0, 0, 0),
                             QDateTime(2020, 2, 1, 0, 0, 0))
        m1.extent().setTemporalExtents([s])
        m2.extent().setTemporalExtents([])
        m1.combine(m2)
        self.assertEqual(m1.extent().temporalExtents()[0], s)

        s = QgsDateTimeRange(QDateTime(2021, 1, 1, 0, 0, 0),
                             QDateTime(2021, 2, 1, 0, 0, 0))
        m1.extent().setTemporalExtents([])
        m2.extent().setTemporalExtents([s])
        m1.combine(m2)
        self.assertEqual(m1.extent().temporalExtents()[0], s)
예제 #9
0
    def testGettersSetters(self):
        m = QgsLayerMetadata()

        m.setIdentifier('identifier')
        self.assertEqual(m.identifier(), 'identifier')

        m.setParentIdentifier('parent identifier')
        self.assertEqual(m.parentIdentifier(), 'parent identifier')

        m.setLanguage('en-us')
        self.assertEqual(m.language(), 'en-us')

        m.setType('type')
        self.assertEqual(m.type(), 'type')

        m.setTitle('title')
        self.assertEqual(m.title(), 'title')

        m.setCategories(['category'])
        self.assertEqual(m.categories(), ['category'])

        m.setAbstract('abstract')
        self.assertEqual(m.abstract(), 'abstract')

        m.setFees('fees')
        self.assertEqual(m.fees(), 'fees')

        m.setConstraints([QgsLayerMetadata.Constraint('constraint a'), QgsLayerMetadata.Constraint('constraint b')])
        m.addConstraint(QgsLayerMetadata.Constraint('constraint c'))
        self.assertEqual(m.constraints()[0].constraint, 'constraint a')
        self.assertEqual(m.constraints()[1].constraint, 'constraint b')
        self.assertEqual(m.constraints()[2].constraint, 'constraint c')

        m.setRights(['right a', 'right b'])
        self.assertEqual(m.rights(), ['right a', 'right b'])

        m.setLicenses(['l a', 'l b'])
        self.assertEqual(m.licenses(), ['l a', 'l b'])

        m.setHistory(['loaded into QGIS'])
        self.assertEqual(m.history(), ['loaded into QGIS'])
        m.setHistory(['accidentally deleted some features'])
        self.assertEqual(m.history(), ['accidentally deleted some features'])
        m.addHistoryItem('panicked and deleted more')
        self.assertEqual(m.history(), ['accidentally deleted some features', 'panicked and deleted more'])

        m.setEncoding('encoding')
        self.assertEqual(m.encoding(), 'encoding')

        m.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3111))
        self.assertEqual(m.crs().authid(), 'EPSG:3111')
예제 #10
0
    def testMetadata(self):
        """ Test that metadata is correctly acquired from provider """

        endpoint = self.basetestpath + '/metadata_fake_qgis_http_endpoint'
        with open(sanitize(endpoint, '?f=json'), 'wb') as f:
            f.write("""
        {"currentVersion":10.22,"id":1,"name":"QGIS Test","type":"Feature Layer","description":
        "QGIS Provider Test Layer","geometryType":"esriGeometryPoint","copyrightText":"not copyright","parentLayer":{"id":2,"name":"QGIS Tests"},"subLayers":[],
        "minScale":72225,"maxScale":0,
        "defaultVisibility":true,
        "extent":{"xmin":-71.123,"ymin":66.33,"xmax":-65.32,"ymax":78.3,
        "spatialReference":{"wkid":4326,"latestWkid":4326}},
        "hasAttachments":false,"htmlPopupType":"esriServerHTMLPopupTypeAsHTMLText",
        "displayField":"LABEL","typeIdField":null,
        "fields":[{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID","domain":null}],
        "relationships":[],"canModifyLayer":false,"canScaleSymbols":false,"hasLabels":false,
        "capabilities":"Map,Query,Data","maxRecordCount":1000,"supportsStatistics":true,
        "supportsAdvancedQueries":true,"supportedQueryFormats":"JSON, AMF",
        "ownershipBasedAccessControlForFeatures":{"allowOthersToQuery":true},"useStandardizedQueries":true}"""
                    .encode('UTF-8'))

        with open(
                sanitize(
                    endpoint,
                    '/query?f=json_where=OBJECTID=OBJECTID_returnIdsOnly=true'
                ), 'wb') as f:
            f.write("""
        {
         "objectIdFieldName": "OBJECTID",
         "objectIds": [
          1
         ]
        }
        """.encode('UTF-8'))

        # Create test layer
        vl = QgsVectorLayer("url='http://" + endpoint + "' crs='epsg:4326'",
                            'test', 'arcgisfeatureserver')
        self.assertTrue(vl.isValid())

        extent = QgsLayerMetadata.Extent()
        extent1 = QgsLayerMetadata.SpatialExtent()
        extent1.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(4326)
        extent1.bounds = QgsBox3d(QgsRectangle(-71.123, 66.33, -65.32, 78.3))
        extent.setSpatialExtents([extent1])
        self.assertEqual(vl.metadata().extent(), extent)

        self.assertEqual(vl.metadata().crs(),
                         QgsCoordinateReferenceSystem.fromEpsgId(4326))
        self.assertEqual(vl.metadata().identifier(),
                         'http://' + sanitize(endpoint, ''))
        self.assertEqual(vl.metadata().parentIdentifier(),
                         'http://' + self.basetestpath + '/2')
        self.assertEqual(vl.metadata().type(), 'dataset')
        self.assertEqual(vl.metadata().abstract(), 'QGIS Provider Test Layer')
        self.assertEqual(vl.metadata().title(), 'QGIS Test')
        self.assertEqual(vl.metadata().rights(), ['not copyright'])
        l = QgsLayerMetadata.Link()
        l.name = 'Source'
        l.type = 'WWW:LINK'
        l.url = 'http://' + sanitize(endpoint, '')
        self.assertEqual(vl.metadata().links(), [l])
예제 #11
0
    def testLinks(self):
        l = QgsLayerMetadata.Link()
        l.name = 'Trashbat'
        l.type = 'fashion'
        l.description = 'registered in the cook islands!'
        l.url = 'http://trashbat.co.uk'
        l.format = 'whois'
        l.mimeType = 'text/string'
        l.size = '112'
        self.assertEqual(l.name, 'Trashbat')
        self.assertEqual(l.type, 'fashion')
        self.assertEqual(l.description, 'registered in the cook islands!')
        self.assertEqual(l.url, 'http://trashbat.co.uk')
        self.assertEqual(l.format, 'whois')
        self.assertEqual(l.mimeType, 'text/string')
        self.assertEqual(l.size, '112')

        m = QgsLayerMetadata()
        l2 = QgsLayerMetadata.Link(l)
        l2.name = 'Trashbat2'

        m.setLinks([l, l2])
        self.assertEqual(m.links()[0].name, 'Trashbat')
        self.assertEqual(m.links()[1].name, 'Trashbat2')

        # add link
        l3 = QgsLayerMetadata.Link(l)
        l3.name = 'Trashbat3'
        m.addLink(l3)
        self.assertEqual(len(m.links()), 3)
        self.assertEqual(m.links()[2].name, 'Trashbat3')
예제 #12
0
    def testContact(self):
        c = QgsLayerMetadata.Contact()
        c.name = 'Prince Gristle'
        c.organization = 'Bergen co'
        c.position = 'prince'
        c.voice = '1500 515 555'
        c.fax = 'who the f*** still uses fax?'
        c.email = '*****@*****.**'
        c.role = 'person to blame when all goes wrong'
        a = QgsLayerMetadata.Address()
        a.type = 'postal'
        a2 = QgsLayerMetadata.Address()
        a2.type = 'street'
        c.addresses = [a, a2]
        self.assertEqual(c.name, 'Prince Gristle')
        self.assertEqual(c.organization, 'Bergen co')
        self.assertEqual(c.position, 'prince')
        self.assertEqual(c.voice, '1500 515 555')
        self.assertEqual(c.fax, 'who the f*** still uses fax?')
        self.assertEqual(c.email, '*****@*****.**')
        self.assertEqual(c.role, 'person to blame when all goes wrong')
        self.assertEqual(c.addresses[0].type, 'postal')
        self.assertEqual(c.addresses[1].type, 'street')

        m = QgsLayerMetadata()
        c2 = QgsLayerMetadata.Contact(c)
        c2.name = 'Bridgette'

        m.setContacts([c, c2])
        self.assertEqual(m.contacts()[0].name, 'Prince Gristle')
        self.assertEqual(m.contacts()[1].name, 'Bridgette')

        # add contact
        c3 = QgsLayerMetadata.Contact(c)
        c3.name = 'Princess Poppy'
        m.addContact(c3)
        self.assertEqual(len(m.contacts()), 3)
        self.assertEqual(m.contacts()[2].name, 'Princess Poppy')
예제 #13
0
    def testKeywords(self):
        m = QgsLayerMetadata()

        m.setKeywords({'gmd:topicCategory': ['natural']})
        self.assertEqual(m.keywords(), {'gmd:topicCategory': ['natural']})
        self.assertEqual(m.categories(), ['natural'])
        self.assertTrue(m.removeKeywords('gmd:topicCategory'))

        m.setKeywords({'vocab a': ['keyword a', 'other a'],
                       'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywordVocabularies(), ['vocab a', 'vocab b'])
        self.assertEqual(m.keywords('vocab a'), ['keyword a', 'other a'])
        self.assertEqual(m.keywords('vocab b'), ['keyword b', 'other b'])
        self.assertEqual(m.keywords('not valid'), [])

        m.addKeywords('vocab c', ['keyword c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['keyword c']})
        # replace existing using addKeywords
        m.addKeywords('vocab c', ['c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['c']})
        # replace existing using setKeywords
        m.setKeywords({'x': ['x'], 'y': ['y']})
        self.assertEqual(m.keywords(), {'x': ['x'],
                                        'y': ['y']})
예제 #14
0
    def testKeywords(self):
        m = QgsLayerMetadata()

        m.setKeywords({
            'vocab a': ['keyword a', 'other a'],
            'vocab b': ['keyword b', 'other b']
        })
        self.assertEqual(
            m.keywords(), {
                'vocab a': ['keyword a', 'other a'],
                'vocab b': ['keyword b', 'other b']
            })
        self.assertEqual(m.keywordVocabularies(), ['vocab a', 'vocab b'])
        self.assertEqual(m.keywords('vocab a'), ['keyword a', 'other a'])
        self.assertEqual(m.keywords('vocab b'), ['keyword b', 'other b'])
        self.assertEqual(m.keywords('not valid'), [])

        m.addKeywords('vocab c', ['keyword c'])
        self.assertEqual(
            m.keywords(), {
                'vocab a': ['keyword a', 'other a'],
                'vocab b': ['keyword b', 'other b'],
                'vocab c': ['keyword c']
            })
        # replace existing using addKeywords
        m.addKeywords('vocab c', ['c'])
        self.assertEqual(
            m.keywords(), {
                'vocab a': ['keyword a', 'other a'],
                'vocab b': ['keyword b', 'other b'],
                'vocab c': ['c']
            })
        # replace existing using setKeywords
        m.setKeywords({'x': ['x'], 'y': ['y']})
        self.assertEqual(m.keywords(), {'x': ['x'], 'y': ['y']})
예제 #15
0
    def testEquality(self):
        # spatial extent
        extent = QgsLayerMetadata.SpatialExtent()
        extent.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        extent2 = QgsLayerMetadata.SpatialExtent()
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        self.assertEqual(extent, extent2)
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        self.assertNotEqual(extent, extent2)
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 16.0)
        self.assertNotEqual(extent, extent2)

        # extent
        extent = QgsLayerMetadata.Extent()
        extent1 = QgsLayerMetadata.SpatialExtent()
        extent1.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent1.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        extent2 = QgsLayerMetadata.SpatialExtent()
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 16.0)
        extent.setSpatialExtents([extent1, extent2])
        dates = [
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(
                QDateTime(QDate(2010, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2020, 12, 17), QTime(9, 30, 47)))
        ]
        extent.setTemporalExtents(dates)
        extent_copy = QgsLayerMetadata.Extent(extent)
        self.assertEqual(extent, extent_copy)
        extent_copy.setTemporalExtents([
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(
                QDateTime(QDate(2010, 12, 17), QTime(9, 30, 48)),
                QDateTime(QDate(2020, 12, 17), QTime(9, 30, 49)))
        ])
        self.assertNotEqual(extent, extent_copy)
        extent_copy = QgsLayerMetadata.Extent(extent)
        extent3 = QgsLayerMetadata.SpatialExtent()
        extent3.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        extent3.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 19.0)
        extent_copy.setSpatialExtents([extent1, extent3])
        self.assertNotEqual(extent, extent_copy)

        constraint = QgsLayerMetadata.Constraint('c', 'type1')
        self.assertEqual(constraint, QgsLayerMetadata.Constraint('c', 'type1'))
        self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c2', 'type1'))
        self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c', 'type2'))

        a = QgsLayerMetadata.Address()
        a.type = 'postal'
        a.address = '13 north rd'
        a.city = 'huxleys haven'
        a.administrativeArea = 'land of the queens'
        a.postalCode = '4123'
        a.country = 'straya!'
        a2 = QgsLayerMetadata.Address(a)
        self.assertEqual(a, a2)
        a2.type = 'postal2'
        self.assertNotEqual(a, a2)
        a2 = QgsLayerMetadata.Address(a)
        a2.address = 'address2'
        self.assertNotEqual(a, a2)
        a2 = QgsLayerMetadata.Address(a)
        a2.city = 'city'
        self.assertNotEqual(a, a2)
        a2 = QgsLayerMetadata.Address(a)
        a2.administrativeArea = 'area2'
        self.assertNotEqual(a, a2)
        a2 = QgsLayerMetadata.Address(a)
        a2.postalCode = 'postal2'
        self.assertNotEqual(a, a2)
        a2 = QgsLayerMetadata.Address(a)
        a2.country = 'country2'
        self.assertNotEqual(a, a2)

        c = QgsLayerMetadata.Contact()
        c.name = 'name'
        c.organization = 'org'
        c.position = 'pos'
        c.voice = '1500 515 555'
        c.fax = 'fax'
        c.email = 'email'
        c.role = 'role'
        a = QgsLayerMetadata.Address()
        a.type = 'postal'
        a2 = QgsLayerMetadata.Address()
        a2.type = 'street'
        c.addresses = [a, a2]
        c2 = QgsLayerMetadata.Contact(c)
        self.assertEqual(c, c2)
        c2.name = 'name2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.organization = 'org2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.position = 'pos2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.voice = 'voice2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.fax = 'fax2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.email = 'email2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.role = 'role2'
        self.assertNotEqual(c, c2)
        c2 = QgsLayerMetadata.Contact(c)
        c2.addresses = [a2]
        self.assertNotEqual(c, c2)

        # link
        l = QgsLayerMetadata.Link()
        l.name = 'name'
        l.type = 'type'
        l.description = 'desc'
        l.url = 'url'
        l.format = 'format'
        l.mimeType = 'mime'
        l.size = '112'
        l2 = QgsLayerMetadata.Link(l)
        self.assertEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.name = 'name2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.type = 'type2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.description = 'desc2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.url = 'url2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.format = 'format2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.mimeType = 'mime2'
        self.assertNotEqual(l, l2)
        l2 = QgsLayerMetadata.Link(l)
        l2.size = '113'
        self.assertNotEqual(l, l2)
예제 #16
0
    def testKeywords(self):
        m = QgsLayerMetadata()

        m.setKeywords({'vocab a': ['keyword a', 'other a'],
                       'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b']})
        self.assertEqual(m.keywordVocabularies(), ['vocab a', 'vocab b'])
        self.assertEqual(m.keywords('vocab a'), ['keyword a', 'other a'])
        self.assertEqual(m.keywords('vocab b'), ['keyword b', 'other b'])
        self.assertEqual(m.keywords('not valid'), [])

        m.addKeywords('vocab c', ['keyword c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['keyword c']})
        # replace existing using addKeywords
        m.addKeywords('vocab c', ['c'])
        self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
                                        'vocab b': ['keyword b', 'other b'],
                                        'vocab c': ['c']})
        # replace existing using setKeywords
        m.setKeywords({'x': ['x'], 'y': ['y']})
        self.assertEqual(m.keywords(), {'x': ['x'],
                                        'y': ['y']})
예제 #17
0
    def createTestMetadata(self):
        """
        Returns a standard metadata which can be tested with checkExpectedMetadata
        """
        m = QgsLayerMetadata()
        m.setIdentifier('1234')
        m.setParentIdentifier('xyz')
        m.setLanguage('en-CA')
        m.setType('dataset')
        m.setTitle('roads')
        m.setAbstract('my roads')
        m.setFees('None')
        m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
        m.setRights(['Copyright foo 2017'])
        m.setLicenses(['WTFPL'])
        m.setHistory(['history a', 'history b'])
        m.setKeywords({
            'GEMET': ['kw1', 'kw2'],
            'gmd:topicCategory': ['natural'],
        })
        m.setEncoding('utf-8')
        m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))

        e = QgsLayerMetadata.Extent()
        se = QgsLayerMetadata.SpatialExtent()
        se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')
        se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0)
        e.setSpatialExtents([se])
        dates = [
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(
                QDateTime(QDate(2010, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2020, 12, 17), QTime(9, 30, 47)))
        ]
        e.setTemporalExtents(dates)
        m.setExtent(e)

        c = QgsLayerMetadata.Contact()
        c.name = 'John Smith'
        c.organization = 'ACME'
        c.position = 'staff'
        c.voice = '1500 515 555'
        c.fax = 'xx.xxx.xxx.xxxx'
        c.email = '*****@*****.**'
        c.role = 'pointOfContact'
        address = QgsLayerMetadata.Address()
        address.type = 'postal'
        address.address = '123 Main Street'
        address.city = 'anycity'
        address.administrativeArea = 'anyprovince'
        address.postalCode = '90210'
        address.country = 'Canada'
        c.addresses = [address]
        m.setContacts([c])

        l = QgsLayerMetadata.Link()
        l.name = 'geonode:roads'
        l.type = 'OGC:WMS'
        l.description = 'my GeoNode road layer'
        l.url = 'http://example.org/wms'

        l2 = QgsLayerMetadata.Link()
        l2.name = 'geonode:roads'
        l2.type = 'OGC:WFS'
        l2.description = 'my GeoNode road layer'
        l2.url = 'http://example.org/wfs'

        l3 = QgsLayerMetadata.Link()
        l3.name = 'roads'
        l3.type = 'WWW:LINK'
        l3.description = 'full dataset download'
        l3.url = 'http://example.org/roads.tgz'
        l3.format = 'ESRI Shapefile'
        l3.mimeType = 'application/gzip'
        l3.size = '283676'

        m.setLinks([l, l2, l3])

        return m
예제 #18
0
    def testLayerMode(self):
        """
        Create a fully populated QgsLayerMetadata object, then set it to the widget and re-read back
        the generated metadata to ensure that no content is lost.
        """
        w = QgsMetadataWidget()

        m = QgsLayerMetadata()
        m.setIdentifier('1234')
        m.setParentIdentifier('xyz')
        m.setLanguage('en-CA')
        m.setType('dataset')
        m.setTitle('roads')
        m.setAbstract('my roads')
        m.setFees('None')
        m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
        m.setRights(['Copyright foo 2017'])
        m.setLicenses(['WTFPL'])
        m.setHistory(['history a', 'history b'])
        m.setKeywords({
            'GEMET': ['kw1', 'kw2'],
            'gmd:topicCategory': ['natural'],
        })
        #m.setEncoding('utf-8')
        m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))

        e = QgsLayerMetadata.Extent()
        se = QgsLayerMetadata.SpatialExtent()
        se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')
        se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0)
        e.setSpatialExtents([se])
        dates = [
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)))
        ]
        e.setTemporalExtents(dates)
        m.setExtent(e)

        c = QgsLayerMetadata.Contact()
        c.name = 'John Smith'
        c.organization = 'ACME'
        c.position = 'staff'
        c.voice = '1500 515 555'
        c.fax = 'xx.xxx.xxx.xxxx'
        c.email = '*****@*****.**'
        c.role = 'pointOfContact'
        address = QgsLayerMetadata.Address()
        address.type = 'postal'
        address.address = '123 Main Street'
        address.city = 'anycity'
        address.administrativeArea = 'anyprovince'
        address.postalCode = '90210'
        address.country = 'Canada'
        c.addresses = [address]
        m.setContacts([c])

        l = QgsLayerMetadata.Link()
        l.name = 'geonode:roads'
        l.type = 'OGC:WMS'
        l.description = 'my GeoNode road layer'
        l.url = 'http://example.org/wms'

        l2 = QgsLayerMetadata.Link()
        l2.name = 'geonode:roads'
        l2.type = 'OGC:WFS'
        l2.description = 'my GeoNode road layer'
        l2.url = 'http://example.org/wfs'

        l3 = QgsLayerMetadata.Link()
        l3.name = 'roads'
        l3.type = 'WWW:LINK'
        l3.description = 'full dataset download'
        l3.url = 'http://example.org/roads.tgz'
        l3.format = 'ESRI Shapefile'
        l3.mimeType = 'application/gzip'
        l3.size = '283676'

        m.setLinks([l, l2, l3])

        # set widget metadata
        w.setMetadata(m)
        self.assertEqual(w.mode(), QgsMetadataWidget.LayerMetadata)

        m = w.metadata()
        self.assertIsInstance(m, QgsLayerMetadata)

        self.assertEqual(m.identifier(), '1234')
        self.assertEqual(m.parentIdentifier(), 'xyz')
        self.assertEqual(m.language(), 'en-CA')
        self.assertEqual(m.type(), 'dataset')
        self.assertEqual(m.title(), 'roads')
        self.assertEqual(m.abstract(), 'my roads')
        self.assertEqual(m.fees(), 'None')
        self.assertEqual(m.constraints()[0].constraint, 'None')
        self.assertEqual(m.constraints()[0].type, 'access')
        self.assertEqual(m.rights(), ['Copyright foo 2017'])
        self.assertEqual(m.licenses(), ['WTFPL'])
        self.assertEqual(m.history(), ['history a', 'history b'])
        #self.assertEqual(m.encoding(), 'utf-8')
        self.assertEqual(
            m.keywords(),
            {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']})
        self.assertEqual(m.crs().authid(), 'EPSG:4326')

        extent = m.extent().spatialExtents()[0]
        self.assertEqual(extent.extentCrs.authid(), 'EPSG:4326')
        self.assertEqual(extent.bounds.xMinimum(), -180.0)
        self.assertEqual(extent.bounds.yMinimum(), -90.0)
        self.assertEqual(extent.bounds.xMaximum(), 180.0)
        self.assertEqual(extent.bounds.yMaximum(), 90.0)
        self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)))
        self.assertTrue(m.extent().temporalExtents()[0].isInstant())

        self.assertEqual(m.contacts()[0].name, 'John Smith')
        self.assertEqual(m.contacts()[0].organization, 'ACME')
        self.assertEqual(m.contacts()[0].position, 'staff')
        self.assertEqual(m.contacts()[0].voice, '1500 515 555')
        self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx')
        self.assertEqual(m.contacts()[0].email, '*****@*****.**')
        self.assertEqual(m.contacts()[0].role, 'pointOfContact')
        self.assertEqual(m.contacts()[0].addresses[0].type, 'postal')
        self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street')
        self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity')
        self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince')
        self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210')
        self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada')
        self.assertEqual(m.links()[0].name, 'geonode:roads')
        self.assertEqual(m.links()[0].type, 'OGC:WMS')
        self.assertEqual(m.links()[0].description, 'my GeoNode road layer')
        self.assertEqual(m.links()[0].url, 'http://example.org/wms')
        self.assertEqual(m.links()[1].name, 'geonode:roads')
        self.assertEqual(m.links()[1].type, 'OGC:WFS')
        self.assertEqual(m.links()[1].description, 'my GeoNode road layer')
        self.assertEqual(m.links()[1].url, 'http://example.org/wfs')
        self.assertEqual(m.links()[2].name, 'roads')
        self.assertEqual(m.links()[2].type, 'WWW:LINK')
        self.assertEqual(m.links()[2].description, 'full dataset download')
        self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz')
        self.assertEqual(m.links()[2].format, 'ESRI Shapefile')
        self.assertEqual(m.links()[2].mimeType, 'application/gzip')
        self.assertEqual(m.links()[2].size, '283676')
예제 #19
0
    def testEquality(self):
        # spatial extent
        extent = QgsLayerMetadata.SpatialExtent()
        extent.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        extent2 = QgsLayerMetadata.SpatialExtent()
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        self.assertEqual(extent, extent2)
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        self.assertNotEqual(extent, extent2)
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 16.0)
        self.assertNotEqual(extent, extent2)

        # extent
        extent = QgsLayerMetadata.Extent()
        extent1 = QgsLayerMetadata.SpatialExtent()
        extent1.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
        extent1.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 15.0)
        extent2 = QgsLayerMetadata.SpatialExtent()
        extent2.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        extent2.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 16.0)
        extent.setSpatialExtents([extent1, extent2])
        dates = [
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(
                QDateTime(QDate(2010, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2020, 12, 17), QTime(9, 30, 47)))
        ]
        extent.setTemporalExtents(dates)
        extent_copy = QgsLayerMetadata.Extent(extent)
        self.assertEqual(extent, extent_copy)
        extent_copy.setTemporalExtents([
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(
                QDateTime(QDate(2010, 12, 17), QTime(9, 30, 48)),
                QDateTime(QDate(2020, 12, 17), QTime(9, 30, 49)))
        ])
        self.assertNotEqual(extent, extent_copy)
        extent_copy = QgsLayerMetadata.Extent(extent)
        extent3 = QgsLayerMetadata.SpatialExtent()
        extent3.extentCrs = QgsCoordinateReferenceSystem.fromEpsgId(3113)
        extent3.bounds = QgsBox3d(5.0, 6.0, 7.0, 11.0, 13.0, 19.0)
        extent_copy.setSpatialExtents([extent1, extent3])
        self.assertNotEqual(extent, extent_copy)

        constraint = QgsLayerMetadata.Constraint('c', 'type1')
        self.assertEqual(constraint, QgsLayerMetadata.Constraint('c', 'type1'))
        self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c2', 'type1'))
        self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c', 'type2'))
예제 #20
0
    def createTestMetadata(self):
        """
        Returns a standard metadata which can be tested with checkExpectedMetadata
        """
        m = QgsLayerMetadata()
        m.setIdentifier('1234')
        m.setParentIdentifier('xyz')
        m.setLanguage('en-CA')
        m.setType('dataset')
        m.setTitle('roads')
        m.setAbstract('my roads')
        m.setFees('None')
        m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
        m.setRights(['Copyright foo 2017'])
        m.setLicenses(['WTFPL'])
        m.setHistory(['history a', 'history b'])
        m.setKeywords({
            'GEMET': ['kw1', 'kw2'],
            'gmd:topicCategory': ['natural'],
        })
        m.setEncoding('utf-8')
        m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))

        e = QgsLayerMetadata.Extent()
        se = QgsLayerMetadata.SpatialExtent()
        se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')
        se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0)
        e.setSpatialExtents([se])
        dates = [
            QgsDateTimeRange(QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                             QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))),
            QgsDateTimeRange(QDateTime(QDate(2010, 12, 17), QTime(9, 30, 47)),
                             QDateTime(QDate(2020, 12, 17), QTime(9, 30, 47)))
        ]
        e.setTemporalExtents(dates)
        m.setExtent(e)

        c = QgsLayerMetadata.Contact()
        c.name = 'John Smith'
        c.organization = 'ACME'
        c.position = 'staff'
        c.voice = '1500 515 555'
        c.fax = 'xx.xxx.xxx.xxxx'
        c.email = '*****@*****.**'
        c.role = 'pointOfContact'
        address = QgsLayerMetadata.Address()
        address.type = 'postal'
        address.address = '123 Main Street'
        address.city = 'anycity'
        address.administrativeArea = 'anyprovince'
        address.postalCode = '90210'
        address.country = 'Canada'
        c.addresses = [address]
        m.setContacts([c])

        l = QgsLayerMetadata.Link()
        l.name = 'geonode:roads'
        l.type = 'OGC:WMS'
        l.description = 'my GeoNode road layer'
        l.url = 'http://example.org/wms'

        l2 = QgsLayerMetadata.Link()
        l2.name = 'geonode:roads'
        l2.type = 'OGC:WFS'
        l2.description = 'my GeoNode road layer'
        l2.url = 'http://example.org/wfs'

        l3 = QgsLayerMetadata.Link()
        l3.name = 'roads'
        l3.type = 'WWW:LINK'
        l3.description = 'full dataset download'
        l3.url = 'http://example.org/roads.tgz'
        l3.format = 'ESRI Shapefile'
        l3.mimeType = 'application/gzip'
        l3.size = '283676'

        m.setLinks([l, l2, l3])

        return m
예제 #21
0
    def testLayerMode(self):
        """
        Create a fully populated QgsLayerMetadata object, then set it to the widget and re-read back
        the generated metadata to ensure that no content is lost.
        """
        w = QgsMetadataWidget()

        m = QgsLayerMetadata()
        m.setIdentifier('1234')
        m.setParentIdentifier('xyz')
        m.setLanguage('en-CA')
        m.setType('dataset')
        m.setTitle('roads')
        m.setAbstract('my roads')
        m.setFees('None')
        m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
        m.setRights(['Copyright foo 2017'])
        m.setLicenses(['WTFPL'])
        m.setHistory(['history a', 'history b'])
        m.setKeywords({
            'GEMET': ['kw1', 'kw2'],
            'gmd:topicCategory': ['natural'],
        })
        #m.setEncoding('utf-8')
        m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))

        e = QgsLayerMetadata.Extent()
        se = QgsLayerMetadata.SpatialExtent()
        se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')
        se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0)
        e.setSpatialExtents([se])
        dates = [
            QgsDateTimeRange(
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)),
                QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)))
        ]
        e.setTemporalExtents(dates)
        m.setExtent(e)

        c = QgsLayerMetadata.Contact()
        c.name = 'John Smith'
        c.organization = 'ACME'
        c.position = 'staff'
        c.voice = '1500 515 555'
        c.fax = 'xx.xxx.xxx.xxxx'
        c.email = '*****@*****.**'
        c.role = 'pointOfContact'
        address = QgsLayerMetadata.Address()
        address.type = 'postal'
        address.address = '123 Main Street'
        address.city = 'anycity'
        address.administrativeArea = 'anyprovince'
        address.postalCode = '90210'
        address.country = 'Canada'
        c.addresses = [address]
        m.setContacts([c])

        l = QgsLayerMetadata.Link()
        l.name = 'geonode:roads'
        l.type = 'OGC:WMS'
        l.description = 'my GeoNode road layer'
        l.url = 'http://example.org/wms'

        l2 = QgsLayerMetadata.Link()
        l2.name = 'geonode:roads'
        l2.type = 'OGC:WFS'
        l2.description = 'my GeoNode road layer'
        l2.url = 'http://example.org/wfs'

        l3 = QgsLayerMetadata.Link()
        l3.name = 'roads'
        l3.type = 'WWW:LINK'
        l3.description = 'full dataset download'
        l3.url = 'http://example.org/roads.tgz'
        l3.format = 'ESRI Shapefile'
        l3.mimeType = 'application/gzip'
        l3.size = '283676'

        m.setLinks([l, l2, l3])

        # set widget metadata
        w.setMetadata(m)
        self.assertEqual(w.mode(), QgsMetadataWidget.LayerMetadata)

        m = w.metadata()
        self.assertIsInstance(m, QgsLayerMetadata)

        self.assertEqual(m.identifier(), '1234')
        self.assertEqual(m.parentIdentifier(), 'xyz')
        self.assertEqual(m.language(), 'en-CA')
        self.assertEqual(m.type(), 'dataset')
        self.assertEqual(m.title(), 'roads')
        self.assertEqual(m.abstract(), 'my roads')
        self.assertEqual(m.fees(), 'None')
        self.assertEqual(m.constraints()[0].constraint, 'None')
        self.assertEqual(m.constraints()[0].type, 'access')
        self.assertEqual(m.rights(), ['Copyright foo 2017'])
        self.assertEqual(m.licenses(), ['WTFPL'])
        self.assertEqual(m.history(), ['history a', 'history b'])
        #self.assertEqual(m.encoding(), 'utf-8')
        self.assertEqual(
            m.keywords(),
            {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']})
        self.assertEqual(m.crs().authid(), 'EPSG:4326')

        extent = m.extent().spatialExtents()[0]
        self.assertEqual(extent.extentCrs.authid(), 'EPSG:4326')
        self.assertEqual(extent.bounds.xMinimum(), -180.0)
        self.assertEqual(extent.bounds.yMinimum(), -90.0)
        self.assertEqual(extent.bounds.xMaximum(), 180.0)
        self.assertEqual(extent.bounds.yMaximum(), 90.0)
        self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)))
        self.assertTrue(m.extent().temporalExtents()[0].isInstant())

        self.assertEqual(m.contacts()[0].name, 'John Smith')
        self.assertEqual(m.contacts()[0].organization, 'ACME')
        self.assertEqual(m.contacts()[0].position, 'staff')
        self.assertEqual(m.contacts()[0].voice, '1500 515 555')
        self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx')
        self.assertEqual(m.contacts()[0].email, '*****@*****.**')
        self.assertEqual(m.contacts()[0].role, 'pointOfContact')
        self.assertEqual(m.contacts()[0].addresses[0].type, 'postal')
        self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street')
        self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity')
        self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince')
        self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210')
        self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada')
        self.assertEqual(m.links()[0].name, 'geonode:roads')
        self.assertEqual(m.links()[0].type, 'OGC:WMS')
        self.assertEqual(m.links()[0].description, 'my GeoNode road layer')
        self.assertEqual(m.links()[0].url, 'http://example.org/wms')
        self.assertEqual(m.links()[1].name, 'geonode:roads')
        self.assertEqual(m.links()[1].type, 'OGC:WFS')
        self.assertEqual(m.links()[1].description, 'my GeoNode road layer')
        self.assertEqual(m.links()[1].url, 'http://example.org/wfs')
        self.assertEqual(m.links()[2].name, 'roads')
        self.assertEqual(m.links()[2].type, 'WWW:LINK')
        self.assertEqual(m.links()[2].description, 'full dataset download')
        self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz')
        self.assertEqual(m.links()[2].format, 'ESRI Shapefile')
        self.assertEqual(m.links()[2].mimeType, 'application/gzip')
        self.assertEqual(m.links()[2].size, '283676')
예제 #22
0
파일: process.py 프로젝트: Gustry/QuickOSM
def open_file(dialog: QDialog = None,
              osm_file: str = None,
              output_geom_types: list = None,
              white_list_column: dict = None,
              key: Union[str, List[str]] = None,
              layer_name: str = "OsmFile",
              config_outputs: dict = None,
              output_dir: str = None,
              output_format: Format = None,
              final_query: str = None,
              prefix_file: str = None,
              subset: bool = False,
              subset_query: str = None,
              feedback: QgsFeedback = None) -> int:
    """
    Open an osm file.

    Memory layer if no output directory is set, or Geojson in the output
    directory.

    :param final_query: The query where the file comes from. Might be empty if
    it's a local OSM file.
    :type final_query: basestring
    """

    if output_geom_types is None:
        output_geom_types = OSM_LAYERS
    # Legacy, waiting to remove the OsmParser for QGIS >= 3.6
    # Change in osm_file_dialog.py L131 too
    output_geom_legacy = [geom.value.lower() for geom in output_geom_types]
    if not white_list_column:
        white_list_column = None

    LOGGER.info('The OSM file is: {}'.format(osm_file))
    if feedback:
        if feedback.isCanceled():
            return None

    # Parsing the file
    osm_parser = OsmParser(osm_file=osm_file,
                           layers=output_geom_legacy,
                           output_format=output_format,
                           output_dir=output_dir,
                           prefix_file=prefix_file,
                           layer_name=layer_name,
                           key=key,
                           white_list_column=white_list_column,
                           subset=subset,
                           subset_query=subset_query,
                           feedback=feedback)

    if dialog:
        osm_parser.signalText.connect(dialog.set_progress_text)
        osm_parser.signalPercentage.connect(dialog.set_progress_percentage)

    start_time = time.time()
    layers = osm_parser.processing_parse()
    elapsed_time = time.time() - start_time
    parser_time = time.strftime("%Hh %Mm %Ss", time.gmtime(elapsed_time))
    LOGGER.info('The OSM parser took: {}'.format(parser_time))

    if feedback:
        if feedback.isCanceled():
            return None

    # Finishing the process with an output format or memory layer
    num_layers = 0

    for i, (layer, item) in enumerate(layers.items()):
        if dialog:
            dialog.set_progress_percentage(i / len(layers) * 100)
        QApplication.processEvents()
        if item['featureCount'] and (LayerType(layer.capitalize())
                                     in output_geom_types):

            final_layer_name = layer_name
            # If configOutputs is not None (from My Queries)
            if config_outputs:
                if config_outputs[layer]['namelayer']:
                    final_layer_name = config_outputs[layer]['namelayer']

            new_layer = item['vector_layer']
            new_layer.setName(final_layer_name)

            # Try to set styling if defined
            if config_outputs and config_outputs[layer]['style']:
                new_layer.loadNamedStyle(config_outputs[layer]['style'])
            else:
                if "colour" in item['tags']:
                    index = item['tags'].index('colour')
                    colors = new_layer.uniqueValues(index)
                    categories = []
                    for value in colors:
                        if str(value) == 'None':
                            value = ''
                        if layer in ['lines', 'multilinestrings']:
                            symbol = QgsSymbol.defaultSymbol(
                                QgsWkbTypes.LineGeometry)
                        elif layer == "points":
                            symbol = QgsSymbol.defaultSymbol(
                                QgsWkbTypes.PointGeometry)
                        elif layer == "multipolygons":
                            symbol = QgsSymbol.defaultSymbol(
                                QgsWkbTypes.PolygonGeometry)
                        symbol.setColor(QColor(value))
                        category = QgsRendererCategory(str(value), symbol,
                                                       str(value))
                        categories.append(category)

                    renderer = QgsCategorizedSymbolRenderer(
                        "colour", categories)
                    new_layer.setRenderer(renderer)

            # Add action about OpenStreetMap
            actions.add_actions(new_layer, item['tags'])

            QgsProject.instance().addMapLayer(new_layer)

            if final_query:
                QgsExpressionContextUtils.setLayerVariable(
                    new_layer, 'quickosm_query', final_query)
                actions.add_relaunch_action(new_layer, final_layer_name)
                if dialog:
                    dialog.iface.addCustomActionForLayer(
                        dialog.reload_action, new_layer)

            metadata = QgsLayerMetadata()
            metadata.setRights([tr("© OpenStreetMap contributors")])
            metadata.setLicenses(['https://openstreetmap.org/copyright'])
            new_layer.setMetadata(metadata)
            num_layers += 1

    return num_layers