File: GEOSGeomGeoJSONWriteTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (116 lines) | stat: -rw-r--r-- 2,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Test Suite for C-API GEOSGeoJSONReader_read

#include <tut/tut.hpp>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capigeosgeomwritegeojson_data : public capitest::utility {
    GEOSGeoJSONWriter* writer_;

    test_capigeosgeomwritegeojson_data() : writer_(nullptr)
    {
        writer_ = GEOSGeoJSONWriter_create();
    }

    ~test_capigeosgeomwritegeojson_data()
    {
        GEOSGeoJSONWriter_destroy(writer_);
        writer_ = nullptr;
    }

    void
    test_geojson(std::string const& wkt, std::string const& expected, int indent)
    {
        geom1_ = fromWKT(&wkt[0]);
        char* geojson_c = GEOSGeoJSONWriter_writeGeometry(writer_, geom1_, indent);
        ensure("GEOSGeoJSONWriter_writeGeometry failed to create GeoJSON", nullptr != geojson_c);

        std::string actual(geojson_c);
        free(geojson_c);
        ensure_equals(actual, expected);
    }
};

typedef test_group<test_capigeosgeomwritegeojson_data> group;
typedef group::object object;

group test_capigeosgeomwritegeojson_group("capi::GEOSGeomGeoJSONWrite");

//
// Test Cases
//

// Write a Point to GeoJSON
template<>
template<>
void object::test<1>
()
{
    std::string wkt("POINT(-117.0 33.0)");
    std::string expected("{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]}");
    test_geojson(wkt, expected, -1);
}

// Write a GeometryCollection to GeoJSON
template<>
template<>
void object::test<2>
()
{
    std::string wkt("GEOMETRYCOLLECTION(POINT(1 1),POINT(2 2))");
    std::string expected("{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[1.0,1.0]},{\"type\":\"Point\",\"coordinates\":[2.0,2.0]}]}");
    test_geojson(wkt, expected, -1);
}


// Write a LineString to formatted GeoJSON
template<>
template<>
void object::test<3>
()
{
    std::string wkt("LINESTRING(102.0 0.0, 103.0 1.0, 104.0 0.0, 105.0 1.0)");
    std::string expected(std::string{"{\n"
        "    \"type\": \"LineString\",\n"
        "    \"coordinates\": [\n"
        "        [\n"
        "            102.0,\n"
        "            0.0\n"
        "        ],\n"
        "        [\n"
        "            103.0,\n"
        "            1.0\n"
        "        ],\n"
        "        [\n"
        "            104.0,\n"
        "            0.0\n"
        "        ],\n"
        "        [\n"
        "            105.0,\n"
        "            1.0\n"
        "        ]\n"
        "    ]\n"
        "}"});
    test_geojson(wkt, expected, 4);
}

template<>
template<>
void object::test<4>
()
{
    input_ = fromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
    ensure(input_);

    char* geojson = GEOSGeoJSONWriter_writeGeometry(writer_, input_, 0);
    ensure("curved geometry not supported", geojson == nullptr);
}

}