使用python将一堆点投影到地图上的各种方法

将一堆点投影到地图上

文章里用到了几个地图可视化包

Basemap的官方文档在这里→Basemap

Folium的官方文档在这里→Folium

数据形式是这样的:
(实际用到的是其中的经度、纬度这两列)
在这里插入图片描述

数据介绍

数据选自义乌市卡口的点位数据(包含经纬度),目的是将卡口摄像头的经纬度投影到地图上,看一下大致的分布。

1.Folium包

直接上代码,比较好懂

import folium
import xlrd
import pandas as pd
locations = pd.read_excel(r'1_点位.xlsx')
locations = locations.dropna()
locations = locations[(locations['LNG']>=119) & (locations['LNG']<=130)]
locations = locations[(locations['LAT']>=29) & (locations['LAT']<=30)]
a = []
a.append(locations['LNG'].min())
a.append(locations['LNG'].max())
a.append(locations['LAT'].min())
a.append(locations['LAT'].max())
places_on_boundary = [('1', a[2], a[0]),
('2', a[2], a[1]), ('3',a[3], a[1]),
('4', a[3], a[0]), ('5',a[2], a[0]),
('6', a[2], a[1]), ('7',a[3], a[1])]
# get the coordinates for these places
lats = [x[1] for x in places_on_boundary]
lons = [x[2] for x in places_on_boundary]
coordinates = zip(lats, lons)
# print(coordinates)
m = folium.Map(location=[(a[2]+a[3])/2, (a[0]+a[1])/2], zoom_start=13)
print(m)
boundary = folium.PolyLine(locations=coordinates,weight=1,color = 'red')
m.add_child(boundary)
#for point in coordinates:
 #   print(point)
  #  folium.Marker(point).add_to(m)

workbook = xlrd.open_workbook('点位.xlsx')
sheet = workbook.sheet_by_index(0)
cameralist = []
for row in range(1, sheet.nrows):
    values = sheet.row_values(row)
    cameralist.append((values[4], values[3]))
for point in cameralist:
    folium.CircleMarker(location=[point[0], point[1]],radius=0.3,weight=3).add_to(m)
m.save('点位.html')


最后会得到这样的图,是可交互的。这个图的优点在于可以在html文件中查看地图,进行放大缩小等操作。
在这里插入图片描述

2.Basemap包

Basemap包是一个可以在地图上作图的可视化工具。可以比较好地和matplotlib相结合,但是它不能精确到路网,适合在范围非常大的区域(世界地图或国家地图或各个洲)上画图。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig = plt.figure(1,(10,8),dpi = 100)
m = Basemap(projection='lcc', resolution=None,llcrnrlon =119.848322 , llcrnrlat=29.048459,  urcrnrlon= 120.211491, urcrnrlat=29.509874,lat_1 = 29.048459,lat_0 =29.509874,lon_0 = 120.211491 ) 
A = []
for i in range(len(locations['LNG'])):
    A.append(m(locations['LNG'].iloc[i],locations['LAT'].iloc[i]))
X = []
Y = []
for i in range(len(A)):
    X.append(A[i][0])
    Y.append(A[i][1])
m.etopo(scale=0.5, alpha=0.5)
m.shadedrelief()
m.scatter(X,Y,linewidth =1)

这个画出来的图嘛,emmm
在这里插入图片描述

3.一个优秀的地图可视化包

这里使用了一个大佬自制的plot_map包,可为专题图添加背景底图,大佬的github项目→https://nbviewer.jupyter.org/github/ni1o1/pygeo-tutorial/


import pandas as pd
#读取数据
locations = pd.read_excel(r'1_点位.xlsx')
locations = locations.dropna()
locations = locations[(locations['LNG']>=119) & (locations['LNG']<=130)]
locations = locations[(locations['LAT']>=29) & (locations['LAT']<=30)]
bounds = [119, 29, 130, 30]
a = []
a.append(locations['LNG'].min())
a.append(locations['LNG'].max())
a.append(locations['LAT'].min())
a.append(locations['LAT'].max())
#经纬度小数点保留三位小数
data2 = locations[['LNG','LAT']].round(3).copy()
#集计每个小范围内数据量
data2['count'] = 1
data2 = data2.groupby(['LNG','LAT'])['count'].count().reset_index()
#排序数据,让数据量小的放上面先画,数据大的放下面最后画
data2.sort_values(by = 'count')
data2.head(5)
import matplotlib as mpl
import matplotlib.pyplot as plt
import plot_map
import seaborn as sns
import imp
imp.reload(plot_map)
fig     = plt.figure(1,(8,8),dpi = 100)    
ax      = plt.subplot(111)
plt.sca(ax)
fig.tight_layout(rect = (0.05,0.1,1,0.9))
#背景
plot_map.plot_map(plt,bounds,zoom = 12,style = 4)
#colorbar
pallete_name = "autumn_r"
colors = sns.color_palette(pallete_name, 3)
colors.reverse()
cmap = mpl.colors.LinearSegmentedColormap.from_list(pallete_name, colors)
norm = mpl.colors.Normalize(vmin=0, vmax=vmax)
#plot scatters
plt.scatter(locations['LNG'],locations['LAT'],c = "blue" ,s= 1)
plt.axis('off')
plt.xlim(bounds[0],bounds[2])
plt.ylim(bounds[1],bounds[3])
plt.show()


嗯,这个图就非常优秀了
注意:以上所有图都仅将点投影在了地图上,没有经过集计处理,下一篇将会写到怎么对点集计,从而画出热力图

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Python中,可以使用以下代码来实现将插值投影到最近的三角形边界上的边界处理方法: ```python import numpy as np def project_to_boundary(triangle, point): vertices = triangle[:2] # 提取三角形的前两个顶坐标 edges = np.vstack((vertices, vertices[0])) # 构造三条边的起和终坐标 # 计算插值到三条边的垂直距离 distances = np.abs(np.cross(edges[1:] - edges[:-1], edges[:-1] - point)) / np.linalg.norm(edges[1:] - edges[:-1], axis=1) # 找到离插值最近的边 nearest_edge_index = np.argmin(distances) nearest_edge = edges[nearest_edge_index:nearest_edge_index + 2] # 计算插值在最近边上的投影 t = np.dot(point - nearest_edge[0], nearest_edge[1] - nearest_edge[0]) / np.dot(nearest_edge[1] - nearest_edge[0], nearest_edge[1] - nearest_edge[0]) projected_point = nearest_edge[0] + t * (nearest_edge[1] - nearest_edge[0]) return projected_point # 示例使用 triangle = np.array([[0, 0], [1, 0], [0, 1]]) # 三角形的顶坐标 point = np.array([0.5, -0.5]) # 插值的坐标 projected_point = project_to_boundary(triangle, point) print(projected_point) ``` 上述代码中,`project_to_boundary` 函数接受三角形的顶坐标和插值坐标作为参数,返回投影到最近边界上的的坐标。其中,使用向量操作和线性代数计算实现了向量的投影计算。 在示例中,三角形的顶坐标为 `[[0, 0], [1, 0], [0, 1]]`,插值的坐标为 `[0.5, -0.5]`。运行代码后,将得到投影的坐标。 请注意,上述代码仅提供了一种基本的实现方法,具体的应用场景和需求可能需要进行适当的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值