GEOS 3.14.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
98private:
99 uint8_t defaultOutputDimension = 3;
100
101 std::vector<double> convertCoordinate(const geom::Coordinate* c);
102
103 std::vector<std::vector<double>> convertCoordinateSequence(const geom::CoordinateSequence* c);
104
105 void encode(const geom::Geometry* g, GeoJSONType type, geos_nlohmann::ordered_json& j);
106
107 void encodeGeometry(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
108
109 void encodePoint(const geom::Point* p, geos_nlohmann::ordered_json& j);
110
111 void encodeLineString(const geom::LineString* l, geos_nlohmann::ordered_json& j);
112
113 void encodePolygon(const geom::Polygon* p, geos_nlohmann::ordered_json& j);
114
115 void encodeMultiPoint(const geom::MultiPoint* p, geos_nlohmann::ordered_json& j);
116
117 void encodeMultiLineString(const geom::MultiLineString* l, geos_nlohmann::ordered_json& j);
118
119 void encodeMultiPolygon(const geom::MultiPolygon* m, geos_nlohmann::ordered_json& j);
120
121 void encodeGeometryCollection(const geom::GeometryCollection* g, geos_nlohmann::ordered_json& j);
122
123 void encodeFeature(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
124
125 void encodeFeatureCollection(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
126
127 void encodeFeature(const GeoJSONFeature& feature, geos_nlohmann::ordered_json& j);
128
129 void encodeGeoJSONValue(const std::string& key, const GeoJSONValue& value, geos_nlohmann::ordered_json& j);
130
131};
132
133} // namespace geos::io
134} // namespace geos
135
136#ifdef _MSC_VER
137#pragma warning(pop)
138#endif
139
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:197
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:39