View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.geojson.converter.deserialization;
18  
19  import static java.util.Objects.isNull;
20  import static org.bremersee.geojson.GeoJsonConstants.COORDINATES;
21  import static org.bremersee.geojson.GeoJsonConstants.MULTI_LINESTRING;
22  import static org.bremersee.geojson.GeoJsonConstants.TYPE;
23  
24  import java.io.Serial;
25  import java.util.List;
26  import java.util.Map;
27  import org.locationtech.jts.geom.GeometryFactory;
28  import org.locationtech.jts.geom.LineString;
29  import org.locationtech.jts.geom.MultiLineString;
30  
31  /**
32   * The json to multi line string converter.
33   *
34   * @author Christian Bremer
35   */
36  public class JsonToMultiLineStringConverter extends AbstractJsonToGeometryConverter {
37  
38    @Serial
39    private static final long serialVersionUID = 1L;
40  
41    /**
42     * The line string converter.
43     */
44    private final JsonToLineStringConverter lineStringConverter;
45  
46    /**
47     * Instantiates a new json to multi line string converter.
48     *
49     * @param geometryFactory the geometry factory
50     * @param lineStringConverter the line string converter
51     */
52    JsonToMultiLineStringConverter(
53        GeometryFactory geometryFactory,
54        JsonToLineStringConverter lineStringConverter) {
55  
56      super(geometryFactory);
57      if (isNull(lineStringConverter)) {
58        throw new IllegalArgumentException("LineString converter must be present.");
59      }
60      this.lineStringConverter = lineStringConverter;
61    }
62  
63    /**
64     * Convert multi line string.
65     *
66     * @param source the source
67     * @return the multi line string
68     */
69    MultiLineString convert(Map<String, Object> source) {
70      if (isNull(source)) {
71        return null;
72      }
73      if (!MULTI_LINESTRING.equals(source.get(TYPE))) {
74        throw new IllegalArgumentException(String
75            .format("Source is not a %s: %s", MULTI_LINESTRING, source));
76      }
77      return convertCoordinates(source.get(COORDINATES));
78    }
79  
80    /**
81     * Convert coordinates multi line string.
82     *
83     * @param source the source
84     * @return the multi line string
85     */
86    MultiLineString convertCoordinates(Object source) {
87      //noinspection unchecked
88      List<Object> coordinates = isNull(source) ? List.of() : (List<Object>) source;
89      LineString[] lineStrings = coordinates.stream()
90          .map(lineStringConverter::convertCoordinates)
91          .toArray(LineString[]::new);
92      return getGeometryFactory().createMultiLineString(lineStrings);
93    }
94  
95  }