156 lines
4.6 KiB
PHP
156 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class GridController extends Controller
|
|
{
|
|
// Grid size in degrees (~1km at equator)
|
|
const GRID_SIZE = 0.009;
|
|
|
|
/**
|
|
* Calculate grid ID from coordinates
|
|
*/
|
|
private function getGridId(float $lat, float $lng): string
|
|
{
|
|
$gridLat = floor($lat / self::GRID_SIZE);
|
|
$gridLng = floor($lng / self::GRID_SIZE);
|
|
return "grid_{$gridLat}_{$gridLng}";
|
|
}
|
|
|
|
/**
|
|
* Calculate grid bounds from grid ID
|
|
*/
|
|
private function getGridBounds(string $gridId): array
|
|
{
|
|
$parts = explode('_', $gridId);
|
|
$gridLat = (int) $parts[1];
|
|
$gridLng = (int) $parts[2];
|
|
|
|
return [
|
|
'south' => $gridLat * self::GRID_SIZE,
|
|
'north' => ($gridLat + 1) * self::GRID_SIZE,
|
|
'west' => $gridLng * self::GRID_SIZE,
|
|
'east' => ($gridLng + 1) * self::GRID_SIZE
|
|
];
|
|
}
|
|
|
|
/**
|
|
* GET /api/grids
|
|
* Get all grids within viewport bounds
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$bounds = $request->query('bounds');
|
|
|
|
// For now, return empty since we don't have MongoDB
|
|
// In production, this would query the database
|
|
return response()->json([]);
|
|
}
|
|
|
|
/**
|
|
* GET /api/grids/generate
|
|
* Generate grid cells for a viewport (doesn't save to DB, just returns structure)
|
|
*/
|
|
public function generate(Request $request): JsonResponse
|
|
{
|
|
$bounds = $request->query('bounds');
|
|
|
|
if (!$bounds) {
|
|
return response()->json([
|
|
'error' => 'bounds parameter required (south,west,north,east)'
|
|
], 400);
|
|
}
|
|
|
|
$coords = array_map('floatval', explode(',', $bounds));
|
|
$south = $coords[0];
|
|
$west = $coords[1];
|
|
$north = $coords[2];
|
|
$east = $coords[3];
|
|
|
|
$grids = [];
|
|
|
|
// Generate grid cells for the viewport
|
|
for ($lat = floor($south / self::GRID_SIZE) * self::GRID_SIZE; $lat < $north; $lat += self::GRID_SIZE) {
|
|
for ($lng = floor($west / self::GRID_SIZE) * self::GRID_SIZE; $lng < $east; $lng += self::GRID_SIZE) {
|
|
$centerLat = $lat + self::GRID_SIZE / 2;
|
|
$centerLng = $lng + self::GRID_SIZE / 2;
|
|
$gridId = $this->getGridId($centerLat, $centerLng);
|
|
|
|
$grids[] = [
|
|
'gridId' => $gridId,
|
|
'bounds' => [
|
|
'south' => $lat,
|
|
'north' => $lat + self::GRID_SIZE,
|
|
'west' => $lng,
|
|
'east' => $lng + self::GRID_SIZE
|
|
],
|
|
'centerLat' => $centerLat,
|
|
'centerLng' => $centerLng,
|
|
'status' => 'pending',
|
|
'downloadedTypes' => [],
|
|
'placesCount' => 0
|
|
];
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'gridSize' => self::GRID_SIZE,
|
|
'gridCount' => count($grids),
|
|
'grids' => $grids
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* POST /api/grids/{gridId}/download
|
|
* Mark a grid for download (actual download happens from frontend via Google Places API)
|
|
*/
|
|
public function download(Request $request, string $gridId): JsonResponse
|
|
{
|
|
$types = $request->input('types', []);
|
|
$places = $request->input('places', []);
|
|
|
|
$bounds = $this->getGridBounds($gridId);
|
|
$centerLat = ($bounds['north'] + $bounds['south']) / 2;
|
|
$centerLng = ($bounds['east'] + $bounds['west']) / 2;
|
|
|
|
// In production, this would save to database
|
|
// For now, just return success response
|
|
$grid = [
|
|
'gridId' => $gridId,
|
|
'bounds' => $bounds,
|
|
'centerLat' => $centerLat,
|
|
'centerLng' => $centerLng,
|
|
'status' => 'downloaded',
|
|
'downloadedTypes' => $types,
|
|
'placesCount' => count($places),
|
|
'downloadedAt' => now()->toISOString(),
|
|
'lastUpdated' => now()->toISOString()
|
|
];
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'grid' => $grid,
|
|
'savedPlaces' => count($places)
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /api/grids/stats
|
|
* Get download statistics
|
|
*/
|
|
public function stats(): JsonResponse
|
|
{
|
|
// In production, this would query the database
|
|
// For now, return mock stats
|
|
return response()->json([
|
|
'totalGrids' => 0,
|
|
'downloadedGrids' => 0,
|
|
'totalPlaces' => 0,
|
|
'placesByType' => []
|
|
]);
|
|
}
|
|
}
|