这是我的课程作业,放在这里了,非常基础,但是geopandas模块不学gis应该不会用到吧。。。还是比较小众
综合运用opencv,geopandas,matplotlib等模块,实现地区查询与显示功能小程序。
用户在输入栏输入要查询的国家英文全称,如Canada、Japan等,按下查询按钮,对应世界地图上该地区闪烁。随后窗口呈现该国家轮廓图,并显示出该国家人口数量和人均GDP。
程序思路:
先创建root窗口,放置图片,按钮,输入栏和文本框等。再创建第二个窗口,显示绘制的地图。当输入完要查询的国家后,点击查询按钮,在对应事件中获取输入文本内容,再根据改内容找到该国家,查询国家属性并显示。
具体程序:
import tkinter
from tkinter import *
import cv2
import geopandas
import matplotlib.pyplot as plt
from PIL import Image as imim
from PIL import ImageTk
#查找方法,找到国家,更新地图窗口,更新root的数据与图片
def search():
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
world["name"] = world.name
print(type(world.name))
s = chazhao.get()
print(s)
if s not in list(world["name"]):
print("没有该国家!")
label3['text'] = "没有该国家!"
return 0
#查找,并更新数据
i = list(world.name).index(s)
print(i)
label1['text'] = str(list(world.pop_est)[i])
print(list(world['gdp_per_cap'])[i])
label2['text'] = str(list(world['gdp_per_cap'])[i])
plt.pause(0.5)
plt.cla()
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))#不从新 #读的话数据空了???为啥
world = world[world.name == s]
global ax
#闪烁
for i in range(0, 6):
plt.cla()
worldtem = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
worldtem.plot(ax=ax)
if i % 2:
world.plot(ax=ax, legend=False, color='white')
else:
world.plot(ax=ax, legend=False, color='black')
plt.show()
plt.pause(0.3)
plt.cla()
#最终地图
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
world.plot(column="gdp_per_cap", ax=ax, legend=False)
#更新root中图片
imgtemp = canny()
label_img['image']=imgtemp
root.mainloop()
return 1
#边缘检测图片转换
def canny():
print("aa")
plt.savefig("haha.jpg")
img = cv2.imread("haha.jpg", 0)
blurred = cv2.GaussianBlur(img, (11, 11), 0)
gaussImg = cv2.Canny(blurred, 10, 70)
cv2.imwrite("Img.jpg", gaussImg)
img_open = imim.open("Img.jpg")
img_png = ImageTk.PhotoImage(img_open)
print("bb")
return img_png
#打开窗口交互
plt.ion()
plt.show()
#GUI设计
root = Tk()
root.geometry("700x700")
Label(root, text="输入查询地区:").grid(row=1, column=0, sticky=W)
chazhao = Entry(root)
chazhao.grid(row=2, column=0, sticky=W)
btn = Button(root, text="查询", command=search)
btn.grid(row=3, column=0, sticky=W)
Label(root, text="该地区人口:").grid(row=4, column=0, sticky=W)
label1 = Label(root, text="null")
label1.grid(row=5, column=0, sticky=W)
Label(root, text="该地区人均gdp:").grid(row=6, column=0, sticky=W)
label2 = Label(root, text="null")
label2.grid(row=7, column=0, sticky=W)
label3 = Label(root, text="")
label3.grid(row=7, column=0, sticky=W)
#首次读取并显示地图
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
fig, ax = plt.subplots(1, 1)
world.plot(column="gdp_per_cap", ax=ax, legend=True,
legend_kwds={"label": "Population by Contry", "orientation": "horizontal", 'pad': 0.1})
#第一次将世界地图进行边界检测
imgtemp = canny()
label_img = tkinter.Label(root, image=imgtemp)
label_img.grid(row=0, column=0, sticky=E)
root.mainloop()
效果展示:
注意!!!!!!!:
本程序中,中国领土范围存在严重错误!!!!非本人观点,该程地图数据引用为geopandas模块中数据,存在严重错误!!!!!!!
可能的错误:pad参数在我的版本中必须直接赋值数字而不是“%5”这样的字符串,如果报错删掉’pad‘:0.1即可