This commit is contained in:
Benjamin Takats
2023-01-28 21:48:20 +01:00
parent b9f3f0ba7b
commit 9f153e7630
23 changed files with 441 additions and 19 deletions

View File

@@ -0,0 +1,114 @@
<?php
namespace App\Http\Livewire\Meetup;
use App\Models\Meetup;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
class PrepareForBtcMapItem extends Component
{
public Meetup $meetup;
public $wikipediaSearchResults;
public $osmSearchResults;
public $osmSearchResultsState;
public $selectedItem;
public function mount()
{
$response = Http::acceptJson()
->get(
'https://nominatim.openstreetmap.org/search?city='.$this->meetup->city->name.'&format=json&polygon_geojson=1'
);
$this->osmSearchResults = $response->json();
$response = Http::acceptJson()
->get(
'https://nominatim.openstreetmap.org/search?state='.$this->meetup->city->name.'&format=json&polygon_geojson=1'
);
$this->osmSearchResultsState = $response->json();
if ($this->meetup->city->osm_relation) {
$this->selectedItem = $this->meetup->city->osm_relation;
$wikipediaUrl = 'https://query.wikidata.org/sparql?query=SELECT%20%3Fpopulation%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Amwapi%20%7B%0A%20%20%20%20%20%20bd%3AserviceParam%20mwapi%3Asearch%20%22'.urlencode($this->meetup->city->name).'%22%20.%20%20%20%20%0A%20%20%20%20%20%20bd%3AserviceParam%20mwapi%3Alanguage%20%22en%22%20.%20%20%20%20%0A%20%20%20%20%20%20bd%3AserviceParam%20wikibase%3Aapi%20%22EntitySearch%22%20.%0A%20%20%20%20%20%20bd%3AserviceParam%20wikibase%3Aendpoint%20%22www.wikidata.org%22%20.%0A%20%20%20%20%20%20bd%3AserviceParam%20wikibase%3Alimit%201%20.%0A%20%20%20%20%20%20%3Fitem%20wikibase%3AapiOutputItem%20mwapi%3Aitem%20.%0A%20%20%7D%0A%20%20%3Fitem%20wdt%3AP1082%20%3Fpopulation%0A%7D';
$response = Http::acceptJson()
->get(
$wikipediaUrl
);
$this->wikipediaSearchResults = $response->json();
}
}
public function selectItem($index, bool $isState = false)
{
if ($isState) {
$this->selectedItem = $this->osmSearchResultsState[$index];
} else {
$this->selectedItem = $this->osmSearchResults[$index];
}
Storage::disk('geo')
->put('geojson_'.$this->selectedItem['osm_id'].'.json',
json_encode($this->selectedItem['geojson'], JSON_THROW_ON_ERROR));
$input = storage_path('app/geo/geojson_'.$this->selectedItem['osm_id'].'.json');
$output = storage_path('app/geo/output_'.$this->selectedItem['osm_id'].'.json');
exec('mapshaper '.$input.' -simplify dp 4% -o '.$output);
Storage::disk('geo')
->put(
'trimmed_'.$this->selectedItem['osm_id'].'.json',
str(Storage::disk('geo')
->get('output_'.$this->selectedItem['osm_id'].'.json'))
->after('{"type":"GeometryCollection", "geometries": [')
->beforeLast(']}')
->toString()
);
$this->meetup->city->osm_relation = $this->selectedItem;
$this->meetup->city->simplified_geojson = json_decode(trim(Storage::disk('geo')
->get('trimmed_'.$this->selectedItem['osm_id'].'.json')),
false, 512, JSON_THROW_ON_ERROR);
$this->meetup->city->population = 0;
$this->meetup->city->population_date = date('Y');
$this->meetup->city->save();
return to_route('osm.meetups.item', ['meetup' => $this->meetup]);
}
public function setPercent($percent)
{
$input = storage_path('app/geo/geojson_'.$this->selectedItem['osm_id'].'.json');
$output = storage_path('app/geo/output_'.$this->selectedItem['osm_id'].'.json');
exec('mapshaper '.$input.' -simplify dp '.$percent.'% -o '.$output);
Storage::disk('geo')
->put(
'trimmed_'.$this->selectedItem['osm_id'].'.json',
str(Storage::disk('geo')
->get('output_'.$this->selectedItem['osm_id'].'.json'))
->after('{"type":"GeometryCollection", "geometries": [')
->beforeLast(']}')
->toString()
);
$this->meetup->city->simplified_geojson = json_decode(trim(Storage::disk('geo')
->get('trimmed_'.$this->selectedItem['osm_id'].'.json')),
false, 512, JSON_THROW_ON_ERROR);
$this->meetup->city->save();
return to_route('osm.meetups.item', ['meetup' => $this->meetup]);
}
public function takePop($value)
{
$this->meetup->city->population = $value;
$this->meetup->city->population_date = date('Y');
$this->meetup->city->save();
return to_route('osm.meetups.item', ['meetup' => $this->meetup]);
}
public function render()
{
return view('livewire.meetup.prepare-for-btc-map-item');
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Livewire\Meetup;
use Livewire\Component;
class PrepareForBtcMapTable extends Component
{
public function render()
{
return view('livewire.meetup.prepare-for-btc-map-table');
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Http\Livewire\Tables;
use App\Models\Meetup;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
class MeetupForBtcMapTable extends DataTableComponent
{
public function configure(): void
{
$this
->setPrimaryKey('id')
->setAdditionalSelects([
'osm_relation',
'simplified_geojson',
'population',
'population_date',
])
->setPerPageAccepted([
100000,
])
->setPerPage(100000);
}
public function columns(): array
{
return [
Column::make("Id", "id")
->sortable(),
Column::make("Name", "name")
->sortable(),
Column::make("City", "city.name")
->sortable(),
Column::make("Country", "city.country.name")
->sortable(),
Column::make("Actions")
->label(fn($row, Column $column) => view('columns.meetups.osm-actions', ['row' => $row])),
];
}
public function builder(): Builder
{
return Meetup::query()
->with([
'city.country',
]);
}
}