pygeofilter

0.3.3 · active · verified Thu Apr 16

pygeofilter is a pure Python library for parsing and evaluating OGC filtering standards, including Filter Encoding 2.0 and CQL2. It provides robust tools to parse filter expressions from various formats (e.g., CQL2-TEXT, CQL2-JSON) and evaluate them against Python data structures or translate them into SQL queries. The current version is 0.3.3, and it receives active development with regular patch and minor releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a CQL2-TEXT filter string and then evaluate it against a Python dictionary representing a data item's properties. It showcases both a successful and a failing evaluation.

from pygeofilter.parsers.cql2_text import parse
from pygeofilter.evaluate import evaluate

# Define a CQL2 filter expression
cql_filter_string = "(name = 'test-feature' AND temperature > 25) OR (id IN ('A1', 'B2'))"

# Parse the filter expression
parsed_filter = parse(cql_filter_string)

# Define a data item (dictionary representing feature properties)
data_item = {
    "name": "test-feature",
    "temperature": 28.5,
    "id": "A1",
    "timestamp": "2024-01-01T10:00:00Z"
}

# Evaluate the filter against the data item
result = evaluate(parsed_filter, data_item)

print(f"Filter expression: {cql_filter_string}")
print(f"Data item: {data_item}")
print(f"Evaluation result: {result}")

# Example with a different data item (should fail the filter)
data_item_fail = {
    "name": "other-feature",
    "temperature": 20.0,
    "id": "C3",
    "timestamp": "2024-01-01T11:00:00Z"
}
result_fail = evaluate(parsed_filter, data_item_fail)
print(f"Evaluation result for other item: {result_fail}")

view raw JSON →