-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlIdCoordinateSource.java
274 lines (236 loc) · 9.44 KB
/
SqlIdCoordinateSource.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* © 2022. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.source.sql;
import static edu.ie3.datamodel.io.source.sql.SqlDataSource.createBaseQueryString;
import edu.ie3.datamodel.io.connectors.SqlConnector;
import edu.ie3.datamodel.io.factory.SimpleFactoryData;
import edu.ie3.datamodel.io.factory.timeseries.SqlIdCoordinateFactory;
import edu.ie3.datamodel.io.naming.DatabaseNamingStrategy;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.datamodel.models.value.CoordinateValue;
import edu.ie3.util.geo.CoordinateDistance;
import edu.ie3.util.geo.GeoUtils;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import javax.measure.quantity.Length;
import org.apache.commons.lang3.tuple.Pair;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Point;
import tech.units.indriya.ComparableQuantity;
/** SQL source for coordinate data */
public class SqlIdCoordinateSource implements IdCoordinateSource {
private static final String WHERE = " WHERE ";
/**
* Queries that are available within this source. Motivation to have them as field value is to
* avoid creating a new string each time, bc they're always the same.
*/
private final String basicQuery;
private final String queryForPoint;
private final String queryForPoints;
private final String queryForId;
private final String queryForBoundingBox;
private final String queryForNearestPoints;
private final SqlDataSource dataSource;
private final SqlIdCoordinateFactory factory;
public SqlIdCoordinateSource(
SqlIdCoordinateFactory factory, String coordinateTableName, SqlDataSource dataSource)
throws SQLException {
this.factory = factory;
this.dataSource = dataSource;
String dbIdColumnName = dataSource.getDbColumnName(factory.getIdField(), coordinateTableName);
String dbPointColumnName =
dataSource.getDbColumnName(factory.getCoordinateField(), coordinateTableName);
// validating table
dataSource.validateDBTable(coordinateTableName, Pair.class, factory);
// setup queries
this.basicQuery = createBaseQueryString(dataSource.schemaName, coordinateTableName);
this.queryForPoint = createQueryForPoint(dbIdColumnName);
this.queryForPoints = createQueryForPoints(dbIdColumnName);
this.queryForId = createQueryForId(dbPointColumnName);
this.queryForBoundingBox = createQueryForBoundingBox(dbPointColumnName);
this.queryForNearestPoints =
createQueryForNearestPoints(
dataSource.schemaName, coordinateTableName, dbIdColumnName, dbPointColumnName);
}
/**
* Initializes a new SqlIdCoordinateSource
*
* @param connector the connector needed for the database connection
* @param schemaName the database schema to use
* @param coordinateTableName the name of the table containing coordinate data
* @param factory instance of a coordinate factory
*/
public SqlIdCoordinateSource(
SqlConnector connector,
String schemaName,
String coordinateTableName,
SqlIdCoordinateFactory factory)
throws SQLException {
this(
factory,
coordinateTableName,
new SqlDataSource(connector, schemaName, new DatabaseNamingStrategy()));
}
@Override
public Optional<Point> getCoordinate(int id) {
List<CoordinateValue> values = executeQueryToList(queryForPoint, ps -> ps.setInt(1, id));
if (values.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(values.get(0).coordinate);
}
}
@Override
public Collection<Point> getCoordinates(int... ids) {
Object[] idSet = Arrays.stream(ids).boxed().distinct().toArray();
List<CoordinateValue> values =
executeQueryToList(
queryForPoints,
ps -> {
Array sqlArray = ps.getConnection().createArrayOf("int", idSet);
ps.setArray(1, sqlArray);
});
return values.stream().map(value -> value.coordinate).toList();
}
@Override
public Optional<Integer> getId(Point coordinate) {
double latitude = coordinate.getY();
double longitude = coordinate.getX();
List<CoordinateValue> values =
executeQueryToList(
queryForId,
ps -> {
ps.setDouble(1, longitude);
ps.setDouble(2, latitude);
});
if (values.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(values.get(0).id);
}
}
@Override
public Collection<Point> getAllCoordinates() {
List<CoordinateValue> values = executeQueryToList(basicQuery + ";", PreparedStatement::execute);
return values.stream().map(value -> value.coordinate).toList();
}
@Override
public List<CoordinateDistance> getNearestCoordinates(Point coordinate, int n) {
List<CoordinateValue> values =
executeQueryToList(
queryForNearestPoints,
ps -> {
ps.setDouble(1, coordinate.getX());
ps.setDouble(2, coordinate.getY());
ps.setInt(3, n);
});
List<Point> points = values.stream().map(value -> value.coordinate).toList();
return calculateCoordinateDistances(coordinate, n, points);
}
@Override
public List<CoordinateDistance> getClosestCoordinates(
Point coordinate, int n, ComparableQuantity<Length> distance) {
Envelope envelope = GeoUtils.calculateBoundingBox(coordinate, distance);
List<CoordinateValue> values =
executeQueryToList(
queryForBoundingBox,
ps -> {
ps.setDouble(1, envelope.getMinX());
ps.setDouble(2, envelope.getMinY());
ps.setDouble(3, envelope.getMaxX());
ps.setDouble(4, envelope.getMaxY());
});
List<Point> points = values.stream().map(value -> value.coordinate).toList();
return calculateCoordinateDistances(coordinate, n, points);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private CoordinateValue createCoordinateValue(Map<String, String> fieldToValues) {
fieldToValues.remove("distance");
SimpleFactoryData simpleFactoryData = new SimpleFactoryData(fieldToValues, Pair.class);
Pair<Integer, Point> pair = factory.get(simpleFactoryData).getOrThrow();
return new CoordinateValue(pair.getKey(), pair.getValue());
}
private List<CoordinateValue> executeQueryToList(
String query, SqlDataSource.AddParams addParams) {
return dataSource.executeQuery(query, addParams).map(this::createCoordinateValue).toList();
}
/**
* Creates a basic query to retrieve entries for given ids with the following pattern: <br>
* {@code <base query> WHERE <id column>=?;}
*
* @param idColumn the name of the column holding the id info
* @return the query string
*/
private String createQueryForPoint(String idColumn) {
return basicQuery + WHERE + idColumn + " =?; ";
}
/**
* Creates a basic query to retrieve entries for given ids with the following pattern: <br>
* {@code <base query> WHERE <id column>= ANY (?);}
*
* @param idColumn the name of the column holding the id info
* @return the query string
*/
private String createQueryForPoints(String idColumn) {
return basicQuery + WHERE + idColumn + " = ANY (?); ";
}
/**
* Creates a basic query to retrieve an id for a given point with the following pattern: <br>
* {@code <base query> WHERE <point column> = ST_Point( ?, ?);}
*
* @param pointColumn the name of the column holding the geometry information
* @return the query string
*/
private String createQueryForId(String pointColumn) {
return basicQuery + WHERE + pointColumn + " = ST_Point( ?, ?); ";
}
/**
* Creates a basic query to retrieve all entries in a given box. The box is defines by a latitude
* interval and a longitude interval. The intervals are provided via an envelope. The pattern
* looks like this: <br>
* {@code <base query> WHERE ST_Intersects(ST_MakeEnvelope(?, ?, ?, ?, 4326 ) , <point column> )
* ;}
*
* @param pointColumn the name of the column holding the geometry information
* @return the query string
*/
private String createQueryForBoundingBox(String pointColumn) {
return basicQuery
+ WHERE
+ " ST_Intersects(ST_MakeEnvelope(?, ?, ?, ?, 4326 ) , "
+ pointColumn
+ ");";
}
/**
* Creates a query to retrieve the nearest n entries. The pattern looks like this: <br>
* {@code SELECT <id column> AS id, <coordinate column> AS coordinate, <coordinate column> <->
* ST_Point( ?, ?) AS distance FROM <schema>.<table> ORDER BY distance LIMIT ?;}
*
* @param schemaName the name of the database schema
* @param tableName the name of the database table
* @param idColumn the name of the column holding the id information
* @param pointColumn the name of the column holding the geometry information
* @return the query string
*/
private String createQueryForNearestPoints(
String schemaName, String tableName, String idColumn, String pointColumn) {
return "SELECT "
+ idColumn
+ " AS id , "
+ pointColumn
+ " AS coordinate, "
+ pointColumn
+ " <-> ST_Point( ?, ?) AS distance "
+ "FROM "
+ schemaName
+ ".\""
+ tableName
+ "\""
+ " ORDER BY distance LIMIT ?;";
}
}