Automate the management of PostGIS and GIS data from PowerShell.
PSPostGIS is a PowerShell module that provides a unified interface for downloading, processing, and importing various GIS data sources into PostGIS databases. It abstracts common PostGIS CLI tools (ogr2ogr, shp2pgsql, raster2pgsql) and provides data source-specific functions for popular GIS data providers.
- Core PostGIS Utilities: Wrapper functions for ogr2ogr, shp2pgsql, and other PostGIS CLI tools
- FEMA NFHL Integration: Download and import FEMA National Flood Hazard Layer data
- Census TIGER/Line Support: Download and import U.S. Census Bureau TIGER/Line shapefiles
- OpenStreetMap Integration: Download and import OSM data (PBF, XML)
- ArcGIS Feature Services: Import data directly from Esri ArcGIS Feature Services
- OpenTopography DEM: Download and import digital elevation model data
- Unified Interface: Consistent command structure across all data sources
- PowerShell 5.1 or later (PowerShell 7+ recommended)
- PostgreSQL with PostGIS extension installed (or use the included Docker test database)
- GDAL/OGR tools (for ogr2ogr)
- PostGIS command-line tools (for shp2pgsql, raster2pgsql)
A Docker-based test database is included for development and testing:
# Import module
Import-Module .\src\PSPostGIS\PSPostGIS.psd1
# Start the test database
Start-PSPostGISTestDatabase
# Test connection
Test-PSPostGISTestDatabase
# Stop the database
Stop-PSPostGISTestDatabaseSee db/README.md for complete test database documentation.
# Using Chocolatey
choco install gdal
# Or download from: https://gdal.org/download.htmlPostGIS tools are typically included with PostGIS installation. Ensure they're in your PATH.
# Clone the repository
git clone https://github.com/jimbrig/PSPostGIS.git
cd PSPostGIS
# Import the module
Import-Module .\src\PSPostGIS\PSPostGIS.psd1# Test connection and PostGIS availability
Test-PostGISConnection -Host 'localhost' -Database 'gis' -User 'postgres'
# Get PostGIS version
Get-PostGISVersion -ConnectionString 'host=localhost dbname=gis user=postgres'# Create a new schema for FEMA data
New-PostGISSchema -SchemaName 'fema' -ConnectionString 'host=localhost dbname=gis user=postgres' -EnablePostGIS# Download California NFHL data
$zipFile = Get-FemaNfhlZip -StateFips '06' -OutputPath 'C:\Data\FEMA'
# Import into PostGIS
Import-FemaNfhlToPostGIS `
-ZipPath $zipFile.FullName `
-ConnectionString 'PG:host=localhost dbname=gis user=postgres password=secret' `
-Schema 'fema' `
-CleanupPSPostGIS includes functions to manage a Docker-based test database:
# Start the test database
$dbInfo = Start-PSPostGISTestDatabase -Detached
# Test connection and get status
$testResult = Test-PSPostGISTestDatabase
if ($testResult.Connected) {
Write-Host "PostGIS Version: $($testResult.PostGISVersion)"
Write-Host "Schemas: $($testResult.Schemas -join ', ')"
}
# Stop the database
Stop-PSPostGISTestDatabase
# Stop and remove all data
Stop-PSPostGISTestDatabase -RemoveVolumesThe test database comes pre-configured with PostGIS extensions and schemas for all data sources.
Import spatial data using ogr2ogr:
Invoke-Ogr2Ogr `
-Source 'C:\Data\boundaries.shp' `
-Destination 'PG:host=localhost dbname=gis user=postgres password=secret' `
-TableName 'boundaries' `
-Schema 'public' `
-OverwriteImport shapefiles using shp2pgsql:
# Generate SQL file
Invoke-Shp2Pgsql `
-Shapefile 'C:\Data\tracts.shp' `
-TableName 'tracts' `
-Schema 'census' `
-Srid 4326 `
-OutputFile 'C:\Data\tracts.sql'
# Or import directly
Invoke-Shp2Pgsql `
-Shapefile 'C:\Data\tracts.shp' `
-TableName 'tracts' `
-ConnectionString 'host=localhost dbname=gis user=postgres'# Build connection string
$connString = New-PostGISConnectionString `
-Host 'localhost' `
-Database 'gis' `
-User 'postgres' `
-Password 'secret' `
-Format 'GDAL'
# Test connection
Test-PostGISConnection -ConnectionString $connString# Download by state FIPS code
Get-FemaNfhlZip -StateFips '06' -OutputPath 'C:\Data\FEMA'
# Download by county FIPS code
Get-FemaNfhlZip -StateFips '36' -CountyFips '061' -OutputPath 'C:\Data\FEMA'
# Import to PostGIS
Import-FemaNfhlToPostGIS `
-ZipPath 'C:\Data\FEMA\NFHL_06.zip' `
-ConnectionString 'PG:host=localhost dbname=gis user=postgres' `
-Schema 'fema'# Download Census TIGER/Line data
Get-CensusTigerLineData `
-Year 2023 `
-Geography 'Tract' `
-StateFips '06' `
-OutputPath 'C:\Data\Census'
# Import to PostGIS
Import-CensusTigerLineToPostGIS `
-ShapefilePath 'C:\Data\Census\tl_2023_06_tract.shp' `
-ConnectionString 'PG:host=localhost dbname=gis user=postgres' `
-Schema 'census'# Download OSM data
Get-OsmData `
-Region 'north-america/us/california' `
-Format 'pbf' `
-OutputPath 'C:\Data\OSM'
# Import to PostGIS
Import-OsmToPostGIS `
-OsmFile 'C:\Data\OSM\california-latest.osm.pbf' `
-ConnectionString 'PG:host=localhost dbname=gis user=postgres' `
-Schema 'osm' `
-Method 'ogr2ogr'# Import directly from ArcGIS Feature Service
Import-ArcGISToPostGIS `
-ServiceUrl 'https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Counties/FeatureServer' `
-ConnectionString 'PG:host=localhost dbname=gis user=postgres' `
-Schema 'arcgis' `
-TableName 'counties' `
-LayerId 0# Download DEM data
Get-OpenTopographyDEM `
-BoundingBox @(-122.5, 37.5, -122.0, 38.0) `
-Product 'SRTMGL3' `
-OutputPath 'C:\Data\DEM'
# Import to PostGIS
Import-DEMToPostGIS `
-RasterPath 'C:\Data\DEM\srtm.tif' `
-ConnectionString 'host=localhost dbname=gis user=postgres' `
-Schema 'elevation'Get-PSPostGISDataSources# List all commands
Get-PSPostGISCommands
# Filter by module
Get-PSPostGISCommands -Module 'Core'
# Filter by command type
Get-PSPostGISCommands -CommandType 'Function'# 1. Test connection
$connString = 'PG:host=localhost dbname=gis user=postgres password=secret'
Test-PostGISConnection -ConnectionString $connString
# 2. Create schema
New-PostGISSchema -SchemaName 'fema' -ConnectionString $connString -EnablePostGIS
# 3. Download data
$zipFile = Get-FemaNfhlZip -StateFips '06' -OutputPath 'C:\Data\FEMA'
# 4. Import to PostGIS
Import-FemaNfhlToPostGIS `
-ZipPath $zipFile.FullName `
-ConnectionString $connString `
-Schema 'fema' `
-Cleanup
# 5. Verify import
# (Query database to verify tables were created)$shapefiles = Get-ChildItem -Path 'C:\Data\Shapefiles' -Filter '*.shp'
$connString = 'PG:host=localhost dbname=gis user=postgres'
foreach ($shp in $shapefiles) {
Invoke-Ogr2Ogr `
-Source $shp.FullName `
-Destination $connString `
-TableName $shp.BaseName `
-Schema 'imports' `
-Overwrite
}PSPostGIS is organized as a main module with nested sub-modules:
- Core: PostGIS CLI utilities and helper functions
- DataSources: Data source-specific sub-modules
- FemaFloodZones
- CensusTigerLine
- OpenStreetMap
- ArcGIS
- OpenTopography
See STRUCTURE.md for detailed architecture documentation.
- PowerShell 5.1+ (PowerShell 7+ recommended)
- PostgreSQL 9.5+ with PostGIS 2.5+
- GDAL 3.0+ (for ogr2ogr)
- PostGIS command-line tools (shp2pgsql, raster2pgsql)
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Copyright (c) Jimmy Briggs. All rights reserved.