Advertisement
bdill

USGS_GNIS_FIPS_counties.csv.ps1

Nov 25th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #================================================================================
  2. # MS SQL Server and PowerShell using dbatools.io module (https://dbatools.io/)
  3. # 1) Download files from USGS and unzip to desired $UnzipPath
  4. #    https://www.usgs.gov/us-board-on-geographic-names/download-gnis-data
  5. #    https://prd-tnm.s3.amazonaws.com/StagedProducts/GeographicNames/FederalCodes/FedCodes_AllStates_Text.zip
  6. # 2) Load all the txt files into a single SQL table
  7. # 3) Create a view vw_USGS_Counties to select only the counties
  8. # 4) Export the results from the view to USGS_GNIS_FIPS_counties.csv
  9. #    results are here: https://pastebin.com/Uk8D9p22
  10. #--------------------------------------------------------------------------------
  11. # Config parameters
  12. $Server = "MyServer"
  13. $Database = "MyDatabase"
  14. $UnzipPath = "C:\opendata\USGS_GNIS\Text\"
  15. #--------------------------------------------------------------------------------
  16. # Create and populate USGS_GNIS table containint Counties, their FIPS, and USGS FeatureID fields
  17. $query_create = @"
  18. CREATE TABLE dbo.USGS_GNIS (
  19.     feature_id VARCHAR(10) NOT NULL
  20.  , feature_name VARCHAR(120) NULL
  21.  , feature_class VARCHAR(50) NULL
  22.  , census_code VARCHAR(5) NULL
  23.  , census_class_code VARCHAR(2) NULL
  24.  , gsa_code VARCHAR(4) NULL
  25.  , opm_code VARCHAR(9) NULL
  26.  , state_name VARCHAR(100) NULL
  27.  , state_numeric VARCHAR(2) NULL
  28.  , county_sequence VARCHAR(3) NOT NULL
  29.  , county_name VARCHAR(100) NULL
  30.  , county_numeric VARCHAR(3) NULL
  31.  , date_created DATE NULL
  32.  , date_edited DATE NULL
  33.  , bgn_type VARCHAR(12) NULL
  34.  , bgn_authority VARCHAR(25) NULL
  35.  , bgn_date DATE NULL
  36.  , prim_lat_dms VARCHAR(10) NULL
  37.  , prim_long_dms VARCHAR(10) NULL
  38.  , prim_lat_dec DECIMAL(12, 7) NULL
  39.  , prim_long_dec DECIMAL(12, 7) NULL
  40. ) ON [PRIMARY];
  41. "@
  42. Invoke-DbaQuery -SqlInstance $Server -Database $Database -Query $query_create
  43. #--------------------------------------------------------------------------------
  44. # loop through files: FederalCodes_AK.txt, ... , FederalCodes_WY.txt
  45. Get-ChildItem $UnzipPath | foreach {
  46.     #$_.FullName
  47.     Import-DbaCsv -Path $_.FullName -SqlInstance $Server -Database $Database -Schema dbo -Table USGS_GNIS -Delimiter "|"
  48. }
  49. #--------------------------------------------------------------------------------
  50. $query_dedupe = @"
  51. ; WITH foo AS (
  52.     SELECT ROW_NUMBER() OVER (PARTITION BY feature_id, feature_name, feature_class, census_code, census_class_code, gsa_code, opm_code
  53.                                 , state_name, state_numeric, county_sequence, county_name, county_numeric, date_created, date_edited
  54.                                 , bgn_authority, bgn_date, prim_lat_dms, prim_long_dms, prim_lat_dec, prim_long_dec -- ommiting bgn_type
  55.                               ORDER BY feature_id, bgn_type ) AS RowNum
  56.          , *
  57.     FROM dbo.USGS_GNIS
  58. )
  59. --SELECT * FROM Foo WHERE foo.RowNum > 1
  60. DELETE FROM Foo WHERE foo.RowNum > 1
  61. "@
  62. Invoke-DbaQuery -SqlInstance $Server -Database $Database -Query $query_dedupe
  63. #--------------------------------------------------------------------------------
  64. $query_create_view = @"
  65. CREATE OR ALTER VIEW dbo.vw_USGS_Counties
  66. AS
  67.     SELECT feature_id AS USGS_FeatureID
  68.         , census_code AS CensusCode
  69.         , state_numeric AS FIPS_State
  70.         , county_numeric AS FIPS_County
  71.         , CONCAT(state_numeric, county_numeric) AS FIPS_County5
  72.         , state_name AS StateName
  73.         , county_name AS CountyName
  74.         , prim_lat_dec AS Lat
  75.         , prim_long_dec AS Long
  76.         , feature_name AS FeatureName
  77.     FROM dbo.USGS_GNIS
  78.     WHERE census_class_code IN ('H1', 'H6')
  79. "@
  80. Invoke-DbaQuery -SqlInstance $Server -Database $Database -Query $query_create_view
  81. #--------------------------------------------------------------------------------
  82. Invoke-DbaQuery -SqlInstance $Server -Database $Database -Query "SELECT * FROM dbo.vw_USGS_Counties ORDER BY FIPS_County5" | Export-Csv -Path "C:\opendata\USGS_GNIS\USGS_GNIS_FIPS_counties.csv" -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement