diff --git a/.gitignore b/.gitignore index a6715f0..8148d81 100644 --- a/.gitignore +++ b/.gitignore @@ -89,4 +89,7 @@ ENV/ .ropeproject # Google API Key -ground_station/resources/core/key \ No newline at end of file +ground_station/resources/core/key + +# Map Images +*.jpg diff --git a/ground_station/resources/core/mapping.py b/ground_station/resources/core/mapping.py index fa7c6d8..0458758 100644 --- a/ground_station/resources/core/mapping.py +++ b/ground_station/resources/core/mapping.py @@ -5,7 +5,7 @@ ReWritten by Chris Pham Copyright OSURC, orginal code from GooMPy by Alec Singer and Simon D. Levy This code is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as +it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, @@ -27,15 +27,12 @@ from io import StringIO, BytesIO import os import time import PIL.Image +import signing ##################################### # Constants ##################################### _KEYS = [] -file_pointer = open('key', 'w') -for i in file_pointer: - _KEYS.append(file_pointer.readline().rstrip()) -file_pointer.close() # Number of pixels in half the earth's circumference at zoom = 21 _EARTHPIX = 268435456 @@ -48,6 +45,13 @@ _GRABRATE = 4 # Pixel Radius of Earth for calculations _PIXRAD = _EARTHPIX / math.pi +file_pointer = open('key', 'r') +for i in file_pointer: + _KEYS.append(i.rstrip()) +file_pointer.close() + +print _KEYS + class GMapsStitcher(object): def __init__(self, width, height, @@ -79,11 +83,11 @@ class GMapsStitcher(object): # Make the url string for polling # GET request header gets appended to the string urlbase = 'https://maps.googleapis.com/maps/api/staticmap?' - urlbase += 'center=%f%f&zoom=%d&maptype=%s&size=%dx%d&format=jpg&key=%s&signature=%s' + urlbase += 'center=%f,%f&zoom=%d&maptype=%s&size=%dx%d&format=jpg&key=%s' # Fill the formatting - specs = latitude, longitude, self.zoom, self.maptype, _TILESIZE, _KEYS[0], _KEYS[1] - filename = 'Resources/Maps/' + ('%f_%f_%d_%s_%d_%d_%s_%s' % specs) + '.jpg' + specs = latitude, longitude, self.zoom, self.maptype, _TILESIZE, _TILESIZE, _KEYS[0] + filename = 'Resources/Maps/' + ('%f_%f_%d_%s_%d_%d_%s' % specs) + '.jpg' # Tile Image object tile_object = None @@ -95,8 +99,10 @@ class GMapsStitcher(object): else: # make the url url = urlbase % specs - - result = urllib2.Request(url).read() + print url + url = signing.sign_url(url, _KEYS[1]) + print url + result = urllib2.urlopen(urllib2.Request(url)).read() tile_object = PIL.Image.open(BytesIO(result)) if not os.path.exists('Resources/Maps'): os.mkdir('Resources/Maps') @@ -117,14 +123,14 @@ class GMapsStitcher(object): temp = math.atan(math.exp(((lat_pixels + degree) - _EARTHPIX))/ _PIXRAD) return math.degrees(math.pi / 2 - 2 * temp) - def fetch_tiles(self,): + def fetch_tiles(self): # cap floats to precision amount self.latitude = self._fast_round(self.latitude, _DEGREE_PRECISION) self.longitude = self._fast_round(self.longitude, _DEGREE_PRECISION) # number of tiles required to go from center latitude to desired radius in meters if self.radius_meters is not None: - self.num_tiles = int(round(2*self._pixels_to_meters / (_TILESIZE / 2. / self.radius_meters))) + self.num_tiles = int(round(2*self._pixels_to_meters() / (_TILESIZE / 2. / self.radius_meters))) lon_pixels = _EARTHPIX + self.longitude * math.radians(_PIXRAD) diff --git a/ground_station/resources/core/mappingtest.py b/ground_station/resources/core/mappingtest.py new file mode 100644 index 0000000..12ad56c --- /dev/null +++ b/ground_station/resources/core/mappingtest.py @@ -0,0 +1,5 @@ +import mapping + +obj = mapping.GMapsStitcher(10000, 10000, 44.57078, -123.275998, 21, 'terrain', 200) + +obj.fetch_tiles() \ No newline at end of file diff --git a/ground_station/resources/core/signing.py b/ground_station/resources/core/signing.py new file mode 100644 index 0000000..d5f519a --- /dev/null +++ b/ground_station/resources/core/signing.py @@ -0,0 +1,53 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" Signs a URL using a URL signing secret """ + +import hashlib +import hmac +import base64 +import urlparse + +def sign_url(input_url=None, secret=None): + """ Sign a request URL with a URL signing secret. + + Usage: + from urlsigner import sign_url + + signed_url = sign_url(input_url=my_url, secret=SECRET) + + Args: + input_url - The URL to sign + secret - Your URL signing secret + + Returns: + The signed request URL + """ + + if not input_url or not secret: + raise Exception("Both input_url and secret are required") + + url = urlparse.urlparse(input_url) + + # We only need to sign the path+query part of the string + url_to_sign = url.path + "?" + url.query + + # Decode the private key into its binary format + # We need to decode the URL-encoded private key + decoded_key = base64.urlsafe_b64decode(secret) + + # Create a signature using the private key and the URL-encoded + # string using HMAC SHA1. This signature will be binary. + signature = hmac.new(decoded_key, url_to_sign, hashlib.sha1) + + # Encode the binary signature into base64 for use within a URL + encoded_signature = base64.urlsafe_b64encode(signature.digest()) + + original_url = url.scheme + "://" + url.netloc + url.path + "?" + url.query + + # Return signed URL + return original_url + "&signature=" + encoded_signature + +if __name__ == "__main__": + input_url = raw_input("URL to Sign: ") + secret = raw_input("URL signing secret: ") + print "Signed URL: " + sign_url(input_url, secret)