Pandas can be used to load and filter Excel datasets containing geographic coordinates. For example, to extract Starbucks store locations in Shanghai from a spreadsheet, read the file and apply a city-based filter.
import pandas as pd
data_frame = pd.read_excel("stores_data.xlsx")
shanghai_locations = data_frame[data_frame['city'] == '上海市']
for _, entry in shanghai_locations.iterrows():
print(f"Store: {entry['name']}, Longitude: {entry['longitude']}, Latitude: {entry['latitude']}")
To collect store data via web APIs, inspect network requests in browser developer tools. Stabrucks provides a nearby stores endpoint that accepts parameters like latitude, longitude, and radius.
import requests
def fetch_starbucks_locations(coordinate_row):
endpoint = "https://www.starbucks.com.cn/api/stores/nearby"
request_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
query_params = {
"lat": coordinate_row['lat'],
"lon": coordinate_row['lon'],
"limit": "3000",
"locale": "ZH",
"features": "",
"radius": "200000"
}
response = requests.get(endpoint, headers=request_headers, params=query_params)
return response.json()
Baidu Maps offers a static image API for plotting coordinates on maps. However, it has limitations such as marker count restrictions and potential data latency.
import requests
from urllib.parse import quote
def generate_baidu_static_map(api_key, center_coord, markers_list, output_file):
base_url = "https://api.map.baidu.com/staticimage/v2"
map_params = {
'ak': api_key,
'center': center_coord,
'width': 800,
'height': 600,
'zoom': 10,
'markers': '|'.join([quote(f"{lon},{lat}") for lon, lat in markers_list])
}
image_response = requests.get(base_url, params=map_params)
if image_response.status_code == 200:
with open(output_file, 'wb') as img_file:
img_file.write(image_response.content)
For interactive visualizations, Pyecharts creates geographic scatter plots. It supports custom coordinate registration and styling.
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType
def create_geo_chart(store_dataframe, region_name, output_html):
geo_chart = Geo().add_schema(maptype=region_name)
filtered_stores = store_dataframe[store_dataframe['city'] == region_name]
for _, record in filtered_stores.iterrows():
geo_chart.add_coordinate(record['name'], record['longitude'], record['latitude'])
plot_data = [(record['name'], 1) for _, record in filtered_stores.iterrows()]
geo_chart.add('', plot_data, type_=GeoType.EFFECT_SCATTER, symbol_size=2)
geo_chart.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo_chart.set_global_opts(title_opts=opts.TitleOpts(title=f"Store Locations in {region_name}"))
geo_chart.render(output_html)
Baidu Maps Place API allows location searches within circular regions, similar to Starbucks' API. Implement pagination to handle result limits.
def search_baidu_places(keyword, area, max_results, api_key):
search_url = "http://api.map.baidu.com/place/v2/search"
all_results = []
page_num = 0
while len(all_results) < max_results:
params = {
'query': keyword,
'region': area,
'output': 'json',
'page_size': 20,
'page_num': page_num,
'ak': api_key
}
result_page = requests.get(search_url, params=params).json()
if 'results' not in result_page:
break
all_results.extend(result_page['results'])
page_num += 1
return all_results[:max_results]