GEOS  3.13.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
30 namespace geos {
31 namespace geom {
32 class Coordinate;
33 class CoordinateSequence;
34 class Geometry;
35 class GeometryCollection;
36 class Point;
37 class LineString;
38 class LinearRing;
39 class Polygon;
40 class MultiPoint;
41 class MultiLineString;
42 class MultiPolygon;
43 class PrecisionModel;
44 }
45 namespace io {
46 class Writer;
47 }
48 }
49 
50 
51 namespace geos {
52 namespace io {
53 
54 enum class GeoJSONType {
55  GEOMETRY, FEATURE, FEATURE_COLLECTION
56 };
57 
65 class GEOS_DLL GeoJSONWriter {
66 public:
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 private:
78 
79  std::pair<double, double> convertCoordinate(const geom::CoordinateXY* 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:56
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:51
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: LineString.h:65
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:60
Outputs the GeoJSON representation of a Geometry. See also GeoJSONReader for parsing.
Definition: GeoJSONWriter.h:65
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25