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

brick / geo / 17456208570

04 Sep 2025 07:10AM UTC coverage: 50.432%. Remained the same
17456208570

push

github

BenMorel
Use @extends and @implements instead of @template-* variants

For consistency with the rest of the project.

1867 of 3702 relevant lines covered (50.43%)

1140.21 hits per line

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

98.95
/src/Io/Internal/AbstractWktWriter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Io\Internal;
6

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

23
use function implode;
24
use function strtoupper;
25

26
/**
27
 * Base class for WktWriter and EwktWriter.
28
 *
29
 * @internal
30
 */
31
abstract class AbstractWktWriter
32
{
33
    /**
34
     * A space if prettyPrint is true, an empty string otherwise.
35
     */
36
    protected string $prettyPrintSpace = ' ';
37

38
    public function setPrettyPrint(bool $prettyPrint): void
39
    {
40
        $this->prettyPrintSpace = $prettyPrint ? ' ' : '';
2,192✔
41
    }
42

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

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

63
        $cs = $geometry->coordinateSystem();
12,396✔
64

65
        $hasZ = $cs->hasZ();
12,396✔
66
        $hasM = $cs->hasM();
12,396✔
67

68
        $dimensionality = '';
12,396✔
69

70
        if ($hasZ || $hasM) {
12,396✔
71
            $dimensionality .= ' ';
8,366✔
72

73
            if ($hasZ) {
8,366✔
74
                $dimensionality .= 'Z';
5,659✔
75
            }
76
            if ($hasM) {
8,366✔
77
                $dimensionality .= 'M';
5,390✔
78
            }
79
        }
80

81
        if ($geometry instanceof GeometryCollection) {
12,396✔
82
            $isEmpty = ($geometry->numGeometries() === 0);
4,001✔
83
        } else {
84
            $isEmpty = $geometry->isEmpty();
8,920✔
85
        }
86

87
        if ($isEmpty) {
12,396✔
88
            return $type . $dimensionality . ' EMPTY';
4,365✔
89
        }
90

91
        if ($geometry instanceof Point) {
8,215✔
92
            $data = $this->writePoint($geometry);
1,507✔
93
        } elseif ($geometry instanceof LineString) {
7,211✔
94
            $data = $this->writeLineString($geometry);
1,191✔
95
        } elseif ($geometry instanceof CircularString) {
6,423✔
96
            $data = $this->writeCircularString($geometry);
1,397✔
97
        } elseif ($geometry instanceof CompoundCurve) {
5,932✔
98
            $data = $this->writeCompoundCurve($geometry);
666✔
99
        } elseif ($geometry instanceof Triangle) {
5,410✔
100
            $data = $this->writePolygon($geometry);
328✔
101
        } elseif ($geometry instanceof Polygon) {
5,082✔
102
            $data = $this->writePolygon($geometry);
1,241✔
103
        } elseif ($geometry instanceof CurvePolygon) {
3,904✔
104
            $data = $this->writeCurvePolygon($geometry);
448✔
105
        } elseif ($geometry instanceof MultiPoint) {
3,456✔
106
            $data = $this->writeMultiPoint($geometry);
784✔
107
        } elseif ($geometry instanceof MultiLineString) {
2,672✔
108
            $data = $this->writeMultiLineString($geometry);
712✔
109
        } elseif ($geometry instanceof MultiPolygon) {
1,992✔
110
            $data = $this->writeMultiPolygon($geometry);
698✔
111
        } elseif ($geometry instanceof GeometryCollection) {
1,294✔
112
            $data = $this->writeGeometryCollection($geometry);
502✔
113
        } elseif ($geometry instanceof Tin) {
792✔
114
            /** @psalm-suppress InvalidArgument Not sure how to fix this. */
115
            $data = $this->writePolyhedralSurface($geometry);
384✔
116
        } elseif ($geometry instanceof PolyhedralSurface) {
408✔
117
            $data = $this->writePolyhedralSurface($geometry);
408✔
118
        } else {
119
            throw GeometryIoException::unsupportedGeometryType($geometry->geometryType());
×
120
        }
121

122
        return $type . $dimensionality . $this->prettyPrintSpace . '(' . $data . ')';
8,215✔
123
    }
124

125
    /**
126
     * @param Point $point The point. Must not be empty.
127
     */
128
    private function writePoint(Point $point): string
129
    {
130
        /** @psalm-suppress PossiblyNullOperand */
131
        $result = $point->x() . ' ' . $point->y();
8,199✔
132

133
        if (null !== $z = $point->z()) {
8,199✔
134
            $result .= ' ' . $z;
3,608✔
135
        }
136

137
        if (null !== $m = $point->m()) {
8,199✔
138
            $result .= ' ' . $m;
3,376✔
139
        }
140

141
        return $result;
8,199✔
142
    }
143

144
    private function writeLineString(LineString $lineString): string
145
    {
146
        $result = [];
5,886✔
147

148
        foreach ($lineString as $point) {
5,886✔
149
            $result[] = $this->writePoint($point);
5,886✔
150
        }
151

152
        return implode(',' . $this->prettyPrintSpace, $result);
5,886✔
153
    }
154

155
    private function writeCircularString(CircularString $circularString): string
156
    {
157
        $result = [];
1,397✔
158

159
        foreach ($circularString as $point) {
1,397✔
160
            $result[] = $this->writePoint($point);
1,397✔
161
        }
162

163
        return implode(',' . $this->prettyPrintSpace, $result);
1,397✔
164
    }
165

166
    /**
167
     * @throws GeometryIoException
168
     */
169
    private function writeCompoundCurve(CompoundCurve $compoundCurve): string
170
    {
171
        $result = [];
666✔
172

173
        foreach ($compoundCurve as $curve) {
666✔
174
            if ($curve instanceof LineString) {
666✔
175
                // LineString does not need the LINESTRING keyword
176
                $result[] = '(' . $this->writeLineString($curve) . ')';
658✔
177
            } else {
178
                // CircularString needs the CIRCULARSTRING keyword
179
                $result[] = $this->doWrite($curve);
642✔
180
            }
181
        }
182

183
        return implode(',' . $this->prettyPrintSpace, $result);
666✔
184
    }
185

186
    private function writePolygon(Polygon $polygon): string
187
    {
188
        $result = [];
3,059✔
189

190
        foreach ($polygon as $ring) {
3,059✔
191
            $result[] = '(' . $this->writeLineString($ring) . ')';
3,059✔
192
        }
193

194
        return implode(',' . $this->prettyPrintSpace, $result);
3,059✔
195
    }
196

197
    private function writeCurvePolygon(CurvePolygon $curvePolygon): string
198
    {
199
        $result = [];
448✔
200

201
        foreach ($curvePolygon as $ring) {
448✔
202
            if ($ring instanceof LineString) {
448✔
203
                $result[] = '(' . $this->writeLineString($ring) . ')';
440✔
204
            } else {
205
                $result[] = $this->doWrite($ring);
408✔
206
            }
207
        }
208

209
        return implode(',' . $this->prettyPrintSpace, $result);
448✔
210
    }
211

212
    private function writeMultiPoint(MultiPoint $multiPoint): string
213
    {
214
        $result = [];
784✔
215

216
        foreach ($multiPoint as $point) {
784✔
217
            $result[] = $this->writePoint($point);
784✔
218
        }
219

220
        return implode(',' . $this->prettyPrintSpace, $result);
784✔
221
    }
222

223
    private function writeMultiLineString(MultiLineString $multiLineString): string
224
    {
225
        $result = [];
712✔
226

227
        foreach ($multiLineString as $lineString) {
712✔
228
            $result[] = '(' . $this->writeLineString($lineString) . ')';
712✔
229
        }
230

231
        return implode(',' . $this->prettyPrintSpace, $result);
712✔
232
    }
233

234
    private function writeMultiPolygon(MultiPolygon $multiPolygon): string
235
    {
236
        $result = [];
698✔
237

238
        foreach ($multiPolygon as $polygon) {
698✔
239
            $result[] = '(' . $this->writePolygon($polygon) . ')';
698✔
240
        }
241

242
        return implode(',' . $this->prettyPrintSpace, $result);
698✔
243
    }
244

245
    private function writeGeometryCollection(GeometryCollection $collection): string
246
    {
247
        $result = [];
502✔
248

249
        foreach ($collection as $geometry) {
502✔
250
            $result[] = $this->doWrite($geometry);
502✔
251
        }
252

253
        return implode(',' . $this->prettyPrintSpace, $result);
502✔
254
    }
255

256
    private function writePolyhedralSurface(PolyhedralSurface $polyhedralSurface): string
257
    {
258
        $result = [];
792✔
259

260
        foreach ($polyhedralSurface as $patch) {
792✔
261
            $result[] = '(' . $this->writePolygon($patch) . ')';
792✔
262
        }
263

264
        return implode(',' . $this->prettyPrintSpace, $result);
792✔
265
    }
266
}
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