GEOS 3.15.0dev
GeoJSONWriter.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2021 Jared Erickson
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15#pragma once
16
17#include <geos/export.h>
18
19#include "GeoJSON.h"
20#include <string>
21#include <cctype>
22#include "geos/vend/include_nlohmann_json.hpp"
23
24#ifdef _MSC_VER
25#pragma warning(push)
26#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
27#endif
28
29// Forward declarations
30namespace geos {
31namespace geom {
32class Coordinate;
33class CoordinateSequence;
34class Geometry;
35class GeometryCollection;
36class Point;
37class LineString;
38class LinearRing;
39class Polygon;
40class MultiPoint;
41class MultiLineString;
42class MultiPolygon;
43class PrecisionModel;
44}
45namespace io {
46class Writer;
47}
48}
49
50
51namespace geos {
52namespace io {
53
54enum class GeoJSONType {
55 GEOMETRY, FEATURE, FEATURE_COLLECTION
56};
57
65class GEOS_DLL GeoJSONWriter {
66public:
67 ~GeoJSONWriter() = default;
68
69 std::string write(const geom::Geometry* geometry, GeoJSONType type = GeoJSONType::GEOMETRY);
70
71 std::string writeFormatted(const geom::Geometry* geometry, GeoJSONType type = GeoJSONType::GEOMETRY, int indent = 4);
72
73 std::string write(const GeoJSONFeature& feature);
74
75 std::string write(const GeoJSONFeatureCollection& features);
76
77 /*
78 * \brief
79 * Returns the output dimension used by the
80 * <code>GeoJSONWriter</code>.
81 */
82 int
83 getOutputDimension() const
84 {
85 return defaultOutputDimension;
86 }
87
88 /*
89 * Sets the output dimension used by the <code>GeoJSONWriter</code>.
90 *
91 * @param newOutputDimension Supported values are 2 or 3.
92 * Default since GEOS 3.12 is 3.
93 * Note that 3 indicates up to 3 dimensions will be
94 * written but 2D GeoJSON is still produced for 2D geometries.
95 */
96 void setOutputDimension(uint8_t newOutputDimension);
97
98 /*
99 * Sets whether the GeoJSON should be output following counter-clockwise orientation aka Right Hand Rule defined in
100 * RFC7946. See <a href="https://tools.ietf.org/html/rfc7946#section-3.1.6">RFC 7946 Specification</a>
101 * for more context.
102 *
103 * @param newIsForceCCW true if the GeoJSON should be output following the RFC7946
104 * counter-clockwise orientation aka Right Hand Rule
105 */
106 void setForceCCW(bool newIsForceCCW);
107
108private:
109 uint8_t defaultOutputDimension = 3;
110
111 bool isForceCCW = false;
112
113 std::vector<double> convertCoordinate(const geom::Coordinate* c);
114
115 std::vector<std::vector<double>> convertCoordinateSequence(const geom::CoordinateSequence* c);
116
117 std::vector<std::vector<std::vector<double>>> convertLinearRings(const geom::Polygon *poly);
118
119 void encode(const geom::Geometry* g, GeoJSONType type, geos_nlohmann::ordered_json& j);
120
121 void encodeGeometry(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
122
123 void encodePoint(const geom::Point* p, geos_nlohmann::ordered_json& j);
124
125 void encodeLineString(const geom::LineString* l, geos_nlohmann::ordered_json& j);
126
127 void encodePolygon(const geom::Polygon* p, geos_nlohmann::ordered_json& j);
128
129 void encodeMultiPoint(const geom::MultiPoint* p, geos_nlohmann::ordered_json& j);
130
131 void encodeMultiLineString(const geom::MultiLineString* l, geos_nlohmann::ordered_json& j);
132
133 void encodeMultiPolygon(const geom::MultiPolygon* m, geos_nlohmann::ordered_json& j);
134
135 void encodeGeometryCollection(const geom::GeometryCollection* g, geos_nlohmann::ordered_json& j);
136
137 void encodeFeature(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
138
139 void encodeFeatureCollection(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
140
141 void encodeFeature(const GeoJSONFeature& feature, geos_nlohmann::ordered_json& j);
142
143 void encodeGeoJSONValue(const std::string& key, const GeoJSONValue& value, geos_nlohmann::ordered_json& j);
144
145};
146
147} // namespace geos::io
148} // namespace geos
149
150#ifdef _MSC_VER
151#pragma warning(pop)
152#endif
153
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:217
Represents a collection of heterogeneous Geometry objects.
Definition GeometryCollection.h:51
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:196
Definition LineString.h:66
Models a collection of LineStrings.
Definition MultiLineString.h:49
Definition MultiPoint.h:50
Definition MultiPolygon.h:58
Definition Point.h:61
Represents a linear polygon, which may include holes.
Definition Polygon.h:61
Outputs the GeoJSON representation of a Geometry. See also GeoJSONReader for parsing.
Definition GeoJSONWriter.h:65
Basic namespace for all GEOS functionalities.
Definition geos.h:38