Search in sources :

Example 1 with FeatureCollection

use of org.openlca.geo.geojson.FeatureCollection in project olca-modules by GreenDelta.

the class GeoJsonImport method run.

@Override
public void run() {
    try {
        // parse GeoJSON
        log.trace("parse GeoJSON file {}", file);
        FeatureCollection coll = GeoJSON.read(file);
        if (coll == null || coll.features.isEmpty())
            return;
        // index locations
        LocationDao dao = new LocationDao(db);
        indexLocations(dao);
        // match and update the locations
        for (Feature f : coll.features) {
            if (f.geometry == null || f.properties == null)
                continue;
            Location loc = findMatch(f);
            if (loc == null)
                continue;
            loc.geodata = GeoJSON.pack(FeatureCollection.of(f.geometry));
            dao.update(loc);
        }
    } catch (Exception e) {
        log.error("Failed to import GeoJSON file " + file, e);
    }
}
Also used : FeatureCollection(org.openlca.geo.geojson.FeatureCollection) LocationDao(org.openlca.core.database.LocationDao) Feature(org.openlca.geo.geojson.Feature) Location(org.openlca.core.model.Location)

Example 2 with FeatureCollection

use of org.openlca.geo.geojson.FeatureCollection in project olca-modules by GreenDelta.

the class PrecisionReduction method run.

public static FeatureCollection run(FeatureCollection coll, int decimalPlaces) {
    if (coll == null)
        return new FeatureCollection();
    // set up the reducer
    PrecisionModel pm = new PrecisionModel(Math.pow(10, decimalPlaces));
    GeometryPrecisionReducer reducer = new GeometryPrecisionReducer(pm);
    // run the reduction
    List<Feature> features = coll.features.parallelStream().map(f -> {
        if (f.geometry == null)
            return f.copy();
        Feature mapped = new Feature();
        if (f.properties != null) {
            mapped.properties = new HashMap<>(f.properties);
        }
        if (f.geometry == null)
            return mapped;
        Geometry g = JTS.fromGeoJSON(f.geometry);
        if (g == null)
            return mapped;
        try {
            Geometry reduced = reducer.reduce(g);
            mapped.geometry = JTS.toGeoJSON(reduced);
        } catch (Exception e) {
            LoggerFactory.getLogger(PrecisionReduction.class).error("precision reduction failed for " + g, e);
            mapped.geometry = f.geometry.copy();
        }
        return mapped;
    }).collect(Collectors.toList());
    // build the result
    FeatureCollection r = new FeatureCollection();
    r.features.addAll(features);
    return r;
}
Also used : List(java.util.List) GeometryPrecisionReducer(org.locationtech.jts.precision.GeometryPrecisionReducer) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) LoggerFactory(org.slf4j.LoggerFactory) Geometry(org.locationtech.jts.geom.Geometry) Feature(org.openlca.geo.geojson.Feature) HashMap(java.util.HashMap) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) Collectors(java.util.stream.Collectors) Geometry(org.locationtech.jts.geom.Geometry) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) HashMap(java.util.HashMap) GeometryPrecisionReducer(org.locationtech.jts.precision.GeometryPrecisionReducer) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) Feature(org.openlca.geo.geojson.Feature)

Example 3 with FeatureCollection

use of org.openlca.geo.geojson.FeatureCollection in project olca-modules by GreenDelta.

the class LocationWriter method mapGeometry.

private void mapGeometry(Location location, JsonObject obj) {
    if (location.geodata == null)
        return;
    try {
        FeatureCollection coll = GeoJSON.unpack(location.geodata);
        if (coll == null)
            return;
        Feature f = coll.first();
        if (f == null || f.geometry == null)
            return;
        Json.put(obj, "geometry", f.geometry.toJson());
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("failed to convert KML to GeoJSON", e);
    }
}
Also used : FeatureCollection(org.openlca.geo.geojson.FeatureCollection) Logger(org.slf4j.Logger) Feature(org.openlca.geo.geojson.Feature)

Example 4 with FeatureCollection

use of org.openlca.geo.geojson.FeatureCollection 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 5 with FeatureCollection

use of org.openlca.geo.geojson.FeatureCollection in project olca-app by GreenDelta.

the class MapView method render.

private void render(GC gc) {
    if (projections.size() != layers.size()) {
        initProjection();
    }
    Rectangle canvasSize = canvas.getBounds();
    translation.update(canvasSize, zoom);
    // white background
    gc.setBackground(white);
    gc.fillRectangle(canvasSize);
    if (projections.isEmpty())
        return;
    for (int i = 0; i < projections.size(); i++) {
        LayerConfig config = layers.get(i);
        gc.setForeground(config.getBorderColor());
        FeatureCollection projection = projections.get(i);
        for (Feature f : projection.features) {
            if (!translation.visible(f)) {
                continue;
            }
            render(gc, config, f, f.geometry);
        }
    }
}
Also used : FeatureCollection(org.openlca.geo.geojson.FeatureCollection) Rectangle(org.eclipse.swt.graphics.Rectangle) Feature(org.openlca.geo.geojson.Feature) MultiPoint(org.openlca.geo.geojson.MultiPoint) Point(org.openlca.geo.geojson.Point)

Aggregations

FeatureCollection (org.openlca.geo.geojson.FeatureCollection)14 Feature (org.openlca.geo.geojson.Feature)9 Location (org.openlca.core.model.Location)5 List (java.util.List)4 Logger (org.slf4j.Logger)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Rectangle (org.eclipse.swt.graphics.Rectangle)3 Composite (org.eclipse.swt.widgets.Composite)3 Bounds (org.openlca.geo.calc.Bounds)3 GeoJSON (org.openlca.geo.geojson.GeoJSON)3 MultiPoint (org.openlca.geo.geojson.MultiPoint)3 Point (org.openlca.geo.geojson.Point)3 Pair (org.openlca.util.Pair)3 LoggerFactory (org.slf4j.LoggerFactory)3 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 TLongByteHashMap (gnu.trove.map.hash.TLongByteHashMap)1