oportunidades 7
tests / ci (push) Waiting to run

This commit is contained in:
2026-10-13 11:40:29 +02:00
parent 20971d523f
commit 287e991486
2902 changed files with 1085456 additions and 25957 deletions
+11 -10
View File
@@ -13,7 +13,7 @@ class TerritoryGeometryImport
{
public function fingerprint(): string
{
return hash('sha256', implode('', array_map(fn ($f) => hash_file('sha256', $f), [__FILE__, config_path('geometry-import.php'), config_path('geometry-correspondences.php'), app_path('Http/Controllers/TerritoryGeometryImportController.php'), base_path('routes/web.php'), resource_path('js/pages/management/territory-geometry-import.tsx'),
return hash('sha256', implode('', array_map(fn ($f) => hash_file('sha256', $f), [__FILE__, app_path('Services/RetainedGeoJsonCollection.php'), config_path('geometry-import.php'), config_path('geometry-correspondences.php'), app_path('Http/Controllers/TerritoryGeometryImportController.php'), base_path('routes/web.php'), resource_path('js/pages/management/territory-geometry-import.tsx'),
base_path('bootstrap/app.php'), app_path('Providers/FortifyServiceProvider.php'), config_path('fortify.php'),
resource_path('js/pages/auth/login.tsx'), resource_path('js/pages/auth/two-factor-challenge.tsx')])));
}
@@ -52,19 +52,20 @@ class TerritoryGeometryImport
$text = $this->readFile($metadata);
abort_unless(str_contains($text, 'Mozambique INE - Instituto Nacional de Estatistica') && str_contains($text, '2019-06-07'), 422, 'Metadados do exemplar divergentes.');
$sourceHash = hash_file('sha256', $file);
$source = json_decode($this->readFile($file), true, 512, JSON_THROW_ON_ERROR);
abort_unless(($source['type'] ?? '') === 'FeatureCollection' && count($source['features'] ?? []) === [1 => 11, 2 => 159, 3 => 411][$level] && ! ($source['exceededTransferLimit'] ?? false), 422, 'Exemplar incompleto.');
$collection = new RetainedGeoJsonCollection($file);
$source = $collection->manifest();
abort_unless(($source['type'] ?? '') === 'FeatureCollection' && $source['feature_count'] === [1 => 11, 2 => 159, 3 => 411][$level] && ! ($source['exceededTransferLimit'] ?? false), 422, 'Exemplar incompleto.');
$locations = DB::table('geo_locations')->where('location_type', [1 => 'province', 2 => 'district', 3 => 'admin_post'][$level])->whereNull('deleted_at')->get(['id', 'name', 'parent_id', 'source_system', 'source_code', 'external_id']);
$crosswalk = config('geometry-correspondences');
$correspondences = $crosswalk['rows'][$level] ?? [];
abort_if($correspondences && (($crosswalk['edition'] ?? null) !== '2019-06-07' || ($crosswalk['source_sha256'][$level] ?? null) !== $sourceHash), 422, 'Correspondências não pertencem a este exemplar; nenhuma aproximação aplicada.');
$normalize = fn ($n) => strtolower(trim(Str::ascii($n)));
$countryNames = array_map($normalize, [$source['features'][0]['properties']['ADM0_PT'], $source['features'][0]['properties']['ADM0_EN']]);
$countryNames = array_map($normalize, [$source['first_properties']['ADM0_PT'], $source['first_properties']['ADM0_EN']]);
$countries = DB::table('geo_locations')->where('location_type', 'country')->whereNull('deleted_at')->get(['id', 'name'])->filter(fn ($c) => in_array($normalize($c->name), $countryNames, true))->values();
$countryId = count($countries) === 1 ? $countries[0]->id : null;
$parentSourceHash = $level > 1 ? hash_file('sha256', $directory.'/moz-adm'.($level - 1).'-ine-20190607.geojson') : null;
$rows = [];
foreach ($source['features'] as $feature) {
foreach ($collection->features() as $feature) {
$p = $feature['properties'];
abort_unless(($p['ADM0_PCODE'] ?? '') === 'MZ', 422, 'Âmbito territorial do exemplar divergente.');
$external = '2019-06-07:adm'.$level.':'.$p['ADM'.$level.'_PCODE'];
@@ -136,9 +137,9 @@ class TerritoryGeometryImport
abort_unless(hash_equals($plan['hash'], $data['hash']), 409, 'Correspondências, fonte ou estado alterados; repita a pré-visualização.');
abort_unless($plan['scan_ready'], 423, 'Scan limpo atual ligado ao original necessário.');
abort_if(count(array_filter($plan['rows'], fn ($r) => str_contains($r['status'], 'conflict') || $r['status'] === 'invalid_geometry')) > 0, 409, 'Conflito de identidade/geometria: nenhuma escrita aplicada.');
$bytes = $this->readFile(config('geometry-import.directory').'/moz-adm'.$data['level'].'-ine-20190607.geojson');
abort_unless(hash('sha256', $bytes) === $plan['source_sha256'], 409);
$source = json_decode($bytes, true, 512, JSON_THROW_ON_ERROR);
$sourcePath = config('geometry-import.directory').'/moz-adm'.$data['level'].'-ine-20190607.geojson';
abort_unless(hash_file('sha256', $sourcePath) === $plan['source_sha256'], 409);
$collection = new RetainedGeoJsonCollection($sourcePath);
$metadata = config('geometry-import.directory').'/moz-adm'.$data['level'].'-ine-metadata.html';
$metadataSize = filesize($metadata);
abort_unless(is_int($metadataSize), 422, 'Metadados indisponíveis durante a aplicação.');
@@ -149,14 +150,14 @@ class TerritoryGeometryImport
$batch = (string) Str::uuid();
$created = [];
$reused = [];
foreach ($plan['rows'] as $i => $row) {
foreach ($collection->features() as $i => $feature) {
$row = $plan['rows'][$i];
if ($row['status'] === 'reuse') {
$reused[] = $row['geometry_id'];
}
if ($row['status'] !== 'insert') {
continue;
}
$feature = $source['features'][$i];
$id = (string) Str::uuid();
$trace = ['authority' => 'Mozambique INE - Instituto Nacional de Estatistica', 'provider' => 'UNOCHA; exemplar hosted by World Bank', 'source_version' => $plan['edition'], 'source_sha256' => $plan['source_sha256'], 'metadata_sha256' => $plan['metadata_sha256'], 'source_url' => 'https://services.arcgis.com/iQ1dY19aHwbSDYIF/arcgis/rest/services/Mozambique/FeatureServer/'.($data['level'] + 2), 'source_properties' => $feature['properties'], 'source_scan' => $plan['scan'], 'batch_id' => $batch, 'purpose' => 'administrative_boundary', 'investment_site' => false, 'legal_currentness' => 'not_determined', 'match_basis' => $row['match_basis'], 'correspondence' => $row['correspondence'], 'correspondence_version' => $row['correspondence_version'], 'correspondence_sha256' => $plan['correspondence_sha256']];
DB::insert('INSERT INTO geo_geometries(id,source_system,external_id,geo_location_id,geometry_type,geom,properties,data_classification) VALUES (?,?,?,?,?,ST_SetSRID(ST_GeomFromGeoJSON(?),4326),?::jsonb,?)', [$id, 'ine-cod-ab', $row['external_id'], $row['location_id'], strtolower($feature['geometry']['type']), json_encode($feature['geometry'], JSON_THROW_ON_ERROR), json_encode($trace, JSON_THROW_ON_ERROR), 'internal']);