企查查导出excel数据添加WGS84坐标系

这段代码用于从Excel表格中读取地址信息,通过百度地图API获取经纬度,并将坐标转换为WGS84标准,同时在Excel表格中新增经度和纬度列并保存结果。涉及到的技术包括Python的openpyxl、urllib、hashlib等库,以及地理坐标转换算法。
摘要由CSDN通过智能技术生成

使用前准备

  1. 删除表格的第一行“企查查”图片,在最后两列添加新的字段(经度和纬度),然后另存为xlsx格式。
  2. 代码可直接赋值粘贴使用,只需修改 可能要替换的位置
  3. 直接对原excel进行操作——谨慎操作.
from urllib.request import urlopen, quote
import time
from openpyxl.reader.excel import load_workbook
import re
from urllib import parse
import hashlib
from urllib.request import urlopen, quote
import json
#############################################打开excel
# excel文件绝对路径
file_home = "表格路径"    ###替换##
sheet_name = 'sheet名称'  ###替换##
ak = '你的ak'			  ###替换##
jingdu = 'z'        #经度对应的excel的列号(如a1表示第一列,第一行的单元格).    ###替换##
weidu = 'aa'      ###替换## 同上
dizhi = 'w'     ###替换## 中文地址对应的列号,如,江苏省南京市鼓楼区....

wb = load_workbook(filename=file_home)  # 打开excel文件
#sheet_ranges = wb['市北']     #打开sheet
ws = wb[sheet_name]  # 根据Sheet1这个sheet名字来获取该sheet
row=ws.max_row     #获取行数

def get_urt(address):
 
    # 以get请求为例http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=你的ak
    queryStr = '/geocoder/v2/?address=%s&output=json&ak=' + ak 
    queryStr =  queryStr % address
    # 对queryStr进行转码,safe内的保留字符不转换
    encodedStr = parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
    # 在最后直接追加上yoursk
    rawStr = encodedStr + ak
    #计算sn
    sn = (hashlib.md5(parse.quote_plus(rawStr).encode("utf8")).hexdigest()) 
    #由于URL里面含有中文,所以需要用parse.quote进行处理,然后返回最终可调用的url
    url = parse.quote("http://api.map.baidu.com"+queryStr+"&sn="+sn, safe="/:=&?#+!$,;'@()*[]")  
    return url

import math

x_pi = float(3.14159265358979324 * 3000.0 / 180.0)
# //pai
pi = float(3.1415926535897932384626)
# //离心率
ee = float(0.00669342162296594323)
# //长半轴
a = float(6378245.0)

# //百度转国测局
def bd09togcj02(bd_lon, bd_lat):
    x = (bd_lon - 0.0065)
    y = (bd_lat - 0.006)
    z = (math.sqrt(x * x + y * y)) - (0.00002 * math.sin(y * x_pi))
    theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi)
    gg_lng = z * math.cos(theta)
    gg_lat = z * math.sin(theta)

    return gg_lng, gg_lat

# //国测局转百度
def gcj02tobd09(lng, lat):
    z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi)
    theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi)
    bd_lng = z * math.cos(theta) + 0.0065
    bd_lat = z * math.sin(theta) + 0.006
    return bd_lng, bd_lat

# //国测局转84
def gcj02towgs84(lng, lat):
    dlat = transformlat(lng - 105.0, lat - 35.0)
    dlng = transformlng(lng - 105.0, lat - 35.0)
    radlat = lat / 180.0 * pi
    magic = math.sin(radlat)
    magic = 1 - ee * magic * magic
    sqrtmagic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
    dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
    mglat = lat + dlat
    mglng = lng + dlng
    return [lng * 2 - mglng, lat * 2 - mglat]

# //经度转换
def transformlat(lng, lat):
    ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * math.sqrt(abs(lng))
    ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lat * pi) + 40.0 * math.sin(lat / 3.0 * pi)) * 2.0 / 3.0
    ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 * math.sin(lat * pi / 30.0)) * 2.0 / 3.0
    return ret


# //纬度转换
def transformlng(lng, lat):
    ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * math.sqrt(abs(lng))
    ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lng * pi) + 40.0 * math.sin(lng / 3.0 * pi)) * 2.0 / 3.0
    ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 * math.sin(lng / 30.0 * pi)) * 2.0 / 3.0
    return ret

def getWgs84xy(x, y):
    # //先转 国测局坐标
    doubles_gcj = bd09togcj02(x, y)
    # //(x 117.   y 36. )
    # //国测局坐标转wgs84
    doubles_wgs84 = gcj02towgs84(doubles_gcj[0], doubles_gcj[1])
    # //返回 纠偏后 坐标
    return doubles_wgs84

for i in range(2,row+1):
    address = ws[dizhi+str(i)].value    #获取表中的字符串地址
    print(dizhi+str(i))
    url = get_urt(address)
    req = urlopen(url)
    res = req.read().decode() 
    x=re.findall(r'lng":(.*?),"lat"',res)
    y=re.findall(r'lat":(.*?)},',res)
    print(x,y)
    zuobiao = getWgs84xy(float(x[0]),float(y[0]))
    
    ws[jingdu+str(i)],ws[weidu+str(i)] = zuobiao[0], zuobiao[1]  # 修改单元格的值
    print("第{0}个坐标获取成功".format(i))
    wb.save(file_home)  # 保存修改后的excel
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
温州2000坐标系转换为WGS84坐标系可以使用以下方法: 1. 使用Proj4库进行转换: ```java import org.osgeo.proj4j.CoordinateReferenceSystem; import org.osgeo.proj4j.CoordinateTransform; import org.osgeo.proj4j.CRSFactory; import org.osgeo.proj4j.ProjCoordinate; public class CoordinateConversion { public static void main(String[] args) { // 定义温州2000坐标系的EPSG代码 String wenzhou2000EPSG = "EPSG:4490"; // 定义WGS84坐标系的EPSG代码 String wgs84EPSG = "EPSG:4326"; // 创建坐标系工厂 CRSFactory crsFactory = new CRSFactory(); // 根据EPSG代码获取坐标系对象 CoordinateReferenceSystem wenzhou2000CRS = crsFactory.createFromName(wenzhou2000EPSG); CoordinateReferenceSystem wgs84CRS = crsFactory.createFromName(wgs84EPSG); // 创建坐标转换对象 CoordinateTransform transform = new CoordinateTransform(wenzhou2000CRS, wgs84CRS); // 定义温州2000坐标系中的坐标点 ProjCoordinate wenzhou2000Point = new ProjCoordinate(120.7059, 27.9944); // 创建用于存储转换结果的坐标点对象 ProjCoordinate wgs84Point = new ProjCoordinate(); // 进行坐标转换 transform.transform(wenzhou2000Point, wgs84Point); // 输出转换后的WGS84坐标系中的坐标点 System.out.println("WGS84坐标系中的坐标点:"); System.out.println("经度:" + wgs84Point.x); System.out.println("纬度:" + wgs84Point.y); } } ``` 2. 使用GeoTools库进行转换: ```java import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; public class CoordinateConversion { public static void main(String[] args) throws Exception { // 定义温州2000坐标系的EPSG代码 String wenzhou2000EPSG = "EPSG:4490"; // 定义WGS84坐标系的EPSG代码 String wgs84EPSG = "EPSG:4326"; // 根据EPSG代码获取坐标系对象 CoordinateReferenceSystem wenzhou2000CRS = CRS.decode(wenzhou2000EPSG); CoordinateReferenceSystem wgs84CRS = CRS.decode(wgs84EPSG); // 创建坐标转换对象 MathTransform transform = CRS.findMathTransform(wenzhou2000CRS, wgs84CRS); // 定义温州2000坐标系中的坐标点 Coordinate wenzhou2000Point = new Coordinate(120.7059, 27.9944); // 创建几何对象工厂 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); // 创建温州2000坐标系中的点几何对象 Geometry wenzhou2000Geometry = geometryFactory.createPoint(wenzhou2000Point); // 进行坐标转换 Geometry wgs84Geometry = JTS.transform(wenzhou2000Geometry, transform); // 获取转换后的WGS84坐标系中的坐标点 Coordinate wgs84Point = wgs84Geometry.getCoordinate(); // 输出转换后的WGS84坐标系中的坐标点 System.out.println("WGS84坐标系中的坐标点:"); System.out.println("经度:" + wgs84Point.x); System.out.println("纬度:" + wgs84Point.y); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小酥muse

不想填问卷,打赏1元获得提取码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值