diff --git a/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/CgmesExportContext.java b/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/CgmesExportContext.java index ee6dcecc717..2be01030d02 100644 --- a/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/CgmesExportContext.java +++ b/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/CgmesExportContext.java @@ -6,6 +6,8 @@ */ package com.powsybl.cgmes.conversion.export; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import com.powsybl.cgmes.conversion.Conversion; import com.powsybl.cgmes.extensions.*; import com.powsybl.cgmes.model.CgmesNames; @@ -48,6 +50,9 @@ public class CgmesExportContext { private final Map baseVoltageByNominalVoltageMapping = new HashMap<>(); + private final BiMap regionsIdsByRegionName = HashBiMap.create(); + private final BiMap subRegionsIdsBySubRegionName = HashBiMap.create(); + public static final class ModelDescription { private String description; @@ -189,13 +194,24 @@ public void addIidmMappings(Network network, boolean withTopologicalMapping) { private void addIidmMappingsSubstations(Network network) { for (Substation substation : network.getSubstations()) { + String country = substation.getCountry().map(Country::name).orElseGet(network::getNameOrId); if (!substation.hasProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId")) { - String regionId = CgmesExportUtil.getUniqueId(); - substation.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId", regionId); + String id = regionsIdsByRegionName.computeIfAbsent(country, k -> CgmesExportUtil.getUniqueId()); + substation.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId", id); + } else { + regionsIdsByRegionName.computeIfAbsent(country, k -> substation.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId")); + } + String geoTag; + if (substation.getGeographicalTags().size() == 1) { + geoTag = substation.getGeographicalTags().iterator().next(); + } else { + geoTag = country; } if (!substation.hasProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "subRegionId")) { - String subRegionId = CgmesExportUtil.getUniqueId(); - substation.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "subRegionId", subRegionId); + String id = subRegionsIdsBySubRegionName.computeIfAbsent(geoTag, k -> CgmesExportUtil.getUniqueId()); + substation.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "subRegionId", id); + } else { + subRegionsIdsBySubRegionName.computeIfAbsent(geoTag, k -> substation.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "subRegionId")); } } } @@ -597,4 +613,16 @@ public CgmesIidmMapping.CgmesTopologicalNode getUnmappedTopologicalNode(String t public BaseVoltageMapping.BaseVoltageSource getBaseVoltageByNominalVoltage(double nominalV) { return baseVoltageByNominalVoltageMapping.get(nominalV); } + + public Collection getRegionsIds() { + return Collections.unmodifiableSet(regionsIdsByRegionName.values()); + } + + public String getRegionName(String regionId) { + return regionsIdsByRegionName.inverse().get(regionId); + } + + public String getSubRegionName(String subRegionId) { + return subRegionsIdsBySubRegionName.inverse().get(subRegionId); + } } diff --git a/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/EquipmentExport.java b/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/EquipmentExport.java index 8981b834f18..b8de1126d80 100644 --- a/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/EquipmentExport.java +++ b/cgmes/cgmes-conversion/src/main/java/com/powsybl/cgmes/conversion/export/EquipmentExport.java @@ -6,8 +6,6 @@ */ package com.powsybl.cgmes.conversion.export; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; import com.powsybl.cgmes.conversion.Conversion; import com.powsybl.cgmes.conversion.export.elements.*; import com.powsybl.cgmes.extensions.*; @@ -50,7 +48,7 @@ public static void write(Network network, XMLStreamWriter writer, CgmesExportCon writeTerminals(network, exportedTerminals, exportedNodes, cimNamespace, writer); writeSwitches(network, cimNamespace, writer); - writeSubstations(network, cimNamespace, writer); + writeSubstations(network, cimNamespace, writer, context); writeVoltageLevels(network, cimNamespace, writer, context); writeBusbarSections(network, cimNamespace, writer, context); writeLoads(network, cimNamespace, writer); @@ -167,47 +165,33 @@ private static String getSwitchNode2Id(VoltageLevel vl, Switch sw) { } } - private static void writeSubstations(Network network, String cimNamespace, XMLStreamWriter writer) throws XMLStreamException { - Map geographicalRegionIds = new HashMap<>(); - BiMap subGeographicalRegionIds = HashBiMap.create(); + private static void writeSubstations(Network network, String cimNamespace, XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException { + for (String geographicalRegionId : context.getRegionsIds()) { + writeGeographicalRegion(geographicalRegionId, context.getRegionName(geographicalRegionId), cimNamespace, writer); + } + List writtenSubRegions = new ArrayList<>(); for (Substation substation : network.getSubstations()) { - String geographicalRegionId = substation.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId"); - String geoName = getGeographicalRegionName(substation, network); - geographicalRegionIds.computeIfAbsent(geoName, name -> writeGeographicalRegion(geographicalRegionId, name, cimNamespace, writer)); String subGeographicalRegionId = substation.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "subRegionId"); - subGeographicalRegionIds.computeIfAbsent(subGeographicalRegionId, id -> writeSubGeographicalRegion(id, geoName, substation, geographicalRegionIds, cimNamespace, writer)); + String geographicalRegionId = substation.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId"); + if (!writtenSubRegions.contains(subGeographicalRegionId)) { + writeSubGeographicalRegion(subGeographicalRegionId, context.getSubRegionName(subGeographicalRegionId), geographicalRegionId, cimNamespace, writer); + writtenSubRegions.add(subGeographicalRegionId); + } SubstationEq.write(substation.getId(), substation.getNameOrId(), subGeographicalRegionId, cimNamespace, writer); } } - private static String writeGeographicalRegion(String geographicalRegionId, String geoName, String cimNamespace, XMLStreamWriter writer) { + private static void writeGeographicalRegion(String geographicalRegionId, String geoName, String cimNamespace, XMLStreamWriter writer) { try { GeographicalRegionEq.write(geographicalRegionId, geoName, cimNamespace, writer); - return geographicalRegionId; } catch (XMLStreamException e) { throw new UncheckedXmlStreamException(e); } } - private static String getGeographicalRegionName(Substation substation, Network network) { - String geoName = network.getNameOrId(); - Optional country = substation.getCountry(); - if (country.isPresent()) { - geoName = country.get().toString(); - } - return geoName; - } - - private static String writeSubGeographicalRegion(String subGeographicalRegionId, String geoName, Substation substation, Map geographicalRegionIds, String cimNamespace, XMLStreamWriter writer) { + private static void writeSubGeographicalRegion(String subGeographicalRegionId, String subGeographicalRegionName, String geographicalRegionId, String cimNamespace, XMLStreamWriter writer) { try { - String subGeoName; - if (substation.getGeographicalTags().size() == 1) { - subGeoName = substation.getGeographicalTags().iterator().next(); - } else { - subGeoName = geoName; - } - SubGeographicalRegionEq.write(subGeographicalRegionId, subGeoName, geographicalRegionIds.get(geoName), cimNamespace, writer); - return subGeoName; + SubGeographicalRegionEq.write(subGeographicalRegionId, subGeographicalRegionName, geographicalRegionId, cimNamespace, writer); } catch (XMLStreamException e) { throw new UncheckedXmlStreamException(e); }