forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_geo.py
More file actions
155 lines (128 loc) · 4.55 KB
/
test_geo.py
File metadata and controls
155 lines (128 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
from pathlib import Path
import lance
import numpy as np
import pyarrow as pa
import pytest
pytest.importorskip("geoarrow.rust.core")
from geoarrow.rust.core import (
linestring,
linestrings,
point,
points,
polygon,
polygons,
)
def test_geo_types(tmp_path: Path):
uri = str(tmp_path / "test_geo_types.lance")
# Points
points_2d = points([np.random.rand(3), np.random.rand(3)])
# LineStrings
line_offsets = np.array([0, 2, 6, 10], dtype=np.int32)
linestrings_2d = linestrings([np.random.rand(10), np.random.rand(10)], line_offsets)
# Polygons
ring_offsets = np.array([0, 3, 7, 12], dtype=np.int32)
geom_offsets = np.array([0, 1, 2, 3], dtype=np.int32)
polygons_2d = polygons(
[np.random.rand(12), np.random.rand(12)],
ring_offsets=ring_offsets,
geom_offsets=geom_offsets,
)
schema = pa.schema(
[
pa.field(point("xy")).with_name("geometry_points"),
pa.field(linestring("xy")).with_name("geometry_lines"),
pa.field(polygon("xy")).with_name("geometry_polygons_2d"),
]
)
table = pa.Table.from_arrays(
[points_2d, linestrings_2d, polygons_2d], schema=schema
)
lance.write_dataset(table, uri)
ds = lance.dataset(uri)
assert ds.schema.field(0) == table.schema.field(0)
assert ds.schema.field(1) == table.schema.field(1)
assert ds.schema.field(2) == table.schema.field(2)
read_table = ds.to_table()
assert read_table.schema.field(0) == table.schema.field(0)
assert read_table.schema.field(1) == table.schema.field(1)
assert read_table.schema.field(2) == table.schema.field(2)
assert (
read_table.schema.field(0).metadata[b"ARROW:extension:name"]
== b"geoarrow.point"
)
assert (
read_table.schema.field(1).metadata[b"ARROW:extension:name"]
== b"geoarrow.linestring"
)
assert (
read_table.schema.field(2).metadata[b"ARROW:extension:name"]
== b"geoarrow.polygon"
)
assert read_table.num_rows == 3
def test_geo_sql(tmp_path: Path):
# Points
points_2d = points([np.array([1.0]), np.array([2.0])])
# LineStrings
line_offsets = np.array([0, 2], dtype=np.int32)
linestrings_2d = linestrings(
[np.array([3.0, 4.0]), np.array([5.0, 0.0])], line_offsets
)
schema = pa.schema(
[
pa.field(point("xy")).with_name("point"),
pa.field(linestring("xy")).with_name("linestring"),
]
)
table = pa.Table.from_arrays([points_2d, linestrings_2d], schema=schema)
ds = lance.write_dataset(table, str(tmp_path / "test_geo_udf_distance.lance"))
batches = (
ds.sql("SELECT St_Distance(point, linestring) as dist FROM dataset")
.build()
.to_batch_records()
)
assert len(batches) == 1
result = batches[0].to_pydict()
assert result["dist"]
assert np.allclose(
np.array(result["dist"]), np.array([2.5495097567963922]), atol=1e-8
)
def test_rtree_index(tmp_path: Path):
# LineStrings
num_lines = 10000
line_offsets = np.arange(num_lines + 1, dtype=np.int32) * 2
linestrings_2d = linestrings(
[np.random.randn(num_lines * 2) * 100, np.random.randn(num_lines * 2) * 100],
line_offsets,
)
assert len(linestrings_2d) == num_lines
schema = pa.schema(
[
pa.field("id", pa.int64()),
pa.field(linestring("xy")).with_name("linestring"),
]
)
table = pa.Table.from_arrays(
[np.arange(num_lines, dtype=np.int64), linestrings_2d], schema=schema
)
ds = lance.write_dataset(table, str(tmp_path / "test_rtree_index.lance"))
def query(ds: lance.LanceDataset, has_index=False):
sql = """
SELECT `id`, linestring
FROM dataset
WHERE
St_Intersects(linestring, ST_GeomFromText('LINESTRING ( 2 0, 0 2 )'))
"""
batches = ds.sql("EXPLAIN ANALYZE " + sql).build().to_batch_records()
explain = pa.Table.from_batches(batches).to_pandas().to_string()
if has_index:
assert "ScalarIndexQuery" in explain
else:
assert "ScalarIndexQuery" not in explain
batches = ds.sql(sql).build().to_batch_records()
return pa.Table.from_batches(batches)
table_without_index = query(ds)
ds.create_scalar_index("linestring", "RTREE")
table_with_index = query(ds, has_index=True)
assert table_with_index == table_without_index