-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvIdCoordinateSource.java
183 lines (158 loc) · 6.55 KB
/
CsvIdCoordinateSource.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
/*
* © 2021. 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.csv;
import edu.ie3.datamodel.io.factory.FactoryData;
import edu.ie3.datamodel.io.factory.SimpleFactoryData;
import edu.ie3.datamodel.io.factory.timeseries.IdCoordinateFactory;
import edu.ie3.datamodel.io.naming.FileNamingStrategy;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.datamodel.utils.options.Try;
import edu.ie3.util.geo.CoordinateDistance;
import edu.ie3.util.geo.GeoUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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;
import tech.units.indriya.quantity.Quantities;
import tech.units.indriya.unit.Units;
/**
* Implementation of {@link IdCoordinateSource} to read the mapping between coordinate id and actual
* coordinate from csv file and build a mapping from it.
*/
public class CsvIdCoordinateSource extends CsvDataSource implements IdCoordinateSource {
private final IdCoordinateFactory factory;
/** Mapping in both ways (id -> coordinate) and (coordinate -> id) have to be unique */
private final Map<Integer, Point> idToCoordinate;
private final Map<Point, Integer> coordinateToId;
public CsvIdCoordinateSource(
String csvSep,
String folderPath,
FileNamingStrategy fileNamingStrategy,
IdCoordinateFactory factory) {
super(csvSep, folderPath, fileNamingStrategy);
this.factory = factory;
/* set up the coordinate id to lat/long mapping */
idToCoordinate = setupIdToCoordinateMap();
coordinateToId = invert(idToCoordinate);
}
/**
* Read in and process the mapping
*
* @return Mapping from coordinate id to coordinate
*/
private Map<Integer, Point> setupIdToCoordinateMap() {
return buildStreamWithFieldsToAttributesMap()
.map(mapWithRowIndex -> new SimpleFactoryData(mapWithRowIndex, Pair.class))
.map(factory::get)
.map(Try::get)
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}
/**
* Inverts the mapping, so that former values map to keys
*
* @param map Mapping in the "right" direction
* @param <V> Type of values
* @param <K> Type of keys
* @return Mapping in the "left" direction (Bad joke, I know...)
*/
private <V, K> Map<V, K> invert(Map<K, V> map) {
Map<V, K> inv = new HashMap<>();
for (Map.Entry<K, V> entry : map.entrySet()) inv.put(entry.getValue(), entry.getKey());
return inv;
}
@Override
public Optional<Point> getCoordinate(int id) {
return Optional.ofNullable(idToCoordinate.get(id));
}
@Override
public Collection<Point> getCoordinates(int... ids) {
return Arrays.stream(ids)
.mapToObj(this::getCoordinate)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}
@Override
public Optional<Integer> getId(Point coordinate) {
return Optional.ofNullable(coordinateToId.get(coordinate));
}
@Override
public Collection<Point> getAllCoordinates() {
return coordinateToId.keySet();
}
@Override
public List<CoordinateDistance> getNearestCoordinates(Point coordinate, int n) {
Set<Point> points = coordinateToId.keySet();
if (idToCoordinate.size() > n) {
ArrayList<Point> foundPoints = new ArrayList<>();
ComparableQuantity<Length> distance = Quantities.getQuantity(10000, Units.METRE);
// extends the search radius until n points are found
while (foundPoints.size() < n) {
foundPoints.clear();
distance = distance.multiply(2);
Envelope envelope = GeoUtils.calculateBoundingBox(coordinate, distance);
points.stream()
.filter(point -> envelope.contains(point.getCoordinate()))
.forEach(foundPoints::add);
}
// replaces all point with smaller size of found points
points.clear();
points.addAll(foundPoints);
}
return calculateCoordinateDistances(coordinate, n, points);
}
@Override
public List<CoordinateDistance> getClosestCoordinates(
Point coordinate, int n, ComparableQuantity<Length> distance) {
Set<Point> points = coordinateToId.keySet();
Envelope envelope = GeoUtils.calculateBoundingBox(coordinate, distance);
Set<Point> reducedPoints =
points.stream()
.filter(point -> envelope.contains(point.getCoordinate()))
.collect(Collectors.toSet());
return calculateCoordinateDistances(coordinate, n, reducedPoints);
}
public int getCoordinateCount() {
return idToCoordinate.keySet().size();
}
/**
* Build a stream with mappings from field identifiers to attributes
*
* @return Stream with mappings from field identifiers to attributes
*/
protected Stream<FactoryData.MapWithRowIndex> buildStreamWithFieldsToAttributesMap() {
try (BufferedReader reader = connector.initIdCoordinateReader()) {
final String[] headline = parseCsvRow(reader.readLine(), csvSep);
// by default try-with-resources closes the reader directly when we leave this method (which
// is wanted to avoid a lock on the file), but this causes a closing of the stream as well.
// As we still want to consume the data at other places, we start a new stream instead of
// returning the original one
Collection<FactoryData.MapWithRowIndex> allRows = csvRowFieldValueMapping(reader, headline);
Function<FactoryData.MapWithRowIndex, String> idExtractor =
mapWithRowIndex -> mapWithRowIndex.fieldsToAttribute().get(factory.getIdField());
Set<FactoryData.MapWithRowIndex> withDistinctCoordinateId =
distinctRowsWithLog(allRows, idExtractor, "coordinate id mapping", "coordinate id");
Function<FactoryData.MapWithRowIndex, String> coordinateExtractor =
mapWithRowIndex ->
mapWithRowIndex
.fieldsToAttribute()
.get(factory.getLatField())
.concat(mapWithRowIndex.fieldsToAttribute().get(factory.getLonField()));
return distinctRowsWithLog(
withDistinctCoordinateId, coordinateExtractor, "coordinate id mapping", "coordinate")
.parallelStream();
} catch (IOException e) {
log.error("Cannot read the file for coordinate id to coordinate mapping.", e);
}
return Stream.empty();
}
}