Exploring streets and space with Python

Tags

, , , , , , , , , , , , ,

Arkwood, my languid Belgian buddy, was busy tapping on the wall. Plumes of plaster dust enveloped us both. ‘I am looking for the second world,’ he told me. ‘Ah, the synthetic world!’ I exclaimed, ‘Let me show you.’

Now, the natural world contains land, sea and space. Today I shall write some synthetic Python code to reach out and touch land and space. To understand it.

For land, I will walk the pavements of Google Street View. I have already written the code to take a random stroll through Google Street View. Indeed, I recently used this code to find out how green certain cities of the world are.

For space, I will traverse the stars of Microsoft WorldWide Telescope. I have previously used computer vision techniques to find the biggest stars and track the planet Venus. There is also a WorldWide Telescope SDK, which I have employed to whizz about space.

Wouldn’t it be cool to join streets and space together, so that we can look up at the stars from whatever city we happen to be in. No problem!

from storage import Storage
import webbrowser
from time import sleep

storage = Storage()

# constants
SPACE_EXPLORER_URL = "http://localhost/space_explorer.htm?Lat={}&Lng={}&Alt=45"

# load street urls
street_urls = storage.read_all_values("StreetsAndSpace/streetview_replay.conf")

##loop urls
for url in street_urls:
 
    # open web browser
    webbrowser.open(url)    
    sleep(40)

# build space url
street_params = street_urls[-1].split("&cbll=")[1].split("&cbp=")[0].split(",")
space_url = SPACE_EXPLORER_URL.format(street_params[0], street_params[1])

# open web broswer
webbrowser.open(space_url)    
sleep(60)

First up, we load all the street urls that we have generated from our random stroll through Google Street View. We loop the urls, opening each in a browser.

Next, we build our space url, using the latitude and longitude of our last street url. Stackoverflow provides a breakdown of the Google Street View url parameters, as well as some nifty ideas for how to extract our coordinates.

With our space url built, we can open a browser to see the night sky.

The important point to note is that by sharing the latitude and longitude between Google Street View and Microsoft WorldWide Telescope, we are able to see a view of the night sky specific to the city we are strolling through.

Time for a demo. Here’s our stroll though the streets of London, finishing with a peer up to the astronomical sky:

https://maps.google.com/?layer=c&cbll=51.509164,-0.13567&cbp=12,90,0,0,0
StreetsAndSpace_LondonStreet_1

https://maps.google.com/?layer=c&cbll=51.509164,-0.13547&cbp=12,90,0,0,0
StreetsAndSpace_LondonStreet_2


http://localhost/space_explorer.htm?Lat=51.509164&Lng=-0.13547&Alt=45

StreetsAndSpace_LondonSpace_1

Ha! Google Street View has strolled into a quintessentially English polo shop before going into space.

Note that we are rendering space using the WorldWide Telescope SDK on a local web page – see details at the foot of this post.

Here’s Sydney:

https://maps.google.com/?layer=c&cbll=-33.894291,151.266311&cbp=12,90,0,0,0
StreetsAndSpace_SydneyStreet_1

https://maps.google.com/?layer=c&cbll=-33.894291,151.266511&cbp=12,90,0,0,0
StreetsAndSpace_SydneyStreet_2

http://localhost/space_explorer.htm?Lat=-33.894291&Lng=151.266511&Alt=45
StreetsAndSpace_SydneySpace_1

And what about San Francisco?

https://maps.google.com/?layer=c&cbll=37.773038,-122.406598&cbp=12,0,0,0,0
StreetsAndSpace_SanFranciscoStreet_1

https://maps.google.com/?layer=c&cbll=37.773238,-122.406598&cbp=12,0,0,0,0
StreetsAndSpace_SanFranciscoStreet_2

http://localhost/space_explorer.htm?Lat=37.773238&Lng=-122.406598&Alt=45
StreetsAndSpace_SanFranciscoSpace_1

Brilliant! Lots of celestial bodies in the San Francisco ether.

As you can see, each city shows its own current view of the night sky.

I told Arkwood that I had connected the synthetic and natural worlds together. ‘I will now be able to use computer vision techniques to understand space from different cities!’

Arkwood continued to read The Inscrutable Diaries Of Rodger Saltwash, merely stating, ‘Whoopty fuckin doo.’

Looks like this journey between the two worlds is mine alone.

Ciao!

P.S.

Here’s the web page I put together, to render the night sky from each city. It’s based on the WorldWide Telescope SDK WWT Web Client Local View code sample.

<!DOCTYPE html >
<html>
<head>
<meta charset="utf-8" http-equiv="X-UA-Compatible" content="chrome=1, IE=edge"/>
<title>Space Explorer</title>
<script src="http://www.worldwidetelescope.org/scripts/wwtsdk.aspx"></script>
<script>
  
// Worldwide Telescope object
var wwt;
    
// initialize wwt and fire wwtReady event
function initialize() {
	wwt = wwtlib.WWTControl.initControl("WWTCanvas");
	wwt.add_ready(wwtReady);
	wwt.endInit();
}

// custom code for WWT
function wwtReady() {
    wwt.loadImageCollection("http://www.worldwidetelescope.org/COMPLETE/wwtcomplete.wtml");

    // wwt preference settings
    wwt.settings.set_showCrosshairs(false);
    wwt.settings.set_showConstellationFigures(false);
    wwt.settings.set_showConstellationBoundries(false);

    // wwt location settings
    wwt.settings.set_locationLat(getUrlParam("Lat"));
    wwt.settings.set_locationLng(getUrlParam("Lng"));
    wwt.settings.set_locationAltitude(getUrlParam("Alt"));
    wwt.settings.set_localHorizonMode(true);
}

// get value from url querystring parameter
function getUrlParam(key) {
    return unescape(window.location.search.replace(
        new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"
        ));
}

</script>
</head>
<body onload="initialize()">
<div id="WorldWideTelescopeControlHost">
	<div id="WWTCanvas" style="width:1500px; height:750px;" />
</div>
</body>
</html>

Note how the javascript makes use of the latitude and longitude parameters we passed across on the url querystring.

Follow

Get every new post delivered to your Inbox.

Join 78 other followers