-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgps2city.sh
More file actions
executable file
·28 lines (18 loc) · 923 Bytes
/
gps2city.sh
File metadata and controls
executable file
·28 lines (18 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
gps2city () {
# Input from $1: "21.2621477,-97.93331916"
input="$1"
IFS=',' read -r lat lon <<< "$input"
# Query OpenStreetMap Nominatim reverse geocoding API
response=$(curl -s "https://nominatim.openstreetmap.org/reverse?lat=$lat&lon=$lon&format=json&zoom=18&addressdetails=1" \
-H "User-Agent: bash-reverse-geocoder")
# Extract fields with fallbacks
house_number=$(echo "$response" | jq -r '.address.house_number // empty')
street=$(echo "$response" | jq -r '.address.road // .address.pedestrian // .address.footway // .address.path // empty')
city=$(echo "$response" | jq -r '.address.city // .address.town // .address.village // .address.hamlet // .address.county // empty')
state=$(echo "$response" | jq -r '.address.state // empty')
country=$(echo "$response" | jq -r '.address.country // empty')
# Output as CSV-style line
echo "$house_number,$street,$city,$state,$country"
}
gps2city $1