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

brick / geo / 13642551372

03 Mar 2025 11:25PM UTC coverage: 84.406% (+0.8%) from 83.61%
13642551372

push

github

BenMorel
Merge WKTParser & EWKTParser

31 of 32 new or added lines in 3 files covered. (96.88%)

69 existing lines in 8 files now uncovered.

1548 of 1834 relevant lines covered (84.41%)

1988.83 hits per line

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

97.94
/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
abstract class AbstractWKTWriter
27
{
28
    /**
29
     * A space if prettyPrint is true, an empty string otherwise.
30
     */
31
    protected string $prettyPrintSpace = ' ';
32

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

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

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

58
        $cs = $geometry->coordinateSystem();
11,812✔
59

60
        $hasZ = $cs->hasZ();
11,812✔
61
        $hasM = $cs->hasM();
11,812✔
62

63
        $dimensionality = '';
11,812✔
64

65
        if ($hasZ || $hasM) {
11,812✔
66
            $dimensionality .= ' ';
8,309✔
67

68
            if ($hasZ) {
8,309✔
69
                $dimensionality .= 'Z';
5,611✔
70
            }
71
            if ($hasM) {
8,309✔
72
                $dimensionality .= 'M';
5,374✔
73
            }
74
        }
75

76
        if ($geometry instanceof GeometryCollection) {
11,812✔
77
            $isEmpty = ($geometry->numGeometries() === 0);
3,783✔
78
        } else {
79
            $isEmpty = $geometry->isEmpty();
8,523✔
80
        }
81

82
        if ($isEmpty) {
11,812✔
83
            return $type . $dimensionality . ' EMPTY';
4,500✔
84
        }
85

86
        if ($geometry instanceof Point) {
7,468✔
87
            $data = $this->writePoint($geometry);
1,225✔
88
        } elseif ($geometry instanceof LineString) {
6,719✔
89
            $data = $this->writeLineString($geometry);
1,110✔
90
        } elseif ($geometry instanceof CircularString) {
6,003✔
91
            $data = $this->writeCircularString($geometry);
1,302✔
92
        } elseif ($geometry instanceof CompoundCurve) {
5,520✔
93
            $data = $this->writeCompoundCurve($geometry);
581✔
94
        } elseif ($geometry instanceof Triangle) {
5,093✔
95
            $data = $this->writePolygon($geometry);
343✔
96
        } elseif ($geometry instanceof Polygon) {
4,750✔
97
            $data = $this->writePolygon($geometry);
1,148✔
98
        } elseif ($geometry instanceof CurvePolygon) {
3,646✔
99
            $data = $this->writeCurvePolygon($geometry);
448✔
100
        } elseif ($geometry instanceof MultiPoint) {
3,198✔
101
            $data = $this->writeMultiPoint($geometry);
482✔
102
        } elseif ($geometry instanceof MultiLineString) {
2,716✔
103
            $data = $this->writeMultiLineString($geometry);
730✔
104
        } elseif ($geometry instanceof MultiPolygon) {
2,014✔
105
            $data = $this->writeMultiPolygon($geometry);
722✔
106
        } elseif ($geometry instanceof GeometryCollection) {
1,292✔
107
            $data = $this->writeGeometryCollection($geometry);
487✔
108
        } elseif ($geometry instanceof TIN) {
805✔
109
            /** @psalm-suppress InvalidArgument Not sure how to fix this. */
110
            $data = $this->writePolyhedralSurface($geometry);
392✔
111
        } elseif ($geometry instanceof PolyhedralSurface) {
413✔
112
            $data = $this->writePolyhedralSurface($geometry);
413✔
113
        } else {
UNCOV
114
            throw GeometryIOException::unsupportedGeometryType($geometry->geometryType());
×
115
        }
116

117
        return $type . $dimensionality . $this->prettyPrintSpace . '(' . $data . ')';
7,468✔
118
    }
119

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

128
        if (null !== $z = $point->z()) {
7,454✔
129
            $result .= ' ' . $z;
3,472✔
130
        }
131

132
        if (null !== $m = $point->m()) {
7,454✔
133
            $result .= ' ' . $m;
3,270✔
134
        }
135

136
        return $result;
7,454✔
137
    }
138

139
    private function writeLineString(LineString $lineString) : string
140
    {
141
        $result = [];
5,705✔
142

143
        foreach ($lineString as $point) {
5,705✔
144
            $result[] = $this->writePoint($point);
5,705✔
145
        }
146

147
        return implode(',' . $this->prettyPrintSpace, $result);
5,705✔
148
    }
149

150
    private function writeCircularString(CircularString $circularString) : string
151
    {
152
        $result = [];
1,302✔
153

154
        foreach ($circularString as $point) {
1,302✔
155
            $result[] = $this->writePoint($point);
1,302✔
156
        }
157

158
        return implode(',' . $this->prettyPrintSpace, $result);
1,302✔
159
    }
160

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

168
        foreach ($compoundCurve as $curve) {
581✔
169
            if ($curve instanceof LineString) {
581✔
170
                $result[] = '(' . $this->writeLineString($curve). ')';
574✔
171
            } elseif ($curve instanceof CircularString) {
560✔
172
                $result[] = $this->doWrite($curve);
560✔
173
            } else {
UNCOV
174
                throw new GeometryIOException('Only LineString and CircularString are allowed in CompoundCurve WKT.');
×
175
            }
176
        }
177

178
        return implode(',' . $this->prettyPrintSpace, $result);
581✔
179
    }
180

181
    private function writePolygon(Polygon $polygon) : string
182
    {
183
        $result = [];
3,018✔
184

185
        foreach ($polygon as $ring) {
3,018✔
186
            $result[] = '(' . $this->writeLineString($ring) . ')';
3,018✔
187
        }
188

189
        return implode(',' . $this->prettyPrintSpace, $result);
3,018✔
190
    }
191

192
    private function writeCurvePolygon(CurvePolygon $curvePolygon) : string
193
    {
194
        $result = [];
448✔
195

196
        foreach ($curvePolygon as $ring) {
448✔
197
            if ($ring instanceof LineString) {
448✔
198
                $result[] = '(' . $this->writeLineString($ring) . ')';
441✔
199
            } else {
200
                $result[] = $this->doWrite($ring);
413✔
201
            }
202
        }
203

204
        return implode(',' . $this->prettyPrintSpace, $result);
448✔
205
    }
206

207
    private function writeMultiPoint(MultiPoint $multiPoint) : string
208
    {
209
        $result = [];
482✔
210

211
        foreach ($multiPoint as $point) {
482✔
212
            $result[] = $this->writePoint($point);
482✔
213
        }
214

215
        return implode(',' . $this->prettyPrintSpace, $result);
482✔
216
    }
217

218
    private function writeMultiLineString(MultiLineString $multiLineString) : string
219
    {
220
        $result = [];
730✔
221

222
        foreach ($multiLineString as $lineString) {
730✔
223
            $result[] = '(' . $this->writeLineString($lineString) . ')';
730✔
224
        }
225

226
        return implode(',' . $this->prettyPrintSpace, $result);
730✔
227
    }
228

229
    private function writeMultiPolygon(MultiPolygon $multiPolygon) : string
230
    {
231
        $result = [];
722✔
232

233
        foreach ($multiPolygon as $polygon) {
722✔
234
            $result[] = '(' . $this->writePolygon($polygon) . ')';
722✔
235
        }
236

237
        return implode(',' . $this->prettyPrintSpace, $result);
722✔
238
    }
239

240
    private function writeGeometryCollection(GeometryCollection $collection) : string
241
    {
242
        $result = [];
487✔
243

244
        foreach ($collection as $geometry) {
487✔
245
            $result[] = $this->doWrite($geometry);
487✔
246
        }
247

248
        return implode(',' . $this->prettyPrintSpace, $result);
487✔
249
    }
250

251
    private function writePolyhedralSurface(PolyhedralSurface $polyhedralSurface) : string
252
    {
253
        $result = [];
805✔
254

255
        foreach ($polyhedralSurface as $patch) {
805✔
256
            $result[] = '(' . $this->writePolygon($patch) . ')';
805✔
257
        }
258

259
        return implode(',' . $this->prettyPrintSpace, $result);
805✔
260
    }
261
}
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