From 5eca8de2c5d2c5e8827b4852d72b395bdd65d127 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 26 Nov 2024 12:05:29 -0400 Subject: [PATCH 1/9] [TM-1460] add migration to add index --- ...3_153432_create_polygon_geometry_table.php | 1 - ..._add_spatial_index_to_polygon_geometry.php | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php diff --git a/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php b/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php index 4bbd26d7a..b50a45c40 100644 --- a/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php +++ b/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php @@ -15,7 +15,6 @@ public function up(): void $table->id(); $table->uuid('uuid')->unique(); $table->geometry('geom')->nullable(); - ; $table->softDeletes(); $table->timestamps(); }); diff --git a/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php new file mode 100644 index 000000000..e23d31b13 --- /dev/null +++ b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php @@ -0,0 +1,34 @@ +geometry('geom')->nullable(false)->change(); + // $table->spatialIndex('geom', 'polygon_geometry_geom_spatial_idx'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('polygon_geometry', function (Blueprint $table) { + // $table->dropIndex('polygon_geometry_geom_spatial_idx'); + $table->geometry('geom')->nullable()->change(); + }); + } +} From f4aa3aaa4e34ac58737ebcd4ecdace7cbacbeb5a Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 26 Nov 2024 16:53:49 -0400 Subject: [PATCH 2/9] [TM-1460] add index to polygon geometry and validate overlapping with bbox --- .../Extensions/Polygons/NotOverlapping.php | 25 +++++++-------- ...3_153432_create_polygon_geometry_table.php | 1 + ..._add_spatial_index_to_polygon_geometry.php | 31 +++++++++---------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/app/Validators/Extensions/Polygons/NotOverlapping.php b/app/Validators/Extensions/Polygons/NotOverlapping.php index d8d039696..6e4582b6a 100644 --- a/app/Validators/Extensions/Polygons/NotOverlapping.php +++ b/app/Validators/Extensions/Polygons/NotOverlapping.php @@ -31,12 +31,13 @@ public static function getIntersectionData(string $polygonUuid): array 'status' => 404, ]; } - - $relatedPolyIds = $sitePolygon->project->sitePolygons() - ->where('poly_id', '!=', $polygonUuid) - ->pluck('poly_id'); + $bboxFilteredPolyIds = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') + ->where('polygon_geometry.uuid', '!=', $polygonUuid) + ->whereRaw('ST_Envelope(polygon_geometry.geom) && (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?)', [$polygonUuid]) + ->pluck('polygon_geometry.uuid'); + $intersects = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') - ->whereIn('polygon_geometry.uuid', $relatedPolyIds) + ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) ->select([ 'polygon_geometry.uuid', 'site_polygon.poly_name', @@ -47,19 +48,15 @@ public static function getIntersectionData(string $polygonUuid): array ->addBinding($polygonUuid, 'select') ->addBinding($polygonUuid, 'select') ->get(); - + $mainPolygonArea = PolygonGeometry::where('uuid', $polygonUuid) ->value(DB::raw('ST_Area(geom)')); + $extra_info = []; foreach ($intersects as $intersect) { if ($intersect->intersects) { $minArea = min($mainPolygonArea, $intersect->area); - if ($minArea > 0) { - $percentage = ($intersect->intersection_area / $minArea) * 100; - $percentage = round($percentage, 2); - } else { - $percentage = 100; - } + $percentage = $minArea > 0 ? round(($intersect->intersection_area / $minArea) * 100, 2) : 100; $extra_info[] = [ 'poly_uuid' => $intersect->uuid, 'poly_name' => $intersect->poly_name, @@ -68,8 +65,7 @@ public static function getIntersectionData(string $polygonUuid): array ]; } } - - + return [ 'valid' => ! $intersects->contains('intersects', 1), 'uuid' => $polygonUuid, @@ -77,6 +73,7 @@ public static function getIntersectionData(string $polygonUuid): array 'extra_info' => $extra_info, ]; } + public static function checkFeatureIntersections($geojsonFeatures): array { diff --git a/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php b/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php index b50a45c40..4bbd26d7a 100644 --- a/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php +++ b/database/migrations/2024_03_13_153432_create_polygon_geometry_table.php @@ -15,6 +15,7 @@ public function up(): void $table->id(); $table->uuid('uuid')->unique(); $table->geometry('geom')->nullable(); + ; $table->softDeletes(); $table->timestamps(); }); diff --git a/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php index e23d31b13..15c5d3b5f 100644 --- a/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php +++ b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; class AddSpatialIndexToPolygonGeometry extends Migration @@ -12,23 +13,19 @@ class AddSpatialIndexToPolygonGeometry extends Migration * @return void */ public function up() - { - Schema::table('polygon_geometry', function (Blueprint $table) { - $table->geometry('geom')->nullable(false)->change(); - // $table->spatialIndex('geom', 'polygon_geometry_geom_spatial_idx'); - }); - } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() { - Schema::table('polygon_geometry', function (Blueprint $table) { - // $table->dropIndex('polygon_geometry_geom_spatial_idx'); - $table->geometry('geom')->nullable()->change(); - }); - } + Schema::table('polygon_geometry', function (Blueprint $table) { + DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NOT NULL'); + DB::statement('CREATE SPATIAL INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry (geom)'); + }); + } + + public function down() + { + Schema::table('polygon_geometry', function (Blueprint $table) { + DB::statement('DROP INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry'); + DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NULL'); + }); + } } From 8cee16433239a7c6e11dc0144c7cbfb7c91a57ee Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 26 Nov 2024 16:54:07 -0400 Subject: [PATCH 3/9] [TM-1460] lint --- .../Extensions/Polygons/NotOverlapping.php | 9 +++---- ..._add_spatial_index_to_polygon_geometry.php | 25 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/Validators/Extensions/Polygons/NotOverlapping.php b/app/Validators/Extensions/Polygons/NotOverlapping.php index 6e4582b6a..f7774814d 100644 --- a/app/Validators/Extensions/Polygons/NotOverlapping.php +++ b/app/Validators/Extensions/Polygons/NotOverlapping.php @@ -35,7 +35,7 @@ public static function getIntersectionData(string $polygonUuid): array ->where('polygon_geometry.uuid', '!=', $polygonUuid) ->whereRaw('ST_Envelope(polygon_geometry.geom) && (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?)', [$polygonUuid]) ->pluck('polygon_geometry.uuid'); - + $intersects = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) ->select([ @@ -48,10 +48,10 @@ public static function getIntersectionData(string $polygonUuid): array ->addBinding($polygonUuid, 'select') ->addBinding($polygonUuid, 'select') ->get(); - + $mainPolygonArea = PolygonGeometry::where('uuid', $polygonUuid) ->value(DB::raw('ST_Area(geom)')); - + $extra_info = []; foreach ($intersects as $intersect) { if ($intersect->intersects) { @@ -65,7 +65,7 @@ public static function getIntersectionData(string $polygonUuid): array ]; } } - + return [ 'valid' => ! $intersects->contains('intersects', 1), 'uuid' => $polygonUuid, @@ -73,7 +73,6 @@ public static function getIntersectionData(string $polygonUuid): array 'extra_info' => $extra_info, ]; } - public static function checkFeatureIntersections($geojsonFeatures): array { diff --git a/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php index 15c5d3b5f..1a2e14336 100644 --- a/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php +++ b/database/migrations/2024_11_26_154102_add_spatial_index_to_polygon_geometry.php @@ -13,19 +13,18 @@ class AddSpatialIndexToPolygonGeometry extends Migration * @return void */ public function up() + { + Schema::table('polygon_geometry', function (Blueprint $table) { + DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NOT NULL'); + DB::statement('CREATE SPATIAL INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry (geom)'); + }); + } + public function down() { - Schema::table('polygon_geometry', function (Blueprint $table) { - DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NOT NULL'); - DB::statement('CREATE SPATIAL INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry (geom)'); - }); - } - - public function down() - { - Schema::table('polygon_geometry', function (Blueprint $table) { - DB::statement('DROP INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry'); - DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NULL'); - }); - } + Schema::table('polygon_geometry', function (Blueprint $table) { + DB::statement('DROP INDEX polygon_geometry_geom_spatial_idx ON polygon_geometry'); + DB::statement('ALTER TABLE polygon_geometry MODIFY COLUMN geom GEOMETRY NULL'); + }); + } } From ad3d8e3f06e6652ddff6a44984d3faa9723fe990 Mon Sep 17 00:00:00 2001 From: JORGE Date: Wed, 27 Nov 2024 12:51:49 -0400 Subject: [PATCH 4/9] [TM-1460] add overlap check with bboxes --- app/Jobs/RunSitePolygonsValidationJob.php | 4 +++ .../Extensions/Polygons/NotOverlapping.php | 32 +++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/Jobs/RunSitePolygonsValidationJob.php b/app/Jobs/RunSitePolygonsValidationJob.php index 96fe5e6f5..fba32831e 100644 --- a/app/Jobs/RunSitePolygonsValidationJob.php +++ b/app/Jobs/RunSitePolygonsValidationJob.php @@ -49,6 +49,7 @@ public function handle(PolygonValidationService $validationService) { try { $delayedJob = DelayedJob::findOrFail($this->delayed_job_id); + $startOld = microtime(true); foreach ($this->sitePolygonsUuids as $polygonUuid) { $request = new Request(['uuid' => $polygonUuid]); $validationService->validateOverlapping($request); @@ -61,6 +62,9 @@ public function handle(PolygonValidationService $validationService) $validationService->validateEstimatedArea($request); $validationService->validateDataInDB($request); } + $timeOld = microtime(true) - $startOld; + + Log::info("Approach took {$timeOld} seconds"); $delayedJob->update([ 'status' => DelayedJob::STATUS_SUCCEEDED, diff --git a/app/Validators/Extensions/Polygons/NotOverlapping.php b/app/Validators/Extensions/Polygons/NotOverlapping.php index f7774814d..38157c8a3 100644 --- a/app/Validators/Extensions/Polygons/NotOverlapping.php +++ b/app/Validators/Extensions/Polygons/NotOverlapping.php @@ -6,6 +6,7 @@ use App\Models\V2\Sites\SitePolygon; use App\Validators\Extensions\Extension; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class NotOverlapping extends Extension { @@ -31,23 +32,26 @@ public static function getIntersectionData(string $polygonUuid): array 'status' => 404, ]; } + $relatedPolyIds = $sitePolygon->project->sitePolygons() + ->where('poly_id', '!=', $polygonUuid) + ->pluck('poly_id'); $bboxFilteredPolyIds = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') - ->where('polygon_geometry.uuid', '!=', $polygonUuid) - ->whereRaw('ST_Envelope(polygon_geometry.geom) && (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?)', [$polygonUuid]) - ->pluck('polygon_geometry.uuid'); + ->whereIn('polygon_geometry.uuid', $relatedPolyIds) + ->whereRaw('ST_Intersects(ST_Envelope(polygon_geometry.geom), (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?))', [$polygonUuid]) + ->pluck('polygon_geometry.uuid'); $intersects = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') - ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) - ->select([ - 'polygon_geometry.uuid', - 'site_polygon.poly_name', - DB::raw('ST_Intersects(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?)) as intersects'), - DB::raw('ST_Area(ST_Intersection(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?))) as intersection_area'), - DB::raw('ST_Area(polygon_geometry.geom) as area'), - ]) - ->addBinding($polygonUuid, 'select') - ->addBinding($polygonUuid, 'select') - ->get(); + ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) + ->select([ + 'polygon_geometry.uuid', + 'site_polygon.poly_name', + DB::raw('ST_Intersects(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?)) as intersects'), + DB::raw('ST_Area(ST_Intersection(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?))) as intersection_area'), + DB::raw('ST_Area(polygon_geometry.geom) as area'), + ]) + ->addBinding($polygonUuid, 'select') + ->addBinding($polygonUuid, 'select') + ->get(); $mainPolygonArea = PolygonGeometry::where('uuid', $polygonUuid) ->value(DB::raw('ST_Area(geom)')); From e32f9109c740eff07b30431d1d3e9dd4888d4956 Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 28 Nov 2024 15:55:49 -0400 Subject: [PATCH 5/9] [TM-1523] finish bbox precalculus for overlapping check --- app/Jobs/RunSitePolygonsValidationJob.php | 4 -- .../Extensions/Polygons/NotOverlapping.php | 59 ++++++++++--------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/app/Jobs/RunSitePolygonsValidationJob.php b/app/Jobs/RunSitePolygonsValidationJob.php index fba32831e..96fe5e6f5 100644 --- a/app/Jobs/RunSitePolygonsValidationJob.php +++ b/app/Jobs/RunSitePolygonsValidationJob.php @@ -49,7 +49,6 @@ public function handle(PolygonValidationService $validationService) { try { $delayedJob = DelayedJob::findOrFail($this->delayed_job_id); - $startOld = microtime(true); foreach ($this->sitePolygonsUuids as $polygonUuid) { $request = new Request(['uuid' => $polygonUuid]); $validationService->validateOverlapping($request); @@ -62,9 +61,6 @@ public function handle(PolygonValidationService $validationService) $validationService->validateEstimatedArea($request); $validationService->validateDataInDB($request); } - $timeOld = microtime(true) - $startOld; - - Log::info("Approach took {$timeOld} seconds"); $delayedJob->update([ 'status' => DelayedJob::STATUS_SUCCEEDED, diff --git a/app/Validators/Extensions/Polygons/NotOverlapping.php b/app/Validators/Extensions/Polygons/NotOverlapping.php index 38157c8a3..de9862bcc 100644 --- a/app/Validators/Extensions/Polygons/NotOverlapping.php +++ b/app/Validators/Extensions/Polygons/NotOverlapping.php @@ -6,7 +6,6 @@ use App\Models\V2\Sites\SitePolygon; use App\Validators\Extensions\Extension; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Log; class NotOverlapping extends Extension { @@ -25,53 +24,55 @@ public static function passes($attribute, $value, $parameters, $validator): bool public static function getIntersectionData(string $polygonUuid): array { $sitePolygon = SitePolygon::forPolygonGeometry($polygonUuid)->first(); - if ($sitePolygon === null) { - return [ - 'valid' => false, - 'error' => 'Site polygon not found for the given polygon ID', - 'status' => 404, - ]; + if (! $sitePolygon) { + return ['valid' => false, 'error' => 'Site polygon not found', 'status' => 404]; } + $relatedPolyIds = $sitePolygon->project->sitePolygons() ->where('poly_id', '!=', $polygonUuid) ->pluck('poly_id'); + $bboxFilteredPolyIds = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') - ->whereIn('polygon_geometry.uuid', $relatedPolyIds) - ->whereRaw('ST_Intersects(ST_Envelope(polygon_geometry.geom), (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?))', [$polygonUuid]) - ->pluck('polygon_geometry.uuid'); + ->whereIn('polygon_geometry.uuid', $relatedPolyIds) + ->whereRaw('ST_Intersects(ST_Envelope(polygon_geometry.geom), (SELECT ST_Envelope(geom) FROM polygon_geometry WHERE uuid = ?))', [$polygonUuid]) + ->pluck('polygon_geometry.uuid'); $intersects = PolygonGeometry::join('site_polygon', 'polygon_geometry.uuid', '=', 'site_polygon.poly_id') - ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) - ->select([ - 'polygon_geometry.uuid', - 'site_polygon.poly_name', - DB::raw('ST_Intersects(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?)) as intersects'), - DB::raw('ST_Area(ST_Intersection(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?))) as intersection_area'), - DB::raw('ST_Area(polygon_geometry.geom) as area'), - ]) - ->addBinding($polygonUuid, 'select') - ->addBinding($polygonUuid, 'select') - ->get(); + ->whereIn('polygon_geometry.uuid', $bboxFilteredPolyIds) + ->select([ + 'polygon_geometry.uuid', + 'site_polygon.poly_name', + DB::raw('ST_Intersects(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?)) as intersects'), + DB::raw('ST_Area(ST_Intersection(polygon_geometry.geom, (SELECT geom FROM polygon_geometry WHERE uuid = ?))) as intersection_area'), + DB::raw('ST_Area(polygon_geometry.geom) as area'), + ]) + ->addBinding($polygonUuid, 'select') + ->addBinding($polygonUuid, 'select') + ->get(); $mainPolygonArea = PolygonGeometry::where('uuid', $polygonUuid) ->value(DB::raw('ST_Area(geom)')); - $extra_info = []; - foreach ($intersects as $intersect) { - if ($intersect->intersects) { + $extra_info = $intersects + ->filter(fn ($intersect) => $intersect->intersects) + ->map(function ($intersect) use ($mainPolygonArea) { $minArea = min($mainPolygonArea, $intersect->area); - $percentage = $minArea > 0 ? round(($intersect->intersection_area / $minArea) * 100, 2) : 100; - $extra_info[] = [ + $percentage = $minArea > 0 + ? round(($intersect->intersection_area / $minArea) * 100, 2) + : 100; + + return [ 'poly_uuid' => $intersect->uuid, 'poly_name' => $intersect->poly_name, 'percentage' => $percentage, 'intersectSmaller' => ($intersect->area < $mainPolygonArea), ]; - } - } + }) + ->values() + ->toArray(); return [ - 'valid' => ! $intersects->contains('intersects', 1), + 'valid' => empty($extra_info), 'uuid' => $polygonUuid, 'project_id' => $sitePolygon->project_id, 'extra_info' => $extra_info, From 57e8189bc225cef17cd160c87e087792aba47ea5 Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 28 Nov 2024 16:05:57 -0400 Subject: [PATCH 6/9] [TM-1523] add index to sitepolygon --- ...0341_add_index_to_site_polygon_poly_id.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php diff --git a/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php b/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php new file mode 100644 index 000000000..ab407368e --- /dev/null +++ b/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php @@ -0,0 +1,22 @@ +index('poly_id', 'idx_site_polygon_poly_id'); + }); + } + + public function down(): void + { + Schema::table('site_polygon', function (Blueprint $table) { + $table->dropIndex('idx_site_polygon_poly_id'); + }); + } +}; From 8d141978457bb0cc626008189fc8d4794567b9f0 Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 28 Nov 2024 16:21:55 -0400 Subject: [PATCH 7/9] [TM-1523] lint --- ...0341_add_index_to_site_polygon_poly_id.php | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php b/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php index ab407368e..e072dfafc 100644 --- a/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php +++ b/database/migrations/2024_11_28_200341_add_index_to_site_polygon_poly_id.php @@ -4,19 +4,18 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ - public function up(): void - { - Schema::table('site_polygon', function (Blueprint $table) { - $table->index('poly_id', 'idx_site_polygon_poly_id'); - }); - } +return new class extends Migration { + public function up(): void + { + Schema::table('site_polygon', function (Blueprint $table) { + $table->index('poly_id', 'idx_site_polygon_poly_id'); + }); + } - public function down(): void - { - Schema::table('site_polygon', function (Blueprint $table) { - $table->dropIndex('idx_site_polygon_poly_id'); - }); - } + public function down(): void + { + Schema::table('site_polygon', function (Blueprint $table) { + $table->dropIndex('idx_site_polygon_poly_id'); + }); + } }; From 654ca455b6bed5711fd0bca6775549faf36c5d4a Mon Sep 17 00:00:00 2001 From: JORGE Date: Fri, 29 Nov 2024 14:45:12 -0400 Subject: [PATCH 8/9] [TM-1523] add initial factory with non empty geometry,for index --- .../factories/V2/PolygonGeometryFactory.php | 12 ++++--- .../factories/V2/Sites/SitePolygonFactory.php | 33 ++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/database/factories/V2/PolygonGeometryFactory.php b/database/factories/V2/PolygonGeometryFactory.php index bea9177a1..5c64f4679 100644 --- a/database/factories/V2/PolygonGeometryFactory.php +++ b/database/factories/V2/PolygonGeometryFactory.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class PolygonGeometryFactory extends Factory { @@ -16,12 +17,15 @@ public function definition() public function geojson(string|array $geojson) { - $geom = DB::raw("ST_GeomFromGeoJSON('$geojson')"); - - return $this->state(function (array $attributes) use ($geom) { + if (is_array($geojson)) { + $geojson = json_encode($geojson); + } + $geomExpression = DB::raw("ST_GeomFromGeoJSON('$geojson')"); + return $this->state(function (array $attributes) use ($geomExpression) { return [ - 'geom' => $geom, + 'geom' => $geomExpression, ]; }); } + } diff --git a/database/factories/V2/Sites/SitePolygonFactory.php b/database/factories/V2/Sites/SitePolygonFactory.php index 1e8415ca5..ac960df06 100644 --- a/database/factories/V2/Sites/SitePolygonFactory.php +++ b/database/factories/V2/Sites/SitePolygonFactory.php @@ -8,15 +8,30 @@ class SitePolygonFactory extends Factory { - public function definition() - { - return [ - 'poly_id' => PolygonGeometry::factory()->create()->uuid, - 'site_id' => Site::factory()->create()->uuid, - 'calc_area' => $this->faker->numberBetween(2.0, 50.0), - ]; - } - + public function definition() + { + $geojson = [ + 'type' => 'Polygon', + 'coordinates' => [ + [ + [0, 0], + [1, 0], + [1, 1], + [0, 1], + [0, 0] + ] + ] + ]; + + return [ + 'poly_id' => PolygonGeometry::factory() + ->geojson($geojson) + ->create() + ->uuid, + 'site_id' => Site::factory()->create()->uuid, + 'calc_area' => $this->faker->numberBetween(2.0, 50.0), + ]; + } public function site(Site $site) { return $this->state(function (array $attributes) use ($site) { From c00d7170113e8f8aa0b17f73d34b605802703161 Mon Sep 17 00:00:00 2001 From: JORGE Date: Fri, 29 Nov 2024 14:45:29 -0400 Subject: [PATCH 9/9] [TM-1523] lint --- .../factories/V2/PolygonGeometryFactory.php | 3 +- .../factories/V2/Sites/SitePolygonFactory.php | 49 ++++++++++--------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/database/factories/V2/PolygonGeometryFactory.php b/database/factories/V2/PolygonGeometryFactory.php index 5c64f4679..5c75a788c 100644 --- a/database/factories/V2/PolygonGeometryFactory.php +++ b/database/factories/V2/PolygonGeometryFactory.php @@ -4,7 +4,6 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Log; class PolygonGeometryFactory extends Factory { @@ -21,11 +20,11 @@ public function geojson(string|array $geojson) $geojson = json_encode($geojson); } $geomExpression = DB::raw("ST_GeomFromGeoJSON('$geojson')"); + return $this->state(function (array $attributes) use ($geomExpression) { return [ 'geom' => $geomExpression, ]; }); } - } diff --git a/database/factories/V2/Sites/SitePolygonFactory.php b/database/factories/V2/Sites/SitePolygonFactory.php index ac960df06..13d595f0c 100644 --- a/database/factories/V2/Sites/SitePolygonFactory.php +++ b/database/factories/V2/Sites/SitePolygonFactory.php @@ -8,30 +8,31 @@ class SitePolygonFactory extends Factory { - public function definition() - { - $geojson = [ - 'type' => 'Polygon', - 'coordinates' => [ - [ - [0, 0], - [1, 0], - [1, 1], - [0, 1], - [0, 0] - ] - ] - ]; - - return [ - 'poly_id' => PolygonGeometry::factory() - ->geojson($geojson) - ->create() - ->uuid, - 'site_id' => Site::factory()->create()->uuid, - 'calc_area' => $this->faker->numberBetween(2.0, 50.0), - ]; - } + public function definition() + { + $geojson = [ + 'type' => 'Polygon', + 'coordinates' => [ + [ + [0, 0], + [1, 0], + [1, 1], + [0, 1], + [0, 0], + ], + ], + ]; + + return [ + 'poly_id' => PolygonGeometry::factory() + ->geojson($geojson) + ->create() + ->uuid, + 'site_id' => Site::factory()->create()->uuid, + 'calc_area' => $this->faker->numberBetween(2.0, 50.0), + ]; + } + public function site(Site $site) { return $this->state(function (array $attributes) use ($site) {