Add testing file with updated gitignore for jpgs

This commit is contained in:
Chris Pham
2018-01-20 16:32:58 -08:00
parent 5fa02813f3
commit 1961da734b
4 changed files with 81 additions and 15 deletions

3
.gitignore vendored
View File

@@ -90,3 +90,6 @@ ENV/
# Google API Key
ground_station/resources/core/key
# Map Images
*.jpg

View File

@@ -27,21 +27,17 @@ from io import StringIO, BytesIO
import os
import time
import PIL.Image
import signing
#####################################
# Constants
#####################################
<<<<<<< 1bd9af00f206c528febfe9b024e50b8bd4ee73fb
<<<<<<< 666f522bf5947794d802e8cbbf221ac004fc4eed
=======
=======
_KEYS = []
>>>>>>> Fixed to fit PEP
file_pointer = open('key', 'w')
for i in file_pointer:
_KEYS.append(file_pointer.readline().rstrip())
file_pointer.close()
>>>>>>> fixed fp naming conventions
# Number of pixels in half the earth's circumference at zoom = 21
_EARTHPIX = 268435456
@@ -54,6 +50,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,
@@ -85,11 +88,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
@@ -101,8 +104,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')
@@ -123,14 +128,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)

View File

@@ -0,0 +1,5 @@
import mapping
obj = mapping.GMapsStitcher(10000, 10000, 44.57078, -123.275998, 21, 'terrain', 200)
obj.fetch_tiles()

View File

@@ -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)