use of org.openlca.util.Pair in project olca-app by GreenDelta.
the class ResultMap method update.
void update(Object selection, List<Contribution<Location>> contributions) {
if (map == null)
return;
if (layer != null) {
map.removeLayer(layer);
}
if (contributions == null || contributions.isEmpty()) {
coll = null;
map.update();
return;
}
coll = new FeatureCollection();
List<Pair<Location, Feature>> pairs = new ArrayList<>();
for (Contribution<Location> c : contributions) {
Location loc = c.item;
if (loc == null || loc.geodata == null)
continue;
FeatureCollection fc = GeoJSON.unpack(loc.geodata);
if (fc == null || fc.features.isEmpty())
continue;
Geometry g = fc.features.get(0).geometry;
if (g == null)
continue;
Feature feature = new Feature();
feature.geometry = g;
feature.properties = new HashMap<>();
feature.properties.put("result", c.amount);
addMetaData(loc, feature, selection);
pairs.add(Pair.of(loc, feature));
}
if (pairs.isEmpty())
return;
pairs.stream().sorted((p1, p2) -> {
return Double.compare(bsize(p2), bsize(p1));
}).forEach(p -> coll.features.add(p.second));
layer = map.addLayer(coll).fillScale("result").center();
map.update();
}
use of org.openlca.util.Pair in project olca-app by GreenDelta.
the class ExchangeEditDialog method createFormContent.
@Override
protected void createFormContent(IManagedForm mform) {
var tk = mform.getToolkit();
var body = UI.formBody(mform.getForm(), tk);
UI.gridLayout(body, 2);
text = UI.formText(body, tk, M.Amount);
text.setText(Double.toString(exchange.amount));
combo = UI.formCombo(body, tk, M.Unit);
// fill the combo items
// returns true if the given pair contains the reference unit
Function<Pair<FlowPropertyFactor, Unit>, Boolean> isRef = pair -> {
var prop = pair.first.flowProperty;
var refProp = exchange.flow.referenceFlowProperty;
if (!Objects.equals(prop, refProp))
return false;
var unit = pair.second;
var refUnit = prop.unitGroup.referenceUnit;
return Objects.equals(unit, refUnit);
};
var items = new String[units.size()];
int selection = -1;
for (int i = 0; i < units.size(); i++) {
var pair = units.get(i);
items[i] = Labels.name(pair.first.flowProperty) + " - " + Labels.name(pair.second);
// check if pair matches exchange unit
if (Objects.equals(pair.first, exchange.flowPropertyFactor) && Objects.equals(pair.second, exchange.unit)) {
selection = i;
continue;
}
// check if pair is reference unit
if (selection < 0 && isRef.apply(pair)) {
selection = i;
}
}
combo.setItems(items);
if (selection >= 0) {
combo.select(selection);
}
}
use of org.openlca.util.Pair in project olca-app by GreenDelta.
the class GeoFactorCalculator method calcParamVals.
/**
* Calculates the parameter values for the given locations from the respective
* intersections with the given feature collection and the aggregation function
* that is defined in the respective parameter.
*/
private Map<Location, List<Pair<GeoProperty, Double>>> calcParamVals(FeatureCollection coll) {
IntersectionCalculator calc = IntersectionCalculator.on(coll);
Map<Location, List<Pair<Feature, Double>>> map = locations.parallelStream().map(loc -> Pair.of(loc, calcIntersections(loc, calc))).collect(Collectors.toMap(p -> p.first, p -> p.second));
Map<Location, List<Pair<GeoProperty, Double>>> locParams = new HashMap<>();
map.forEach((loc, pairs) -> {
List<Pair<GeoProperty, Double>> paramVals = new ArrayList<>();
locParams.put(loc, paramVals);
for (GeoProperty param : setup.properties) {
if (pairs.isEmpty()) {
paramVals.add(Pair.of(param, null));
continue;
}
List<Double> vals = new ArrayList<>();
List<Double> shares = new ArrayList<>();
for (Pair<Feature, Double> pair : pairs) {
Feature f = pair.first;
Double share = pair.second;
if (f.properties == null)
continue;
Object valObj = f.properties.get(param.name);
if (!(valObj instanceof Number))
continue;
vals.add(((Number) valObj).doubleValue());
shares.add(share);
}
Double aggVal = aggregate(param, vals, shares);
paramVals.add(Pair.of(param, aggVal));
}
});
return locParams;
}
use of org.openlca.util.Pair in project olca-modules by GreenDelta.
the class IntersectionCalculator method jts.
/**
* Calculates the intersection geometries based on JTS geometries and
* returns the non-empty intersections.
*/
private Stream<Pair<Feature, org.locationtech.jts.geom.Geometry>> jts(Geometry g) {
if (g == null)
return Stream.empty();
org.locationtech.jts.geom.Geometry jts;
if (projection == null) {
jts = JTS.fromGeoJSON(g);
} else {
Geometry clone = g.copy();
projection.project(clone);
jts = JTS.fromGeoJSON(clone);
}
if (jts == null)
return Stream.empty();
return IntStream.range(0, features.length).parallel().mapToObj(i -> Pair.of(features[i], geometries[i].intersection(jts))).filter(p -> p.second != null && !p.second.isEmpty());
}
use of org.openlca.util.Pair in project olca-modules by GreenDelta.
the class IntersectionTest method testLineIntersection.
@Test
public void testLineIntersection() {
LineString a = new LineString();
a.points.add(new Point(0, 0));
a.points.add(new Point(10, 10));
LineString b = new LineString();
b.points.add(new Point(0, 10));
b.points.add(new Point(10, 0));
IntersectionCalculator calc = IntersectionCalculator.on(FeatureCollection.of(a));
List<Pair<Feature, Geometry>> r = calc.calculate(b);
Assert.assertEquals(1, r.size());
Geometry g = r.get(0).second;
Assert.assertTrue(g instanceof Point);
Point p = (Point) g;
Assert.assertEquals(5.0, p.x, 1e-16);
Assert.assertEquals(5.0, p.y, 1e-16);
}
Aggregations