-
Notifications
You must be signed in to change notification settings - Fork 176
/
PostgisGeometryCodec.java
128 lines (102 loc) · 3.96 KB
/
PostgisGeometryCodec.java
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
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.r2dbc.postgresql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.r2dbc.postgresql.client.EncodedParameter;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.ByteBufUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import org.locationtech.jts.io.WKTWriter;
import reactor.core.publisher.Mono;
import javax.annotation.Nullable;
import java.util.Collections;
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
/**
* PostGIS codec using {@link WKBReader} and {@link WKTWriter}.
*/
final class PostgisGeometryCodec implements Codec<Geometry>, CodecMetadata {
private static final Class<Geometry> TYPE = Geometry.class;
private final GeometryFactory geometryFactory = new GeometryFactory();
private final int oid;
/**
* Create a new {@link PostgisGeometryCodec}.
*/
PostgisGeometryCodec(int oid) {
this.oid = oid;
}
@Override
public boolean canDecode(int dataType, Format format, Class<?> type) {
Assert.requireNonNull(format, "format must not be null");
Assert.requireNonNull(type, "type must not be null");
// Object = Geometry or Geometry = type (Geometry subtype)
return dataType == this.oid && (type.isAssignableFrom(TYPE) || TYPE.isAssignableFrom(type));
}
@Override
public boolean canEncode(Object value) {
Assert.requireNonNull(value, "value must not be null");
return TYPE.isInstance(value);
}
@Override
public boolean canEncodeNull(Class<?> type) {
Assert.requireNonNull(type, "type must not be null");
return TYPE.isAssignableFrom(type);
}
@Override
public Geometry decode(@Nullable ByteBuf buffer, int dataType, Format format, Class<? extends Geometry> type) {
if (buffer == null) {
return null;
}
Assert.isTrue(format == FORMAT_TEXT, "format must be FORMAT_TEXT");
try {
return new WKBReader(this.geometryFactory).read(WKBReader.hexToBytes(ByteBufUtils.decode(buffer)));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public EncodedParameter encode(Object value) {
Assert.requireType(value, Geometry.class, "value must be Geometry type");
Geometry geometry = (Geometry) value;
WKBWriter writer = new WKBWriter(2, true);
return new EncodedParameter(FORMAT_BINARY, this.oid, Mono.fromSupplier(
() -> Unpooled.wrappedBuffer(writer.write(geometry))
));
}
@Override
public EncodedParameter encode(Object value, int dataType) {
return encode(value);
}
@Override
public EncodedParameter encodeNull() {
return new EncodedParameter(FORMAT_BINARY, this.oid, NULL_VALUE);
}
@Override
public Class<?> type() {
return TYPE;
}
@Override
public Iterable<PostgresTypeIdentifier> getDataTypes() {
return Collections.singleton(AbstractCodec.getDataType(this.oid));
}
}