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);
}
}
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;
}
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);
}
}
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();
}
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);
}
}
}
Aggregations