I want to search some stores in a country, for example:
Find all Nike stores in US.
I implement it by using Google Map Place API. However I found that Google API seems not allowed to search a target that located in specific country, and it also restrict the searching radius (50000 meters).
So, I'm now trying to use "scan window" as below, which is the most feasible way I can figure out.
The main concept is to give two point with latitude and longitude (blue point in img) to make a plane, and the program would slice this area into many small circle with fixed radius. And then we can use the center of each circle (the red point) to query the Google API.
But when I'm working with the slicing function, I have know idea how to get these red points if I want to shift a point by distance...
Does anyone knows how to implement this function ? Or is there any other way to achieve my goal ?
Thanks a lot.
CodePudding user response:
I just found a method to solve this problem, here's my sample code:
from geopy import distance
# slice the big area into small ones for Google map radius restriction problem
def slice_search_area(point_a=(None, None), point_b=(None, None), region=None, radius=50000):
if (None in point_a) or (None in [point_b]):
return []
p1 = point_a
p2 = (point_a[0], point_b[1])
p3 = point_b
p4 = (point_b[0], point_a[1])
# get the start point (move East first, then move South)
tmp_point = distance.distance(meters=radius).destination(p1, bearing=90)
start_point = distance.distance(meters=radius).destination(tmp_point, bearing=180)
# get every (latitude, longitude) of the points
scan_points = list()
row_start_point = start_point
while row_start_point[0] > p4[0]:
point = row_start_point
while point[1] < p2[1]:
# save (latitude, longitude)
scan_points.append((point.latitude, point.longitude))
# point move East
point = distance.distance(meters=radius).destination(point, bearing=90)
row_start_point = distance.distance(meters=radius).destination(row_start_point, bearing=180)
return scan_points
Here 's my test code:
country_area = {
'tw': [(25.34141, 120.10243), (21.959811, 121.986222)]
}
a = country_area['tw'][0]
b = country_area['tw'][1]
p = slice_search_area(a, b, 'tw')
for each in p:
print(each)
Result:
(24.889205690304994, 120.59910323641213)
(24.88838533068927, 121.0939524816418)
(24.887565001719338, 121.5887984580001)
(24.437811397277283, 120.59910323641213)
(24.437007840789235, 121.09217552708851)
(24.436204314108405, 121.58524469306849)
(23.98639017250887, 120.59910323641213)
(23.985603303344558, 121.09044150686496)
(23.98481646316774, 121.58177679194344)
(23.53494238775737, 120.59910323641213)
(23.53417209335193, 121.08874975902926)
(23.5334018271324, 121.57839343089873)
(23.083468421486256, 120.59910323641213)
(23.082714592440123, 121.08709964298127)
(23.081960790795765, 121.57509332888482)
(22.631968658771996, 120.59910323641213)
(22.631231188779743, 121.08549053882054)
(22.630493745421962, 121.57187524624496)
(22.180443491209363, 120.59910323641213)
(22.179722276991253, 121.0839218467313)
(22.179001088656495, 121.56873798348572)
If I label the points on Google map, I can get:
Which can be used to search the target in each circle through Google Map API.