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