Skip to content

Commit

Permalink
Add bus id in SLD legend (#126)
Browse files Browse the repository at this point in the history
Signed-off-by: LE SAULNIER Kevin <kevin.lesaulnier@rte-france.com>
  • Loading branch information
klesaulnier authored Nov 18, 2024
1 parent 252f697 commit f9e6df8
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/powsybl/sld/server/CommonLabelProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.powsybl.sld.server;

import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.sld.layout.LayoutParameters;
import com.powsybl.sld.library.ComponentLibrary;
import com.powsybl.sld.model.graphs.VoltageLevelGraph;
import com.powsybl.sld.svg.BusLegendInfo;
import com.powsybl.sld.svg.DefaultLabelProvider;
import com.powsybl.sld.svg.SvgParameters;

import java.util.List;
import java.util.stream.Collectors;

public class CommonLabelProvider extends DefaultLabelProvider {
public CommonLabelProvider(Network network, ComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters) {
super(network, componentLibrary, layoutParameters, svgParameters);
}

@Override
public List<BusLegendInfo> getBusLegendInfos(VoltageLevelGraph graph) {
VoltageLevel vl = network.getVoltageLevel(graph.getVoltageLevelInfos().getId());
return vl.getBusView().getBusStream()
.map(b -> new BusLegendInfo(b.getId(), List.of(
new BusLegendInfo.Caption(b.getId(), "busId"),
new BusLegendInfo.Caption(valueFormatter.formatVoltage(b.getV(), "kV"), "v"),
new BusLegendInfo.Caption(valueFormatter.formatAngleInDegrees(b.getAngle()), "angle")
)))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import com.powsybl.sld.model.nodes.EquipmentNode;
import com.powsybl.sld.model.nodes.FeederNode;
import com.powsybl.sld.model.nodes.Node;
import com.powsybl.sld.svg.DefaultLabelProvider;
import com.powsybl.sld.svg.LabelPosition;
import com.powsybl.sld.svg.LabelProviderFactory;
import com.powsybl.sld.svg.SvgParameters;
import com.powsybl.sld.svg.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,7 +29,7 @@
/**
* @author Ben Daamer ahmed<ahmed.bendaamer at rte-france.com>
*/
public class PositionDiagramLabelProvider extends DefaultLabelProvider {
public class PositionDiagramLabelProvider extends CommonLabelProvider {

private static final Logger LOGGER = LoggerFactory.getLogger(PositionDiagramLabelProvider.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.powsybl.sld.server.dto.SvgAndMetadata;
import com.powsybl.sld.server.dto.VoltageLevelInfos;
import com.powsybl.sld.server.utils.SldDisplayMode;
import com.powsybl.sld.svg.DefaultLabelProvider;
import com.powsybl.sld.svg.SvgParameters;
import com.powsybl.sld.svg.styles.NominalVoltageStyleProvider;
import com.powsybl.sld.svg.styles.StyleProvidersList;
Expand Down Expand Up @@ -103,7 +102,7 @@ SvgAndMetadata generateSvgAndMetadata(UUID networkUuid, String variantId, String
sldParameters.setLabelProviderFactory(PositionDiagramLabelProvider.newLabelProviderFactory(id));
} else if (diagParams.getSldDisplayMode() == SldDisplayMode.STATE_VARIABLE) {
svgParameters.setBusesLegendAdded(true);
sldParameters.setLabelProviderFactory(DefaultLabelProvider::new);
sldParameters.setLabelProviderFactory(CommonLabelProvider::new);
} else {
throw new PowsyblException(String.format("Given sld display mode %s doesn't exist", diagParams.getSldDisplayMode()));
}
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/com/powsybl/sld/server/SingleLineDiagramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,86 @@ void testSubstationSingleLineDiagramAdditionalMetadata() {
assertEquals("FR", convertedMetadata.get("country"));
}

/**
* @return a network with 2 voltage level - vl1 with 2 calculated buses, vl2 with 1 calculated bus
*/
public static Network createTwoVoltageLevels() {
Network network = Network.create("dl", "test");
Substation s = network.newSubstation().setId("s1").setName("Substation 1").add();
VoltageLevel vl1 = s.newVoltageLevel()
.setId("vl1")
.setName("Voltage level 1")
.setNominalV(400)
.setTopologyKind(TopologyKind.BUS_BREAKER)
.add();
vl1.getBusBreakerView().newBus()
.setId("busId11")
.add();
vl1.newGenerator()
.setId("g11")
.setConnectableBus("busId11")
.setBus("busId11")
.setTargetP(101.3664)
.setTargetV(390)
.setMinP(0)
.setMaxP(150)
.setVoltageRegulatorOn(true)
.add();
vl1.getBusBreakerView().newBus()
.setId("busId12")
.add();
vl1.newGenerator()
.setId("g12")
.setConnectableBus("busId12")
.setBus("busId12")
.setTargetP(101.3664)
.setTargetV(390)
.setMinP(0)
.setMaxP(150)
.setVoltageRegulatorOn(true)
.add();
VoltageLevel vl2 = s.newVoltageLevel()
.setId("vl2")
.setName("Voltage level 2")
.setNominalV(400)
.setTopologyKind(TopologyKind.BUS_BREAKER)
.add();
vl2.getBusBreakerView().newBus()
.setId("busId2")
.add();
vl2.newDanglingLine()
.setId("dl1")
.setConnectableBus("busId2")
.setBus("busId2")
.setR(0.7)
.setX(1)
.setG(1e-6)
.setB(3e-6)
.setP0(101)
.setQ0(150)
.newGeneration()
.setTargetP(0)
.setTargetQ(0)
.setTargetV(390)
.setVoltageRegulationOn(false)
.add()
.add();
network.newLine()
.setId("l1")
.setVoltageLevel1("vl1")
.setBus1("busId11")
.setVoltageLevel2("vl2")
.setBus2("busId2")
.setR(1)
.setX(3)
.setG1(0)
.setG2(0)
.setB1(0)
.setB2(0)
.add();
return network;
}

private static Network createNetwork() {
Network network = Network.create("test", "test");
Substation substationFr1 = network.newSubstation()
Expand Down Expand Up @@ -583,6 +663,31 @@ void testNetworkAreaDiagramWithMissingVoltageLevel() throws Exception {
.andExpect(status().isNotFound());
}

@Test
void testBusLegendContainsBusId() throws Exception {
UUID testNetworkId = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
given(networkStoreService.getNetwork(testNetworkId, null)).willReturn(createTwoVoltageLevels());

MvcResult result = mvc.perform(get("/v1/svg/{networkUuid}/{voltageLevelId}", testNetworkId, "vl1"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(SingleLineDiagramController.IMAGE_SVG_PLUS_XML))
.andReturn();
String stringResult = result.getResponse().getContentAsString();
assertEquals("<?xml", stringResult.substring(0, 5));
// vl1 should have 2 busId displayed in bus legend
assertTrue(stringResult.contains(">vl1_0<"));
assertTrue(stringResult.contains(">vl1_1<"));

result = mvc.perform(get("/v1/svg/{networkUuid}/{voltageLevelId}", testNetworkId, "vl2"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(SingleLineDiagramController.IMAGE_SVG_PLUS_XML))
.andReturn();
stringResult = result.getResponse().getContentAsString();
assertEquals("<?xml", stringResult.substring(0, 5));
// vl1 should have 1 busId displayed in bus legend
assertTrue(stringResult.contains(">vl2_0<"));
}

private static String toString(String resourceName) throws IOException {
return new String(ByteStreams.toByteArray(Objects.requireNonNull(SingleLineDiagramTest.class.getResourceAsStream(resourceName))), StandardCharsets.UTF_8);
}
Expand Down

0 comments on commit f9e6df8

Please sign in to comment.