|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Parsers\Files; |
| 4 | + |
| 5 | +class GeoJSONParser extends FIleParser |
| 6 | +{ |
| 7 | + public function parseFile(string $filepath): array |
| 8 | + { |
| 9 | + return [ |
| 10 | + 'map' => $this->parseMapDetailsFromFile($filepath), |
| 11 | + 'markers' => $this->parseMarkersFromFile($filepath), |
| 12 | + ]; |
| 13 | + } |
| 14 | + |
| 15 | + public function parseMarkersFromFile(string $filepath): array |
| 16 | + { |
| 17 | + $json = file_get_contents($filepath); |
| 18 | + $data = json_decode($json, true); |
| 19 | + |
| 20 | + $markers = []; |
| 21 | + |
| 22 | + if (isset($data['features'])) { |
| 23 | + foreach ($data['features'] as $feature) { |
| 24 | + // Depending on the type of geometry, the coordinates can be in different places |
| 25 | + // If its just a point, the coordinates are in the first level of the geometry object |
| 26 | + if ($feature['geometry']['type'] === 'Point') { |
| 27 | + $markers[] = [ |
| 28 | + 'lat' => $feature['geometry']['coordinates'][1], |
| 29 | + 'lng' => $feature['geometry']['coordinates'][0], |
| 30 | + 'category_name' => $feature['properties']['name'] ?? 'Feature', |
| 31 | + 'description' => $feature['properties']['description'] ?? null, |
| 32 | + 'link' => $feature['properties']['link'] ?? null, |
| 33 | + 'elevation' => $feature['properties']['elevation'] ?? null, |
| 34 | + 'created_at' => $feature['properties']['time'] ?? null, |
| 35 | + 'updated_at' => $feature['properties']['time'] ?? null, |
| 36 | + 'meta' => $feature['properties'], |
| 37 | + ]; |
| 38 | + } elseif ($feature['geometry']['type'] === 'LineString') { |
| 39 | + // Else if its a lineString, we have to add it to markers.locations for each coordinate |
| 40 | + |
| 41 | + $locations = []; |
| 42 | + |
| 43 | + $pointers = 0; |
| 44 | + foreach ($feature['geometry']['coordinates'] as $coordinate) { |
| 45 | + $locations[] = [ |
| 46 | + 'lat' => $coordinate[1], |
| 47 | + 'lng' => $coordinate[0], |
| 48 | + 'elevation' => $coordinate[2] ?? null, |
| 49 | + |
| 50 | + // There may be a coordTimes array in the properties, which we can use to set the created_at and updated_at |
| 51 | + 'created_at' => $feature['properties']['coordTimes'][$pointers] ?? null, |
| 52 | + 'updated_at' => $feature['properties']['coordTimes'][$pointers] ?? null, |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + $markers[] = [ |
| 57 | + 'category_name' => $feature['properties']['name'] ?? 'Feature', |
| 58 | + 'description' => $feature['properties']['description'] ?? null, |
| 59 | + 'link' => $feature['properties']['link'] ?? null, |
| 60 | + 'created_at' => $feature['properties']['time'] ?? null, |
| 61 | + 'updated_at' => $feature['properties']['time'] ?? null, |
| 62 | + 'meta' => $feature['properties'], |
| 63 | + 'locations' => $locations, |
| 64 | + ]; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return $markers; |
| 70 | + } |
| 71 | + |
| 72 | + public function parseMapDetailsFromFile(string $filepath): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'name' => pathinfo($filepath, PATHINFO_FILENAME), |
| 76 | + 'description' => 'A map of ' . pathinfo($filepath, PATHINFO_FILENAME), |
| 77 | + ]; |
| 78 | + } |
| 79 | +} |
0 commit comments