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

brick / geo / 14057076899

25 Mar 2025 10:14AM UTC coverage: 52.154% (-13.7%) from 65.828%
14057076899

push

github

BenMorel
Wip

0 of 383 new or added lines in 14 files covered. (0.0%)

141 existing lines in 12 files now uncovered.

1634 of 3133 relevant lines covered (52.15%)

396.62 hits per line

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

0.0
/src/Engine/SpatialiteEngine.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine;
6

7
use Brick\Geo\Engine\Database\DatabaseDriver;
8
use Brick\Geo\Engine\Database\Query\BinaryValue;
9
use Brick\Geo\Engine\Database\Query\ScalarValue;
10
use Brick\Geo\Engine\Database\Result\Row;
11
use Brick\Geo\Engine\Internal\TypeChecker;
12
use Brick\Geo\Exception\GeometryEngineException;
13
use Brick\Geo\Geometry;
14
use Brick\Geo\Io\WkbReader;
15
use Brick\Geo\Io\WkbWriter;
16
use Brick\Geo\LineString;
17
use Brick\Geo\Point;
18
use Override;
19

20
/**
21
 * Database engine based on a SQLite3 driver.
22
 *
23
 * The spatialite extension must be loaded in this driver.
24
 */
25
final readonly class SpatialiteEngine extends DatabaseEngine
26
{
27
    private WkbReader $wkbReader;
28
    private WkbWriter $wkbWriter;
29

30
    public function __construct(
31
        // TODO private
32
        public DatabaseDriver $driver,
33
        // TODO
34
        private bool $useProxy = true,
35
    ) {
NEW
36
        $this->wkbReader = new WkbReader();
×
NEW
37
        $this->wkbWriter = new WkbWriter();
×
38
    }
39

40
    /**
41
     * Builds and executes a SQL query for a GIS function.
42
     *
43
     * @param string                $function        The SQL GIS function to execute.
44
     * @param list<Geometry|scalar> $parameters      The Geometry objects or scalar values to pass as parameters.
45
     * @param bool                  $returnsGeometry Whether the GIS function returns a Geometry.
46
     *
47
     * @throws GeometryEngineException
48
     */
49
    private function query(string $function, array $parameters, bool $returnsGeometry) : Row
50
    {
NEW
51
        $query = ['SELECT '];
×
52

NEW
53
        if ($returnsGeometry) {
×
NEW
54
            $query[] = 'ST_AsBinary(g), ST_SRID(g) FROM (SELECT ';
×
55
        }
56

NEW
57
        $query[] = $function . '(';
×
58

NEW
59
        foreach ($parameters as $key => $parameter) {
×
NEW
60
            if ($key !== 0) {
×
NEW
61
                $query[] = ',';
×
62
            }
63

NEW
64
            if ($parameter instanceof Geometry) {
×
NEW
65
                if ($parameter instanceof Point && $parameter->isEmpty()) {
×
66
                    // WKB does not support empty points, and SpatiaLite does not support them either.
NEW
67
                    throw new GeometryEngineException('MySQL does not support empty points');
×
68
                }
NEW
69
                $query[] = 'ST_GeomFromWKB(';
×
NEW
70
                $query[] = new BinaryValue($this->wkbWriter->write($parameter));
×
NEW
71
                $query[] = ',';
×
NEW
72
                $query[] = new ScalarValue($parameter->srid());
×
NEW
73
                $query[] = ')';
×
74
            } else {
NEW
75
                $query[] = new ScalarValue($parameter);
×
76
            }
77
        }
78

NEW
79
        $query[] = ')';
×
80

NEW
81
        if ($returnsGeometry) {
×
NEW
82
            $query[] = ' AS g) AS q';
×
83
        }
84

NEW
85
        return $this->driver->executeQuery(...$query);
×
86
    }
87

88
    /**
89
     * Queries a GIS function returning a boolean value.
90
     *
91
     * @param string          $function      The SQL GIS function to execute.
92
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
93
     *
94
     * @throws GeometryEngineException
95
     */
96
    #[Override]
97
    protected function queryBool(string $function, Geometry|string|float|int|bool ...$parameters) : bool
98
    {
NEW
99
        return $this->query($function, $parameters, false)->get(0)->asBool();
×
100
    }
101

102
    /**
103
     * Queries a GIS function returning a floating point value.
104
     *
105
     * @param string          $function      The SQL GIS function to execute.
106
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
107
     *
108
     * @throws GeometryEngineException
109
     */
110
    #[Override]
111
    protected function queryFloat(string $function, Geometry|string|float|int|bool ...$parameters) : float
112
    {
NEW
113
        return $this->query($function, $parameters, false)->get(0)->asFloat();
×
114
    }
115

116
    /**
117
     * Queries a GIS function returning a Geometry object.
118
     *
119
     * @param string             $function   The SQL GIS function to execute.
120
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
121
     *
122
     * @throws GeometryEngineException
123
     */
124
    #[Override]
125
    protected function queryGeometry(string $function, Geometry|string|float|int|bool ...$parameters) : Geometry
126
    {
NEW
127
        $row = $this->query($function, $parameters, true);
×
128

NEW
129
        $wkb = $row->get(0)->asBinary();
×
NEW
130
        $srid = $row->get(1)->asInt();
×
131

NEW
132
        return $this->wkbReader->read($wkb, $srid);
×
133
    }
134

135
    #[Override]
136
    public function lineInterpolatePoint(LineString $lineString, float $fraction) : Point
137
    {
NEW
138
        $result = $this->queryGeometry('ST_Line_Interpolate_Point', $lineString, $fraction);
×
NEW
139
        TypeChecker::check($result, Point::class);
×
140

NEW
141
        return $result;
×
142
    }
143
}
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