Skip to content

Commit

Permalink
Add text nodes data to metadata in network area diagram SVG (powsybl#632
Browse files Browse the repository at this point in the history
)


Signed-off-by: massimo.ferraro <[email protected]>
  • Loading branch information
massimo-ferraro authored Jul 29, 2024
1 parent 0109f9c commit c10dbd4
Show file tree
Hide file tree
Showing 53 changed files with 673 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,13 @@ private void addMetadata(Graph graph, XMLStreamWriter writer) throws XMLStreamEx
getPrefixedId(graph.getBusGraphNode1(edge).getDiagramId()),
getPrefixedId(graph.getBusGraphNode2(edge).getDiagramId()),
edge.getType()));
graph.getVoltageLevelTextPairs().forEach(textPair -> metadata.addTextNode(getPrefixedId(textPair.getSecond().getDiagramId()),
textPair.getFirst().getEquipmentId(),
getPrefixedId(textPair.getFirst().getDiagramId()),
getFormattedValue(textPair.getSecond().getX() - textPair.getFirst().getX()),
getFormattedValue(textPair.getSecond().getY() - svgParameters.getDetailedTextNodeYShift() - textPair.getFirst().getY()),
getFormattedValue(textPair.getSecond().getX() - textPair.getFirst().getX()),
getFormattedValue(textPair.getSecond().getY() - textPair.getFirst().getY())));
metadata.addSvgParameters(String.valueOf(svgParameters.isInsertNameDesc()), String.valueOf(svgParameters.isSvgWidthAndHeightAdded()),
svgParameters.getCssLocation().name(), svgParameters.getSizeConstraint().name(),
String.valueOf(svgParameters.getFixedWidth()), String.valueOf(svgParameters.getFixedHeight()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public class DiagramMetadata {
private static final String METADATA_BUS_NODES_ELEMENT_NAME = "busNodes";
private static final String METADATA_NODES_ELEMENT_NAME = "nodes";
private static final String METADATA_EDGES_ELEMENT_NAME = "edges";
private static final String METADATA_TEXT_NODES_ELEMENT_NAME = "textNodes";
private static final String METADATA_SVG_PARAMETERS_ELEMENT_NAME = "svgParameters";

private final List<BusNodeMetadata> busNodesMetadata = new ArrayList<>();
private final List<NodeMetadata> nodesMetadata = new ArrayList<>();
private final List<EdgeMetadata> edgesMetadata = new ArrayList<>();
private final List<TextNodeMetadata> textNodesMetadata = new ArrayList<>();
private SvgParametersMetadata svgParametersMetadata;

public static DiagramMetadata readFromSvg(InputStream inputStream) throws XMLStreamException {
Expand All @@ -51,6 +53,7 @@ public static DiagramMetadata readFromSvg(XMLStreamReader reader) throws XMLStre
case METADATA_BUS_NODES_ELEMENT_NAME -> readCollection(metadata.busNodesMetadata, new BusNodeMetadata.Reader(), reader);
case METADATA_NODES_ELEMENT_NAME -> readCollection(metadata.nodesMetadata, new NodeMetadata.Reader(), reader);
case METADATA_EDGES_ELEMENT_NAME -> readCollection(metadata.edgesMetadata, new EdgeMetadata.Reader(), reader);
case METADATA_TEXT_NODES_ELEMENT_NAME -> readCollection(metadata.textNodesMetadata, new TextNodeMetadata.Reader(), reader);
case METADATA_SVG_PARAMETERS_ELEMENT_NAME -> metadata.svgParametersMetadata = new SvgParametersMetadata.Reader().read(reader);
default -> throw new PowsyblException("Unexpected element '" + token + "' in metadata");
}
Expand All @@ -77,6 +80,7 @@ public void writeXml(XMLStreamWriter writer) throws XMLStreamException {
writeCollection(busNodesMetadata, METADATA_BUS_NODES_ELEMENT_NAME, writer);
writeCollection(nodesMetadata, METADATA_NODES_ELEMENT_NAME, writer);
writeCollection(edgesMetadata, METADATA_EDGES_ELEMENT_NAME, writer);
writeCollection(textNodesMetadata, METADATA_TEXT_NODES_ELEMENT_NAME, writer);
if (svgParametersMetadata != null) {
svgParametersMetadata.write(writer);
} else {
Expand Down Expand Up @@ -112,6 +116,10 @@ public void addEdge(String svgId, String equipmentId, String node1SvgId, String
edgesMetadata.add(new EdgeMetadata(svgId, equipmentId, node1SvgId, node2SvgId, busNode1SvgId, busNode2SvgId, edgeType));
}

public void addTextNode(String svgId, String equipmentId, String vlNodeId, String positionShiftX, String positionShiftY, String connectionShiftX, String connectionShiftY) {
textNodesMetadata.add(new TextNodeMetadata(svgId, equipmentId, vlNodeId, positionShiftX, positionShiftY, connectionShiftX, connectionShiftY));
}

public void addSvgParameters(String insertNameDesc, String svgWidthAndHeightAdded, String cssLocation, String sizeConstraint, String fixedWidth,
String fixedHeight, String fixedScale, String arrowShift, String arrowLabelShift, String converterStationWidth,
String voltageLevelCircleRadius, String fictitiousVoltageLevelCircleRadius, String transformerCircleRadius,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.nad.svg.metadata;

import com.powsybl.commons.exceptions.UncheckedXmlStreamException;
import com.powsybl.commons.xml.XmlUtil;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
* @author Massimo Ferraro {@literal <massimo.ferraro at soft.it>}
*/

public class TextNodeMetadata extends AbstractMetadataItem {
private static final String ELEMENT_NAME = "textNode";
private static final String VL_NODE_ATTRIBUTE = "vlNode";
private static final String POSITION_SHIFT_X_ATTRIBUTE = "shiftX";
private static final String POSITION_SHIFT_Y_ATTRIBUTE = "shiftY";
private static final String CONNECTION_SHIFT_X_ATTRIBUTE = "connectionShiftX";
private static final String CONNECTION_SHIFT_Y_ATTRIBUTE = "connectionShiftY";

private final String vlNodeId;
private final String positionShiftX;
private final String positionShiftY;
private final String connectionShiftX;
private final String connectionShiftY;

public TextNodeMetadata(String svgId, String equipmentId, String vlNodeId, String positionShiftX, String positionShiftY,
String connectionShiftX, String connectionShiftY) {
super(svgId, equipmentId);
this.vlNodeId = vlNodeId;
this.positionShiftX = positionShiftX;
this.positionShiftY = positionShiftY;
this.connectionShiftX = connectionShiftX;
this.connectionShiftY = connectionShiftY;
}

@Override
String getElementName() {
return ELEMENT_NAME;
}

@Override
void write(XMLStreamWriter writer) throws XMLStreamException {
super.write(writer);
writer.writeAttribute(VL_NODE_ATTRIBUTE, vlNodeId);
writer.writeAttribute(POSITION_SHIFT_X_ATTRIBUTE, positionShiftX);
writer.writeAttribute(POSITION_SHIFT_Y_ATTRIBUTE, positionShiftY);
writer.writeAttribute(CONNECTION_SHIFT_X_ATTRIBUTE, connectionShiftX);
writer.writeAttribute(CONNECTION_SHIFT_Y_ATTRIBUTE, connectionShiftY);
}

static class Reader implements AbstractMetadataItem.MetadataItemReader<TextNodeMetadata> {
@Override
public String getElementName() {
return ELEMENT_NAME;
}

public TextNodeMetadata read(XMLStreamReader reader) {
try {
String diagramId = readDiagramId(reader);
String equipmentId = readEquipmentId(reader);
String vlNodeId = reader.getAttributeValue(null, VL_NODE_ATTRIBUTE);
String positionShiftX = reader.getAttributeValue(null, POSITION_SHIFT_X_ATTRIBUTE);
String positionShiftY = reader.getAttributeValue(null, POSITION_SHIFT_Y_ATTRIBUTE);
String connectionShiftX = reader.getAttributeValue(null, CONNECTION_SHIFT_X_ATTRIBUTE);
String connectionShiftY = reader.getAttributeValue(null, CONNECTION_SHIFT_Y_ATTRIBUTE);
XmlUtil.readEndElementOrThrow(reader);
return new TextNodeMetadata(diagramId, equipmentId, vlNodeId, positionShiftX, positionShiftY, connectionShiftX, connectionShiftY);
} catch (XMLStreamException e) {
throw new UncheckedXmlStreamException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void testInvalid() throws XMLStreamException {
<nad:busNodes/>
<nad:nodes/>
<nad:edges/>
<nad:textNodes/>
<nad:svgParameters/>
</nad:nad>
</metadata>""";
Expand Down
5 changes: 5 additions & 0 deletions network-area-diagram/src/test/resources/3wt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions network-area-diagram/src/test/resources/3wt_disconnected.svg
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.
3 changes: 3 additions & 0 deletions network-area-diagram/src/test/resources/3wt_partial.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c10dbd4

Please sign in to comment.