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

brick / geo / 13766209603

09 Mar 2025 10:35PM UTC coverage: 87.414% (+3.3%) from 84.117%
13766209603

push

github

BenMorel
Add Point::isEqualTo() (WIP: finish? keep?)

8 of 8 new or added lines in 2 files covered. (100.0%)

73 existing lines in 16 files now uncovered.

1653 of 1891 relevant lines covered (87.41%)

1946.79 hits per line

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

98.92
/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());
12,054✔
59

60
        $cs = $geometry->coordinateSystem;
12,054✔
61

62
        $dimensionality = '';
12,054✔
63

64
        if ($cs->hasZ || $cs->hasM) {
12,054✔
65
            $dimensionality .= ' ';
8,330✔
66

67
            if ($cs->hasZ) {
8,330✔
68
                $dimensionality .= 'Z';
5,625✔
69
            }
70
            if ($cs->hasM) {
8,330✔
71
                $dimensionality .= 'M';
5,388✔
72
            }
73
        }
74

75
        if ($geometry instanceof GeometryCollection) {
12,054✔
76
            $isEmpty = ($geometry->numGeometries() === 0);
3,871✔
77
        } else {
78
            $isEmpty = $geometry->isEmpty();
8,678✔
79
        }
80

81
        if ($isEmpty) {
12,054✔
82
            return $type . $dimensionality . ' EMPTY';
4,505✔
83
        }
84

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

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

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

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

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

135
        return $result;
7,691✔
136
    }
137

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

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

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

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

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

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

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

167
        foreach ($compoundCurve as $curve) {
609✔
168
            if ($curve instanceof LineString) {
609✔
169
                // LineString does not need the LINESTRING keyword
170
                $result[] = '(' . $this->writeLineString($curve). ')';
602✔
171
            } else {
172
                // CircularString needs the CIRCULARSTRING keyword
173
                $result[] = $this->doWrite($curve);
588✔
174
            }
175
        }
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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