Skip to content

Commit 3b18793

Browse files
authored
Geometry::fromArray added $srid optional paramter (and tests) (#118)
1 parent 568aba4 commit 3b18793

9 files changed

+250
-3
lines changed

API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Geometry classes can be also created by these static methods:
1414

15-
* `fromArray(array $geometry)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
15+
* `fromArray(array $geometry, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
1616
* `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
1717
* `fromWkt(string $wkt, int|Srid $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
1818
* `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).

src/Objects/Geometry.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ public static function fromJson(string $geoJson, int|Srid $srid = 0): static
126126
*
127127
* @throws JsonException
128128
*/
129-
public static function fromArray(array $geometry): static
129+
public static function fromArray(array $geometry, int|Srid $srid = 0): static
130130
{
131131
$geoJson = json_encode($geometry, JSON_THROW_ON_ERROR);
132132

133-
return static::fromJson($geoJson);
133+
return static::fromJson($geoJson, $srid);
134134
}
135135

136136
/**

tests/Objects/GeometryCollectionTest.php

+95
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,44 @@
107107
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
108108
});
109109

110+
it('creates geometry collection from Array', function (): void {
111+
$geometryCollection = new GeometryCollection([
112+
new Polygon([
113+
new LineString([
114+
new Point(0, 180),
115+
new Point(1, 179),
116+
new Point(2, 178),
117+
new Point(3, 177),
118+
new Point(0, 180),
119+
]),
120+
]),
121+
new Point(0, 180),
122+
]);
123+
124+
$geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}',true));
125+
126+
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
127+
});
128+
129+
it('creates geometry collection with SRID from Array', function (): void {
130+
$geometryCollection = new GeometryCollection([
131+
new Polygon([
132+
new LineString([
133+
new Point(0, 180),
134+
new Point(1, 179),
135+
new Point(2, 178),
136+
new Point(3, 177),
137+
new Point(0, 180),
138+
]),
139+
]),
140+
new Point(0, 180),
141+
], Srid::WGS84->value);
142+
143+
$geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}',true), Srid::WGS84->value);
144+
145+
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
146+
});
147+
110148
it('creates geometry collection from feature collection JSON', function (): void {
111149
$geometryCollection = new GeometryCollection([
112150
new Polygon([
@@ -126,6 +164,63 @@
126164
expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection);
127165
});
128166

167+
it('creates geometry collection from feature collection with SRID from JSON', function (): void {
168+
$geometryCollection = new GeometryCollection([
169+
new Polygon([
170+
new LineString([
171+
new Point(0, 180),
172+
new Point(1, 179),
173+
new Point(2, 178),
174+
new Point(3, 177),
175+
new Point(0, 180),
176+
]),
177+
]),
178+
new Point(0, 180),
179+
], Srid::WGS84);
180+
181+
$geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}', Srid::WGS84);
182+
183+
expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection);
184+
});
185+
186+
it('creates geometry collection from feature collection from Array', function (): void {
187+
$geometryCollection = new GeometryCollection([
188+
new Polygon([
189+
new LineString([
190+
new Point(0, 180),
191+
new Point(1, 179),
192+
new Point(2, 178),
193+
new Point(3, 177),
194+
new Point(0, 180),
195+
]),
196+
]),
197+
new Point(0, 180),
198+
]);
199+
200+
$geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromArray(json_decode('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}',true));
201+
202+
expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection);
203+
});
204+
205+
it('creates geometry collection from feature collection with SRID from Array', function (): void {
206+
$geometryCollection = new GeometryCollection([
207+
new Polygon([
208+
new LineString([
209+
new Point(0, 180),
210+
new Point(1, 179),
211+
new Point(2, 178),
212+
new Point(3, 177),
213+
new Point(0, 180),
214+
]),
215+
]),
216+
new Point(0, 180),
217+
], Srid::WGS84);
218+
219+
$geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromArray(json_decode('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}',true), Srid::WGS84);
220+
221+
expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection);
222+
});
223+
129224
it('generates geometry collection JSON', function (): void {
130225
$geometryCollection = new GeometryCollection([
131226
new Polygon([

tests/Objects/LineStringTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@
6666
expect($lineStringFromJson)->toEqual($lineString);
6767
});
6868

69+
it('creates line string from Array', function (): void {
70+
$lineString = new LineString([
71+
new Point(0, 180),
72+
new Point(1, 179),
73+
]);
74+
75+
$lineStringFromJson = LineString::fromArray(["type"=>"LineString","coordinates"=>[[180,0],[179,1]]]);
76+
77+
expect($lineStringFromJson)->toEqual($lineString);
78+
});
79+
80+
it('creates line string with SRID from Array', function (): void {
81+
$lineString = new LineString([
82+
new Point(0, 180),
83+
new Point(1, 179),
84+
], Srid::WGS84->value);
85+
86+
$lineStringFromJson = LineString::fromArray(["type"=>"LineString","coordinates"=>[[180,0],[179,1]]], Srid::WGS84->value);
87+
88+
expect($lineStringFromJson)->toEqual($lineString);
89+
});
90+
6991
it('generates line string JSON', function (): void {
7092
$lineString = new LineString([
7193
new Point(0, 180),

tests/Objects/MultiLineStringTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@
7676
expect($multiLineStringFromJson)->toEqual($multiLineString);
7777
});
7878

79+
it('creates multi line string from Array', function (): void {
80+
$multiLineString = new MultiLineString([
81+
new LineString([
82+
new Point(0, 180),
83+
new Point(1, 179),
84+
]),
85+
]);
86+
87+
$multiLineStringFromJson = MultiLineString::fromArray(["type"=>"MultiLineString","coordinates"=>[[[180,0],[179,1]]]]);
88+
89+
expect($multiLineStringFromJson)->toEqual($multiLineString);
90+
});
91+
92+
it('creates multi line string with SRID from Array', function (): void {
93+
$multiLineString = new MultiLineString([
94+
new LineString([
95+
new Point(0, 180),
96+
new Point(1, 179),
97+
]),
98+
], Srid::WGS84->value);
99+
100+
$multiLineStringFromJson = MultiLineString::fromArray(["type"=>"MultiLineString","coordinates"=>[[[180,0],[179,1]]]], Srid::WGS84->value);
101+
102+
expect($multiLineStringFromJson)->toEqual($multiLineString);
103+
});
104+
79105
it('generates multi line string JSON', function (): void {
80106
$multiLineString = new MultiLineString([
81107
new LineString([

tests/Objects/MultiPointTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@
6161
expect($multiPointFromJson)->toEqual($multiPoint);
6262
});
6363

64+
it('creates multi point from Array', function (): void {
65+
$multiPoint = new MultiPoint([
66+
new Point(0, 180),
67+
]);
68+
69+
$multiPointFromJson = MultiPoint::fromArray(["type"=>"MultiPoint","coordinates"=>[[180,0]]]);
70+
71+
expect($multiPointFromJson)->toEqual($multiPoint);
72+
});
73+
74+
it('creates multi point with SRID from Array', function (): void {
75+
$multiPoint = new MultiPoint([
76+
new Point(0, 180),
77+
], Srid::WGS84->value);
78+
79+
$multiPointFromJson = MultiPoint::fromArray(["type"=>"MultiPoint","coordinates"=>[[180,0]]], Srid::WGS84->value);
80+
81+
expect($multiPointFromJson)->toEqual($multiPoint);
82+
});
83+
6484
it('generates multi point JSON', function (): void {
6585
$multiPoint = new MultiPoint([
6686
new Point(0, 180),

tests/Objects/MultiPolygonTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,42 @@
102102
expect($multiPolygonFromJson)->toEqual($multiPolygon);
103103
});
104104

105+
it('creates multi polygon from Array', function (): void {
106+
$multiPolygon = new MultiPolygon([
107+
new Polygon([
108+
new LineString([
109+
new Point(0, 180),
110+
new Point(1, 179),
111+
new Point(2, 178),
112+
new Point(3, 177),
113+
new Point(0, 180),
114+
]),
115+
]),
116+
]);
117+
118+
$multiPolygonFromJson = MultiPolygon::fromArray(["type"=>"MultiPolygon","coordinates"=>[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]]);
119+
120+
expect($multiPolygonFromJson)->toEqual($multiPolygon);
121+
});
122+
123+
it('creates multi polygon with SRID from Array', function (): void {
124+
$multiPolygon = new MultiPolygon([
125+
new Polygon([
126+
new LineString([
127+
new Point(0, 180),
128+
new Point(1, 179),
129+
new Point(2, 178),
130+
new Point(3, 177),
131+
new Point(0, 180),
132+
]),
133+
]),
134+
], Srid::WGS84->value);
135+
136+
$multiPolygonFromJson = MultiPolygon::fromArray(["type"=>"MultiPolygon","coordinates"=>[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]], Srid::WGS84->value);
137+
138+
expect($multiPolygonFromJson)->toEqual($multiPolygon);
139+
});
140+
105141
it('generates multi polygon JSON', function (): void {
106142
$multiPolygon = new MultiPolygon([
107143
new Polygon([

tests/Objects/PointTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@
4949
expect($pointFromJson)->toEqual($point);
5050
});
5151

52+
it('creates point from Array', function (): void {
53+
$point = new Point(0, 180);
54+
55+
$pointFromJson = Point::fromArray(["type"=>"Point","coordinates"=>[180,0]]);
56+
57+
expect($pointFromJson)->toEqual($point);
58+
});
59+
60+
it('creates point with SRID from Array', function (): void {
61+
$point = new Point(0, 180, Srid::WGS84->value);
62+
63+
$pointFromJson = Point::fromArray(["type"=>"Point","coordinates"=>[180,0]], Srid::WGS84->value);
64+
65+
expect($pointFromJson)->toEqual($point);
66+
});
67+
5268
it('generates point JSON', function (): void {
5369
$point = new Point(0, 180);
5470

tests/Objects/PolygonTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@
9191
expect($polygonFromJson)->toEqual($polygon);
9292
});
9393

94+
it('creates polygon from Array', function (): void {
95+
$polygon = new Polygon([
96+
new LineString([
97+
new Point(0, 180),
98+
new Point(1, 179),
99+
new Point(2, 178),
100+
new Point(3, 177),
101+
new Point(0, 180),
102+
]),
103+
]);
104+
105+
$polygonFromJson = Polygon::fromArray(["type"=>"Polygon","coordinates"=>[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]);
106+
107+
expect($polygonFromJson)->toEqual($polygon);
108+
});
109+
110+
it('creates polygon with SRID from Array', function (): void {
111+
$polygon = new Polygon([
112+
new LineString([
113+
new Point(0, 180),
114+
new Point(1, 179),
115+
new Point(2, 178),
116+
new Point(3, 177),
117+
new Point(0, 180),
118+
]),
119+
], Srid::WGS84->value);
120+
121+
$polygonFromJson = Polygon::fromArray(["type"=>"Polygon","coordinates"=>[[[180,0],[179,1],[178,2],[177,3],[180,0]]]], Srid::WGS84->value);
122+
123+
expect($polygonFromJson)->toEqual($polygon);
124+
});
125+
94126
it('generates polygon JSON', function (): void {
95127
$polygon = new Polygon([
96128
new LineString([

0 commit comments

Comments
 (0)