-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement CQL to OGC filter transforms
- Loading branch information
1 parent
c27c247
commit 522873e
Showing
7 changed files
with
213 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
# ================================================================= | ||
# | ||
# Authors: Tom Kralidis <[email protected]> | ||
# | ||
# Copyright (c) 2016 Tom Kralidis | ||
# | ||
# Permission is hereby granted, free of charge, to any person | ||
# obtaining a copy of this software and associated documentation | ||
# files (the "Software"), to deal in the Software without | ||
# restriction, including without limitation the rights to use, | ||
# copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following | ||
# conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
# ================================================================= | ||
|
||
import logging | ||
|
||
from lxml import etree | ||
from pycsw import util | ||
from pycsw.fes import MODEL as fes1_model | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def cql2fes1(cql, namespaces): | ||
"""transforms Common Query Language (CQL) query into OGC fes1 syntax""" | ||
|
||
filters = [] | ||
tmp_list = [] | ||
logical_op = None | ||
|
||
LOGGER.debug('CQL: %s', cql) | ||
|
||
if ' or ' in cql: | ||
logical_op = etree.Element(util.nspath_eval('ogc:Or', namespaces)) | ||
tmp_list = cql.split(' or ') | ||
elif ' OR ' in cql: | ||
logical_op = etree.Element(util.nspath_eval('ogc:Or', namespaces)) | ||
tmp_list = cql.split(' OR ') | ||
elif ' and ' in cql: | ||
logical_op = etree.Element(util.nspath_eval('ogc:And', namespaces)) | ||
tmp_list = cql.split(' and ') | ||
elif ' AND ' in cql: | ||
logical_op = etree.Element(util.nspath_eval('ogc:And', namespaces)) | ||
tmp_list = cql.split(' AND ') | ||
|
||
if tmp_list: | ||
LOGGER.debug('Logical operator found (AND/OR)') | ||
else: | ||
tmp_list.append(cql) | ||
|
||
for t in tmp_list: | ||
filters.append(_parse_condition(t)) | ||
|
||
root = etree.Element(util.nspath_eval('ogc:Filter', namespaces)) | ||
|
||
if logical_op is not None: | ||
root.append(logical_op) | ||
|
||
for flt in filters: | ||
condition = etree.Element(util.nspath_eval(flt[0], namespaces)) | ||
|
||
etree.SubElement( | ||
condition, | ||
util.nspath_eval('ogc:PropertyName', namespaces)).text = flt[1] | ||
|
||
etree.SubElement( | ||
condition, | ||
util.nspath_eval('ogc:Literal', namespaces)).text = flt[2] | ||
|
||
if logical_op is not None: | ||
logical_op.append(condition) | ||
else: | ||
root.append(condition) | ||
|
||
LOGGER.debug('Resulting OGC Filter: %s', | ||
etree.tostring(root, pretty_print=1)) | ||
|
||
return root | ||
|
||
|
||
def _parse_condition(condition): | ||
"""parses a single condition""" | ||
|
||
LOGGER.debug('condition: %s', condition) | ||
|
||
property_name, operator, literal = condition.split() | ||
|
||
literal = literal.replace('"', '').replace('\'', '') | ||
|
||
for k, v in fes1_model['ComparisonOperators'].items(): | ||
if v['opvalue'] == operator: | ||
fes1_predicate = k | ||
|
||
LOGGER.debug('parsed condition: %s %s %s', property_name, fes1_predicate, | ||
literal) | ||
|
||
return (fes1_predicate, property_name, literal) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/expected/suites_default_get_GetRecords-filter-cql-title-or-abstract.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<!-- PYCSW_VERSION --> | ||
<csw:GetRecordsResponse xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:gml="http://www.opengis.net/gml" xmlns:dif="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ows="http://www.opengis.net/ows" xmlns:fgdc="http://www.opengis.net/cat/csw/csdgm" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> | ||
<csw:SearchStatus timestamp="PYCSW_TIMESTAMP"/> | ||
<csw:SearchResults nextRecord="0" numberOfRecordsMatched="2" numberOfRecordsReturned="2" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> | ||
<csw:Record> | ||
<dc:identifier>urn:uuid:19887a8a-f6b0-4a63-ae56-7fba0e17801f</dc:identifier> | ||
<dc:type>http://purl.org/dc/dcmitype/Image</dc:type> | ||
<dc:format>image/svg+xml</dc:format> | ||
<dc:title>Lorem ipsum</dc:title> | ||
<dct:spatial>GR-22</dct:spatial> | ||
<dc:subject>Tourism--Greece</dc:subject> | ||
<dct:abstract>Quisque lacus diam, placerat mollis, pharetra in, commodo sed, augue. Duis iaculis arcu vel arcu.</dct:abstract> | ||
</csw:Record> | ||
<csw:Record> | ||
<dc:identifier>urn:uuid:a06af396-3105-442d-8b40-22b57a90d2f2</dc:identifier> | ||
<dc:type>http://purl.org/dc/dcmitype/Image</dc:type> | ||
<dc:title>Lorem ipsum dolor sit amet</dc:title> | ||
<dc:format>image/jpeg</dc:format> | ||
<dct:spatial>IT-FI</dct:spatial> | ||
</csw:Record> | ||
</csw:SearchResults> | ||
</csw:GetRecordsResponse> |
23 changes: 23 additions & 0 deletions
23
tests/expected/suites_default_get_GetRecords-filter-cql-title.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<!-- PYCSW_VERSION --> | ||
<csw:GetRecordsResponse xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:gml="http://www.opengis.net/gml" xmlns:dif="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ows="http://www.opengis.net/ows" xmlns:fgdc="http://www.opengis.net/cat/csw/csdgm" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> | ||
<csw:SearchStatus timestamp="PYCSW_TIMESTAMP"/> | ||
<csw:SearchResults nextRecord="0" numberOfRecordsMatched="2" numberOfRecordsReturned="2" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> | ||
<csw:Record> | ||
<dc:identifier>urn:uuid:19887a8a-f6b0-4a63-ae56-7fba0e17801f</dc:identifier> | ||
<dc:type>http://purl.org/dc/dcmitype/Image</dc:type> | ||
<dc:format>image/svg+xml</dc:format> | ||
<dc:title>Lorem ipsum</dc:title> | ||
<dct:spatial>GR-22</dct:spatial> | ||
<dc:subject>Tourism--Greece</dc:subject> | ||
<dct:abstract>Quisque lacus diam, placerat mollis, pharetra in, commodo sed, augue. Duis iaculis arcu vel arcu.</dct:abstract> | ||
</csw:Record> | ||
<csw:Record> | ||
<dc:identifier>urn:uuid:a06af396-3105-442d-8b40-22b57a90d2f2</dc:identifier> | ||
<dc:type>http://purl.org/dc/dcmitype/Image</dc:type> | ||
<dc:title>Lorem ipsum dolor sit amet</dc:title> | ||
<dc:format>image/jpeg</dc:format> | ||
<dct:spatial>IT-FI</dct:spatial> | ||
</csw:Record> | ||
</csw:SearchResults> | ||
</csw:GetRecordsResponse> |
12 changes: 12 additions & 0 deletions
12
tests/expected/suites_default_post_GetRecords-cql-title-and-abstract.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<!-- PYCSW_VERSION --> | ||
<csw:GetRecordsResponse xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:gml="http://www.opengis.net/gml" xmlns:dif="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ows="http://www.opengis.net/ows" xmlns:fgdc="http://www.opengis.net/cat/csw/csdgm" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> | ||
<csw:SearchStatus timestamp="PYCSW_TIMESTAMP"/> | ||
<csw:SearchResults nextRecord="0" numberOfRecordsMatched="1" numberOfRecordsReturned="1" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="brief"> | ||
<csw:BriefRecord> | ||
<dc:identifier>urn:uuid:19887a8a-f6b0-4a63-ae56-7fba0e17801f</dc:identifier> | ||
<dc:title>Lorem ipsum</dc:title> | ||
<dc:type>http://purl.org/dc/dcmitype/Image</dc:type> | ||
</csw:BriefRecord> | ||
</csw:SearchResults> | ||
</csw:GetRecordsResponse> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/suites/default/post/GetRecords-cql-title-and-abstract.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> | ||
<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:ogc="http://www.opengis.net/ogc" service="CSW" version="2.0.2" resultType="results" startPosition="1" maxRecords="5" outputFormat="application/xml" outputSchema="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> | ||
<csw:Query typeNames="csw:Record"> | ||
<csw:ElementSetName>brief</csw:ElementSetName> | ||
<csw:Constraint version="1.1.0"> | ||
<csw:CqlText>dc:title like '%ips%' and dct:abstract like '%pharetra%'</csw:CqlText> | ||
</csw:Constraint> | ||
</csw:Query> | ||
</csw:GetRecords> |