-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlIdCoordinateSource.java
231 lines (195 loc) · 7.75 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
/*
* © 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 edu.ie3.datamodel.io.connectors.SqlConnector;
import edu.ie3.datamodel.io.factory.SimpleFactoryData;
import edu.ie3.datamodel.io.factory.timeseries.IdCoordinateFactory;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.datamodel.models.value.CoordinateValue;
import edu.ie3.util.geo.CoordinateDistance;
import java.sql.Array;
import java.util.*;
import org.apache.commons.lang3.tuple.Pair;
import org.locationtech.jts.geom.Point;
/** SQL source for coordinate data */
public class SqlIdCoordinateSource extends SqlDataSource<CoordinateValue>
implements IdCoordinateSource {
private static final String WHERE = " WHERE ";
private final IdCoordinateFactory factory;
private final double maxDistance;
private static final double earthRadius = 6378137.0;
/**
* 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 queryForId;
private final String queryForBoundingBox;
/**
* 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
* @param maxDistance maximal search distance for points
*/
public SqlIdCoordinateSource(
SqlConnector connector,
String schemaName,
String coordinateTableName,
IdCoordinateFactory factory,
double maxDistance) {
super(connector);
this.factory = factory;
this.maxDistance = maxDistance;
String dbIdColumnName = getDbColumnName(factory.getIdField(), coordinateTableName);
String dbLatitudeColumnName = getDbColumnName(factory.getLatField(), coordinateTableName);
String dbLongitudeColumnName = getDbColumnName(factory.getLonField(), coordinateTableName);
// setup queries
this.basicQuery = createBaseQueryString(schemaName, coordinateTableName) + " ?;";
this.queryForPoint = createQueryForPoint(schemaName, coordinateTableName, dbIdColumnName);
this.queryForId =
createQueryForId(
schemaName, coordinateTableName, dbLatitudeColumnName, dbLongitudeColumnName);
this.queryForBoundingBox =
createQueryForBoundingBox(
schemaName, coordinateTableName, dbLatitudeColumnName, dbLongitudeColumnName);
}
@Override
protected Optional<CoordinateValue> createEntity(Map<String, String> fieldToValues) {
SimpleFactoryData factoryData = new SimpleFactoryData(fieldToValues, Pair.class);
Optional<Pair<Integer, Point>> option = factory.get(factoryData);
if (option.isEmpty()) return Optional.empty();
Pair<Integer, Point> data = option.get();
return Optional.of(new CoordinateValue(data.getKey(), data.getValue()));
}
@Override
public Optional<Point> getCoordinate(int id) {
List<CoordinateValue> values = executeQuery(queryForPoint, ps -> ps.setInt(1, id));
return Optional.of(values.get(0).coordinate);
}
@Override
public Collection<Point> getCoordinates(int... ids) {
Object[] idSet = Arrays.asList(ids, ids.length).toArray();
List<CoordinateValue> values =
executeQuery(
queryForPoint,
ps -> {
Array sqlArray = ps.getConnection().createArrayOf("integer", idSet);
ps.setArray(1, sqlArray);
});
List<Point> points = new ArrayList<>();
for (CoordinateValue value : values) {
points.add(value.coordinate);
}
return points;
}
@Override
public Optional<Integer> getId(Point coordinate) {
double latitude = coordinate.getY();
double longitude = coordinate.getX();
List<CoordinateValue> values =
executeQuery(
queryForId,
ps -> {
ps.setDouble(1, latitude);
ps.setDouble(2, longitude);
});
return Optional.of(values.get(0).id);
}
@Override
public Collection<Point> getAllCoordinates() {
List<CoordinateValue> values = executeQuery(basicQuery, ps -> ps.setString(1, ";"));
ArrayList<Point> points = new ArrayList<>();
for (CoordinateValue value : values) {
points.add(value.coordinate);
}
return points;
}
@Override
public List<CoordinateDistance> getNearestCoordinates(Point coordinate, int n) {
double[] xyDeltas = calculateXYDelta(coordinate, maxDistance, earthRadius);
double longitude = coordinate.getX();
double latitude = coordinate.getY();
List<CoordinateValue> values =
executeQuery(
queryForBoundingBox,
ps -> {
ps.setDouble(1, longitude - xyDeltas[0]);
ps.setDouble(2, longitude + xyDeltas[0]);
ps.setDouble(3, latitude - xyDeltas[1]);
ps.setDouble(4, latitude + xyDeltas[1]);
});
ArrayList<Point> points = new ArrayList<>();
for (CoordinateValue value : values) {
points.add(value.coordinate);
}
List<CoordinateDistance> distances = getNearestCoordinates(coordinate, 2 * n, points);
return restrictToBoundingBoxWithSetNumberOfCorner(coordinate, distances, n);
}
/**
* Creates a basic query to retrieve an entry for a given id with the following pattern: <br>
* {@code <base query> WHERE <id column>=?;}
*
* @param schemaName the name of the database schema
* @param coordinateTableName the name of the database table
* @param idColumn the name of the column holding the id info
* @return the query string
*/
private String createQueryForPoint(
String schemaName, String coordinateTableName, String idColumn) {
return createBaseQueryString(schemaName, coordinateTableName) + WHERE + idColumn + " =?; ";
}
/**
* Creates a basic query to retrieve an id for a given point with the following pattern: <br>
* {@code <base query> WHERE <latitude column>=? AND <longitude column>=?;}
*
* @param schemaName the name of the database schema
* @param coordinateTableName the name of the database table
* @param latitudeColumnName the name of the latitude column
* @param longitudeColumnName the name of the longitude column
* @return the query string
*/
private String createQueryForId(
String schemaName,
String coordinateTableName,
String latitudeColumnName,
String longitudeColumnName) {
return createBaseQueryString(schemaName, coordinateTableName)
+ WHERE
+ latitudeColumnName
+ " =? AND "
+ longitudeColumnName
+ " =?; ";
}
/**
* 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 pattern looks like this: <br>
* {@code <base query> WHERE <latitude column> BETWEEN ? AND ? AND <longitude column> BETWEEN ?
* AND ?;}
*
* @param schemaName the name of the database
* @param coordinateTableName the name of the database table
* @param longitudeColumnName the name of the second column
* @param latitudeColumnName the name of the first column
* @return the query string
*/
private String createQueryForBoundingBox(
String schemaName,
String coordinateTableName,
String longitudeColumnName,
String latitudeColumnName) {
return createBaseQueryString(schemaName, coordinateTableName)
+ WHERE
+ longitudeColumnName
+ " BETWEEN ? AND ? "
+ " AND "
+ latitudeColumnName
+ " BETWEEN ? AND ?; ";
}
}