Skip to content

Commit

Permalink
Merge pull request #12 from KnowWhereGraph/develop
Browse files Browse the repository at this point in the history
Support integrating against standalone geometries
  • Loading branch information
ckfisher authored Dec 18, 2024
2 parents c12a6c0 + 7989454 commit 1e43902
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,41 @@ Rather than relying on geosparql functions (which in turn rely on geosparql supp

This breaks the reliance on the need for the graph database to support s2 indexing and instead make use of the predicate index from the pre-materialized spatial relations.

## Cell Generation and Integration

There are two tools:

1. s2.py: This generates the S2 cell structure at a desired layer. For example, generating cells at level 3 and 4.
2. integrate.py: This performs s2 integrations against existing geometries. These may be your own geometries, they may be the output of the s2 tool.

## Running

### Docker

Docker should be used to generate the s2 coverings, which can be done by running the following from the root folder.
The project dependencies can be difficult to install; docker images are provided so that the code can be run in different environments without needing to install dependencies. Rather than offering a docker image for each cript, both scripts are included in the image and they can be called externally

#### Generating S2 Cells

```bash
git clone https://github.com/KnowWhereGraph/s2-coverings.git
cd s2-coverings
docker run -v ./:/s2 ghcr.io/knowwheregraph/s2-coverings:main python3 src/s2.py --level <level>
```

Cell integration can be disabled by adding the `--ni` flag to the command,
#### S2 Integration


```bash
docker run -v ./:/s2 ghcr.io/knowwheregraph/s2-coverings:main python3 src/s2.py --level <level> --ni
docker run -v ./:/s2 ghcr.io/knowwheregraph/s2-coverings:main python3 src/integrate.py --path <path to geometries>
```

A complete list of options can be found by running
A complete list of options can be found by running the help command on each tool. For example,
```bash
python3 src/s2.py --help
options:
-h, --help show this help message and exit
--level LEVEL Level at which the s2 cells are generated for
--format [FORMAT] The format to write the RDF in. Options are xml, n3, turtle, nt, pretty-xml, trix, trig, nquads, json-ld, hext
--ni [NI] When used, s2 integration is disabled
--compressed [COMPRESSED]
use the S2 hierarchy to write a compressed collection of relations at various levels
```
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fiona==1.9.5
fiona==1.10.1
pytest==8.3.2
rasterio==1.3.10
rasterstats==0.19.0
rdflib==7.0.0
rasterio==1.4.3
rasterstats==0.20.0
rdflib==7.1.1
s2sphere==0.2.5
Shapely==2.0.5
Shapely==2.0.6
SPARQLWrapper==2.0.0
24 changes: 24 additions & 0 deletions src/integrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

import argparse

from lib.integrator import Integrator

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--path",
help="Path to the folder with triples being integrated",
type=str,
nargs="?",
default="./output",
)
parser.add_argument(
"--compressed",
help="use the S2 hierarchy to write a compressed collection of relations at various levels",
type=bool,
nargs="?",
default=True,
)
args = parser.parse_args()
Integrator(args.compressed, args.path)
10 changes: 8 additions & 2 deletions src/lib/integrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ class Integrator:
Abstraction over the process for integrating s2 cells together with spatial relations.
"""

def __init__(self, compressed: bool):
def __init__(self, compressed: bool, folder: str | Path):
"""
Creates a new Integrator
:param compressed: Whether the triples are compressed or not
:param folder: Path to the folder where the triples are
"""
if compressed:
print(
"Compression is on. Relations will be compressed using the S2 hierarchy..."
)
data_path = Path("./output/")
data_path = Path(folder)
output_folder = f"./output/{data_path.stem}"

if compressed:
Expand Down
18 changes: 0 additions & 18 deletions src/s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,6 @@ def get_vertex_polygon(cell: S2Cell) -> Polygon:
nargs="?",
default="ttl",
)
parser.add_argument(
"--ni",
help="When used, s2 integration is disabled",
nargs="?",
const=1,
type=int,
)
parser.add_argument(
"--compressed",
help="use the S2 hierarchy to write a compressed collection of relations at various levels",
type=bool,
nargs="?",
default=True,
)
args = parser.parse_args()

level = args.level
Expand Down Expand Up @@ -190,7 +176,3 @@ def get_vertex_polygon(cell: S2Cell) -> Polygon:
)
cell_id_integers = [parent.id() for parent in parents]
level -= 1

# Handle integration
if not args.ni:
Integrator(args.compressed)
5 changes: 3 additions & 2 deletions tests/test_kwg_ont.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ def test_generate_cell_iri():
"""
cell_id_int = 288230376151711744
cell_id = S2CellId(cell_id_int)
assert generate_cell_iri(cell_id) == URIRef("http://stko-kwg.geog.ucsb.edu/lod/resource/s2.level1"
".288230376151711744")
assert generate_cell_iri(cell_id) == URIRef(
"http://stko-kwg.geog.ucsb.edu/lod/resource/s2.level1" ".288230376151711744"
)

0 comments on commit 1e43902

Please sign in to comment.