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
31 namespace geos {
32 namespace geom {
33 class Coordinate;
34 class CoordinateSequence;
35 class Geometry;
36 class GeometryCollection;
37 class Point;
38 class LineString;
39 class LinearRing;
40 class Polygon;
41 class MultiPoint;
42 class MultiLineString;
43 class MultiPolygon;
44 class PrecisionModel;
45 }
46 namespace io {
47 class Writer;
48 }
49 }
50 
51 
52 namespace geos {
53 namespace io {
54 
55 enum class GeoJSONType {
56  GEOMETRY, FEATURE, FEATURE_COLLECTION
57 };
58 
66 class GEOS_DLL GeoJSONWriter {
67 public:
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 
78 private:
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
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Definition: MultiPolygon.h:59
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Definition: LineString.h:68
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
Basic namespace for all GEOS functionalities.
Definition: Angle.h:26
Definition: MultiPoint.h:54
Outputs the GeoJSON representation of a Geometry. See also GeoJSONReader for parsing.
Definition: GeoJSONWriter.h:66
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
Definition: Point.h:66
Models a collection of LineStrings.
Definition: MultiLineString.h:51