Road warriors: (Auto-)updating a "Current location" note, great for Obsidian Leaflet!

Road warriors: (Auto-)updating a “Current location” note, great for Obsidian Leaflet!

I’m impressed about all the features @valentine.195 adds to the Obsidian Leaflet map plugin, one of the latest being immediate response to changing the location: [lat, lon] entry in notes, and picking up a map center and “nearby” overlay from a note’s YAML.

That said, not everyone has a GPS-enabled laptop (I do, fortunately, but it only works when having a WAN connection), so I thought it might be useful to get an (approximate) geo location from the machine’s/network’s IP address instead.

This way, one could still update one’s location while being in a hotel, at a friend’s place or sitting in a conference. Thus, you could have a current “Nearby” map note in Obsidian, to find what places to visit or where to go for lunch.

The Python script

So let’s make a little Python3 script that does exactly that:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# location-update
# print current location, gotten from IP address
# Also write an Obsidian Markdown "Current Location" file.
# This can be used in a crontab, for automatic updating.
# MCH 2021-06-09 v0.1.0

__author__ = 'Matthias C. Hormann'
__version__ = '0.1.0'

import sys
import datetime
try:
    import geocoder
except ImportError:
    print("'geocoder' not installed, try 'pip install geocoder'.")
    sys.exit()

# CHANGE YOUR FILE LOCATION HERE!
file = "/home/matthias/Dokumente/Schreiben/Locations/-Current Location.md"

try:
    g = geocoder.ip('me')
except:
    print("Could not determine your location, no file changed.")
    sys.exit()

if g.ok:
    try:
        with open(file, "w") as f:
            f.write("""---
date: {date}
location: {location}
address: "{address}"
mapmarker: Current
nearby: 10 km
---

### My current location

Current location: **`=this.location`**

Current address: **`=this.address`**

Last update: `=this.date`

*Note:* This should update automatically, via a cron job. **If it does not**,
*use `location-update` in a terminal for a manual update.*
""".format(
                date = datetime.datetime.now().replace(microsecond=0).isoformat(),
                location = str(g.latlng),
                address = g.address
            )
            )

    except:
        print("Error: Could not write to '" + file + "'")

    print('location:', g.latlng)

else:

    print("Could not determine your location, no file changed.")

sys.exit()

This will require the geocoder module to be installed (pip install geocoder) and should be saved in either your ~/bin/ or your ~/.local/bin/ folder (assuming you use Linux). I saved it as location-update. (On Windows machinery, you might have to add a .py extension.)

Edit this file using a normal text editor, and put the correct file path for your “current location” note into it. Use an absolute path, i.e. one starting with /home/…, because ~ and $HOME won’t work when using this in a crontab, for automated updates. Yes, that is possible!

Try if it works by typing location-update in a terminal window. It should create a note like this:

---
date: 2021-06-09T13:38:15
location: [48.2418, 10.3632]
address: "Krumbach, Bavaria, DE"
mapmarker: Current
nearby: 10 km
---

### My current location

Current location: **`=this.location`**

Current address: **`=this.address`**

Last update: `=this.date`

*Note:* This should update automatically, via a cron job. **If it does not**,
*use `location-update` in a terminal for a manual update.*

In my case, that’s in /home/matthias/Dokumente/Schreiben/Locations/-Current Location.md.

You can of course alter the text to your personal preferences.

Usage with Dataview

You can now use the information in other notes, using Dataview. Assuming your note was called -Current Location.md, you can access your current location, map radius, approximate address and date/time of last update like so (with ` added around the commands):

  • =[[-Current Location]].location
  • =[[-Current Location]].nearby
  • =[[-Current Location]].address
  • =[[-Current Location]].date

Usage with Obsidian Leaflet maps

I’ve set up a “Current” marker in Obsidian Leaflet to go with this:

Here is an example of a (self-updating) “Nearby” map:

## Nearby

People, locations and events within `=[[-Current Location]].nearby` around location `=[[-Current Location]].location` (`=[[-Current Location]].address`).

```leaflet
id: Nearby
markerFile: [[-Current Location]]
height: 500px
minZoom: 1
maxZoom: 18
unit: km
overlayTag: nearby
overlayColor: 'rgba(218, 165, 32, .6)'
coordinates: [[-Current Location]]
zoomTag: nearby
```

And here is how it looks like:

After you’ve arrived somewhere and connected to their WiFi, just run location-update and BOOM! the map will magically update.

Putting it in a crontab for fully-automated use

This is normally not needed, but can be done. Edit your crontab (crontab -e) and add a line like the following, assuming you have location-update in your ~/bin/ folder, and want an update every 15 minutes:

*/15 * * * * $HOME/bin/location-update

Voilà!

Hope this is useful for some of us!

2 Likes