1 package net.sumaris.server.http.geojson.extraction;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import net.sumaris.core.dao.referential.location.Locations;
26 import net.sumaris.core.extraction.vo.ExtractionResultVO;
27 import net.sumaris.core.vo.technical.extraction.ExtractionProductColumnVO;
28 import net.sumaris.server.http.geojson.GeoJsonGeometries;
29 import org.geojson.Feature;
30 import org.geojson.FeatureCollection;
31 import org.geojson.Geometry;
32
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.function.Function;
37 import java.util.stream.Collectors;
38
39
40
41
42 public class GeoJsonExtractions {
43
44 protected GeoJsonExtractions() {
45
46 }
47
48 public static FeatureCollection toFeatureCollection(ExtractionResultVO result, String spaceColumnName) {
49
50 Function<String, Geometry> mapper = null;
51 if ("square".equalsIgnoreCase(spaceColumnName)) {
52 mapper = (value) -> {
53 if (value == null) return null;
54 com.vividsolutions.jts.geom.Geometry geom = Locations.getGeometryFromMinuteSquareLabel(value, 10, false);
55 return GeoJsonGeometries.jtsGeometry(geom);
56 };
57 }
58 else if ("statistical_rectangle".equals(spaceColumnName) || "rect".equalsIgnoreCase(spaceColumnName)){
59 mapper = (value) -> {
60 if (value == null) return null;
61 com.vividsolutions.jts.geom.Geometry geom = Locations.getGeometryFromRectangleLabel(value, false);
62 return GeoJsonGeometries.jtsGeometry(geom);
63 };
64 }
65
66 FeatureCollection features = new FeatureCollection();
67
68 final Function<String, Geometry> finalMapper = mapper;
69 List<String> propertyNames = result.getColumns().stream()
70 .map(ExtractionProductColumnVO::getColumnName)
71
72 .map(String::toLowerCase)
73 .collect(Collectors.toList());
74 List<Feature> rows = result.getRows().stream().map(row -> {
75 Feature feature = new Feature();
76 Map<String, Object> properties = new HashMap<>();
77 int index = 0;
78 for (String property: propertyNames) {
79 Object value = row[index++];
80 if (value != null) {
81 properties.put(property, value);
82 }
83 }
84 feature.setProperties(properties);
85
86
87 if (finalMapper != null) {
88 String spaceValue = (String) properties.get(spaceColumnName);
89 feature.setGeometry(finalMapper.apply(spaceValue));
90 }
91
92 return feature;
93 }).collect(Collectors.toList());
94
95 features.setFeatures(rows);
96
97 return features;
98
99 }
100 }