示例#1
0
    def test_expressionFromOgcFilterWithAndOrPropertyIsLike(self):
        """
        Test expressionFromOgcFilter with And, Or and PropertyIsLike with wildCard
        """

        vl = QgsVectorLayer('Point', 'vl', 'memory')
        vl.dataProvider().addAttributes([
            QgsField('id', QVariant.LongLong),
            QgsField('THEME', QVariant.String),
            QgsField('PROGRAMME', QVariant.String)
        ])
        vl.updateFields()

        f = '''<?xml version="1.0" encoding="UTF-8"?>
            <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
              <ogc:And>
                <ogc:PropertyIsLike escape="\\" wildCard="%" singleChar="_">
                  <ogc:PropertyName>THEME</ogc:PropertyName>
                  <ogc:Literal>%Phytoplancton total%</ogc:Literal>
                </ogc:PropertyIsLike>
                <ogc:Or>
                  <ogc:PropertyIsLike escapeChar="\\" matchCase="false" singleChar="?" wildCard="*">
                    <ogc:PropertyName>PROGRAMME</ogc:PropertyName>
                    <ogc:Literal>REPHY;*</ogc:Literal>
                  </ogc:PropertyIsLike>
                  <ogc:PropertyIsLike escapeChar="\\" matchCase="false" singleChar="?" wildCard="*">
                    <ogc:PropertyName>PROGRAMME</ogc:PropertyName>
                    <ogc:Literal>*;REPHY</ogc:Literal>
                  </ogc:PropertyIsLike>
                  <ogc:PropertyIsLike escapeChar="\\" matchCase="false" singleChar="?" wildCard="*">
                    <ogc:PropertyName>PROGRAMME</ogc:PropertyName>
                    <ogc:Literal>*;REPHY;*</ogc:Literal>
                  </ogc:PropertyIsLike>
                  <ogc:PropertyIsLike escapeChar="\\" matchCase="false" singleChar="?" wildCard="*">
                    <ogc:PropertyName>PROGRAMME</ogc:PropertyName>
                    <ogc:Literal>^REPHY$</ogc:Literal>
                  </ogc:PropertyIsLike>
                </ogc:Or>
              </ogc:And>
            </ogc:Filter>
        '''
        d = QDomDocument('filter')
        d.setContent(f, True)

        e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl)
        self.assertEqual(
            e.expression(),
            'THEME LIKE \'%Phytoplancton total%\' AND (PROGRAMME ILIKE \'REPHY;%\' OR PROGRAMME ILIKE \'%;REPHY\' OR PROGRAMME ILIKE \'%;REPHY;%\' OR PROGRAMME ILIKE \'^REPHY$\')'
        )
示例#2
0
    def test_expressionFromOgcFilterWithString(self):
        """
        Test expressionFromOgcFilter with String type field
        """
        vl = QgsVectorLayer('Point', 'vl', 'memory')
        vl.dataProvider().addAttributes([QgsField('id', QVariant.String)])
        vl.updateFields()

        # Literals are Integer 1 and 3
        f = '''<?xml version="1.0" encoding="UTF-8"?>
            <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
              <ogc:And>
                <ogc:PropertyIsGreaterThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>1</ogc:Literal>
                </ogc:PropertyIsGreaterThan>
                <ogc:PropertyIsLessThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>3</ogc:Literal>
                </ogc:PropertyIsLessThan>
              </ogc:And>
            </ogc:Filter>
        '''
        d = QDomDocument('filter')
        d.setContent(f, True)

        e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl)
        self.assertEqual(e.expression(), 'id > \'1\' AND id < \'3\'')

        # Literals are Double 1.0 and 3.0
        f = '''<?xml version="1.0" encoding="UTF-8"?>
            <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
              <ogc:And>
                <ogc:PropertyIsGreaterThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>1.0</ogc:Literal>
                </ogc:PropertyIsGreaterThan>
                <ogc:PropertyIsLessThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>3.0</ogc:Literal>
                </ogc:PropertyIsLessThan>
              </ogc:And>
            </ogc:Filter>
        '''
        d = QDomDocument('filter')
        d.setContent(f, True)

        e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl)
        self.assertEqual(e.expression(), 'id > \'1.0\' AND id < \'3.0\'')

        # Literals are Double 1.5 and 3.5
        f = '''<?xml version="1.0" encoding="UTF-8"?>
            <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
              <ogc:And>
                <ogc:PropertyIsGreaterThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>1.5</ogc:Literal>
                </ogc:PropertyIsGreaterThan>
                <ogc:PropertyIsLessThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>3.5</ogc:Literal>
                </ogc:PropertyIsLessThan>
              </ogc:And>
            </ogc:Filter>
        '''
        d = QDomDocument('filter')
        d.setContent(f, True)

        e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl)
        self.assertEqual(e.expression(), 'id > \'1.5\' AND id < \'3.5\'')

        # Literals are Scientific notation 15e-01 and 35e-01
        f = '''<?xml version="1.0" encoding="UTF-8"?>
            <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
              <ogc:And>
                <ogc:PropertyIsGreaterThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>15e-01</ogc:Literal>
                </ogc:PropertyIsGreaterThan>
                <ogc:PropertyIsLessThan>
                  <ogc:PropertyName>id</ogc:PropertyName>
                  <ogc:Literal>35e-01</ogc:Literal>
                </ogc:PropertyIsLessThan>
              </ogc:And>
            </ogc:Filter>
        '''
        d = QDomDocument('filter')
        d.setContent(f, True)

        e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl)
        self.assertEqual(e.expression(), 'id > \'15e-01\' AND id < \'35e-01\'')