GEOS 3.10.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#ifndef GEOS_IO_GEOJSONWRITER_H
16#define GEOS_IO_GEOJSONWRITER_H
17
18#include <geos/export.h>
19
20#include "GeoJSON.h"
21#include <string>
22#include <cctype>
23#include "geos/vend/include_nlohmann_json.hpp"
24
25#ifdef _MSC_VER
26#pragma warning(push)
27#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
28#endif
29
30// Forward declarations
31namespace geos {
32namespace geom {
33class Coordinate;
34class CoordinateSequence;
35class Geometry;
36class GeometryCollection;
37class Point;
38class LineString;
39class LinearRing;
40class Polygon;
41class MultiPoint;
42class MultiLineString;
43class MultiPolygon;
44class PrecisionModel;
45}
46namespace io {
47class Writer;
48}
49}
50
51
52namespace geos {
53namespace io {
54
55enum class GeoJSONType {
56 GEOMETRY, FEATURE, FEATURE_COLLECTION
57};
58
66class GEOS_DLL GeoJSONWriter {
67public:
68 ~GeoJSONWriter() = default;
69
70 std::string write(const geom::Geometry* geometry, GeoJSONType type = GeoJSONType::GEOMETRY);
71
72 std::string writeFormatted(const geom::Geometry* geometry, GeoJSONType type = GeoJSONType::GEOMETRY, int indent = 4);
73
74 std::string write(const GeoJSONFeature& feature);
75
76 std::string write(const GeoJSONFeatureCollection& features);
77
78private:
79
80 std::pair<double, double> convertCoordinate(const geom::Coordinate* c);
81
82 std::vector<std::pair<double, double>> convertCoordinateSequence(const geom::CoordinateSequence* c);
83
84 void encode(const geom::Geometry* g, GeoJSONType type, geos_nlohmann::ordered_json& j);
85
86 void encodeGeometry(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
87
88 void encodePoint(const geom::Point* p, geos_nlohmann::ordered_json& j);
89
90 void encodeLineString(const geom::LineString* l, geos_nlohmann::ordered_json& j);
91
92 void encodePolygon(const geom::Polygon* p, geos_nlohmann::ordered_json& j);
93
94 void encodeMultiPoint(const geom::MultiPoint* p, geos_nlohmann::ordered_json& j);
95
96 void encodeMultiLineString(const geom::MultiLineString* l, geos_nlohmann::ordered_json& j);
97
98 void encodeMultiPolygon(const geom::MultiPolygon* m, geos_nlohmann::ordered_json& j);
99
100 void encodeGeometryCollection(const geom::GeometryCollection* g, geos_nlohmann::ordered_json& j);
101
102 void encodeFeature(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
103
104 void encodeFeatureCollection(const geom::Geometry* g, geos_nlohmann::ordered_json& j);
105
106 void encodeFeature(const GeoJSONFeature& feature, geos_nlohmann::ordered_json& j);
107
108 void encodeGeoJSONValue(const std::string& key, const GeoJSONValue& value, geos_nlohmann::ordered_json& j);
109
110};
111
112} // namespace geos::io
113} // namespace geos
114
115#ifdef _MSC_VER
116#pragma warning(pop)
117#endif
118
119#endif // #ifndef GEOS_IO_GEOJSONWRITER_H
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Definition: LineString.h:68
Models a collection of LineStrings.
Definition: MultiLineString.h:51
Definition: MultiPoint.h:54
Definition: MultiPolygon.h:59
Definition: Point.h:66
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Outputs the GeoJSON representation of a Geometry. See also GeoJSONReader for parsing.
Definition: GeoJSONWriter.h:66
Basic namespace for all GEOS functionalities.
Definition: geos.h:40