Bulk geocoding tool with automatic directional offset correction and quality flagging.
- Automatic directional offset correction (e.g., "5mi NW of X")
- Smart flagging system for quality control
- Batch processing with rate limiting
- Dual coordinate output (base + offset-corrected)
git clone https://github.com/yourusername/smart-geocoding.git
cd smart-geocoding
python3 -m venv geocode_env
source geocode_env/bin/activate # Windows: geocode_env\Scripts\activate
pip install -r requirements.txt- Go to Google Cloud Console
- Create a project and enable the Geocoding API
- Create an API Key
- Set up billing (free tier: 40,000 requests/month)
export GOOGLE_API_KEY='your-api-key-here'python geocode_locations_google.py input.csv output.csvCSV with these columns (all optional except province_state recommended):
| Column | Description |
|---|---|
province_state |
State/province (e.g., "California") |
county |
County name |
city |
City name |
prec_location |
Precise location (e.g., "5mi NW of Niland") |
latitude |
Leave blank to geocode |
longitude |
Leave blank to geocode |
The script adds these columns:
| Column | Description |
|---|---|
latitude |
Base coordinates from Google |
longitude |
Base coordinates from Google |
latitude_shifted |
Offset-corrected coordinates (if applicable) |
longitude_shifted |
Offset-corrected coordinates (if applicable) |
offset_applied |
Details of offset calculation |
google_formatted_address |
Google's standardized address |
location_type |
Accuracy level (ROOFTOP, APPROXIMATE, etc.) |
flag_status |
Quality flag (OK, REVIEW, WARNING) |
flag_reason |
Explanation of issues |
The script automatically handles directional offsets:
Input: 5mi NW of Niland, Imperial County, California
Process:
- Extracts base location:
Niland, Imperial County, California - Geocodes base location
- Calculates offset mathematically (5 miles at 315° bearing)
- Returns both base and shifted coordinates
Supported patterns:
5mi NW of X10 mi E of Y3.5mi S of Z50 miles west of A
Supported directions: N, NE, E, SE, S, SW, W, NW, NNE, ENE, ESE, SSE, SSW, WSW, WNW, NNW
WARNING: Requires manual verification
- County/state only (no city)
- Missing state
REVIEW: Double-check recommended
- Directional offsets
- Vague reference points ("behind X", "near Y")
- Water features
- Parks/natural areas
- Abbreviated names
OK: No issues detected
Edit script to customize:
INPUT_FILE = "locations.csv"
OUTPUT_FILE = "geocoded_locations.csv"
RATE_LIMIT_DELAY = 0.1 # seconds between requestsMIT License