Geolocate with openstreetmap
by teckk from LinuxQuestions.org on (#6JW8K)
Did you know that you can geolocate fairly easy in shell with openstreetmap?
No python, just bash and friends.
A little example:
GeoLocate.sh
Code:#!/usr/bin/bash
# Geolocate with openstreetmap
# Needs curl or wget and jq
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
read -p "Geolocate City/State, Latt/Long, or Street Address? C or L or S: " cls
case "$cls" in
[Cc]* ) read -p "Enter City: " city
read -p "Enter State: " state
curl -A "$agent" "https://nominatim.openstreetmap.org/search.php?q="$city"+"$state"&format=jsonv2" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/search.php?q="$city"+"$state"&format=jsonv2" -O - | jq .
;;
[Ll]* ) read -p "Enter Latitude: " lat
read -p "Enter Longitude: " lon
curl -A "$agent" "https://nominatim.openstreetmap.org/reverse?lat="$lat"&lon="$lon"&format=jsonv2" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/reverse?lat="$lat"&lon="$lon"&format=jsonv2" -O - | jq .
;;
[Ss]* ) read -p "Enter Street Address, City, State: " sa
sa=${sa// /+}
curl -A "$agent" "https://nominatim.openstreetmap.org/search?q="$sa"&format=json&polygon=1&addressdetails=1" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/search?q="$sa"&format=json&polygon=1&addressdetails=1" -O - | jq .
;;
*) echo -e "You did not enter C, L, or S try again.\n" ;;
esacExample usage and outputs. A city in Germany, and a Walmart super center in the US.
Code:bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: c
Enter City: erlangen
Enter State: germany
[
{
"place_id": 141419719,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "relation",
"osm_id": 62403,
"lat": "49.5928616",
"lon": "11.0056",
"category": "boundary",
"type": "administrative",
"place_rank": 12,
"importance": 0.5749803334439526,
"addresstype": "city",
"name": "Erlangen",
"display_name": "Erlangen, Bayern, Deutschland",
"boundingbox": [
"49.5327088",
"49.6455844",
"10.9153629",
"11.0536043"
]
}
]
bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: l
Enter Latitude: 49.5928616
Enter Longitude: 11.0056
{
"place_id": 370285162,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 43542116,
"lat": "49.5928544",
"lon": "11.006090013295362",
"category": "building",
"type": "commercial",
"place_rank": 30,
"importance": 0.00000999999999995449,
"addresstype": "building",
"name": "",
"display_name": "24-26, Nirnberger Straie, Innenstadt, Erlangen, Bayern, 91052, Deutschland",
"address": {
"house_number": "24-26",
"road": "Nirnberger Straie",
"suburb": "Innenstadt",
"city": "Erlangen",
"state": "Bayern",
"ISO3166-2-lvl4": "DE-BY",
"postcode": "91052",
"country": "Deutschland",
"country_code": "de"
},
"boundingbox": [
"49.5924942",
"49.5931097",
"11.0055676",
"11.0065303"
]
}
bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: s
Enter Street Address, City, State: 4545 Lafayette Rd, Indianapolis, IN 46254
[
{
"place_id": 339230446,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 437510523,
"lat": "39.83754349180328",
"lon": "-86.24796547540983",
"class": "place",
"type": "house",
"place_rank": 30,
"importance": 9.999999994736442E-8,
"addresstype": "place",
"name": "",
"display_name": "4545, Lafayette Road, Indianapolis, Marion County, Indiana, 46254, United States",
"address": {
"house_number": "4545",
"road": "Lafayette Road",
"city": "Indianapolis",
"county": "Marion County",
"state": "Indiana",
"ISO3166-2-lvl4": "US-IN",
"postcode": "46254",
"country": "United States",
"country_code": "us"
},
"boundingbox": [
"39.8374935",
"39.8375935",
"-86.2480155",
"-86.2479155"
]
},
{
"place_id": 324260744,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 221723182,
"lat": "39.8381331",
"lon": "-86.24422095855766",
"class": "shop",
"type": "supermarket",
"place_rank": 30,
"importance": 0.00000999999999995449,
"addresstype": "shop",
"name": "Walmart Supercenter",
"display_name": "Walmart Supercenter, 4545, Lafayette Road, Indianapolis, Marion County, Indiana, 46254, United States",
"address": {
"shop": "Walmart Supercenter",
"house_number": "4545",
"road": "Lafayette Road",
"city": "Indianapolis",
"county": "Marion County",
"state": "Indiana",
"ISO3166-2-lvl4": "US-IN",
"postcode": "46254",
"country": "United States",
"country_code": "us"
},
"boundingbox": [
"39.8374698",
"39.8390050",
"-86.2448576",
"-86.2433854"
]
}
]
No python, just bash and friends.
A little example:
GeoLocate.sh
Code:#!/usr/bin/bash
# Geolocate with openstreetmap
# Needs curl or wget and jq
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
read -p "Geolocate City/State, Latt/Long, or Street Address? C or L or S: " cls
case "$cls" in
[Cc]* ) read -p "Enter City: " city
read -p "Enter State: " state
curl -A "$agent" "https://nominatim.openstreetmap.org/search.php?q="$city"+"$state"&format=jsonv2" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/search.php?q="$city"+"$state"&format=jsonv2" -O - | jq .
;;
[Ll]* ) read -p "Enter Latitude: " lat
read -p "Enter Longitude: " lon
curl -A "$agent" "https://nominatim.openstreetmap.org/reverse?lat="$lat"&lon="$lon"&format=jsonv2" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/reverse?lat="$lat"&lon="$lon"&format=jsonv2" -O - | jq .
;;
[Ss]* ) read -p "Enter Street Address, City, State: " sa
sa=${sa// /+}
curl -A "$agent" "https://nominatim.openstreetmap.org/search?q="$sa"&format=json&polygon=1&addressdetails=1" | jq .
#wget -U "$agent" "https://nominatim.openstreetmap.org/search?q="$sa"&format=json&polygon=1&addressdetails=1" -O - | jq .
;;
*) echo -e "You did not enter C, L, or S try again.\n" ;;
esacExample usage and outputs. A city in Germany, and a Walmart super center in the US.
Code:bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: c
Enter City: erlangen
Enter State: germany
[
{
"place_id": 141419719,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "relation",
"osm_id": 62403,
"lat": "49.5928616",
"lon": "11.0056",
"category": "boundary",
"type": "administrative",
"place_rank": 12,
"importance": 0.5749803334439526,
"addresstype": "city",
"name": "Erlangen",
"display_name": "Erlangen, Bayern, Deutschland",
"boundingbox": [
"49.5327088",
"49.6455844",
"10.9153629",
"11.0536043"
]
}
]
bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: l
Enter Latitude: 49.5928616
Enter Longitude: 11.0056
{
"place_id": 370285162,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 43542116,
"lat": "49.5928544",
"lon": "11.006090013295362",
"category": "building",
"type": "commercial",
"place_rank": 30,
"importance": 0.00000999999999995449,
"addresstype": "building",
"name": "",
"display_name": "24-26, Nirnberger Straie, Innenstadt, Erlangen, Bayern, 91052, Deutschland",
"address": {
"house_number": "24-26",
"road": "Nirnberger Straie",
"suburb": "Innenstadt",
"city": "Erlangen",
"state": "Bayern",
"ISO3166-2-lvl4": "DE-BY",
"postcode": "91052",
"country": "Deutschland",
"country_code": "de"
},
"boundingbox": [
"49.5924942",
"49.5931097",
"11.0055676",
"11.0065303"
]
}
bash ./GeoLocate.sh
Geolocate City/State, Latt/Long, or Street Address? C or L or S: s
Enter Street Address, City, State: 4545 Lafayette Rd, Indianapolis, IN 46254
[
{
"place_id": 339230446,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 437510523,
"lat": "39.83754349180328",
"lon": "-86.24796547540983",
"class": "place",
"type": "house",
"place_rank": 30,
"importance": 9.999999994736442E-8,
"addresstype": "place",
"name": "",
"display_name": "4545, Lafayette Road, Indianapolis, Marion County, Indiana, 46254, United States",
"address": {
"house_number": "4545",
"road": "Lafayette Road",
"city": "Indianapolis",
"county": "Marion County",
"state": "Indiana",
"ISO3166-2-lvl4": "US-IN",
"postcode": "46254",
"country": "United States",
"country_code": "us"
},
"boundingbox": [
"39.8374935",
"39.8375935",
"-86.2480155",
"-86.2479155"
]
},
{
"place_id": 324260744,
"licence": "Data i OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 221723182,
"lat": "39.8381331",
"lon": "-86.24422095855766",
"class": "shop",
"type": "supermarket",
"place_rank": 30,
"importance": 0.00000999999999995449,
"addresstype": "shop",
"name": "Walmart Supercenter",
"display_name": "Walmart Supercenter, 4545, Lafayette Road, Indianapolis, Marion County, Indiana, 46254, United States",
"address": {
"shop": "Walmart Supercenter",
"house_number": "4545",
"road": "Lafayette Road",
"city": "Indianapolis",
"county": "Marion County",
"state": "Indiana",
"ISO3166-2-lvl4": "US-IN",
"postcode": "46254",
"country": "United States",
"country_code": "us"
},
"boundingbox": [
"39.8374698",
"39.8390050",
"-86.2448576",
"-86.2433854"
]
}
]