LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2024, 03:28 PM   #1
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
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"
    ]
  }
]

Last edited by teckk; 02-24-2024 at 03:30 PM.
 
Old 02-25-2024, 07:26 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by teckk View Post
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: French grant OpenStreetMap access to land registry data LXer Syndicated Linux News 0 01-21-2009 06:50 PM
LXer: This week at LWN: OpenStreetMap contemplates licensing LXer Syndicated Linux News 0 11-07-2008 02:00 AM
LXer: Spend your vacation getting started with OpenStreetMap LXer Syndicated Linux News 0 08-16-2008 02:40 PM
LXer: OpenStreetMap project completes import of United States TIGER data LXer Syndicated Linux News 0 01-24-2008 01:30 PM
LXer: OpenStreetMap project imports US government maps LXer Syndicated Linux News 0 10-12-2007 07:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration