View Javadoc
1   package net.sumaris.server.http.geojson.extraction;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>*
41   */
42  public class GeoJsonExtractions {
43  
44      protected GeoJsonExtractions() {
45          // Helper class
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                  //.map(StringUtils::underscoreToChangeCase)
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              // Geometry
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 }