Skip to content

Instantly share code, notes, and snippets.

@pranavr
Last active August 29, 2015 13:56
Show Gist options
  • Save pranavr/9312314 to your computer and use it in GitHub Desktop.
Save pranavr/9312314 to your computer and use it in GitHub Desktop.
# topLeft, bottomRight = [lat, lng],
# numPoints in each row/column, so numPoints = 10 will give you a 10x10 grid.
def makeGrid(topLeft, bottomRight, numPoints):
topLat = topLeft[0]
leftLng = topLeft[1]
bottomLat = bottomRight[0]
rightLng = bottomRight[1]
horizontal_offset = (compute_distance(topLeft, [topLat, rightLng])/numPoints)
vertical_offset = (compute_distance(topLeft, [bottomLat, leftLng])/numPoints)
print horizontal_offset, vertical_offset
pointer = topLeft
rowheader = topLeft
grid = [pointer]
for i in range(numPoints):
for j in range(numPoints):
next = compute_offset(pointer, horizontal_offset, 90)
grid.append(next)
pointer = next
rowheader = compute_offset(rowheader, vertical_offset, 180)
pointer = rowheader
return grid
## Helper functions for makeGrid
def degrees_to_radians(degrees):
return degrees * math.pi /180;
def radians_to_degrees(radians):
return radians * 180 / math.pi
def compute_distance(p1, p2):
[lat1, long1] = p1
[lat2, long2] = p2
degree_to_radians = math.pi/180
dist_to_miles = 3960
dist_to_kms = 6371.01
#phi = 90 - latitude
phi1 = (90.0 - lat1)* degree_to_radians
phi2 = (90.0 - lat2)* degree_to_radians
#theta = longitude
theta1 = long1*degree_to_radians
theta2 = long2*degree_to_radians
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1-theta2) + math.cos(phi1)*math.cos(phi2))
arc = math.acos(cos)
return arc * dist_to_miles
def compute_offset(startPoint, distance, bearing):
lat = startPoint[0]
lng = startPoint[1]
# in miles. divide by 6371.01for kms
distRatio = distance/ 3960
distSine = math.sin(distRatio)
distCosine = math.cos(distRatio)
bearing = degrees_to_radians(bearing)
startLatRad = degrees_to_radians(lat)
startLngRad = degrees_to_radians(lng)
startLatCos = math.cos(startLatRad)
startLatSin = math.sin(startLatRad)
endLatRads = math.asin((startLatSin * distCosine) + (startLatCos * distSine * math.cos(bearing)))
endLonRads = startLngRad + math.atan((math.sin(bearing) * distSine * startLatCos)/(distCosine - startLatSin * math.sin(endLatRads)));
finalLat = radians_to_degrees(endLatRads)
finalLng = radians_to_degrees(endLonRads)
return [finalLat, finalLng]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment