GeoLookup.Postal 2.0.0

dotnet add package GeoLookup.Postal --version 2.0.0
                    
NuGet\Install-Package GeoLookup.Postal -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GeoLookup.Postal" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GeoLookup.Postal" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="GeoLookup.Postal" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GeoLookup.Postal --version 2.0.0
                    
#r "nuget: GeoLookup.Postal, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GeoLookup.Postal@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GeoLookup.Postal&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=GeoLookup.Postal&version=2.0.0
                    
Install as a Cake Tool

GeoLookup.Postal

High-performance postal code geolocation library for .NET 9+ using GeoNames data with binary search optimization.

Features

  • Fast Lookups: O(log n) binary search for postal code lookups
  • US & Canada Support: Covers all postal codes for United States and Canada
  • Auto-Detection: Automatically detects country from postal code format - no country code required!
  • Normalization: Automatic postal code normalization (US: 5-digit format, CA: uppercase without spaces)
  • Batch Operations: Efficient batch lookups for multiple postal codes
  • Radius Search: Find postal codes within a specified distance
  • Embedded Data: No external dependencies or database required
  • Thread Safe: Safe for concurrent access

Data Source

This package uses postal code data from GeoNames (http://www.geonames.org/), which is licensed under the Creative Commons Attribution 4.0 License. The data includes:

  • Postal codes for United States and Canada
  • Geographic coordinates (latitude/longitude centroids)
  • Place names and administrative divisions
  • High accuracy for proximity searches and mapping

Attribution

Postal code data © GeoNames contributors, licensed under Creative Commons Attribution 4.0 (CC BY 4.0). See: https://creativecommons.org/licenses/by/4.0/

Installation

dotnet add package GeoLookup.Postal

Quick Start

using GeoLookup.Postal;

// Create lookup service
using var lookup = new PostalCodeLookup();

// 🚀 NEW: Auto-detection - No country code required!
var location = lookup.Lookup("90210");  // Automatically detects US format
if (location.HasValue)
{
    Console.WriteLine($"Location: {location.Value.PlaceName}");
    Console.WriteLine($"Coordinates: {location.Value.Latitude}, {location.Value.Longitude}");
    Console.WriteLine($"Country: {location.Value.CountryCode}");
}

var caLocation = lookup.Lookup("K1A 0B1");  // Automatically detects Canadian format
if (caLocation.HasValue)
{
    Console.WriteLine($"Location: {caLocation.Value.PlaceName}");
    Console.WriteLine($"Country: {caLocation.Value.CountryCode}");
}

// Traditional method (still supported)
var traditionalLookup = lookup.Lookup("90210", "US");

// Auto-detection normalization
var normalized = PostalCodeNormalizer.Normalize("k1a 0b1");  // Returns ("K1A0B1", "CA")
if (normalized.HasValue)
{
    Console.WriteLine($"Normalized: {normalized.Value.NormalizedCode} ({normalized.Value.CountryCode})");
}

// Country detection
var country = PostalCodeNormalizer.DetectCountry("90210");  // Returns "US"
var country2 = PostalCodeNormalizer.DetectCountry("K1A 0B1");  // Returns "CA"

// Batch lookup with auto-detection
var postalCodes = new[] { "10001", "90210", "K1A 0B1", "M5V 3L9" };
var results = lookup.LookupBatch(postalCodes);
foreach (var result in results)
{
    Console.WriteLine($"{result.Key}: {result.Value?.PlaceName ?? "Not found"}");
}

// Traditional batch lookup (still supported)
var requests = new[]
{
    ("10001", "US"),
    ("90210", "US"),
    ("K1A 0B1", "CA"),
    ("M5V 3L9", "CA")
};
var traditionalResults = lookup.LookupBatch(requests);

// Find postal codes within radius
var nearby = lookup.FindWithinRadius(40.7128, -74.0060, 10); // 10km around NYC
foreach (var location in nearby.Take(5))
{
    Console.WriteLine($"{location.PostalCode}: {location.PlaceName}");
}

API Reference

PostalCodeLookup

Main service class for postal code lookups.

Methods
  • Lookup(string postalCode): 🆕 Look up a postal code with automatic country detection
  • Lookup(string postalCode, string countryCode): Look up a single postal code with explicit country
  • LookupBatch(IEnumerable<string> postalCodes): 🆕 Batch lookup with auto-detection
  • LookupBatch(IEnumerable<(string, string)> requests): Batch lookup for multiple postal codes with explicit countries
  • FindWithinRadius(double lat, double lon, double radiusKm, string? countryCode = null): Find postal codes within radius

PostalCodeNormalizer

Utility class for postal code normalization and validation.

Methods
  • DetectCountry(string postalCode): 🆕 Automatically detect country from postal code format
  • Normalize(string postalCode): 🆕 Normalize postal code with automatic country detection
  • IsValid(string postalCode): 🆕 Validate postal code format with auto-detection
  • Normalize(string postalCode, string countryCode): Normalize postal code format with explicit country
  • IsValid(string postalCode, string countryCode): Validate postal code format for specific country
  • NormalizeUs(string postalCode): US-specific normalization
  • NormalizeCa(string postalCode): Canada-specific normalization

PostalCodeLocation

Result structure containing location data.

Properties
  • PostalCode: Normalized postal code
  • Latitude: Geographic latitude
  • Longitude: Geographic longitude
  • PlaceName: Associated place name
  • AdminCode1: State/province code
  • AdminCode2: County/district code
  • CountryCode: Country code (US or CA)

Postal Code Formats

United States

  • Input: 12345 or 12345-6789
  • Normalized: 12345 (5-digit format)

Canada

  • Input: K1A 0B1 or K1A0B1
  • Normalized: K1A0B1 (uppercase, no spaces)

Performance

  • Lookup Time: O(log n) binary search ~1-5 microseconds per lookup
  • Memory Usage: ~50-100MB for full US+CA dataset
  • Package Size: ~25-40MB with compressed binary data
  • Thread Safety: Yes, safe for concurrent access

Building from Source

git clone <repository-url>
cd GeoLookup.Postal
dotnet restore
dotnet build
dotnet test

License

This package is licensed under the Creative Commons Attribution 4.0 License (CC BY 4.0) to comply with the GeoNames data license.

Contributing

Contributions are welcome! Please ensure all tests pass and follow the existing code style.

Changelog

1.0.0

  • Initial release
  • US and Canada postal code support
  • Binary search optimization
  • Batch lookup support
  • Radius search functionality
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 183 8/22/2025
1.1.3 318 8/22/2025
1.1.2 187 8/21/2025
1.1.1 192 8/21/2025
1.1.0 196 8/21/2025
1.0.0 194 8/21/2025