Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion app/Services/StationDetailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
*/
class StationDetailService
{
/**
* FIPS numeric code → 2-letter state/territory abbreviation.
*
* @var array<string, string>
*/
private const FIPS_STATES = [
'01' => 'AL', '02' => 'AK', '04' => 'AZ', '05' => 'AR', '06' => 'CA',
'08' => 'CO', '09' => 'CT', '10' => 'DE', '11' => 'DC', '12' => 'FL',
'13' => 'GA', '15' => 'HI', '16' => 'ID', '17' => 'IL', '18' => 'IN',
'19' => 'IA', '20' => 'KS', '21' => 'KY', '22' => 'LA', '23' => 'ME',
'24' => 'MD', '25' => 'MA', '26' => 'MI', '27' => 'MN', '28' => 'MS',
'29' => 'MO', '30' => 'MT', '31' => 'NE', '32' => 'NV', '33' => 'NH',
'34' => 'NJ', '35' => 'NM', '36' => 'NY', '37' => 'NC', '38' => 'ND',
'39' => 'OH', '40' => 'OK', '41' => 'OR', '42' => 'PA', '44' => 'RI',
'45' => 'SC', '46' => 'SD', '47' => 'TN', '48' => 'TX', '49' => 'UT',
'50' => 'VT', '51' => 'VA', '53' => 'WA', '54' => 'WV', '55' => 'WI',
'56' => 'WY', '72' => 'PR', '78' => 'VI', '66' => 'GU', '60' => 'AS',
];

/**
* @param USGSWaterServices $client Raw USGS NWIS IV API client.
*/
Expand Down Expand Up @@ -128,11 +147,19 @@ private function upsertStation(string $siteNo, array $sourceInfo): UsgsStation
{
$geoLoc = $sourceInfo['geoLocation']['geogLocation'] ?? [];

$fips = null;
foreach ($sourceInfo['siteProperty'] ?? [] as $prop) {
if (($prop['name'] ?? '') === 'stateCd') {
$fips = (string) ($prop['value'] ?? '');
break;
}
}

UsgsStation::updateOrCreate(
['site_no' => $siteNo],
[
'name' => (string) ($sourceInfo['siteName'] ?? $siteNo),
'state' => null,
'state' => $fips !== null ? (self::FIPS_STATES[$fips] ?? null) : null,
'county' => null,
'huc' => null,
'site_type' => 'ST',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Services/StationDetailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_load_station_persists_station_and_returns_chart_data(): voi
$this->assertDatabaseHas('usgs_stations', [
'site_no' => '01646500',
'name' => 'Potomac River near Washington DC',
'state' => null,
'state' => 'VA',
'huc' => null,
]);

Expand Down
Loading