Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
03f79615
提交
03f79615
authored
5月 28, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add SRID support to EWKT format that is de-facto used instead of WKT anyway
上级
40418025
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
77 行增加
和
62 行删除
+77
-62
help.csv
h2/src/docsrc/help/help.csv
+3
-2
ValueGeometry.java
h2/src/main/org/h2/value/ValueGeometry.java
+64
-58
TestSpatial.java
h2/src/test/org/h2/test/db/TestSpatial.java
+10
-2
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
03f79615
...
@@ -2895,9 +2895,10 @@ ENUM('clubs', 'diamonds', 'hearts', 'spades')
...
@@ -2895,9 +2895,10 @@ ENUM('clubs', 'diamonds', 'hearts', 'spades')
GEOMETRY
GEOMETRY
","
","
A spatial geometry type, based on the ""org.locationtech.jts"" library.
A spatial geometry type, based on the ""org.locationtech.jts"" library.
Normally represented in textual format using the WKT (well known text) format.
May be represented in textual format using the WKT (well-known text) or EWKT (extended well-known text) format.
Values are stored internally in EWKB (extended well-known binary) format.
Use a quoted string containing a WKT formatted string or ""PreparedStatement.setObject()"" to store values,
Use a quoted string containing a WKT
/EWKT
formatted string or ""PreparedStatement.setObject()"" to store values,
and ""ResultSet.getObject(..)"" or ""ResultSet.getString(..)"" to retrieve the values.
and ""ResultSet.getObject(..)"" or ""ResultSet.getString(..)"" to retrieve the values.
","
","
GEOMETRY
GEOMETRY
...
...
h2/src/main/org/h2/value/ValueGeometry.java
浏览文件 @
03f79615
...
@@ -100,31 +100,25 @@ public class ValueGeometry extends Value {
...
@@ -100,31 +100,25 @@ public class ValueGeometry extends Value {
/**
/**
* Get or create a geometry value for the given geometry.
* Get or create a geometry value for the given geometry.
*
*
* @param s the WKT representation of the geometry
* @param s the WKT
or EWKT
representation of the geometry
* @return the value
* @return the value
*/
*/
public
static
ValueGeometry
get
(
String
s
)
{
public
static
ValueGeometry
get
(
String
s
)
{
try
{
try
{
Geometry
g
=
new
WKTReader
().
read
(
s
);
int
srid
;
return
get
(
g
);
if
(
s
.
startsWith
(
"SRID="
))
{
}
catch
(
ParseException
ex
)
{
int
idx
=
s
.
indexOf
(
';'
,
5
);
throw
DbException
.
convert
(
ex
);
srid
=
Integer
.
parseInt
(
s
.
substring
(
5
,
idx
));
}
s
=
s
.
substring
(
idx
+
1
);
}
else
{
srid
=
0
;
}
}
/*
/**
* No-arg WKTReader() constructor instantiates a new GeometryFactory and a new
* Get or create a geometry value for the given geometry.
* PrecisionModel anyway, so special case for srid == 0 is not needed.
*
* @param s the WKT representation of the geometry
* @param srid the srid of the object
* @return the value
*/
*/
public
static
ValueGeometry
get
(
String
s
,
int
srid
)
{
return
get
(
new
WKTReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
srid
)).
read
(
s
));
try
{
}
catch
(
ParseException
|
StringIndexOutOfBoundsException
|
NumberFormatException
ex
)
{
GeometryFactory
geometryFactory
=
new
GeometryFactory
(
new
PrecisionModel
(),
srid
);
Geometry
g
=
new
WKTReader
(
geometryFactory
).
read
(
s
);
return
get
(
g
);
}
catch
(
ParseException
ex
)
{
throw
DbException
.
convert
(
ex
);
throw
DbException
.
convert
(
ex
);
}
}
}
}
...
@@ -154,8 +148,25 @@ public class ValueGeometry extends Value {
...
@@ -154,8 +148,25 @@ public class ValueGeometry extends Value {
public
Geometry
getGeometryNoCopy
()
{
public
Geometry
getGeometryNoCopy
()
{
if
(
geometry
==
null
)
{
if
(
geometry
==
null
)
{
try
{
try
{
int
srid
=
0
;
/*
getSRID:
if
(
bytes
.
length
>=
9
)
{
* No-arg WKBReader() constructor instantiates a new GeometryFactory and a new
* PrecisionModel anyway, so special case for srid == 0 is not needed.
*/
geometry
=
new
WKBReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
getSRID
())).
read
(
bytes
);
}
catch
(
ParseException
ex
)
{
throw
DbException
.
convert
(
ex
);
}
}
return
geometry
;
}
/**
* Return the SRID (Spatial Reference Identifier).
*
* @return spatial reference identifier
*/
public
int
getSRID
()
{
if
(
bytes
.
length
>=
9
)
{
boolean
bigEndian
;
boolean
bigEndian
;
switch
(
bytes
[
0
])
{
switch
(
bytes
[
0
])
{
case
0
:
case
0
:
...
@@ -165,25 +176,17 @@ public class ValueGeometry extends Value {
...
@@ -165,25 +176,17 @@ public class ValueGeometry extends Value {
bigEndian
=
false
;
bigEndian
=
false
;
break
;
break
;
default
:
default
:
break
getSRID
;
return
0
;
}
}
if
((
bytes
[
bigEndian
?
1
:
4
]
&
0x20
)
!=
0
)
{
if
((
bytes
[
bigEndian
?
1
:
4
]
&
0x20
)
!=
0
)
{
srid
=
Bits
.
readInt
(
bytes
,
5
);
int
srid
=
Bits
.
readInt
(
bytes
,
5
);
if
(!
bigEndian
)
{
if
(!
bigEndian
)
{
srid
=
Integer
.
reverseBytes
(
srid
);
srid
=
Integer
.
reverseBytes
(
srid
);
}
}
return
srid
;
}
}
}
}
/*
return
0
;
* No-arg WKBReader() constructor instantiates a new GeometryFactory and a new
* PrecisionModel anyway, so special case for srid == 0 is not needed.
*/
geometry
=
new
WKBReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
srid
)).
read
(
bytes
);
}
catch
(
ParseException
ex
)
{
throw
DbException
.
convert
(
ex
);
}
}
return
geometry
;
}
}
/**
/**
...
@@ -219,9 +222,7 @@ public class ValueGeometry extends Value {
...
@@ -219,9 +222,7 @@ public class ValueGeometry extends Value {
@Override
@Override
public
String
getSQL
()
{
public
String
getSQL
()
{
// WKT does not hold Z or SRID with JTS 1.13. As getSQL is used to
// Using bytes is faster than converting EWKB to Geometry then EWKT.
// export database, it should contains all object attributes. Moreover
// using bytes is faster than converting WKB to Geometry then to WKT.
return
"X'"
+
StringUtils
.
convertBytesToHex
(
getBytesNoCopy
())
+
"'::Geometry"
;
return
"X'"
+
StringUtils
.
convertBytesToHex
(
getBytesNoCopy
())
+
"'::Geometry"
;
}
}
...
@@ -233,7 +234,7 @@ public class ValueGeometry extends Value {
...
@@ -233,7 +234,7 @@ public class ValueGeometry extends Value {
@Override
@Override
public
String
getString
()
{
public
String
getString
()
{
return
getWKT
();
return
get
E
WKT
();
}
}
@Override
@Override
...
@@ -253,12 +254,12 @@ public class ValueGeometry extends Value {
...
@@ -253,12 +254,12 @@ public class ValueGeometry extends Value {
@Override
@Override
public
byte
[]
getBytes
()
{
public
byte
[]
getBytes
()
{
return
Utils
.
cloneByteArray
(
getWKB
());
return
Utils
.
cloneByteArray
(
get
E
WKB
());
}
}
@Override
@Override
public
byte
[]
getBytesNoCopy
()
{
public
byte
[]
getBytesNoCopy
()
{
return
getWKB
();
return
get
E
WKB
();
}
}
@Override
@Override
...
@@ -269,12 +270,12 @@ public class ValueGeometry extends Value {
...
@@ -269,12 +270,12 @@ public class ValueGeometry extends Value {
@Override
@Override
public
int
getDisplaySize
()
{
public
int
getDisplaySize
()
{
return
getWKT
().
length
();
return
get
E
WKT
().
length
();
}
}
@Override
@Override
public
int
getMemory
()
{
public
int
getMemory
()
{
return
getWKB
().
length
*
20
+
24
;
return
get
E
WKB
().
length
*
20
+
24
;
}
}
@Override
@Override
...
@@ -282,24 +283,29 @@ public class ValueGeometry extends Value {
...
@@ -282,24 +283,29 @@ public class ValueGeometry extends Value {
// The JTS library only does half-way support for 3D coordinates, so
// The JTS library only does half-way support for 3D coordinates, so
// their equals method only checks the first two coordinates.
// their equals method only checks the first two coordinates.
return
other
instanceof
ValueGeometry
&&
return
other
instanceof
ValueGeometry
&&
Arrays
.
equals
(
get
WKB
(),
((
ValueGeometry
)
other
).
get
WKB
());
Arrays
.
equals
(
get
EWKB
(),
((
ValueGeometry
)
other
).
getE
WKB
());
}
}
/**
/**
* Get the value in
Well-Known-
Text format.
* Get the value in
Extended Well-Known
Text format.
*
*
* @return the
well-known-
text
* @return the
extended well-known
text
*/
*/
public
String
getWKT
()
{
public
String
getEWKT
()
{
return
new
WKTWriter
(
3
).
write
(
getGeometryNoCopy
());
String
wkt
=
new
WKTWriter
(
3
).
write
(
getGeometryNoCopy
());
int
srid
=
getSRID
();
return
srid
==
0
?
wkt
// "SRID=-2147483648;".length() == 17
:
new
StringBuilder
(
wkt
.
length
()
+
17
).
append
(
"SRID="
).
append
(
srid
).
append
(
';'
).
append
(
wkt
).
toString
();
}
}
/**
/**
* Get the value in
Well-Known-
Binary format.
* Get the value in
extended Well-Known
Binary format.
*
*
* @return the
well-known-
binary
* @return the
extended well-known
binary
*/
*/
public
byte
[]
getWKB
()
{
public
byte
[]
get
E
WKB
()
{
return
bytes
;
return
bytes
;
}
}
...
...
h2/src/test/org/h2/test/db/TestSpatial.java
浏览文件 @
03f79615
...
@@ -597,8 +597,9 @@ public class TestSpatial extends TestBase {
...
@@ -597,8 +597,9 @@ public class TestSpatial extends TestBase {
* Test serialization of Z and SRID values.
* Test serialization of Z and SRID values.
*/
*/
private
void
testWKB
()
{
private
void
testWKB
()
{
ValueGeometry
geom3d
=
ValueGeometry
.
get
(
String
ewkt
=
"SRID=27572;POLYGON ((67 13 6, 67 18 5, 59 18 4, 59 13 6, 67 13 6))"
;
"POLYGON ((67 13 6, 67 18 5, 59 18 4, 59 13 6, 67 13 6))"
,
27572
);
ValueGeometry
geom3d
=
ValueGeometry
.
get
(
ewkt
);
assertEquals
(
ewkt
,
geom3d
.
getString
());
ValueGeometry
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
ValueGeometry
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
assertEquals
(
6
,
copy
.
getGeometry
().
getCoordinates
()[
0
].
z
);
assertEquals
(
6
,
copy
.
getGeometry
().
getCoordinates
()[
0
].
z
);
assertEquals
(
5
,
copy
.
getGeometry
().
getCoordinates
()[
1
].
z
);
assertEquals
(
5
,
copy
.
getGeometry
().
getCoordinates
()[
1
].
z
);
...
@@ -606,6 +607,7 @@ public class TestSpatial extends TestBase {
...
@@ -606,6 +607,7 @@ public class TestSpatial extends TestBase {
// Test SRID
// Test SRID
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
assertEquals
(
27572
,
copy
.
getGeometry
().
getSRID
());
assertEquals
(
27572
,
copy
.
getGeometry
().
getSRID
());
Point
point
=
new
GeometryFactory
().
createPoint
((
new
Coordinate
(
1.1d
,
1.2d
)));
Point
point
=
new
GeometryFactory
().
createPoint
((
new
Coordinate
(
1.1d
,
1.2d
)));
// SRID 0
// SRID 0
checkSRID
(
ValueGeometry
.
getFromGeometry
(
point
).
getBytes
(),
0
);
checkSRID
(
ValueGeometry
.
getFromGeometry
(
point
).
getBytes
(),
0
);
...
@@ -613,11 +615,17 @@ public class TestSpatial extends TestBase {
...
@@ -613,11 +615,17 @@ public class TestSpatial extends TestBase {
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
BIG_ENDIAN
,
true
).
write
(
point
),
0
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
BIG_ENDIAN
,
true
).
write
(
point
),
0
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
false
).
write
(
point
),
0
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
false
).
write
(
point
),
0
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
true
).
write
(
point
),
0
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
true
).
write
(
point
),
0
);
ewkt
=
"POINT (1.1 1.2)"
;
assertEquals
(
ewkt
,
ValueGeometry
.
getFromGeometry
(
point
).
getString
());
assertEquals
(
ewkt
,
ValueGeometry
.
get
(
ewkt
).
getString
());
// SRID 1,000,000,000
// SRID 1,000,000,000
point
.
setSRID
(
1_000_000_000
);
point
.
setSRID
(
1_000_000_000
);
checkSRID
(
ValueGeometry
.
getFromGeometry
(
point
).
getBytes
(),
1_000_000_000
);
checkSRID
(
ValueGeometry
.
getFromGeometry
(
point
).
getBytes
(),
1_000_000_000
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
BIG_ENDIAN
,
true
).
write
(
point
),
1_000_000_000
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
BIG_ENDIAN
,
true
).
write
(
point
),
1_000_000_000
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
true
).
write
(
point
),
1_000_000_000
);
checkSRID
(
new
WKBWriter
(
2
,
ByteOrderValues
.
LITTLE_ENDIAN
,
true
).
write
(
point
),
1_000_000_000
);
ewkt
=
"SRID=1000000000;POINT (1.1 1.2)"
;
assertEquals
(
ewkt
,
ValueGeometry
.
getFromGeometry
(
point
).
getString
());
assertEquals
(
ewkt
,
ValueGeometry
.
get
(
ewkt
).
getString
());
}
}
private
void
checkSRID
(
byte
[]
bytes
,
int
srid
)
{
private
void
checkSRID
(
byte
[]
bytes
,
int
srid
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论