-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper support for GeoJSON Feature & FeatureCollection
- Loading branch information
Showing
11 changed files
with
620 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brick\Geo\IO\GeoJSON; | ||
|
||
use Brick\Geo\Geometry; | ||
use stdClass; | ||
|
||
/** | ||
* A GeoJSON Feature. This class is immutable. | ||
*/ | ||
final class Feature | ||
{ | ||
/** | ||
* The contained geometry, or null if this feature is not associated with a geometry. | ||
* | ||
* @var Geometry|null | ||
*/ | ||
private ?Geometry $geometry; | ||
|
||
/** | ||
* An optional key-value map of feature properties. Must be convertible to JSON. | ||
*/ | ||
private ?stdClass $properties; | ||
|
||
/** | ||
* @param Geometry|null $geometry | ||
* @param stdClass|null $properties | ||
*/ | ||
public function __construct(?Geometry $geometry = null, ?stdClass $properties = null) | ||
{ | ||
$this->geometry = $geometry; | ||
$this->properties = $properties; | ||
} | ||
|
||
public function getGeometry(): ?Geometry | ||
{ | ||
return $this->geometry; | ||
} | ||
|
||
/** | ||
* Returns a copy of this Feature with the given geometry. | ||
*/ | ||
public function withGeometry(?Geometry $geometry): Feature | ||
{ | ||
$that = clone $this; | ||
$that->geometry = $geometry; | ||
|
||
return $that; | ||
} | ||
|
||
public function getProperties(): ?stdClass | ||
{ | ||
return $this->properties; | ||
} | ||
|
||
/** | ||
* Returns a copy of this Feature with the given properties. | ||
* | ||
* @param stdClass|null $properties An optional key-value map of feature properties. Must be convertible to JSON. | ||
*/ | ||
public function withProperties(?stdClass $properties): Feature | ||
{ | ||
$that = clone $this; | ||
$this->properties = $properties; | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* @param string $name The property name. | ||
* @param mixed $default The default value if the property is not found. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getProperty(string $name, $default = null) | ||
{ | ||
if ($this->properties === null || ! property_exists($this->properties, $name)) { | ||
return $default; | ||
} | ||
|
||
return $this->properties->{$name}; | ||
} | ||
|
||
/** | ||
* Returns a copy of this Feature with the given property set. | ||
* | ||
* @param string $name The property name. | ||
* @param mixed $value The value. Must be convertible to JSON. | ||
*/ | ||
public function withProperty(string $name, $value): Feature | ||
{ | ||
$that = clone $this; | ||
|
||
if ($that->properties === null) { | ||
$that->properties = new stdClass(); | ||
} | ||
|
||
$that->properties->{$name} = $value; | ||
|
||
return $that; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brick\Geo\IO\GeoJSON; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* A GeoJSON FeatureCollection. This class is immutable. | ||
*/ | ||
final class FeatureCollection | ||
{ | ||
/** | ||
* The contained features. | ||
* | ||
* @var Feature[] | ||
*/ | ||
private array $features = []; | ||
|
||
/** | ||
* @param Feature[] $features The GeoJSON Features. | ||
*/ | ||
public function __construct(array $features) | ||
{ | ||
foreach ($features as $feature) { | ||
if (! $feature instanceof Feature) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Expected instance of %s, got %s.', | ||
Feature::class, | ||
is_object($feature) ? get_class($feature) : gettype($feature) | ||
)); | ||
} | ||
} | ||
|
||
$this->features = array_values($features); | ||
} | ||
|
||
/** | ||
* @return Feature[] | ||
*/ | ||
public function getFeatures(): array | ||
{ | ||
return $this->features; | ||
} | ||
|
||
/** | ||
* Returns a copy of this FeatureCollection with the given Feature added. | ||
* This instance is immutable and unaffected by this method call. | ||
*/ | ||
public function withAddedFeature(Feature $feature): FeatureCollection | ||
{ | ||
$that = clone $this; | ||
|
||
$that->features[] = $feature; | ||
|
||
return $that; | ||
} | ||
} |
Oops, something went wrong.