Search in sources :

Example 1 with Pair

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();
}
Also used : Geometry(org.openlca.geo.geojson.Geometry) LayerConfig(org.openlca.app.components.mapview.LayerConfig) M(org.openlca.app.M) MapView(org.openlca.app.components.mapview.MapView) Labels(org.openlca.app.util.Labels) Popup(org.openlca.app.util.Popup) HashMap(java.util.HashMap) Location(org.openlca.core.model.Location) ArrayList(java.util.ArrayList) Bounds(org.openlca.geo.calc.Bounds) Actions(org.openlca.app.util.Actions) Composite(org.eclipse.swt.widgets.Composite) ErrorReporter(org.openlca.app.util.ErrorReporter) Map(java.util.Map) Icon(org.openlca.app.rcp.images.Icon) Feature(org.openlca.geo.geojson.Feature) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) FillLayout(org.eclipse.swt.layout.FillLayout) Section(org.eclipse.ui.forms.widgets.Section) Geometry(org.openlca.geo.geojson.Geometry) GeoJSON(org.openlca.geo.geojson.GeoJSON) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) Pair(org.openlca.util.Pair) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) MsgBox(org.openlca.app.util.MsgBox) List(java.util.List) UI(org.openlca.app.util.UI) Contribution(org.openlca.core.results.Contribution) FileChooser(org.openlca.app.components.FileChooser) CostResultDescriptor(org.openlca.app.util.CostResultDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) ArrayList(java.util.ArrayList) Feature(org.openlca.geo.geojson.Feature) Pair(org.openlca.util.Pair) Location(org.openlca.core.model.Location)

Example 2 with Pair

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);
    }
}
Also used : M(org.openlca.app.M) Text(org.eclipse.swt.widgets.Text) Labels(org.openlca.app.util.Labels) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) Pair(org.openlca.util.Pair) MsgBox(org.openlca.app.util.MsgBox) IManagedForm(org.eclipse.ui.forms.IManagedForm) Unit(org.openlca.core.model.Unit) Function(java.util.function.Function) Point(org.eclipse.swt.graphics.Point) ArrayList(java.util.ArrayList) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Objects(java.util.Objects) List(java.util.List) Strings(org.openlca.util.Strings) FormDialog(org.eclipse.ui.forms.FormDialog) UI(org.openlca.app.util.UI) Exchange(org.openlca.core.model.Exchange) Collections(java.util.Collections) Point(org.eclipse.swt.graphics.Point) Pair(org.openlca.util.Pair)

Example 3 with Pair

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;
}
Also used : GeoJSON(org.openlca.geo.geojson.GeoJSON) Logger(org.slf4j.Logger) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) TLongByteHashMap(gnu.trove.map.hash.TLongByteHashMap) Pair(org.openlca.util.Pair) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Location(org.openlca.core.model.Location) ArrayList(java.util.ArrayList) ImpactFactor(org.openlca.core.model.ImpactFactor) List(java.util.List) DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) ImpactCategory(org.openlca.core.model.ImpactCategory) Database(org.openlca.app.db.Database) IntersectionCalculator(org.openlca.geo.calc.IntersectionCalculator) IDatabase(org.openlca.core.database.IDatabase) FormulaInterpreter(org.openlca.expressions.FormulaInterpreter) Map(java.util.Map) TLongHashSet(gnu.trove.set.hash.TLongHashSet) Feature(org.openlca.geo.geojson.Feature) Collections(java.util.Collections) TLongByteHashMap(gnu.trove.map.hash.TLongByteHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Feature(org.openlca.geo.geojson.Feature) IntersectionCalculator(org.openlca.geo.calc.IntersectionCalculator) ArrayList(java.util.ArrayList) List(java.util.List) Location(org.openlca.core.model.Location) Pair(org.openlca.util.Pair)

Example 4 with Pair

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());
}
Also used : Geometry(org.openlca.geo.geojson.Geometry) IntStream(java.util.stream.IntStream) Geometry(org.openlca.geo.geojson.Geometry) List(java.util.List) Stream(java.util.stream.Stream) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) Pair(org.openlca.util.Pair) Feature(org.openlca.geo.geojson.Feature) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList)

Example 5 with Pair

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);
}
Also used : Geometry(org.openlca.geo.geojson.Geometry) LineString(org.openlca.geo.geojson.LineString) Point(org.openlca.geo.geojson.Point) Pair(org.openlca.util.Pair) Test(org.junit.Test)

Aggregations

Pair (org.openlca.util.Pair)8 ArrayList (java.util.ArrayList)5 List (java.util.List)4 Feature (org.openlca.geo.geojson.Feature)4 FeatureCollection (org.openlca.geo.geojson.FeatureCollection)4 Location (org.openlca.core.model.Location)3 TLongByteHashMap (gnu.trove.map.hash.TLongByteHashMap)2 TLongHashSet (gnu.trove.set.hash.TLongHashSet)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 M (org.openlca.app.M)2 Labels (org.openlca.app.util.Labels)2 MsgBox (org.openlca.app.util.MsgBox)2 UI (org.openlca.app.util.UI)2 GeoJSON (org.openlca.geo.geojson.GeoJSON)2 Geometry (org.openlca.geo.geojson.Geometry)2 LineString (org.openlca.geo.geojson.LineString)2