首页 > 其他分享 >通过百度地图地理查询和逆地址查询

通过百度地图地理查询和逆地址查询

时间:2024-04-26 10:24:10浏览次数:24  
标签:xlsx df 查询 地址 address lat lng 百度

# coding:utf-8
from urllib.request import quote
import requests
import pandas as pd
 
pd.set_option('display.width', 1000)
pd.set_option('display.max_columns', None)
 
import json
 
# 通过百度地图地理查询和逆地址查询
def get_location(address):
    try:
        url = 'http://api.map.baidu.com/place/v2/search?query={}&region=标注范围&output=json&ak=你的ak&scope=2'.format(quote(address))
        response = requests.get(url)
        result = response.json()
        if result['status'] == 0 and result['results']:
            lng = result['results'][0]['location']['lng']
            lat = result['results'][0]['location']['lat']
            return lng, lat
    except Exception as e:
        print(f"Failed to get location for {address}: {e}")
    return '', ''
 
def get_address(lng, lat):
    try:
        url = 'http://api.map.baidu.com/reverse_geocoding/v3/?'
        output = 'json'
        ak = 'BXottO3XSK3SKXn99fHquBoPo620cExI'
        uri = url + '&output=' + output + '&ak=' + ak + '&location=' + str(lat) + ',' + str(lng)
        response = requests.get(uri)
        answer = response.json()
        if answer['status'] == 0:
            address = answer['result']['formatted_address']
            return address
    except Exception as e:
        print(f"Failed to get address for coordinates ({lng}, {lat}): {e}")
    return 'Nan'
 
# 导入Excel文件
df = pd.read_excel('getAdress.xlsx')
 
# 新增两列用于存储查询结果
df['经度'] = ''
df['纬度'] = ''
df['详细地址'] = ''
 
print('选择通过百度地图查询经纬度(输入1)或查询地址(输入2):')
i = int(input())
 
if i == 1:
    # 批量查询经纬度
    for index, row in df.iterrows():
        if pd.isna(row['地址']):
            continue
        lng, lat = get_location(row['地址'])
        df.at[index, '经度'] = lng
        df.at[index, '纬度'] = lat
 
    # 保存结果到新的xlsx文件
    df[['经度', '纬度']].to_excel('经纬度.xlsx', index=False)
    print('经纬度查询结果已保存到经纬度.xlsx')
    
elif i == 2:
    # 加载保存了经纬度的xlsx文件
    df_lnglat = pd.read_excel('经纬度.xlsx')
 
    # 批量查询详细地址
    for index, row in df_lnglat.iterrows():
        lng = row['经度']
        lat = row['纬度']
        address = get_address(lng, lat)
        df.at[index, '详细地址'] = address
 
    # 输出结果并保存到新的xlsx文件
    df.to_excel('详细地址.xlsx', index=False)
    print('详细地址查询结果已保存到详细地址.xlsx')

 

标签:xlsx,df,查询,地址,address,lat,lng,百度
From: https://www.cnblogs.com/OctoberOrange/p/18159400

相关文章

  • Elasticsearch - scoll分批查询
    场景:下载某一个索引中的一亿条数据scoll搜索会在第一次搜索的时候,保存一个当时的视图快照,之后只会基于该旧的视图快照提供数据搜索,如果这个期间数据变更,是不会让用户看到的。每次发送scroll请求,我们还需要指定一个scoll参数,指定一个时间窗口,每次搜索请求只要在这个时间窗口内能......
  • EBS:并发程序查询
    EBS并发程序查询SELECTFCP.APPLICATION_ID,FCP.CONCURRENT_PROGRAM_ID,FCP.CONCURRENT_PROGRAM_NAMEAS"简称",FCP.USER_CONCURRENT_PROGRAM_NAMEAS"程序",FCP.ENABLED_FLAGAS"启用",FCP.DESCRIPTIONAS&......
  • 数据结构_链表_单向循环链表的初始化、插入、删除、修改、查询打印(基于C语言实现)
    版本:2024年4月25日V1.0发布于博客园/***@filename:CircularLinkedList.c*@brief:实现单向循环链表的相关功能*@author:[email protected]*@date:2024/04/25*@version:1.1*@note:*CopyRight(c)2023-2024RISE_A......
  • springboot+mybatisplus+dynicDatasource 从数据库表中查询数据源 动态添加
    1、pom依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.12</version><relativePath/><!--lookuppa......
  • JS之调用百度地图接口进行打卡
    调用百度地图接口进行打卡1.在百度地图开放平台申请AK2.在index.html导入百度地图SDK(此AK值为假)<scripttype="text/javascript"src="https://api.map.baidu.com/api?v=2.0&ak=f029hEOpyCQnXySQsug94D1yUU0Yil"></script>3.新增coordTransform.js//定义一些常量varx_PI......
  • 数据库设计 外键设计 关联表查询 linq efcore
    数据库的设计实体之间的关系图实体EleOrderRowItem.cspublicclassEleOrderRowItem:EleEntity{publicstring?Xxx{get;set;}//外键:一个rowItem属于一个rowpubliclongRowId{get;set;}publicrequiredEleOrderRowRow{get;set;}}......
  • ef core 如何关联查询外键表
    在EFCore中,如果查询查询外键表的内容实体publicclassBlog{publicintBlogId{get;set;}publicstringUrl{get;set;}publicList<Post>Posts{get;set;}//集合导航属性publicList<Comment>Comments{get;set;}//集合导航属性}......
  • blog.admin 查询增加过滤器,添加、删除增加数据审计、统一控制权限操作
    一、查询增加过滤器需求说明:有几张表(医生表、病人表等),有个字段ClinicID都与诊所表主键Id关联。用户登录系统时候,根据所分配的诊所权限,只查看自己诊所的数据。通过查询过滤器,在查询每个表的时候,自动将ClinicID==当前登录用户所属ClinicID,添加上。1、创一个IClinicEntity接口usi......
  • 使用 Visual Studio 调试 .NET 和 ASP.NET Core 源代码 | 学习地址
    使用VisualStudio调试.NET和ASP.NETCore源代码|MicrosoftLearn新建自签名证书|Microsoft学习AuthenticationHttpContextExtensions.ChallengeAsync方法(Microsoft.AspNetCore.Authentication)|MicrosoftLearn.netcore地址:  ASP.NETCore入门|Microsoft......
  • 物理地址和虚拟地址的概念
    目录虚拟地址和物理地址的概念cpu如何访问内存:为什么要用地址映射:物理地址和虚拟地址的概念虚拟地址和物理地址的概念cpu如何访问内存:当CPU需要执行特定的指令或访问特定的数据时,它会将相应的地址放在地址总线上。地址总线上的地址信号传递到内存,指示内存中的特定存储位置。......