-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geographical coordinates to network for NAD drawing and use Geogr…
…aphicalLayout (#102) Signed-off-by: maissa SOUISSI <[email protected]>
- Loading branch information
1 parent
05db252
commit aeff6e7
Showing
13 changed files
with
609 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/main/java/com/powsybl/nad/build/iidm/VoltageLevelFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* 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/. | ||
*/ | ||
package com.powsybl.nad.build.iidm; | ||
|
||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.iidm.network.*; | ||
import com.powsybl.nad.utils.iidm.IidmUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
// FIXME: to be removed at next powsybl-diagram upgrade. | ||
// Copied from https://github.com/powsybl/powsybl-diagram. | ||
|
||
/** | ||
* @author Maissa Souissi {<maissa.souissi at rte-france.com>} | ||
*/ | ||
|
||
public class VoltageLevelFilter implements Predicate<VoltageLevel> { | ||
|
||
protected static final Logger LOGGER = LoggerFactory.getLogger(VoltageLevelFilter.class); | ||
|
||
public static final Predicate<VoltageLevel> NO_FILTER = voltageLevel -> true; | ||
|
||
private static final String UNKNOWN_VOLTAGE_LEVEL = "Unknown voltage level id '"; | ||
|
||
private final Set<VoltageLevel> voltageLevels; | ||
|
||
public VoltageLevelFilter(Set<VoltageLevel> voltageLevels) { | ||
this.voltageLevels = voltageLevels; | ||
} | ||
|
||
public int getNbVoltageLevels() { | ||
return voltageLevels.size(); | ||
} | ||
|
||
public Set<VoltageLevel> getVoltageLevels() { | ||
return voltageLevels; | ||
} | ||
|
||
@Override | ||
public boolean test(VoltageLevel voltageLevel) { | ||
return voltageLevels.contains(voltageLevel); | ||
} | ||
|
||
public static VoltageLevelFilter createVoltageLevelsDepthFilter(Network network, List<String> voltageLevelIds, int depth) { | ||
return createVoltageLevelFilterWithPredicate(network, voltageLevelIds, depth, NO_FILTER); | ||
} | ||
|
||
public static VoltageLevelFilter createVoltageLevelFilterWithPredicate(Network network, List<String> voltageLevelIds, int depth, Predicate<VoltageLevel> voltageLevelPredicate) { | ||
Objects.requireNonNull(network); | ||
Objects.requireNonNull(voltageLevelIds); | ||
Set<VoltageLevel> startingSet = new HashSet<>(); | ||
|
||
for (String voltageLevelId : voltageLevelIds) { | ||
VoltageLevel vl = network.getVoltageLevel(voltageLevelId); | ||
if (vl == null) { | ||
throw new PowsyblException(UNKNOWN_VOLTAGE_LEVEL + voltageLevelId + "'"); | ||
} | ||
if (!voltageLevelPredicate.test(vl)) { | ||
LOGGER.warn("vl '{}' does not comply with the predicate", voltageLevelId); | ||
} | ||
startingSet.add(vl); | ||
} | ||
Set<VoltageLevel> voltageLevels = new HashSet<>(); | ||
VoltageLevelFilter.traverseVoltageLevels(startingSet, depth, voltageLevels, voltageLevelPredicate); | ||
return new VoltageLevelFilter(voltageLevels); | ||
} | ||
|
||
public static Collection<VoltageLevel> getNextDepthVoltageLevels(Network network, List<VoltageLevel> voltageLevels) { | ||
List<String> voltageLevelIds = voltageLevels.stream().map(VoltageLevel::getId).collect(Collectors.toList()); | ||
VoltageLevelFilter voltageLevelFilter = createVoltageLevelsDepthFilter(network, voltageLevelIds, 1); | ||
Set<VoltageLevel> voltageLevelSet = new HashSet<>(voltageLevelFilter.getVoltageLevels()); | ||
voltageLevels.forEach(voltageLevelSet::remove); | ||
return voltageLevelSet; | ||
} | ||
|
||
private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels, Predicate<VoltageLevel> predicate) { | ||
if (depth < 0) { | ||
return; | ||
} | ||
Set<VoltageLevel> nextDepthVoltageLevels = new HashSet<>(); | ||
for (VoltageLevel vl : voltageLevelsDepth) { | ||
if (!visitedVoltageLevels.contains(vl)) { | ||
visitedVoltageLevels.add(vl); | ||
vl.visitEquipments(new VlVisitor(nextDepthVoltageLevels, visitedVoltageLevels, predicate)); | ||
} | ||
} | ||
traverseVoltageLevels(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels, predicate); | ||
} | ||
|
||
private static class VlVisitor extends DefaultTopologyVisitor { | ||
private final Set<VoltageLevel> nextDepthVoltageLevels; | ||
private final Set<VoltageLevel> visitedVoltageLevels; | ||
private final Predicate<VoltageLevel> voltageLevelPredicate; | ||
|
||
public VlVisitor(Set<VoltageLevel> nextDepthVoltageLevels, Set<VoltageLevel> visitedVoltageLevels, Predicate<VoltageLevel> voltageLevelPredicate) { | ||
this.nextDepthVoltageLevels = nextDepthVoltageLevels; | ||
this.visitedVoltageLevels = visitedVoltageLevels; | ||
this.voltageLevelPredicate = voltageLevelPredicate; | ||
} | ||
|
||
@Override | ||
public void visitLine(Line line, TwoSides side) { | ||
visitBranch(line, side); | ||
} | ||
|
||
@Override | ||
public void visitTwoWindingsTransformer(TwoWindingsTransformer twt, TwoSides side) { | ||
visitBranch(twt, side); | ||
} | ||
|
||
@Override | ||
public void visitThreeWindingsTransformer(ThreeWindingsTransformer twt, ThreeSides side) { | ||
if (side == ThreeSides.ONE) { | ||
visitTerminal(twt.getTerminal(ThreeSides.TWO)); | ||
visitTerminal(twt.getTerminal(ThreeSides.THREE)); | ||
} else if (side == ThreeSides.TWO) { | ||
visitTerminal(twt.getTerminal(ThreeSides.ONE)); | ||
visitTerminal(twt.getTerminal(ThreeSides.THREE)); | ||
} else { | ||
visitTerminal(twt.getTerminal(ThreeSides.ONE)); | ||
visitTerminal(twt.getTerminal(ThreeSides.TWO)); | ||
} | ||
} | ||
|
||
@Override | ||
public void visitHvdcConverterStation(HvdcConverterStation<?> converterStation) { | ||
converterStation.getOtherConverterStation().ifPresent(c -> visitTerminal(c.getTerminal())); | ||
} | ||
|
||
private void visitBranch(Branch<?> branch, TwoSides side) { | ||
visitTerminal(branch.getTerminal(IidmUtils.getOpposite(side))); | ||
} | ||
|
||
private void visitTerminal(Terminal terminal) { | ||
VoltageLevel voltageLevel = terminal.getVoltageLevel(); | ||
if (!visitedVoltageLevels.contains(voltageLevel) && voltageLevelPredicate.test(voltageLevel)) { | ||
nextDepthVoltageLevels.add(voltageLevel); | ||
} | ||
} | ||
|
||
@Override | ||
public void visitDanglingLine(DanglingLine danglingLine) { | ||
if (danglingLine.isPaired()) { | ||
danglingLine.getTieLine().ifPresent(tieline -> visitBranch(tieline, tieline.getSide(danglingLine.getTerminal()))); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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/. | ||
*/ | ||
|
||
package com.powsybl.sld.server; | ||
|
||
/** | ||
* @author Maissa SOUISSI <maissa.souissi at rte-france.com> | ||
*/ | ||
|
||
import lombok.Setter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class GeoDataService { | ||
|
||
public static final String QUERY_PARAM_SUBSTATION_ID = "substationId"; | ||
public static final String GEO_DATA_API_VERSION = "v1"; | ||
public static final String QUERY_PARAM_VARIANT_ID = "variantId"; | ||
public static final String NETWORK_UUID = "networkUuid"; | ||
static final String SUBSTATIONS = "substations"; | ||
private static final String DELIMITER = "/"; | ||
private final RestTemplate restTemplate; | ||
@Setter | ||
private String geoDataServerBaseUri; | ||
|
||
public GeoDataService(@Value("${gridsuite.services.geo-data-server.base-uri:http://geo-data-server/}") String geoDataServerBaseUri, | ||
RestTemplate restTemplate) { | ||
this.geoDataServerBaseUri = geoDataServerBaseUri; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
private String getGeoDataServerURI() { | ||
return this.geoDataServerBaseUri + DELIMITER + GEO_DATA_API_VERSION + DELIMITER; | ||
} | ||
|
||
public String getSubstationsGraphics(UUID networkUuid, String variantId, List<String> substationsIds) { | ||
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(getGeoDataServerURI() + SUBSTATIONS) | ||
.queryParam(NETWORK_UUID, networkUuid); | ||
|
||
if (!StringUtils.isBlank(variantId)) { | ||
uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId); | ||
} | ||
|
||
if (substationsIds != null) { | ||
uriComponentsBuilder.queryParam(QUERY_PARAM_SUBSTATION_ID, substationsIds); | ||
} | ||
|
||
var path = uriComponentsBuilder | ||
.buildAndExpand() | ||
.toUriString(); | ||
|
||
return restTemplate.getForObject(path, String.class); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/powsybl/sld/server/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* 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/. | ||
*/ | ||
|
||
package com.powsybl.sld.server; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @author Maissa SOUISSI <maissa.souissi at rte-france.com> | ||
*/ | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
final RestTemplate restTemplate = new RestTemplate(); | ||
|
||
//find and replace Jackson message converter with our own | ||
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++) { | ||
final HttpMessageConverter<?> httpMessageConverter = restTemplate.getMessageConverters().get(i); | ||
if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) { | ||
restTemplate.getMessageConverters().set(i, mappingJackson2HttpMessageConverter()); | ||
} | ||
} | ||
|
||
return restTemplate; | ||
} | ||
|
||
private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { | ||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); | ||
converter.setObjectMapper(objectMapper); | ||
return converter; | ||
} | ||
|
||
} |
Oops, something went wrong.