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.LINESTRING;
22  import static org.bremersee.geojson.GeoJsonConstants.TYPE;
23  
24  import java.io.Serial;
25  import java.util.Map;
26  import org.locationtech.jts.geom.CoordinateSequence;
27  import org.locationtech.jts.geom.GeometryFactory;
28  import org.locationtech.jts.geom.LineString;
29  
30  /**
31   * The json to line string converter.
32   *
33   * @author Christian Bremer
34   */
35  class JsonToLineStringConverter extends AbstractJsonToGeometryConverter {
36  
37    @Serial
38    private static final long serialVersionUID = 1L;
39  
40    private final ObjectToCoordinateSequenceConverter coordinateSequenceConverter;
41  
42    /**
43     * Instantiates a new json to line string converter.
44     *
45     * @param geometryFactory the geometry factory
46     * @param coordinateSequenceConverter the coordinate sequence converter
47     */
48    JsonToLineStringConverter(
49        GeometryFactory geometryFactory,
50        ObjectToCoordinateSequenceConverter coordinateSequenceConverter) {
51  
52      super(geometryFactory);
53      if (isNull(coordinateSequenceConverter)) {
54        throw new IllegalArgumentException("Coordinate sequence converter must be present.");
55      }
56      this.coordinateSequenceConverter = coordinateSequenceConverter;
57    }
58  
59    /**
60     * Convert line string.
61     *
62     * @param source the source
63     * @return the line string
64     */
65    LineString convert(Map<String, Object> source) {
66      if (isNull(source)) {
67        return null;
68      }
69      if (!LINESTRING.equals(source.get(TYPE))) {
70        throw new IllegalArgumentException(String
71            .format("Source is not a %s: %s", LINESTRING, source));
72      }
73      return convertCoordinates(source.get(COORDINATES));
74    }
75  
76    /**
77     * Convert coordinates line string.
78     *
79     * @param source the source
80     * @return the line string
81     */
82    LineString convertCoordinates(Object source) {
83      CoordinateSequence coordinateSequence = coordinateSequenceConverter.convert(source);
84      return getGeometryFactory().createLineString(coordinateSequence);
85    }
86  
87  }