LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Geolocate with openstreetmap (https://www.linuxquestions.org/questions/programming-9/geolocate-with-openstreetmap-4175734209/)

teckk 02-24-2024 03:28 PM

Geolocate with openstreetmap
 
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" ;;
esac

Example 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 © 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 © 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, Nürnberger Straße, Innenstadt, Erlangen, Bayern, 91052, Deutschland",
  "address": {
    "house_number": "24-26",
    "road": "Nürnberger Straße",
    "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 © 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 © 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"
    ]
  }
]


ntubski 02-25-2024 07:26 AM

Quote:

Originally Posted by teckk (Post 6485675)
Did you know that you can geolocate fairly easy in shell with openstreetmap?
No python, just bash and friends.

I'd say the geolocation part is happening on the server, the bash code you posted is just querying it. The stuff the server requires to run is here: https://nominatim.org/release-docs/l.../Installation/ (it's quite a bit).


Also probably worth mentioning the server's terms of use when you post a script like that: https://operations.osmfoundation.org...ies/nominatim/
Quote:

  • No heavy uses (an absolute maximum of 1 request per second).
  • Provide a valid HTTP Referer or User-Agent identifying the application (stock User-Agents as set by http libraries will not do).
  • Clearly display attribution as suitable for your medium.
  • Data is provided under the ODbL license which requires to share alike (although small extractions are likely to be covered by fair usage / fair dealing).



All times are GMT -5. The time now is 07:18 PM.