Skip to content

Commit 1e43902

Browse files
authored
Merge pull request #12 from KnowWhereGraph/develop
Support integrating against standalone geometries
2 parents c12a6c0 + 7989454 commit 1e43902

File tree

6 files changed

+54
-32
lines changed

6 files changed

+54
-32
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,41 @@ Rather than relying on geosparql functions (which in turn rely on geosparql supp
1111

1212
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.
1313

14+
## Cell Generation and Integration
15+
16+
There are two tools:
17+
18+
1. s2.py: This generates the S2 cell structure at a desired layer. For example, generating cells at level 3 and 4.
19+
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.
20+
1421
## Running
1522

1623
### Docker
1724

18-
Docker should be used to generate the s2 coverings, which can be done by running the following from the root folder.
25+
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
26+
27+
#### Generating S2 Cells
1928

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

26-
Cell integration can be disabled by adding the `--ni` flag to the command,
35+
#### S2 Integration
36+
2737

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

32-
A complete list of options can be found by running
42+
A complete list of options can be found by running the help command on each tool. For example,
3343
```bash
3444
python3 src/s2.py --help
3545
options:
3646
-h, --help show this help message and exit
3747
--level LEVEL Level at which the s2 cells are generated for
3848
--format [FORMAT] The format to write the RDF in. Options are xml, n3, turtle, nt, pretty-xml, trix, trig, nquads, json-ld, hext
39-
--ni [NI] When used, s2 integration is disabled
4049
--compressed [COMPRESSED]
4150
use the S2 hierarchy to write a compressed collection of relations at various levels
4251
```

requirements.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
fiona==1.9.5
1+
fiona==1.10.1
22
pytest==8.3.2
3-
rasterio==1.3.10
4-
rasterstats==0.19.0
5-
rdflib==7.0.0
3+
rasterio==1.4.3
4+
rasterstats==0.20.0
5+
rdflib==7.1.1
66
s2sphere==0.2.5
7-
Shapely==2.0.5
7+
Shapely==2.0.6
88
SPARQLWrapper==2.0.0

src/integrate.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
import argparse
4+
5+
from lib.integrator import Integrator
6+
7+
if __name__ == "__main__":
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument(
10+
"--path",
11+
help="Path to the folder with triples being integrated",
12+
type=str,
13+
nargs="?",
14+
default="./output",
15+
)
16+
parser.add_argument(
17+
"--compressed",
18+
help="use the S2 hierarchy to write a compressed collection of relations at various levels",
19+
type=bool,
20+
nargs="?",
21+
default=True,
22+
)
23+
args = parser.parse_args()
24+
Integrator(args.compressed, args.path)

src/lib/integrator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ class Integrator:
1818
Abstraction over the process for integrating s2 cells together with spatial relations.
1919
"""
2020

21-
def __init__(self, compressed: bool):
21+
def __init__(self, compressed: bool, folder: str | Path):
22+
"""
23+
Creates a new Integrator
24+
25+
:param compressed: Whether the triples are compressed or not
26+
:param folder: Path to the folder where the triples are
27+
"""
2228
if compressed:
2329
print(
2430
"Compression is on. Relations will be compressed using the S2 hierarchy..."
2531
)
26-
data_path = Path("./output/")
32+
data_path = Path(folder)
2733
output_folder = f"./output/{data_path.stem}"
2834

2935
if compressed:

src/s2.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,6 @@ def get_vertex_polygon(cell: S2Cell) -> Polygon:
144144
nargs="?",
145145
default="ttl",
146146
)
147-
parser.add_argument(
148-
"--ni",
149-
help="When used, s2 integration is disabled",
150-
nargs="?",
151-
const=1,
152-
type=int,
153-
)
154-
parser.add_argument(
155-
"--compressed",
156-
help="use the S2 hierarchy to write a compressed collection of relations at various levels",
157-
type=bool,
158-
nargs="?",
159-
default=True,
160-
)
161147
args = parser.parse_args()
162148

163149
level = args.level
@@ -190,7 +176,3 @@ def get_vertex_polygon(cell: S2Cell) -> Polygon:
190176
)
191177
cell_id_integers = [parent.id() for parent in parents]
192178
level -= 1
193-
194-
# Handle integration
195-
if not args.ni:
196-
Integrator(args.compressed)

tests/test_kwg_ont.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ def test_generate_cell_iri():
1212
"""
1313
cell_id_int = 288230376151711744
1414
cell_id = S2CellId(cell_id_int)
15-
assert generate_cell_iri(cell_id) == URIRef("http://stko-kwg.geog.ucsb.edu/lod/resource/s2.level1"
16-
".288230376151711744")
15+
assert generate_cell_iri(cell_id) == URIRef(
16+
"http://stko-kwg.geog.ucsb.edu/lod/resource/s2.level1" ".288230376151711744"
17+
)

0 commit comments

Comments
 (0)