<meta name="referrer" content="no-referrer" />
前言
前段时间发了一篇用 qgis 实现GCJ02、WGS84、BD-09 、CGCS-2000坐标系互转的文章,网友@文祥留言说有个 Python 库可以实现坐标系转换。于是,我就找了下这个项目 coordTransform。真的可以,感谢网友留言~
我直接使用 pip install coordTransform,并引用代码报错,然后直接找到了 github 代码原文,放在本地运行是可以跑通的。进过测试发现结果也是正确的。具体代码我就不贴了,后文有地址,感兴趣的可以查看,主要讲一下我基于这个项目做了个 web 转换应用。
坐标转换应用
Web应用是一个基于Streamlit框架开发的坐标转换工具,支持多种地理坐标系之间的相互转换。用户可以通过单点转换和批量转换两种方式,方便快捷地完成坐标系的转换操作。
单点转换
批量转换
输入文件点击转换即可。
代码
Web 应用代码
import streamlit as st
import pandas as pd
from coord import gcj02_to_bd09, bd09_to_gcj02, wgs84_to_gcj02, gcj02_to_wgs84, bd09_to_wgs84, wgs84_to_bd09 # 导入坐标转换函数
# 设置页面标题和布局
st.set_page_config(page_title="坐标转换工具", layout="centered")
st.title("坐标转换工具")
# 使用tabs切换单点转换和批量转换
tab1, tab2 = st.tabs(["单点转换", "批量转换"])
with tab1:
with st.form(key='single_point_form'):
st.subheader("单点转换")
col00, col01 = st.columns(2)
with col00:
source_system = st.selectbox("源坐标系", ["WGS84", "GCJ-02", "BD-09"])
with col01:
target_system_options = ["WGS84", "GCJ-02", "BD-09"]
target_system_options.remove(source_system)
target_system = st.selectbox("目标坐标系", target_system_options)
col1, col2 = st.columns(2)
with col1:
lng = st.number_input("经度", value=108.81001)
with col2:
lat = st.number_input("纬度", value=36.125376)
submit_button = st.form_submit_button(label='转换')
if submit_button:
if source_system == "WGS84" and target_system == "GCJ-02":
result = wgs84_to_gcj02(lng, lat)
elif source_system == "GCJ-02" and target_system == "WGS84":
result = gcj02_to_wgs84(lng, lat)
elif source_system == "GCJ-02" and target_system == "BD-09":
result = gcj02_to_bd09(lng, lat)
elif source_system == "BD-09" and target_system == "GCJ-02":
result = bd09_to_gcj02(lng, lat)
elif source_system == "BD-09" and target_system == "WGS84":
result = bd09_to_wgs84(lng, lat)
elif source_system == "WGS84" and target_system == "BD-09":
result = wgs84_to_bd09(lng, lat)
else:
result = (lng, lat)
st.write(f"转换结果:经度={result[0]}, 纬度={result[1]}")
with tab2:
st.subheader("批量转换")
st.info("请上传包含经度和纬度列的CSV文件。\n文件格式示例:")
st.write(pd.DataFrame({"经度": [120.0, 121.0], "纬度": [30.0, 31.0]}))
uploaded_file = st.file_uploader("上传CSV文件", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
required_columns = {"经度", "纬度"}
if not required_columns.issubset(df.columns):
st.error("CSV文件必须包含“经度”和“纬度”两列。")
else:
with st.form(key='batch_conversion_form'):
col1, col2 = st.columns(2)
with col1:
source_system = st.selectbox("源坐标系(批量)", ["WGS84", "GCJ-02", "BD-09"])
with col2:
target_system_options = ["WGS84", "GCJ-02", "BD-09"]
target_system_options.remove(source_system)
target_system = st.selectbox("目标坐标系(批量)", target_system_options)
batch_convert_button = st.form_submit_button(label='批量转换')
if batch_convert_button:
results = []
for index, row in df.iterrows():
lng, lat = row['经度'], row['纬度']
if source_system == "WGS84" and target_system == "GCJ-02":
result = wgs84_to_gcj02(lng, lat)
elif source_system == "GCJ-02" and target_system == "WGS84":
result = gcj02_to_wgs84(lng, lat)
elif source_system == "GCJ-02" and target_system == "BD-09":
result = gcj02_to_bd09(lng, lat)
elif source_system == "BD-09" and target_system == "GCJ-02":
result = bd09_to_gcj02(lng, lat)
elif source_system == "BD-09" and target_system == "WGS84":
result = bd09_to_wgs84(lng, lat)
elif source_system == "WGS84" and target_system == "BD-09":
result = wgs84_to_bd09(lng, lat)
else:
result = (lng, lat)
results.append(result)
df['转换后经度'] = [r[0] for r in results]
df['转换后纬度'] = [r[1] for r in results]
st.dataframe(df)
# 添加一些样式
st.markdown("""
<style>
body {
font-family: Arial, sans-serif;
}
</style>
""", unsafe_allow_html=True)
注意这里的 from coord import gcj 02_to_bd 09 就是用coordTransform 项目中的代码。
有了这个工具就不用 qgis、奥维地图这些工具转了,相当 nice,最后非常感谢网友的留言。
工具发布
为了让更多人可以用到这个工具,我将其发布到了网上,如果真的能帮助到别人,也是让人非常高兴的。
目前有两个工具:坐标转换和雨型计算,后续如果有新的想法会不断更新,个人精力有限,界面也非常简陋,如果大家有好的想法也可以留言讨论。
- 公开网址: https://tboxes.streamlit.app/
- GitHub 地址: https://github.com/maoyu92/openurl
相关资料
https://github.com/pkufool/coordTransform
BY
纯个人经验,如有帮助,请收藏点赞,如需转载,请注明出处。
微信公众号:环境猫 er
CSDN : 细节处有神明
个人博客: https://maoyu92.github.io/
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。