I looked up reverse geocoding with openstreetmap and found a keyless api.
Reverse geocoding is the process of turning a location as a latitude and
longitude into a place name. This is handy for creating my daily post footer, I
want to have a script that will take in a lat/lng pair and output the full
location name and weather with a map link.
I can use the kindly provided nominatim reverse geocoding URI and a bit of
python. I guess openstreetmap thinks I am in a weird parallel UK that is made
up of states, that is easy to deal with thankfully.
base_url = "http://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1"
uri = base_url.format(lat, lng)
fp = urllib.request.urlopen(uri)
response = fp.read()
location = json.loads(response.decode("utf8"))
fp.close()
city = location['address']['city']
country = location['address']['country']
if country == "UK" or country == "US":
country = location['address']['state']
return {'country':country, 'city':city}
I end up with a single script for generating the location/weather block. The
script will default my 'work' location or it will try and format a lat/lng out
of any arguments passed in.
#!/usr/bin/env python3.5
import forecastio
import pprint
import urllib.request
import json
import sys
api_key = "yer_key_here_bawbag"
lat = 57.168
lng = -2.1055
def forwardweather(lat, lng):
forecast = forecastio.load_forecast(api_key, lat, lng)
weather = forecast.daily().data[0]
temperatureMax = int(weather.apparentTemperatureMax)
temperatureMin = int(weather.apparentTemperatureMin)
summary = weather.summary
return {'temperature':temperatureMax, 'summary':summary}
def reversegeocode(lat, lng):
base_url = "http://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1"
uri = base_url.format(lat, lng)
fp = urllib.request.urlopen(uri)
response = fp.read()
location = json.loads(response.decode("utf8"))
fp.close()
city = location['address']['city']
country = location['address']['country']
if country == "UK" or country == "US":
country = location['address']['state']
return {'country':country, 'city':city}
if __name__ == "__main__":
if len(sys.argv) == 2:
loc = sys.argv[1].split(',')
if len(loc) != 2:
exit()
lat = float(loc[0])
lng = float(loc[1])
if len(sys.argv) == 3:
lat = float(sys.argv[1])
lng = float(sys.argv[2])
print("Getting weather for: {}, {}\n\n".format(lat, lng))
weather = forwardweather(lat, lng)
location = reversegeocode(lat, lng)
base_url = "http://www.openstreetmap.org/search?query={}%2C%20{}"
uri = base_url.format(lat, lng)
print("[{}, {}][0]: {}°C, {}".format(location['city'], location['country'],
weather['temperature'], weather['summary']))
print("\n[0]: {}".format(uri))
Reading:
Virtual Light
I am still playing with other fields to stick onto the daily post. So far I
have been sticking on a reading field that can sort of track how I am
progressing with books. I want to include a fuzzy location and the state of the
weather around me, obviously I know where I am looking back it will be
interesting to me having a record of where I was when I posted.
I have tried with
outside
,
reality
,
being
and a load of other
vague terms, writing this out those all look ridiculous. Now I have tried just
letting the info hang there instead, my current lat/long converted to a place
name with a link to a map, followed by the weather.
Reading:
Cibola Burn, Virtual Light
Union Terrace Gardens has some excellent pieces that were put up as part of a
street art festival
. Adding culture to the city is great, but there is
something about 'santioned creativity' that really annoys me. I know the
residents around here would be up in arms if someone did a giant mural
overnight.
Reading:
Cibola Burn, Virtual Light
Location:
57.1578,-2.2143
Weather:
2°C Partly cloudy starting in the evening.
I think the weather stuff I played with
yesterday
is going to be an input
to a quantified self dashboard I have been toying with building for a long
time.
I have wanted to put together a dash for years, but I have always struggled to
find technologies that I want to work with. For a
demo at work
I have had
to put together a simple dash, all it does is show interface throughput for two
interfaces, but it has give a chance to play with the front end UI and backend
webserving components that I want to use.
I am lurking in a coffee shop now, which is a great time to have a first whack
at the idea.
It
is
Sunday, so that
makes
seven
days
of
writing
.
Reading:
Cibola Burn, Virtual Light
Location:
57.1446, -2.1060
Weather:
6˚C Clear.
My good friend Warren Ellis (well complete stranger, but I read his newsletter
so that is pretty the same thing) tweets pictures of where he is with the
weather info overlaid. I am sure he is using some sort of newfangled social
media filter to provide the info. I want something similar for the footnotes on
my fairly post, but social media stuff is no good for me, I need an API to use.
Now, as hard as I try I cannot find a weather service that will just spit some
data at me. I really want to do
curl weathersite.internet | jq...
and end up
with a nice summary for a location. The web is closing up and locking down,
which means an API key is required.
After putting this off for a while, this morning I remembered I have previously
registered for a weather service. A steaming cup of coffee later and I found
the
python bindings
to the excellent
forecast.io
already installed.
import forecastio
api_key = "yer_key_here_bampot"
lat = 57.168
lng = -2.1055
forecast = forecastio.load_forecast(api_key, lat, lng)
weather = forecast.daily().data[0]
temperatureMax = int(weather.apparentTemperatureMax)
temperatureMin = int(weather.apparentTemperatureMin)
summary = weather.summary
print("{}°C {}".format(temperatureMax, summary))
Gives a nice
4°C Partly cloudy throughout the day.
There isn't anything to this, If I could find an API that didn't require a key
I probably wouldn't even use python. But madness makes more madness, so here we
are.
Reading:
Cibola Burn, Virtual Light
Location:
57.168, -2.1055
Weather:
4°C Partly cloudy throughout the day.
Warren Ellis's
morning.computer
was the main driver for me to start
blogging everyday. I like to think I am being influenced by someone super
productive, rather than blantently copying him.