如何在python中从shapefile绘制虚线? [英] How to plot dotted lines from a shapefile in python?

查看:562
本文介绍了如何在python中从shapefile绘制虚线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何在Python中绘制来自shapefile的虚线.看来readshapefile()没有任何线型供我设置.在下面,我有一个工作代码,我在其中获取一个shapefile并对其进行绘制,但它仅绘制一条实线.有什么想法可以设定正确的方向吗?谢谢!

I am not sure on how to plot a dotted line from a shapefile in Python. It appears that readshapefile() does not have any linestyle for me to set. Below I have a working code where I take a shapefile and plot it, but it only plots a solid line. Any ideas to set me in the right direction? Thanks!

可以在以下位置找到shapefile: http://www.natice.noaa. gov/products/daily_products.html ,其中开始日期"为2月15日,结束日期为2月17日,日期类型"为Ice Edge.它应该是第一个链接.

The shapefile can be found here: http://www.natice.noaa.gov/products/daily_products.html, where the Start Date is Feb 15th, end date is Feb 17th, and the Date Types is Ice Edge. It should be the first link.

#!/awips2/python/bin/python

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

map = Basemap(llcrnrlon=-84.37,llcrnrlat=42.11,urcrnrlon=-20.93,urcrnrlat=66.48,
     resolution='i', projection='tmerc', lat_0 = 55., lon_0 = -50.)

map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='#ddaa66',lake_color='aqua')
map.drawcoastlines(zorder = 3)

map.readshapefile('nic_autoc2018046n_pl_a', 'IceEdge', zorder = 2, color = 'blue')

plt.show()

推荐答案

来自底图文档:

包含形状文件信息的元组(num_shapes,type,min,max)为 回来. num_shapes是形状的数量,type是类型代码 (shapelib模块中定义的SHPT *常量之一,请参见 http://shapelib.maptools.org/shp_api.html ),最小和最大为 具有顶点的最小值和最大值的4元素列表. 如果drawbounds = True,则matplotlib.patches.LineCollection对象为 附加到元组.

A tuple (num_shapes, type, min, max) containing shape file info is returned. num_shapes is the number of shapes, type is the type code (one of the SHPT* constants defined in the shapelib module, see http://shapelib.maptools.org/shp_api.html) and min and max are 4-element lists with the minimum and maximum values of the vertices. If drawbounds=True a matplotlib.patches.LineCollection object is appended to the tuple.

drawbounds默认为True,因此您要做的就是收集readshapefile的返回值并更改返回的LineCollection对象的linestyle,这可以通过LineCollection.set_linestyle()完成. .因此,原则上,您可以使用以下方式更改绘制的图形文件的线条样式:

drawbounds is True by default, so all you have to do is collect the return value of readshapefile and alter the linestyle of the returned LineCollection object, which can be done with LineCollection.set_linestyle(). So in principle you can change the linestyle of your plotted shape file with something like this:

result = m.readshapefile('shapefiles/nic_autoc2018046n_pl_a', 'IceEdge', zorder = 10, color = 'blue')#, drawbounds = False)
col = result[-1]
col.set_linestyle('dotted')
plt.show()

但是,您的shapefile包含5429个不同长度的独立线段,因此matplotlib似乎无法处理大量的非连续线.至少在我的机器上,绘图未在一小时内完成,所以我中断了该过程.我对您的文件进行了一些处理,似乎很多行都不必要地分成了几部分(我猜这是因为冰盖的轮廓是在瓷砖上确定的,然后再拼凑在一起,但只有提供商会真正知道).也许这有助于将相邻的片段拼凑在一起,但是我不确定.

However, your shapefile contains 5429 separate line segments of different length and somehow matplotlib does not seem to be able to deal with this large amount of non-continuous lines. At least on my machine the plotting did not finish within one hour, so I interrupted the process. I played a bit with your file and it seems like many of the lines are broken into segments unnecessarily (I'm guessing this is because the ice sheet outlines are somehow determined on tiles and then pieced together afterwards, but only the providers will really know). Maybe it would help to piece together adjacent pieces, but I'm not sure.

我还想知道,如果有很多急剧的弯曲,结果是否用虚线看起来还是那么好.下面显示的图片仅使用以下代码绘制100条最长线段(省去了drawcoastlines且具有较粗的线):

I was also wondering whether the result would even look that great with a dotted line, because there are so many sharp bends. Below I show a picture where I only plot the 100 longest line segments (leaving out drawcoastlines and with thicker lines) using this code:

import numpy as np
result = m.readshapefile('shapefiles/nic_autoc2018046n_pl_a', 'IceEdge', zorder = 10, color = 'blue')#, drawbounds = False)

col = result[-1]
segments = col.get_segments()
seglens =  [len(seg) for seg in col.get_segments()]

segments = np.array(segments)
seglens  = np.array(seglens)

idx = np.argsort(seglens)
seglens  = seglens[idx]
segments = segments[idx]

col.remove()

new_col = LineCollection(segments[-100:],linewidths = 2, linestyles='dotted', colors='b')
ax.add_collection(new_col)

plt.show()

结果如下:

这篇关于如何在python中从shapefile绘制虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆