GEOS 3.11.1
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
77private:
78
79 std::pair<double, double> convertCoordinate(const geom::Coordinate* c);
80
81 std::vector<std::pair<double, double>> convertCoordinateSequence(const geom::CoordinateSequence* c);
82
83 void encode(const geom::Geometry* g, GeoJSONType type, geos_nlohmann::ordered_json& j);
84
85 void encodeGeometry(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
86
87 void encodePoint(const geom::Point* p, geos_nlohmann::ordered_json& j);
88
89 void encodeLineString(const geom::LineString* l, geos_nlohmann::ordered_json& j);
90
91 void encodePolygon(const geom::Polygon* p, geos_nlohmann::ordered_json& j);
92
93 void encodeMultiPoint(const geom::MultiPoint* p, geos_nlohmann::ordered_json& j);
94
95 void encodeMultiLineString(const geom::MultiLineString* l, geos_nlohmann::ordered_json& j);
96
97 void encodeMultiPolygon(const geom::MultiPolygon* m, geos_nlohmann::ordered_json& j);
98
99 void encodeGeometryCollection(const geom::GeometryCollection* g, geos_nlohmann::ordered_json& j);
100
101 void encodeFeature(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
102
103 void encodeFeatureCollection(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
104
105 void encodeFeature(const GeoJSONFeature& feature, geos_nlohmann::ordered_json& j);
106
107 void encodeGeoJSONValue(const std::string& key, const GeoJSONValue& value, geos_nlohmann::ordered_json& j);
108
109};
110
111} // namespace geos::io
112} // namespace geos
113
114#ifdef _MSC_VER
115#pragma warning(pop)
116#endif
117
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:52
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: LineString.h:66
Models a collection of LineStrings.
Definition: MultiLineString.h:50
Definition: MultiPoint.h:51
Definition: MultiPolygon.h:59
Definition: Point.h:63
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