View Javadoc
1   /*
2    * Copyright 2022-2026 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.serialization;
18  
19  import static java.util.Collections.unmodifiableMap;
20  import static java.util.Objects.isNull;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.io.Serial;
24  import java.io.Serializable;
25  import java.util.Arrays;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  import java.util.Optional;
29  import java.util.stream.Collectors;
30  import org.bremersee.geojson.GeoJsonConstants;
31  import org.bremersee.geojson.GeoJsonGeometryFactory;
32  import org.locationtech.jts.geom.Geometry;
33  
34  /**
35   * The type Abstract geometry to json converter.
36   *
37   * @param <S> the type parameter
38   * @author Christian Bremer
39   */
40  abstract class AbstractGeometryToJsonConverter<S extends Geometry> implements Serializable {
41  
42    @Serial
43    private static final long serialVersionUID = 1L;
44  
45    private final boolean withBoundingBox;
46  
47    /**
48     * Instantiates a new Abstract geometry to json converter.
49     *
50     * @param withBoundingBox the with bounding box
51     */
52    AbstractGeometryToJsonConverter(boolean withBoundingBox) {
53      this.withBoundingBox = withBoundingBox;
54    }
55  
56    /**
57     * Convert map.
58     *
59     * @param source the source
60     * @return the map
61     */
62    Map<String, Object> convert(S source) {
63      if (isNull(source)) {
64        return null;
65      }
66      Map<String, Object> map = new LinkedHashMap<>();
67      map.put(GeoJsonConstants.TYPE, requireNonNull(getGeometryType()));
68      if (withBoundingBox) {
69        Optional.ofNullable(GeoJsonGeometryFactory.getBoundingBox(source))
70            .map(bbox -> Arrays.stream(bbox).boxed().toList())
71            .ifPresent(bbox -> map.put(GeoJsonConstants.BBOX, bbox));
72      }
73      map.put(GeoJsonConstants.COORDINATES, getGeometryJsonValue(source));
74      return unmodifiableMap(map);
75    }
76  
77    /**
78     * Gets geometry type.
79     *
80     * @return the geometry type
81     */
82    abstract String getGeometryType();
83  
84    /**
85     * Gets geometry json value.
86     *
87     * @param source the source
88     * @return the geometry json value
89     */
90    abstract Object getGeometryJsonValue(S source);
91  }