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.POLYGON;
22  import static org.bremersee.geojson.GeoJsonConstants.TYPE;
23  
24  import java.io.Serial;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  import org.locationtech.jts.geom.CoordinateSequence;
29  import org.locationtech.jts.geom.GeometryFactory;
30  import org.locationtech.jts.geom.LinearRing;
31  import org.locationtech.jts.geom.Polygon;
32  
33  /**
34   * The json to polygon converter.
35   *
36   * @author Christian Bremer
37   */
38  class JsonToPolygonConverter extends AbstractJsonToGeometryConverter {
39  
40    @Serial
41    private static final long serialVersionUID = 1L;
42  
43    private final ObjectToCoordinateSequenceConverter coordinateSequenceConverter;
44  
45    /**
46     * Instantiates a new json to polygon converter.
47     *
48     * @param geometryFactory the geometry factory
49     * @param coordinateSequenceConverter the coordinate sequence converter
50     */
51    JsonToPolygonConverter(
52        GeometryFactory geometryFactory,
53        ObjectToCoordinateSequenceConverter coordinateSequenceConverter) {
54  
55      super(geometryFactory);
56      if (isNull(coordinateSequenceConverter)) {
57        throw new IllegalArgumentException("Coordinate sequence converter must be present.");
58      }
59      this.coordinateSequenceConverter = coordinateSequenceConverter;
60    }
61  
62    /**
63     * Convert polygon.
64     *
65     * @param source the source
66     * @return the polygon
67     */
68    Polygon convert(Map<String, Object> source) {
69      if (isNull(source)) {
70        return null;
71      }
72      if (!POLYGON.equals(source.get(TYPE))) {
73        throw new IllegalArgumentException(String
74            .format("Source is not a %s: %s", POLYGON, source));
75      }
76      return convertCoordinates(source.get(COORDINATES));
77    }
78  
79    /**
80     * Convert coordinates polygon.
81     *
82     * @param source the source
83     * @return the polygon
84     */
85    Polygon convertCoordinates(Object source) {
86      Polygon polygon;
87      if (isNull(source)) {
88        polygon = getGeometryFactory().createPolygon();
89      } else {
90        List<CoordinateSequence> list = new ArrayList<>();
91        //noinspection unchecked
92        for (Object coordinates : (List<Object>) source) {
93          list.add(coordinateSequenceConverter.convert(coordinates));
94        }
95        if (list.isEmpty()) {
96          polygon = getGeometryFactory().createPolygon();
97        } else if (list.size() == 1) {
98          polygon = getGeometryFactory().createPolygon(list.get(0));
99        } else {
100         GeometryFactory gf = getGeometryFactory();
101         LinearRing[] holes = new LinearRing[list.size() - 1];
102         for (int i = 1; i < list.size(); i++) {
103           holes[i - 1] = gf.createLinearRing(list.get(i));
104         }
105         polygon = gf.createPolygon(gf.createLinearRing(list.get(0)), holes);
106       }
107     }
108     return polygon;
109   }
110 
111 }