这是我的课程作业,放在这里了,非常基础,但是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 = ad_file(_path("naturalearth_lowres"))world['gdp_per_cap'] = world.gdp_md_est / world.pop_estworld["name"] = world.nameprint(type(world.name))s = ()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 = ad_file(_path("naturalearth_lowres"))#不从新 #读的话数据空了???为啥world = world[world.name == s]global ax#闪烁for i in range(0, 6):plt.cla()worldtem = ad_file(_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_estworld.plot(column="gdp_per_cap", ax=ax, legend=False)#更新root中图片imgtemp = canny()label_img['image']=imgtemproot.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()
ry("700x700")
Label(root, text="输入查询地区:").grid(row=1, column=0, sticky=W)
chazhao = Entry(root)
id(row=2, column=0, sticky=W)
btn = Button(root, text="查询", command=search)
id(row=3, column=0, sticky=W)
Label(root, text="该地区人口:").grid(row=4, column=0, sticky=W)
label1 = Label(root, text="null")
id(row=5, column=0, sticky=W)
Label(root, text="该地区人均gdp:").grid(row=6, column=0, sticky=W)
label2 = Label(root, text="null")
id(row=7, column=0, sticky=W)
label3 = Label(root, text="")
id(row=7, column=0, sticky=W)#首次读取并显示地图
world = ad_file(_path("naturalearth_lowres"))
world['gdp_per_cap'] = world.gdp_md_est / world.pop_estfig, 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)
id(row=0, column=0, sticky=E)root.mainloop()
本程序中,中国领土范围存在严重错误!!!!非本人观点,该程地图数据引用为geopandas模块中数据,存在严重错误!!!!!!!
可能的错误:pad参数在我的版本中必须直接赋值数字而不是“%5”这样的字符串,如果报错删掉’pad‘:0.1即可
本文发布于:2024-01-28 09:57:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064070406605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |