-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,791 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
java/src/main/java/com/powsybl/dataframe/network/extensions/LinePositionDataframeAdder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.dataframe.network.extensions; | ||
|
||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.dataframe.SeriesMetadata; | ||
import com.powsybl.dataframe.network.adders.AbstractSimpleAdder; | ||
import com.powsybl.dataframe.update.DoubleSeries; | ||
import com.powsybl.dataframe.update.IntSeries; | ||
import com.powsybl.dataframe.update.StringSeries; | ||
import com.powsybl.dataframe.update.UpdatingDataframe; | ||
import com.powsybl.iidm.network.Line; | ||
import com.powsybl.iidm.network.Network; | ||
import com.powsybl.iidm.network.extensions.Coordinate; | ||
import com.powsybl.iidm.network.extensions.LinePositionAdder; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com> | ||
*/ | ||
public class LinePositionDataframeAdder extends AbstractSimpleAdder { | ||
|
||
private static final List<SeriesMetadata> METADATA = List.of( | ||
SeriesMetadata.stringIndex("id"), | ||
SeriesMetadata.intIndex("num"), | ||
SeriesMetadata.doubles("latitude"), | ||
SeriesMetadata.doubles("longitude") | ||
); | ||
|
||
@Override | ||
public List<List<SeriesMetadata>> getMetadata() { | ||
return Collections.singletonList(METADATA); | ||
} | ||
|
||
private static Map<String, List<Coordinate>> readCoordinates(UpdatingDataframe dataframe) { | ||
StringSeries idCol = dataframe.getStrings("id"); | ||
IntSeries numCol = dataframe.getInts("num"); | ||
DoubleSeries latitudeCol = dataframe.getDoubles("latitude"); | ||
DoubleSeries longitudeCol = dataframe.getDoubles("longitude"); | ||
Map<String, List<Coordinate>> coordinatesByLineId = new HashMap<>(); | ||
if (numCol != null && latitudeCol != null && longitudeCol != null) { | ||
for (int row = 0; row < dataframe.getRowCount(); row++) { | ||
String id = idCol.get(row); | ||
int num = numCol.get(row); | ||
double latitude = latitudeCol.get(row); | ||
double longitude = longitudeCol.get(row); | ||
List<Coordinate> coordinates = coordinatesByLineId.computeIfAbsent(id, k -> new ArrayList<>()); | ||
// ensure list can store coordinate at num index | ||
if (num > coordinates.size()) { | ||
for (int i = 0; i < num - coordinates.size(); i++) { | ||
coordinates.add(null); | ||
} | ||
} | ||
coordinates.set(num, new Coordinate(latitude, longitude)); | ||
} | ||
} | ||
return coordinatesByLineId; | ||
} | ||
|
||
@Override | ||
public void addElements(Network network, UpdatingDataframe dataframe) { | ||
Map<String, List<Coordinate>> coordinatesByLineId = readCoordinates(dataframe); | ||
for (var e : coordinatesByLineId.entrySet()) { | ||
String id = e.getKey(); | ||
List<Coordinate> coordinates = e.getValue(); | ||
Line l = network.getLine(id); | ||
if (l == null) { | ||
throw new PowsyblException("Line '" + id + "' not found"); | ||
} | ||
// check there is no hole in the coordinate list | ||
for (int num = 0; num < coordinates.size(); num++) { | ||
Coordinate coordinate = coordinates.get(num); | ||
if (coordinate == null) { | ||
throw new PowsyblException("Missing coordinate at " + num + " for line '" + id + "'"); | ||
} | ||
} | ||
l.newExtension(LinePositionAdder.class) | ||
.withCoordinates(coordinates) | ||
.add(); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...src/main/java/com/powsybl/dataframe/network/extensions/LinePositionDataframeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.dataframe.network.extensions; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.dataframe.BaseDataframeMapperBuilder; | ||
import com.powsybl.dataframe.network.ExtensionInformation; | ||
import com.powsybl.dataframe.network.NetworkDataframeMapper; | ||
import com.powsybl.dataframe.network.NetworkDataframeMapperBuilder; | ||
import com.powsybl.dataframe.network.adders.NetworkElementAdder; | ||
import com.powsybl.dataframe.update.UpdatingDataframe; | ||
import com.powsybl.iidm.network.Identifiable; | ||
import com.powsybl.iidm.network.Line; | ||
import com.powsybl.iidm.network.Network; | ||
import com.powsybl.iidm.network.extensions.Coordinate; | ||
import com.powsybl.iidm.network.extensions.LinePosition; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com> | ||
*/ | ||
@AutoService(NetworkExtensionDataframeProvider.class) | ||
public class LinePositionDataframeProvider extends AbstractSingleDataframeNetworkExtension { | ||
|
||
@Override | ||
public String getExtensionName() { | ||
return LinePosition.NAME; | ||
} | ||
|
||
@Override | ||
public ExtensionInformation getExtensionInformation() { | ||
return new ExtensionInformation(LinePosition.NAME, | ||
"Provides information about the line geographical coordinates", | ||
"index : id (str), num (int), latitude (float), longitude (float)"); | ||
} | ||
|
||
record LineCoordinate(LinePosition linePosition, Integer num) { | ||
} | ||
|
||
private Stream<LineCoordinate> itemsStream(Network network) { | ||
return network.getLineStream() | ||
.map(g -> (LinePosition) g.getExtension(LinePosition.class)) | ||
.filter(Objects::nonNull) | ||
.flatMap(lp -> IntStream.range(0, lp.getCoordinates().size() - 1).mapToObj(num -> new LineCoordinate(lp, num))); | ||
} | ||
|
||
private static class LineCoordinateGetter implements BaseDataframeMapperBuilder.ItemGetter<Network, LineCoordinate> { | ||
|
||
@Override | ||
public LineCoordinate getItem(Network network, UpdatingDataframe updatingDataframe, int row) { | ||
String id = updatingDataframe.getStringValue("id", row).orElseThrow(); | ||
Line l = network.getLine(id); | ||
if (l == null) { | ||
throw new PowsyblException("Line '" + id + "' not found"); | ||
} | ||
LinePosition lp = l.getExtension(LinePosition.class); | ||
if (lp == null) { | ||
throw new PowsyblException("Line '" + id + "' has no LinePosition extension"); | ||
} | ||
int num = updatingDataframe.getIntValue("id", row).orElseThrow(); | ||
return new LineCoordinate(lp, num); | ||
} | ||
} | ||
|
||
@Override | ||
public NetworkDataframeMapper createMapper() { | ||
return NetworkDataframeMapperBuilder.ofStream(this::itemsStream, new LineCoordinateGetter()) | ||
.stringsIndex("id", lc -> ((Identifiable<?>) lc.linePosition().getExtendable()).getId()) | ||
.intsIndex("num", lc -> lc.num) | ||
.doubles("latitude", lc -> ((Coordinate) lc.linePosition.getCoordinates().get(lc.num)).getLatitude()) | ||
.doubles("longitude", lc -> ((Coordinate) lc.linePosition.getCoordinates().get(lc.num)).getLongitude()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void removeExtensions(Network network, List<String> ids) { | ||
ids.stream().filter(Objects::nonNull) | ||
.map(network::getIdentifiable) | ||
.filter(Objects::nonNull) | ||
.forEach(g -> g.removeExtension(LinePosition.class)); | ||
} | ||
|
||
@Override | ||
public NetworkElementAdder createAdder() { | ||
return new LinePositionDataframeAdder(); | ||
} | ||
} |
Oops, something went wrong.