Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jimbrig/PSPostGIS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

PSPostGIS

Automate the management of PostGIS and GIS data from PowerShell.

Overview

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.

Features

  • 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

Installation

Prerequisites

  • 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)

Test Database (Docker)

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-PSPostGISTestDatabase

See db/README.md for complete test database documentation.

Install GDAL/OGR (Windows)

# Using Chocolatey
choco install gdal

# Or download from: https://gdal.org/download.html

Install PostGIS Tools

PostGIS tools are typically included with PostGIS installation. Ensure they're in your PATH.

Install PSPostGIS

# Clone the repository
git clone https://github.com/jimbrig/PSPostGIS.git
cd PSPostGIS

# Import the module
Import-Module .\src\PSPostGIS\PSPostGIS.psd1

Quick Start

1. Test PostGIS Connection

# 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'

2. Create a Schema

# Create a new schema for FEMA data
New-PostGISSchema -SchemaName 'fema' -ConnectionString 'host=localhost dbname=gis user=postgres' -EnablePostGIS

3. Download and Import FEMA NFHL Data

# 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' `
    -Cleanup

Core Functions

Test Database Management

PSPostGIS 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 -RemoveVolumes

The test database comes pre-configured with PostGIS extensions and schemas for all data sources.

PostGIS CLI Wrappers

Invoke-Ogr2Ogr

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' `
    -Overwrite

Invoke-Shp2Pgsql

Import 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'

Connection Management

# Build connection string
$connString = New-PostGISConnectionString `
    -Host 'localhost' `
    -Database 'gis' `
    -User 'postgres' `
    -Password 'secret' `
    -Format 'GDAL'

# Test connection
Test-PostGISConnection -ConnectionString $connString

Data Source Modules

FEMA NFHL (National Flood Hazard Layer)

# 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'

Census TIGER/Line

# 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'

OpenStreetMap

# 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'

ArcGIS Feature Services

# 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

OpenTopography DEM

# 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'

Module Discovery

List Available Data Sources

Get-PSPostGISDataSources

List All Commands

# List all commands
Get-PSPostGISCommands

# Filter by module
Get-PSPostGISCommands -Module 'Core'

# Filter by command type
Get-PSPostGISCommands -CommandType 'Function'

Examples

Complete Workflow: FEMA Flood Zones

# 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)

Batch Import Multiple Shapefiles

$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
}

Module Structure

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.

Requirements

  • PowerShell 5.1+ (PowerShell 7+ recommended)
  • PostgreSQL 9.5+ with PostGIS 2.5+
  • GDAL 3.0+ (for ogr2ogr)
  • PostGIS command-line tools (shp2pgsql, raster2pgsql)

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

Copyright (c) Jimmy Briggs. All rights reserved.

Links

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published