Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CGMES GL] Add SubstationPosition extension when location on voltage levels #3159

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ public void importGLData() {

private void importSubstationsPosition() {
LOG.info("Importing substations position");
SubstationPositionImporter positionImporter = new SubstationPositionImporter(network);
cgmesGLModel.getSubstationsPosition().forEach(positionImporter::importPosition);
new SubstationPositionImporter(network, cgmesGLModel).importPositions();
}

private void importLinesPosition() {
LOG.info("Importing lines position");
new LinePositionImporter(network, cgmesGLModel).importPosition();
new LinePositionImporter(network, cgmesGLModel).importPositions();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class CgmesGLModel {

public static final String SUBSTASTION_POSITION_QUERY_KEY = "substationPosition";
public static final String SUBSTATION_VL_POSITION_QUERY_KEY = "substationVoltageLevelPosition";
public static final String LINE_POSITION_QUERY_KEY = "linePosition";

private static final Logger LOG = LoggerFactory.getLogger(CgmesGLModel.class);
Expand All @@ -49,9 +49,9 @@ private PropertyBags queryTripleStore(String queryKey) {
return tripleStore.query(query);
}

public PropertyBags getSubstationsPosition() {
LOG.info("Querying triple store for substations positions");
return queryTripleStore(SUBSTASTION_POSITION_QUERY_KEY);
public PropertyBags getSubstationVoltageLevelPositions() {
LOG.info("Querying triple store for substation and voltage level positions");
return queryTripleStore(SUBSTATION_VL_POSITION_QUERY_KEY);
}

public PropertyBags getLinesPositions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public LinePositionImporter(Network network, CgmesGLModel cgmesGLModel) {
this.cgmesGLModel = Objects.requireNonNull(cgmesGLModel);
}

public void importPosition() {
public void importPositions() {
Map<Identifiable<?>, SortedMap<Integer, Coordinate>> lineOrDanglingLineCoordinates = new HashMap<>();

cgmesGLModel.getLinesPositions().forEach(propertyBag -> importPosition(propertyBag, lineOrDanglingLineCoordinates));
cgmesGLModel.getLinesPositions().forEach(propertyBag -> importPositions(propertyBag, lineOrDanglingLineCoordinates));

lineOrDanglingLineCoordinates.forEach((lOrDl, coordinates) ->
lOrDl.newExtension(LinePositionAdder.class).withCoordinates(coordinates.values().stream().toList()).add());
}

private void importPosition(PropertyBag linePositionData, Map<Identifiable<?>, SortedMap<Integer, Coordinate>> lineOrDanglingLineCoordinates) {
private void importPositions(PropertyBag linePositionData, Map<Identifiable<?>, SortedMap<Integer, Coordinate>> lineOrDanglingLineCoordinates) {
Objects.requireNonNull(linePositionData);
String crsUrn = linePositionData.getId("crsUrn");
if (!CgmesGLUtils.checkCoordinateSystem(crsUrn)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Substation;
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.extensions.Coordinate;
import com.powsybl.iidm.network.extensions.SubstationPosition;
import com.powsybl.iidm.network.extensions.SubstationPositionAdder;
import com.powsybl.triplestore.api.PropertyBag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.*;

/**
*
Expand All @@ -28,26 +30,51 @@ public class SubstationPositionImporter {

private final Network network;

public SubstationPositionImporter(Network network) {
private final CgmesGLModel cgmesGLModel;

public SubstationPositionImporter(Network network, CgmesGLModel cgmesGLModel) {
this.network = Objects.requireNonNull(network);
this.cgmesGLModel = Objects.requireNonNull(cgmesGLModel);
}

public void importPosition(PropertyBag substationPositionData) {
Objects.requireNonNull(substationPositionData);
String crsUrn = substationPositionData.getId("crsUrn");
public void importPositions() {
Map<Substation, List<Coordinate>> vlCoordinates = new HashMap<>();

cgmesGLModel.getSubstationVoltageLevelPositions().forEach(propertyBag -> importPositions(propertyBag, vlCoordinates));

vlCoordinates.forEach((substation, coordinates) -> {
// only calculating the average position if there's no position on the corresponding substation
if (substation.getExtension(SubstationPosition.class) == null) {
double latG = coordinates.stream().mapToDouble(Coordinate::getLatitude).average().orElse(0);
double longG = coordinates.stream().mapToDouble(Coordinate::getLongitude).average().orElse(0);
substation.newExtension(SubstationPositionAdder.class).withCoordinate(new Coordinate(latG, longG)).add();
}
});
}

public void importPositions(PropertyBag psrPositionData, Map<Substation, List<Coordinate>> vlCoordinates) {
Objects.requireNonNull(psrPositionData);
String crsUrn = psrPositionData.getId("crsUrn");
if (!CgmesGLUtils.checkCoordinateSystem(crsUrn)) {
throw new PowsyblException("Unsupported coordinates system: " + crsUrn);
}
String substationId = substationPositionData.getId("powerSystemResource");
Substation substation = network.getSubstation(substationId);
// Coordinate system EPSG::4326 is WGS84 with y <=> lat, x <=> lon
Coordinate coordinate = new Coordinate(psrPositionData.asDouble("y"), psrPositionData.asDouble("x"));

String psrId = psrPositionData.getId("powerSystemResource");
Substation substation = network.getSubstation(psrId);
if (substation != null) {
// y <=> lat, x <=> lon
substation.newExtension(SubstationPositionAdder.class).withCoordinate(
new Coordinate(substationPositionData.asDouble("y"), substationPositionData.asDouble("x"))
).add();
// the extension is added right away
substation.newExtension(SubstationPositionAdder.class).withCoordinate(coordinate).add();
} else {
LOG.warn("Cannot find substation {}, name {} in network {}: skipping substation position", substationId, substationPositionData.get("name"), network.getId());
VoltageLevel vl = network.getVoltageLevel(psrId);
if (vl != null) {
// we need to collect all the positions of all voltage levels for the corresponding substation before adding the extension
vl.getSubstation().ifPresent(s -> vlCoordinates.computeIfAbsent(s, k -> new ArrayList<>()).add(coordinate));
} else {
String name = psrPositionData.get("name");
LOG.warn("Cannot find substation/voltage level {}, name {} in network {}: skipping substation position", psrId, name, network.getId());
}
}
}

}
7 changes: 4 additions & 3 deletions cgmes/cgmes-gl/src/main/resources/CGMES-GL.sparql
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# query: substationPosition
# query: substationVoltageLevelPosition
SELECT ?powerSystemResource ?name ?crsName ?crsUrn ?x ?y ?seq
{
WHERE {
?location
a cim:Location ;
cim:Location.PowerSystemResources ?powerSystemResource ;
cim:Location.CoordinateSystem ?coordinateSystem .
?powerSystemResource
a cim:Substation ;
a ?psrType ;
cim:IdentifiedObject.name ?name .
VALUES ?psrType { cim:Substation cim:VoltageLevel }
?coordinateSystem
a cim:CoordinateSystem ;
cim:CoordinateSystem.crsUrn ?crsUrn .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void test() {
assertEquals(49.224448, substation2Position.getCoordinate().getLatitude(), 0);
assertEquals(-2.136596, substation2Position.getCoordinate().getLongitude(), 0);

Substation substation3 = network.getSubstation("Substation3");
SubstationPosition substation3Position = substation3.getExtension(SubstationPosition.class);
assertEquals(49.2092905, substation3Position.getCoordinate().getLatitude(), 1e-11);
assertEquals(-2.19941, substation3Position.getCoordinate().getLongitude(), 0);

Line line = network.getLine("ACLine");
LinePosition<Line> linePosition = line.getExtension(LinePosition.class);
assertEquals(48.7123755, linePosition.getCoordinates().get(0).getLatitude(), 0);
Expand Down
19 changes: 17 additions & 2 deletions cgmes/cgmes-gl/src/test/resources/importGL_EQ.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<cim:Substation rdf:ID="_Substation2">
<cim:Substation.Region rdf:resource="#_Region"/>
<cim:IdentifiedObject.name>Substation 2</cim:IdentifiedObject.name>
</cim:Substation>
</cim:Substation>
<cim:Substation rdf:ID="_Substation3">
<cim:Substation.Region rdf:resource="#_Region"/>
<cim:IdentifiedObject.name>Substation 3</cim:IdentifiedObject.name>
</cim:Substation>
<cim:VoltageLevel rdf:ID="_VoltageLevel1">
<cim:VoltageLevel.BaseVoltage rdf:resource="#_BaseVoltage300"/>
<cim:VoltageLevel.Substation rdf:resource="#_Substation1"/>
Expand All @@ -33,13 +37,24 @@
<cim:VoltageLevel rdf:ID="_VoltageLevel2">
<cim:VoltageLevel.BaseVoltage rdf:resource="#_BaseVoltage300"/>
<cim:VoltageLevel.Substation rdf:resource="#_Substation2"/>
<cim:IdentifiedObject.name>Voltage level 1</cim:IdentifiedObject.name>
<cim:IdentifiedObject.name>Voltage level 2</cim:IdentifiedObject.name>
</cim:VoltageLevel>
<cim:VoltageLevel rdf:ID="_VoltageLevel3">
<cim:VoltageLevel.BaseVoltage rdf:resource="#_BaseVoltage300"/>
<cim:VoltageLevel.Substation rdf:resource="#_Substation3"/>
<cim:IdentifiedObject.name>Voltage level 3</cim:IdentifiedObject.name>
</cim:VoltageLevel>
<cim:VoltageLevel rdf:ID="_VoltageLevel4">
<cim:VoltageLevel.BaseVoltage rdf:resource="#_BaseVoltage300"/>
<cim:VoltageLevel.Substation rdf:resource="#_Substation3"/>
<cim:IdentifiedObject.name>Voltage level 4</cim:IdentifiedObject.name>
</cim:VoltageLevel>
<cim:BaseVoltage rdf:ID="_BaseVoltage300">
<cim:BaseVoltage.nominalVoltage>300</cim:BaseVoltage.nominalVoltage>
</cim:BaseVoltage>
<cim:Line rdf:ID="_Line">
<cim:Line.Region rdf:resource="#_Region"/>
<cim:IdentifiedObject.name>A line</cim:IdentifiedObject.name>
</cim:Line>
<cim:SubGeographicalRegion rdf:ID="_Region">
<cim:SubGeographicalRegion.Region rdf:resource="#_Subregion"/>
Expand Down
36 changes: 36 additions & 0 deletions cgmes/cgmes-gl/src/test/resources/importGL_GL.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_ACLine" />
</cim:Location>
<cim:Location rdf:ID="_locationLineDismissed">
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_Line" />
</cim:Location>
<cim:Location rdf:ID="_location1">
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_Substation1" />
Expand All @@ -20,6 +24,18 @@
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_Substation2" />
</cim:Location>
<cim:Location rdf:ID="_location3">
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_VoltageLevel3" />
</cim:Location>
<cim:Location rdf:ID="_location4">
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_VoltageLevel4" />
</cim:Location>
<cim:Location rdf:ID="_location_dismissed">
<cim:Location.CoordinateSystem rdf:resource="#_wgs84" />
<cim:Location.PowerSystemResources rdf:resource="#_VoltageLevel1" />
</cim:Location>
<cim:PositionPoint rdf:ID="_positionLine0">
<cim:PositionPoint.Location rdf:resource="#_locationLine" />
<cim:PositionPoint.sequenceNumber>0</cim:PositionPoint.sequenceNumber>
Expand Down Expand Up @@ -54,4 +70,24 @@
<cim:PositionPoint.xPosition>-2.136596</cim:PositionPoint.xPosition>
<cim:PositionPoint.yPosition>49.224448</cim:PositionPoint.yPosition>
</cim:PositionPoint>
<cim:PositionPoint rdf:ID="_positionSubstation3a">
<cim:PositionPoint.Location rdf:resource="#_location3" />
<cim:PositionPoint.xPosition>-2.198073</cim:PositionPoint.xPosition>
<cim:PositionPoint.yPosition>49.209432</cim:PositionPoint.yPosition>
</cim:PositionPoint>
<cim:PositionPoint rdf:ID="_positionSubstation3b">
<cim:PositionPoint.Location rdf:resource="#_location4" />
<cim:PositionPoint.xPosition>-2.200747</cim:PositionPoint.xPosition>
<cim:PositionPoint.yPosition>49.209149</cim:PositionPoint.yPosition>
</cim:PositionPoint>
<cim:PositionPoint rdf:ID="_positionVoltageLevel1">
<cim:PositionPoint.Location rdf:resource="#_location_dismissed" />
<cim:PositionPoint.xPosition>0</cim:PositionPoint.xPosition>
<cim:PositionPoint.yPosition>0</cim:PositionPoint.yPosition>
</cim:PositionPoint>
<cim:PositionPoint rdf:ID="_positionLineDismissed">
<cim:PositionPoint.Location rdf:resource="#_locationLineDismissed" />
<cim:PositionPoint.xPosition>0</cim:PositionPoint.xPosition>
<cim:PositionPoint.yPosition>0</cim:PositionPoint.yPosition>
</cim:PositionPoint>
</rdf:RDF>