• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13753277563

09 Mar 2025 10:43PM UTC coverage: 49.787% (+2.5%) from 47.295%
13753277563

push

github

BenMorel
Prepare for release

1749 of 3513 relevant lines covered (49.79%)

975.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.95
/src/IO/AbstractWKTWriter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\IO;
6

7
use Brick\Geo\Exception\GeometryIOException;
8
use Brick\Geo\Geometry;
9
use Brick\Geo\Point;
10
use Brick\Geo\LineString;
11
use Brick\Geo\CircularString;
12
use Brick\Geo\CompoundCurve;
13
use Brick\Geo\Polygon;
14
use Brick\Geo\CurvePolygon;
15
use Brick\Geo\MultiPoint;
16
use Brick\Geo\MultiLineString;
17
use Brick\Geo\MultiPolygon;
18
use Brick\Geo\GeometryCollection;
19
use Brick\Geo\PolyhedralSurface;
20
use Brick\Geo\TIN;
21
use Brick\Geo\Triangle;
22

23
/**
24
 * Base class for WKTWriter and EWKTWriter.
25
 *
26
 * @internal
27
 */
28
abstract class AbstractWKTWriter
29
{
30
    /**
31
     * A space if prettyPrint is true, an empty string otherwise.
32
     */
33
    protected string $prettyPrintSpace = ' ';
34

35
    public function setPrettyPrint(bool $prettyPrint) : void
36
    {
37
        $this->prettyPrintSpace = $prettyPrint ? ' ' : '';
1,694✔
38
    }
39

40
    /**
41
     * @param Geometry $geometry The geometry to export as WKT.
42
     *
43
     * @return string The WKT representation of the given geometry.
44
     *
45
     * @throws GeometryIOException If the given geometry cannot be exported as WKT.
46
     */
47
    abstract public function write(Geometry $geometry) : string;
48

49
    /**
50
     * @param Geometry $geometry The geometry to export as WKT.
51
     *
52
     * @return string The WKT representation of the given geometry.
53
     *
54
     * @throws GeometryIOException If the given geometry cannot be exported as WKT.
55
     */
56
    protected function doWrite(Geometry $geometry) : string
57
    {
58
        $type = strtoupper($geometry->geometryType());
10,496✔
59

60
        $cs = $geometry->coordinateSystem();
10,496✔
61

62
        $hasZ = $cs->hasZ();
10,496✔
63
        $hasM = $cs->hasM();
10,496✔
64

65
        $dimensionality = '';
10,496✔
66

67
        if ($hasZ || $hasM) {
10,496✔
68
            $dimensionality .= ' ';
7,151✔
69

70
            if ($hasZ) {
7,151✔
71
                $dimensionality .= 'Z';
4,839✔
72
            }
73
            if ($hasM) {
7,151✔
74
                $dimensionality .= 'M';
4,602✔
75
            }
76
        }
77

78
        if ($geometry instanceof GeometryCollection) {
10,496✔
79
            $isEmpty = ($geometry->numGeometries() === 0);
3,311✔
80
        } else {
81
            $isEmpty = $geometry->isEmpty();
7,624✔
82
        }
83

84
        if ($isEmpty) {
10,496✔
85
            return $type . $dimensionality . ' EMPTY';
3,829✔
86
        }
87

88
        if ($geometry instanceof Point) {
6,823✔
89
            $data = $this->writePoint($geometry);
1,249✔
90
        } elseif ($geometry instanceof LineString) {
5,994✔
91
            $data = $this->writeLineString($geometry);
998✔
92
        } elseif ($geometry instanceof CircularString) {
5,334✔
93
            $data = $this->writeCircularString($geometry);
1,162✔
94
        } elseif ($geometry instanceof CompoundCurve) {
4,907✔
95
            $data = $this->writeCompoundCurve($geometry);
525✔
96
        } elseif ($geometry instanceof Triangle) {
4,508✔
97
            $data = $this->writePolygon($geometry);
287✔
98
        } elseif ($geometry instanceof Polygon) {
4,221✔
99
            $data = $this->writePolygon($geometry);
1,036✔
100
        } elseif ($geometry instanceof CurvePolygon) {
3,229✔
101
            $data = $this->writeCurvePolygon($geometry);
392✔
102
        } elseif ($geometry instanceof MultiPoint) {
2,837✔
103
            $data = $this->writeMultiPoint($geometry);
513✔
104
        } elseif ($geometry instanceof MultiLineString) {
2,324✔
105
            $data = $this->writeMultiLineString($geometry);
618✔
106
        } elseif ($geometry instanceof MultiPolygon) {
1,734✔
107
            $data = $this->writeMultiPolygon($geometry);
610✔
108
        } elseif ($geometry instanceof GeometryCollection) {
1,124✔
109
            $data = $this->writeGeometryCollection($geometry);
431✔
110
        } elseif ($geometry instanceof TIN) {
693✔
111
            /** @psalm-suppress InvalidArgument Not sure how to fix this. */
112
            $data = $this->writePolyhedralSurface($geometry);
336✔
113
        } elseif ($geometry instanceof PolyhedralSurface) {
357✔
114
            $data = $this->writePolyhedralSurface($geometry);
357✔
115
        } else {
116
            throw GeometryIOException::unsupportedGeometryType($geometry->geometryType());
×
117
        }
118

119
        return $type . $dimensionality . $this->prettyPrintSpace . '(' . $data . ')';
6,823✔
120
    }
121

122
    /**
123
     * @param Point $point The point. Must not be empty.
124
     */
125
    private function writePoint(Point $point) : string
126
    {
127
        /** @psalm-suppress PossiblyNullOperand */
128
        $result = $point->x() . ' ' . $point->y();
6,809✔
129

130
        if (null !== $z = $point->z()) {
6,809✔
131
            $result .= ' ' . $z;
3,038✔
132
        }
133

134
        if (null !== $m = $point->m()) {
6,809✔
135
            $result .= ' ' . $m;
2,836✔
136
        }
137

138
        return $result;
6,809✔
139
    }
140

141
    private function writeLineString(LineString $lineString) : string
142
    {
143
        $result = [];
5,005✔
144

145
        foreach ($lineString as $point) {
5,005✔
146
            $result[] = $this->writePoint($point);
5,005✔
147
        }
148

149
        return implode(',' . $this->prettyPrintSpace, $result);
5,005✔
150
    }
151

152
    private function writeCircularString(CircularString $circularString) : string
153
    {
154
        $result = [];
1,162✔
155

156
        foreach ($circularString as $point) {
1,162✔
157
            $result[] = $this->writePoint($point);
1,162✔
158
        }
159

160
        return implode(',' . $this->prettyPrintSpace, $result);
1,162✔
161
    }
162

163
    /**
164
     * @throws GeometryIOException
165
     */
166
    private function writeCompoundCurve(CompoundCurve $compoundCurve) : string
167
    {
168
        $result = [];
525✔
169

170
        foreach ($compoundCurve as $curve) {
525✔
171
            if ($curve instanceof LineString) {
525✔
172
                // LineString does not need the LINESTRING keyword
173
                $result[] = '(' . $this->writeLineString($curve). ')';
518✔
174
            } else {
175
                // CircularString needs the CIRCULARSTRING keyword
176
                $result[] = $this->doWrite($curve);
504✔
177
            }
178
        }
179

180
        return implode(',' . $this->prettyPrintSpace, $result);
525✔
181
    }
182

183
    private function writePolygon(Polygon $polygon) : string
184
    {
185
        $result = [];
2,626✔
186

187
        foreach ($polygon as $ring) {
2,626✔
188
            $result[] = '(' . $this->writeLineString($ring) . ')';
2,626✔
189
        }
190

191
        return implode(',' . $this->prettyPrintSpace, $result);
2,626✔
192
    }
193

194
    private function writeCurvePolygon(CurvePolygon $curvePolygon) : string
195
    {
196
        $result = [];
392✔
197

198
        foreach ($curvePolygon as $ring) {
392✔
199
            if ($ring instanceof LineString) {
392✔
200
                $result[] = '(' . $this->writeLineString($ring) . ')';
385✔
201
            } else {
202
                $result[] = $this->doWrite($ring);
357✔
203
            }
204
        }
205

206
        return implode(',' . $this->prettyPrintSpace, $result);
392✔
207
    }
208

209
    private function writeMultiPoint(MultiPoint $multiPoint) : string
210
    {
211
        $result = [];
513✔
212

213
        foreach ($multiPoint as $point) {
513✔
214
            $result[] = $this->writePoint($point);
513✔
215
        }
216

217
        return implode(',' . $this->prettyPrintSpace, $result);
513✔
218
    }
219

220
    private function writeMultiLineString(MultiLineString $multiLineString) : string
221
    {
222
        $result = [];
618✔
223

224
        foreach ($multiLineString as $lineString) {
618✔
225
            $result[] = '(' . $this->writeLineString($lineString) . ')';
618✔
226
        }
227

228
        return implode(',' . $this->prettyPrintSpace, $result);
618✔
229
    }
230

231
    private function writeMultiPolygon(MultiPolygon $multiPolygon) : string
232
    {
233
        $result = [];
610✔
234

235
        foreach ($multiPolygon as $polygon) {
610✔
236
            $result[] = '(' . $this->writePolygon($polygon) . ')';
610✔
237
        }
238

239
        return implode(',' . $this->prettyPrintSpace, $result);
610✔
240
    }
241

242
    private function writeGeometryCollection(GeometryCollection $collection) : string
243
    {
244
        $result = [];
431✔
245

246
        foreach ($collection as $geometry) {
431✔
247
            $result[] = $this->doWrite($geometry);
431✔
248
        }
249

250
        return implode(',' . $this->prettyPrintSpace, $result);
431✔
251
    }
252

253
    private function writePolyhedralSurface(PolyhedralSurface $polyhedralSurface) : string
254
    {
255
        $result = [];
693✔
256

257
        foreach ($polyhedralSurface as $patch) {
693✔
258
            $result[] = '(' . $this->writePolygon($patch) . ')';
693✔
259
        }
260

261
        return implode(',' . $this->prettyPrintSpace, $result);
693✔
262
    }
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc