home / skills / dkyazzentwatwa / chatgpt-skills / geocoder

geocoder skill

/geocoder

This skill geocodes addresses and reverse-geocodes coordinates, enriching location data and validating addresses with multiple providers.

npx playbooks add skill dkyazzentwatwa/chatgpt-skills --skill geocoder

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
4.3 KB
---
name: geocoder
description: Convert addresses to coordinates (geocoding) and coordinates to addresses (reverse geocoding). Use for location data enrichment or address validation.
---

# Geocoder

Convert between addresses and geographic coordinates.

## Features

- **Geocoding**: Address to coordinates
- **Reverse Geocoding**: Coordinates to address
- **Batch Processing**: Process CSV files
- **Multiple Providers**: Nominatim (free), Google, Bing
- **Address Components**: Structured address parsing
- **Caching**: Built-in result caching

## Quick Start

```python
from geocoder import Geocoder

geo = Geocoder()

# Address to coordinates
result = geo.geocode("1600 Amphitheatre Parkway, Mountain View, CA")
print(f"Coordinates: {result['lat']}, {result['lon']}")

# Coordinates to address
result = geo.reverse(37.4224, -122.0840)
print(f"Address: {result['address']}")
```

## CLI Usage

```bash
# Geocode address
python geocoder.py --geocode "Empire State Building, New York"

# Reverse geocode
python geocoder.py --reverse "40.7484,-73.9857"

# Batch geocode CSV
python geocoder.py --input addresses.csv --column address --output geocoded.csv

# Batch reverse geocode
python geocoder.py --input coords.csv --lat lat --lon lon --reverse-batch --output addresses.csv
```

## API Reference

### Geocoder Class

```python
class Geocoder:
    def __init__(self, provider: str = "nominatim", api_key: str = None)

    # Single operations
    def geocode(self, address: str) -> dict
    def reverse(self, lat: float, lon: float) -> dict

    # Batch operations
    def batch_geocode(self, addresses: list, delay: float = 1.0) -> list
    def batch_reverse(self, coordinates: list, delay: float = 1.0) -> list

    # File operations
    def geocode_csv(self, input: str, column: str, output: str) -> str
    def reverse_csv(self, input: str, lat: str, lon: str, output: str) -> str
```

## Providers

### Nominatim (Default)
- Free, no API key required
- Rate limited (1 request/second)
- Uses OpenStreetMap data

### Google Maps
```python
geo = Geocoder(provider="google", api_key="YOUR_KEY")
```

### Bing Maps
```python
geo = Geocoder(provider="bing", api_key="YOUR_KEY")
```

## Geocoding Result

```python
{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA",
    "lat": 37.4224764,
    "lon": -122.0842499,
    "components": {
        "house_number": "1600",
        "road": "Amphitheatre Parkway",
        "city": "Mountain View",
        "state": "California",
        "postcode": "94043",
        "country": "United States"
    },
    "raw": {...}  # Provider-specific data
}
```

## Reverse Geocoding Result

```python
{
    "lat": 40.7484,
    "lon": -73.9857,
    "address": "20 W 34th St, New York, NY 10001, USA",
    "components": {
        "house_number": "20",
        "road": "West 34th Street",
        "city": "New York",
        "state": "New York",
        "postcode": "10001",
        "country": "United States"
    }
}
```

## Example Workflows

### Geocode Customer Addresses
```python
geo = Geocoder()
result = geo.geocode_csv(
    input="customers.csv",
    column="shipping_address",
    output="customers_geocoded.csv"
)
print(f"Geocoded {result['success']} of {result['total']} addresses")
```

### Validate Addresses
```python
geo = Geocoder()
address = "123 Main St, Anytown"

result = geo.geocode(address)
if result:
    print(f"Valid: {result['address']}")
    print(f"Standardized: {result['components']}")
else:
    print("Address not found")
```

### Add Addresses to Coordinates
```python
geo = Geocoder()

locations = [
    (40.7128, -74.0060),
    (34.0522, -118.2437),
    (41.8781, -87.6298)
]

for lat, lon in locations:
    result = geo.reverse(lat, lon)
    print(f"({lat}, {lon}): {result['address']}")
```

## Rate Limiting

Nominatim requires 1 second between requests. The batch functions handle this automatically.

```python
# Automatic delay in batch operations
results = geo.batch_geocode(addresses, delay=1.0)

# For paid providers, can reduce delay
geo = Geocoder(provider="google", api_key="KEY")
results = geo.batch_geocode(addresses, delay=0.1)
```

## Error Handling

```python
result = geo.geocode("Invalid Address XYZ123")
if result is None:
    print("Address not found")
elif result.get('error'):
    print(f"Error: {result['error']}")
else:
    print(f"Found: {result['address']}")
```

## Dependencies

- geopy>=2.4.0
- pandas>=2.0.0

Overview

This skill converts postal addresses to geographic coordinates (geocoding) and converts coordinates to human-readable addresses (reverse geocoding). It supports single queries, CSV batch processing, multiple providers (Nominatim, Google, Bing), structured address components, and built-in caching for repeated lookups. Use it to enrich location data, validate addresses, or populate maps and analytics pipelines.

How this skill works

Instantiate the Geocoder with an optional provider and API key, then call geocode(address) to receive lat/lon and structured components or reverse(lat, lon) to retrieve a formatted address and components. Batch functions handle CSV input/output, respect provider rate limits by applying delays, and cache results to speed repeated lookups. Provider-specific raw data is preserved in results for advanced use.

When to use it

  • Enrich customer or asset records with latitude/longitude for mapping and spatial analysis
  • Validate and standardize addresses before mailing or billing
  • Reverse geocode device or event coordinates to show readable locations
  • Batch-process large address lists from CSV files with automatic rate-limit handling
  • Switch providers when you need higher accuracy, coverage, or quota (Google/Bing vs Nominatim)

Best practices

  • Use Nominatim for free, low-volume tasks and respect its 1 request/second rate limit
  • Provide an API key and prefer paid providers for high-volume or commercial workflows
  • Cache results and deduplicate addresses before batch geocoding to reduce requests
  • Normalize input addresses (trim, consistent formatting) to improve match rates
  • Inspect provider-specific raw results when components are incomplete or ambiguous

Example use cases

  • Geocode customer CSV to add lat/lon columns for delivery route optimization
  • Validate user-supplied shipping addresses at checkout and suggest standardized versions
  • Reverse geocode logged GPS events to attach neighborhood, city, or postal codes
  • Migrate legacy address data by batch-processing and flagging unmatched records
  • Integrate into ETL pipelines to enrich rows with structured address components

FAQ

What providers are supported and when should I switch?

Nominatim is default and free but rate-limited; switch to Google or Bing with an API key for higher volume, SLA, or improved coverage in some regions.

How does batch rate limiting work?

Batch functions accept a delay parameter and apply it between requests. Defaults enforce Nominatim's 1s limit; reduce delay for paid providers as allowed by their quotas.

What does the geocoding result include?

Results include formatted address, lat/lon, structured components (house number, road, city, state, postcode, country), and provider raw data for troubleshooting.