Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-1574] add validator to accept points or polygons #641

Merged
merged 8 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -755,7 +755,7 @@ public function validateGeojson($filePath)
'nonSurpassSizeLimit' => PolygonSize::geoJsonValid($feature['geometry']),
'insideCountry' => WithinCountry::getIntersectionDataWithSiteId($geojsonInside, $feature['properties']['site_id'])['valid'] ?? false,
'noSpikes' => Spikes::geoJsonValid($feature['geometry']),
'validPolygonType' => GeometryType::geoJsonValid($feature['geometry']),
'validPolygonType' => GeometryType::getGeometryType($feature['geometry']),
'nonSurpassEstimatedArea' => $isValidArea,
'completeData' => SitePolygonValidator::isValid('SCHEMA', $validationGeojson) && SitePolygonValidator::isValid('DATA', $validationGeojson),
];
45 changes: 39 additions & 6 deletions app/Validators/Extensions/Polygons/GeometryType.php
Original file line number Diff line number Diff line change
@@ -5,28 +5,47 @@
use App\Models\V2\PolygonGeometry;
use App\Validators\Extensions\Extension;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GeometryType extends Extension
{
public static $name = 'geometry_type';

public static $message = [
'key' => 'GEOMETRY_TYPE',
'message' => 'The geometry must be of polygon or multipolygon type',
'message' => 'All geometries in the collection must be either all points OR all polygons/multipolygons',
];

public const VALID_TYPE_POLYGON = 'POLYGON';
public const VALID_TYPE_MULTIPOLYGON = 'MULTIPOLYGON';
public const VALID_TYPE_POINT = 'POINT';

public static function passes($attribute, $value, $parameters, $validator): bool
{
if (is_string($value)) {
// assume we have a DB UUID
return self::uuidValid($value);
if (! is_array($value)) {
return false;
}

// assume we have GeoJSON
return self::geoJsonValid($value);
$types = [];
foreach ($value as $feature) {
$type = self::getGeometryType($feature);
Log::info("Geometry type: $type");
if (! in_array($type, [
self::VALID_TYPE_POLYGON,
self::VALID_TYPE_MULTIPOLYGON,
self::VALID_TYPE_POINT,
])) {
return false;
}

$types[] = $type;
}

$hasPoints = in_array(self::VALID_TYPE_POINT, $types);
$hasPolygons = in_array(self::VALID_TYPE_POLYGON, $types) ||
in_array(self::VALID_TYPE_MULTIPOLYGON, $types);

return ! ($hasPoints && $hasPolygons);
}

public static function uuidValid($uuid): bool
@@ -36,6 +55,20 @@ public static function uuidValid($uuid): bool
return $geometryType === self::VALID_TYPE_POLYGON || $geometryType === self::VALID_TYPE_MULTIPOLYGON;
}

public static function getGeometryType($feature): string
{
if (is_string($feature)) {
return PolygonGeometry::getGeometryType($feature);
}

$result = DB::selectOne(
'SELECT ST_GeometryType(ST_GeomFromGeoJSON(:geojson)) AS geometry_type',
['geojson' => json_encode($feature)]
);

return $result->geometry_type;
}

public static function geoJsonValid($geojson): bool
{
$result = DB::selectOne(
4 changes: 2 additions & 2 deletions app/Validators/SitePolygonValidator.php
Original file line number Diff line number Diff line change
@@ -53,8 +53,8 @@ class SitePolygonValidator extends Validator
];

public const GEOMETRY_TYPE = [
'features' => 'required|array',
'features.*' => 'geometry_type',
'type' => 'required|in:FeatureCollection',
'features' => 'required|array|geometry_type',
];

public const GEOMETRY_TYPE_UUID = [
5 changes: 5 additions & 0 deletions tests/Unit/Validators/Extensions/PolygonValidatorsTest.php
Original file line number Diff line number Diff line change
@@ -54,6 +54,11 @@ public function test_estimated_area()
public function test_geometry_type()
{
$this->runValidationTest('GEOMETRY_TYPE');
$mixedGeojsonPath = self::FILES_DIR . 'geometry_type_mixed.geojson';
$mixedGeojson = json_decode(file_get_contents($mixedGeojsonPath), true);
$this->assertFalse(
SitePolygonValidator::isValid('GEOMETRY_TYPE', $mixedGeojson, false)
);
}

public function test_schema()
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"coordinates":[[[33.03873351756249,-2.0430255122929424],[33.040856449501575,-2.0647156071144366]]],"type":"LineString"}}]}
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"coordinates":[[33.03873351756249,-2.0430255122929424],[33.040856449501575,-2.0647156071144366]],"type":"LineString"}}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"poly_name": "Point Feature"
},
"geometry": {
"type": "Point",
"coordinates": [
30,
10
]
}
},
{
"type": "Feature",
"properties": {
"poly_name": "Polygon Feature"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
30,
10
],
[
40,
40
],
[
20,
40
],
[
10,
20
],
[
30,
10
]
]
]
}
},
{
"type": "Feature",
"properties": {
"poly_name": "Another Point Feature"
},
"geometry": {
"type": "Point",
"coordinates": [
15,
15
]
}
}
]
}