需求
如何在地图上动态展示水位随时间变化,并且用不同颜色表示该点的水位量,今天介绍如何通过Python来实现这个目标。
去看原文
数据
数据主要是某城市的每天的水位量,包含4个字段:日期、水位量、经度、纬度。(所有数据都经过处理,非原始数据)。
实现
#Python
#coding=utf-8
import shapefile
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.cm import get_cmap
import numpy as np
import pandas as pd
import math
import sys
import PlotUtil
# 画底图
def initFunc(handle):
# -- input --
sf = shapefile.Reader("yc_shp/HBcities")
shapes = sf.shapes()
Nshp = len(shapes)
# 获得颜色条
cm = get_cmap('Dark2')
cccol = cm(1.* np.arange(Nshp) / Nshp)
# -- plot --
for nshp in xrange(Nshp):
ptchs = []
pts = np.array(shapes[nshp].points) # 该矢量对象所有的点集合
prt = shapes[nshp].parts # 返回这个组合的多个部分的第一个点对应的index
par = list(prt) + [pts.shape[0]]
for pij in xrange(len(prt)):
ptchs.append(Polygon(pts[par[pij]:par[pij+1]])) # 第pij个部分
handle.add_collection(PatchCollection(ptchs, facecolor = cccol[nshp,:], edgecolor = 'k', linewidths=.1))
handle.autoscale_view()
def main():
water = pd.read_excel("waterlevel.xlsx")
water.sort_values(by = ["date"], inplace = True) # 按时间排序
water = water.round({'water': 2})
max_water_level = math.ceil(water["water"].max()) + 1
min_water_level = math.floor(water["water"].min()) - 1
datas = []
for index, row in water.iterrows():
datas.append([row["lat"], row["lnt"], row["water"]])
# datas.append([4582867, 3277697, row["water"]])
datas = np.array(datas)
plotF = PlotUtil.Plot(initFunc, datas)
plotF.setColorBox([min_water_level, max_water_level])
plotF.showGif("test.gif")
if __name__ == '__main__':
main()
结果
原文有源码,更多内容,请关注地学数据处理分析。