Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb backend support #49

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
host node port: 9300
node port: 9300
discovery type: 'single-node'
- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: '5.0'
- name: Run unit tests
run: |
pytest
Expand Down
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ pygeofilter is a pure Python parser implementation of OGC filtering standards
* [CQL as defined in CSW 2.0](https://portal.ogc.org/files/?artifact_id=20555)
* [CQL JSON as defined in OGC API - Features - Part 3: Filtering and the Common Query Language (CQL)](https://portal.ogc.org/files/96288#cql-json-schema)
* [JSON Filter Expressions (JFE)](https://github.com/tschaub/ogcapi-features/tree/json-array-expression/extensions/cql/jfe)
* [FES](http://docs.opengeospatial.org/is/09-026r2/09-026r2.html)
* Soon:
* [CQL Text as defined in OGC API - Features - Part 3: Filtering and the Common Query Language (CQL)](https://portal.ogc.org/files/96288#cql-bnf)
* [FES](http://docs.opengeospatial.org/is/09-026r2/09-026r2.html)
* Several backends included
* [Django](https://www.djangoproject.com/)
* [SQLAlchemy](https://www.sqlalchemy.org/)
* [(Geo)Pandas](https://pandas.pydata.org/)
* [Elasticsearch]()
* [MongoDB]()
* Native Python objects


Expand Down Expand Up @@ -328,6 +330,35 @@ Note that it is vital to specify the `SQLite` dialect as this is the one used in
:warning: Input values are *not* sanitized/separated from the generated SQL text. This is due to the compatibility with the OGR API not allowing to separate the SQL from the arguments.


### MongoDB

The `MongoDBEvaluator` creates a JSON structure that can be sent via the pymongo client.

```python
from pymongo import MongoClient
from pygeofilter.parsers.ecql import parse
from pygeofilter.backends.mongodb import to_filter

# connect to the MongoDB database and create a spatial index for the geometry
client = MongoClient()
client.db.collection.create_index([
("geometry", pymongo.GEOSPHERE),
])

# insert records here
# ...

# parse a filter
ast_ = parse('INTERSECTS(geometry, ENVELOPE (0.0 1.0 0.0 1.0))')

# turn the AST to a MongoDB query
query = to_filter(ast_)

# perform the query
results = collection.find(query)
```


### Optimization

This is a special kind of backend, as the result of the AST evaluation is actually a new AST. The purpose of this backend is to eliminate static branches of the AST, potentially reducing the cost of an actual evaluation for filtering values.
Expand Down
33 changes: 33 additions & 0 deletions pygeofilter/backends/mongodb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ------------------------------------------------------------------------------
#
# Project: pygeofilter <https://github.com/geopython/pygeofilter>
# Authors: Fabian Schindler <[email protected]>
#
# ------------------------------------------------------------------------------
# Copyright (C) 2022 EOX IT Services GmbH
#
# 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 of this Software or works derived from this 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.
# ------------------------------------------------------------------------------

""" MongoDB backend for pygeofilter.
"""

from .evaluate import to_filter

__all__ = ["to_filter"]
Loading