Skip to content

Commit

Permalink
Bug fix: region and subregion are set from country and geotag (not ra…
Browse files Browse the repository at this point in the history
…ndomly) (#2048)

Signed-off-by: VEDELAGO MIORA <miora.ralambotiana@rte-france.com>
  • Loading branch information
miovd authored Mar 30, 2022
1 parent 71691c6 commit e1de32b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +50,9 @@ public class CgmesExportContext {

private final Map<Double, BaseVoltageMapping.BaseVoltageSource> baseVoltageByNominalVoltageMapping = new HashMap<>();

private final BiMap<String, String> regionsIdsByRegionName = HashBiMap.create();
private final BiMap<String, String> subRegionsIdsBySubRegionName = HashBiMap.create();

public static final class ModelDescription {

private String description;
Expand Down Expand Up @@ -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"));
}
}
}
Expand Down Expand Up @@ -597,4 +613,16 @@ public CgmesIidmMapping.CgmesTopologicalNode getUnmappedTopologicalNode(String t
public BaseVoltageMapping.BaseVoltageSource getBaseVoltageByNominalVoltage(double nominalV) {
return baseVoltageByNominalVoltageMapping.get(nominalV);
}

public Collection<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String, String> geographicalRegionIds = new HashMap<>();
BiMap<String, String> 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<String> 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> country = substation.getCountry();
if (country.isPresent()) {
geoName = country.get().toString();
}
return geoName;
}

private static String writeSubGeographicalRegion(String subGeographicalRegionId, String geoName, Substation substation, Map<String, String> 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);
}
Expand Down

0 comments on commit e1de32b

Please sign in to comment.