Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4aeb5900
Unverified
提交
4aeb5900
authored
5月 31, 2018
作者:
Evgenij Ryazanov
提交者:
GitHub
5月 31, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1166 from katzyn/EWKT
Add SRID support to EWKT format
上级
40418025
dfc38f4b
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
79 行增加
和
48 行删除
+79
-48
help.csv
h2/src/docsrc/help/help.csv
+3
-2
ValueGeometry.java
h2/src/main/org/h2/value/ValueGeometry.java
+66
-44
TestSpatial.java
h2/src/test/org/h2/test/db/TestSpatial.java
+10
-2
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
4aeb5900
...
...
@@ -2895,9 +2895,10 @@ ENUM('clubs', 'diamonds', 'hearts', 'spades')
GEOMETRY
","
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.
","
GEOMETRY
...
...
h2/src/main/org/h2/value/ValueGeometry.java
浏览文件 @
4aeb5900
...
...
@@ -100,14 +100,25 @@ public class ValueGeometry extends Value {
/**
* 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
*/
public
static
ValueGeometry
get
(
String
s
)
{
try
{
Geometry
g
=
new
WKTReader
().
read
(
s
);
return
get
(
g
);
}
catch
(
ParseException
ex
)
{
int
srid
;
if
(
s
.
startsWith
(
"SRID="
))
{
int
idx
=
s
.
indexOf
(
';'
,
5
);
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
* PrecisionModel anyway, so special case for srid == 0 is not needed.
*/
return
get
(
new
WKTReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
srid
)).
read
(
s
));
}
catch
(
ParseException
|
StringIndexOutOfBoundsException
|
NumberFormatException
ex
)
{
throw
DbException
.
convert
(
ex
);
}
}
...
...
@@ -120,10 +131,9 @@ public class ValueGeometry extends Value {
* @return the value
*/
public
static
ValueGeometry
get
(
String
s
,
int
srid
)
{
// This method is not used in H2, but preserved for H2GIS
try
{
GeometryFactory
geometryFactory
=
new
GeometryFactory
(
new
PrecisionModel
(),
srid
);
Geometry
g
=
new
WKTReader
(
geometryFactory
).
read
(
s
);
return
get
(
g
);
return
get
(
new
WKTReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
srid
)).
read
(
s
));
}
catch
(
ParseException
ex
)
{
throw
DbException
.
convert
(
ex
);
}
...
...
@@ -154,31 +164,11 @@ public class ValueGeometry extends Value {
public
Geometry
getGeometryNoCopy
()
{
if
(
geometry
==
null
)
{
try
{
int
srid
=
0
;
getSRID:
if
(
bytes
.
length
>=
9
)
{
boolean
bigEndian
;
switch
(
bytes
[
0
])
{
case
0
:
bigEndian
=
true
;
break
;
case
1
:
bigEndian
=
false
;
break
;
default
:
break
getSRID
;
}
if
((
bytes
[
bigEndian
?
1
:
4
]
&
0x20
)
!=
0
)
{
srid
=
Bits
.
readInt
(
bytes
,
5
);
if
(!
bigEndian
)
{
srid
=
Integer
.
reverseBytes
(
srid
);
}
}
}
/*
* 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
);
geometry
=
new
WKBReader
(
new
GeometryFactory
(
new
PrecisionModel
(),
getSRID
()
)).
read
(
bytes
);
}
catch
(
ParseException
ex
)
{
throw
DbException
.
convert
(
ex
);
}
...
...
@@ -186,6 +176,35 @@ public class ValueGeometry extends Value {
return
geometry
;
}
/**
* Return the SRID (Spatial Reference Identifier).
*
* @return spatial reference identifier
*/
public
int
getSRID
()
{
if
(
bytes
.
length
>=
9
)
{
boolean
bigEndian
;
switch
(
bytes
[
0
])
{
case
0
:
bigEndian
=
true
;
break
;
case
1
:
bigEndian
=
false
;
break
;
default
:
return
0
;
}
if
((
bytes
[
bigEndian
?
1
:
4
]
&
0x20
)
!=
0
)
{
int
srid
=
Bits
.
readInt
(
bytes
,
5
);
if
(!
bigEndian
)
{
srid
=
Integer
.
reverseBytes
(
srid
);
}
return
srid
;
}
}
return
0
;
}
/**
* Test if this geometry envelope intersects with the other geometry
* envelope.
...
...
@@ -219,9 +238,7 @@ public class ValueGeometry extends Value {
@Override
public
String
getSQL
()
{
// WKT does not hold Z or SRID with JTS 1.13. As getSQL is used to
// export database, it should contains all object attributes. Moreover
// using bytes is faster than converting WKB to Geometry then to WKT.
// Using bytes is faster than converting EWKB to Geometry then EWKT.
return
"X'"
+
StringUtils
.
convertBytesToHex
(
getBytesNoCopy
())
+
"'::Geometry"
;
}
...
...
@@ -233,7 +250,7 @@ public class ValueGeometry extends Value {
@Override
public
String
getString
()
{
return
getWKT
();
return
get
E
WKT
();
}
@Override
...
...
@@ -253,12 +270,12 @@ public class ValueGeometry extends Value {
@Override
public
byte
[]
getBytes
()
{
return
Utils
.
cloneByteArray
(
getWKB
());
return
Utils
.
cloneByteArray
(
get
E
WKB
());
}
@Override
public
byte
[]
getBytesNoCopy
()
{
return
getWKB
();
return
get
E
WKB
();
}
@Override
...
...
@@ -269,12 +286,12 @@ public class ValueGeometry extends Value {
@Override
public
int
getDisplaySize
()
{
return
getWKT
().
length
();
return
get
E
WKT
().
length
();
}
@Override
public
int
getMemory
()
{
return
getWKB
().
length
*
20
+
24
;
return
get
E
WKB
().
length
*
20
+
24
;
}
@Override
...
...
@@ -282,24 +299,29 @@ public class ValueGeometry extends Value {
// The JTS library only does half-way support for 3D coordinates, so
// their equals method only checks the first two coordinates.
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
()
{
return
new
WKTWriter
(
3
).
write
(
getGeometryNoCopy
());
public
String
getEWKT
()
{
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
;
}
...
...
h2/src/test/org/h2/test/db/TestSpatial.java
浏览文件 @
4aeb5900
...
...
@@ -597,8 +597,9 @@ public class TestSpatial extends TestBase {
* Test serialization of Z and SRID values.
*/
private
void
testWKB
()
{
ValueGeometry
geom3d
=
ValueGeometry
.
get
(
"POLYGON ((67 13 6, 67 18 5, 59 18 4, 59 13 6, 67 13 6))"
,
27572
);
String
ewkt
=
"SRID=27572;POLYGON ((67 13 6, 67 18 5, 59 18 4, 59 13 6, 67 13 6))"
;
ValueGeometry
geom3d
=
ValueGeometry
.
get
(
ewkt
);
assertEquals
(
ewkt
,
geom3d
.
getString
());
ValueGeometry
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
assertEquals
(
6
,
copy
.
getGeometry
().
getCoordinates
()[
0
].
z
);
assertEquals
(
5
,
copy
.
getGeometry
().
getCoordinates
()[
1
].
z
);
...
...
@@ -606,6 +607,7 @@ public class TestSpatial extends TestBase {
// Test SRID
copy
=
ValueGeometry
.
get
(
geom3d
.
getBytes
());
assertEquals
(
27572
,
copy
.
getGeometry
().
getSRID
());
Point
point
=
new
GeometryFactory
().
createPoint
((
new
Coordinate
(
1.1d
,
1.2d
)));
// SRID 0
checkSRID
(
ValueGeometry
.
getFromGeometry
(
point
).
getBytes
(),
0
);
...
...
@@ -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
.
LITTLE_ENDIAN
,
false
).
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
point
.
setSRID
(
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
.
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
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论