Skip to content

Instantly share code, notes, and snippets.

@previtus
Created June 22, 2017 02:10
Show Gist options
  • Save previtus/47dd466ff86bf79709f8a1ef3b5a40a6 to your computer and use it in GitHub Desktop.
Save previtus/47dd466ff86bf79709f8a1ef3b5a40a6 to your computer and use it in GitHub Desktop.
Scheme for generation of image urls on GoogleStreetView from edge described as tupple (self.Start, self.End)
### Inside Segment object
def getGoogleViewUrls(self, PIXELS_X, PIXELS_Y, minimal_length):
urls = []
filenames = []
min_allowed_distance = minimal_length
d = 1000*distance_between_two_points(self.Start, self.End)
number_of_splits = int(max((floor(d / min_allowed_distance)),1.0))
#print "---", d, min_allowed_distance, number_of_splits
before_frac = 0.0
loc = 0
for frac in range(0,number_of_splits):
current_frac = (frac+1) * (1.0 / number_of_splits)
# before_frac, current_frac will be 0.0-0.2, 0.2-0.4, etc all the way till 0.8-1.0
PointA = interpolation(self.Start, self.End, fraction=before_frac)
PointB = interpolation(self.Start, self.End, fraction=current_frac)
#d = 1000*distance_between_two_points(PointA, PointB)
#print before_frac, current_frac, d
urls1, filenames1 = self.betweenPoints(PointA, PointB)
urls += urls1
filenames += filenames1
loc = self.DistinctLocations.pop()
before_frac = current_frac
self.DistinctLocations.append(loc)
def betweenPoints(self, pointStart, pointEnd):
urls = []
filenames = []
# Looking from START to END
self.DistinctLocations.append(pointStart) # we are standing in Start
k = len(self.DistinctLocations) - 1
loaded_before = len(self.LocationsIndex)
# 3 IMGs
urls.append(self.getGoogleViewUrl(pointStart, pointEnd, PIXELS_X, PIXELS_Y))
filenames.append(self.getImageFilename(loaded_before+len(filenames)))
self.LocationsIndex.append(k)
urls.append(self.getGoogleViewUrl(pointStart, pointEnd, PIXELS_X, PIXELS_Y, degrees_offset=120.0))
filenames.append(self.getImageFilename(loaded_before+len(filenames)))
self.LocationsIndex.append(k)
urls.append(self.getGoogleViewUrl(pointStart, pointEnd, PIXELS_X, PIXELS_Y, degrees_offset=240.0))
filenames.append(self.getImageFilename(loaded_before+len(filenames)))
self.LocationsIndex.append(k)
# Looking from END to START
self.DistinctLocations.append(pointEnd) # we are standing in Start
k = len(self.DistinctLocations) - 1
# 3 IMGs
urls.append( self.getGoogleViewUrl(pointEnd, pointStart, PIXELS_X,PIXELS_Y) )
filenames.append( self.getImageFilename(loaded_before+len(filenames)) )
self.LocationsIndex.append(k)
urls.append( self.getGoogleViewUrl(pointEnd, pointStart, PIXELS_X,PIXELS_Y, degrees_offset=120.0))
filenames.append(self.getImageFilename(loaded_before+len(filenames)))
self.LocationsIndex.append(k)
urls.append( self.getGoogleViewUrl(pointEnd, pointStart, PIXELS_X,PIXELS_Y, degrees_offset=240.0))
filenames.append(self.getImageFilename(loaded_before+len(filenames)))
self.LocationsIndex.append(k)
return urls, filenames
def getBearingString(self, Location, Direction, degrees_offset=0.0):
return bearing_between_two_points(Location, Direction, degrees_offset)
def getGoogleViewUrl(self, Location, Direction, resx, resy, degrees_offset=0.0):
'Google View url from the start of this segment'
# http://maps.googleapis.com/maps/api/
# streetview?size=600x400&location=<lat>,<long>&heading=<angle from north>&key=<api>
lat = Location[0]
lon = Location[1]
bearing = round(self.getBearingString(Location, Direction, degrees_offset), 2)
url_start = "http://maps.googleapis.com/maps/api/streetview?size="
api = getApi()
full_url = [url_start, str(resx), "x", str(resy), "&location=", str(lat), ",", str(lon), "&heading=", str(bearing), "&key=", api]
return "".join(full_url)
### Outside Segment object, helper functions
def bearing_between_two_points(start, end, degrees_offset=0.0):
'''
Calculates the initial bearing between two geographical locations.
see: http://www.movable-type.co.uk/scripts/latlong.html
The bearing is angular distance from NORTH.
'''
if (type(start) != tuple) or (type(end) != tuple):
print type(start)
raise TypeError("Function bearing_between_two_points takes only tuples.")
lat1 = radians(start[0])
lat2 = radians(end[0])
dLong = radians(end[1] - start[1])
x = sin(dLong) * cos(lat2)
y = cos(lat1) * sin(lat2) - ( sin(lat1) * cos(lat2) * cos(dLong) )
bearing = degrees( atan2(x,y) )
bearing = bearing + degrees_offset
bearing_from_north = (bearing + 360) % 360
return bearing_from_north
def distance_between_two_points(start, end):
lat1 = start[0]
lat2 = end[0]
lon1 = start[1]
lon2 = end[1]
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
def interpolation(start, end, fraction):
# see: http://www.movable-type.co.uk/scripts/latlong.html
lat1 = start[0]
lat2 = end[0]
lon1 = start[1]
lon2 = end[1]
lat1, lon1, lat2, lon2 = map(radians, (lat1, lon1, lat2, lon2))
sinlat1 = sin(lat1)
coslat1 = cos(lat1)
sinlon1 = sin(lon1)
coslon1 = cos(lon1)
sinlat2 = sin(lat2)
coslat2 = cos(lat2)
sinlon2 = sin(lon2)
coslon2 = cos(lon2)
dlat = lat2 - lat1
dlon = lon2 - lon1
a = sin(dlat/2.0) * sin(dlat/2.0) + cos(lat1) * cos(lat2) * sin(dlon/2.0) * sin(dlon/2.0)
dd = 2.0 * atan2(sqrt(a), sqrt(1.0-a))
A = sin((1.0-fraction)*dd) / sin(dd)
B = sin(fraction*dd) / sin(dd)
x = A * coslat1 * coslon1 + B * coslat2 * coslon2
y = A * coslat1 * sinlon1 + B * coslat2 * sinlon2
z = A * sinlat1 + B * sinlat2
lat3 = atan2(z, sqrt(x*x + y*y))
lon3 = atan2(y, x)
return tuple(map(degrees, ([lat3, lon3])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment