Skip to main content

ohsome filter to SQL

Build Status PyPI - Version LICENSE status: active

Documentation

Please see the section on the ohsome filter in the ohsome API documentation.

Try it out

# USAGE:
#   after running ohsome-filter-to-sql type in ohsome filter and hit enter
$ uvx ohsome-filter-to-sql
natural = tree and leaftype = broadleaf
('tags @> $1 AND tags @> $2', ('{"natural": "tree"}', '{"leaftype": "broadleaf"}'))

Installation

uv add ohsome-filter-to-sql

Usage

Python Library

To generate the SQL WHERE clause (query) alongside its query arguments:

from ohsome_filter_to_sql import ohsome_filter_to_sql

query, query_args = ohsome_filter_to_sql("natural = tree")

The generated SQL WHERE clause contains native PostgresSQL syntax for query arguments: $n.

ohsome-filter-to-sql can also be used to validate a give ohsome filter in Python:

from ohsome_filter_to_sql import validate_filter, ParserValueError

validate_filter("natural = tree")
try:
    validate_filter("geometry:foo")
except ParserValueError:
    pass

Alternatively, ohsome filter can be validated during runtime with Pydantic:

from ohsome_filter_to_sql import OhsomeFilter
from pydantic import validate_call


@validate_call
def request(ohsome_filter: OhsomeFilter):
    pass

Command Line Interface (CLI)

uv run ohsome-filter-to-sql

Development Setup

uv run prek install  # pre-commit hooks

export OHSOME_FILTER_TO_SQL_SCHEMA=""
export OHSOME_FILTER_TO_SQL_DATABASE=""
export OHSOME_FILTER_TO_SQL_USER=""
export OHSOME_FILTER_TO_SQL_PASSWORD=""
export OHSOME_FILTER_TO_SQL_HOST="localhost"
export OHSOME_FILTER_TO_SQL_PORT="5432"
uv run pytest

To develop new features you will need access to the ohsomeDB in production or a local instance of the ohsomeDB.

How to play around with the grammar?

Execute antlr4-parse, type in an ohsome filter and press ctlr+d.

antlr4-parse OFL.g4 root -tree
buildings=yes
(root:1 (expression:8 (tagMatch:1 (string:1 buildings) = (string:1 yes))) <EOF>)

ANTLR Lab can also be used to try out the grammar.

How to generate the parser code?

When the grammar file has changed generate new Python code with antlr4 and move generated files to ohsome_filter_to_sql/.

export ANTLR4_TOOLS_ANTLR_VERSION=$(uv pip show antlr4-python3-runtime | awk '/^Version:/{print $2}')
uv run antlr4 -Dlanguage=Python3 OFL.g4 && mv *.py ohsome_filter_to_sql/

Release

This project uses SemVer.

To make a new release run ./scripts/release.sh <version number>.

Resources

Metadata

Release files for ohsome-filter-to-sql 0.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ohsome-filter-to-sql 0.16.0
File Size Uploaded
ohsome_filter_to_sql-0.16.0.tar.gz 38.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ohsome-filter-to-sql 0.16.0
File Interpreter ABI Platform
ohsome_filter_to_sql-0.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 77.3 kB

Release files / ohsome_filter_to_sql-0.16.0.tar.gz

Download URL ohsome_filter_to_sql-0.16.0.tar.gz
Size 38.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6a08d8afab86471a8c9bb00264d3800d85bf536d7b3ba8fe7b364c2e7de92bd6
BLAKE2b-256 checksum
How to use checksums
f26324ba06f44c9ee8a78665ddd05f18845f957ce4037136d347435383ee32ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ohsome_filter_to_sql-0.16.0-py3-none-any.whl

Download URL ohsome_filter_to_sql-0.16.0-py3-none-any.whl
Size 39.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb6ed0d1728395fd5e0d8e269dc0d7ae3b59590e0e71c70efb2e73e8ea8af57c
BLAKE2b-256 checksum
How to use checksums
355e1a98ab390a816ab43998ac2dfd03f35985e74029a700a9f785c930efb042
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.16.0 This release

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.2

2 release files

0.10.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page