- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一组定义区域边界的经纬度点。我想根据这些点创建一个多边形并在 map 上绘制多边形并填充它。目前,我的多边形似乎由许多连接所有点的补丁组成,但点的顺序不正确,当我尝试填充多边形时,我得到一个看起来很奇怪的区域(见附件)。
我根据多边形的中心对经度-纬度点(mypolyXY 数组)进行排序,但我猜测这是不正确的:
cent=(np.sum([p[0] for p in mypolyXY])/len(mypolyXY),np.sum([p[1] for p in mypolyXY])/len(mypolyXY))
# sort by polar angle
mypolyXY.sort(key=lambda p: math.atan2(p[1]-cent[1],p[0]-cent[0]))
我绘制点位置(黑色圆圈)和我的多边形(彩色 block )使用
scatter([p[0] for p in mypolyXY],[p[1] for p in mypolyXY],2)
p = Polygon(mypolyXY,facecolor=colors,edgecolor='none')
ax.add_artist(p)
我的问题是:如何根据我的经度-纬度点数组来关闭多边形?
更新:我对如何绘制多边形进行了更多测试。我删除了排序例程,只是按照它们在文件中出现的顺序使用数据。这似乎改善了结果,但正如@tcaswell 所提到的,多边形形状仍然削弱了自身(见新图)。我希望有一个路径/多边形例程可以解决我的问题,并在某种程度上合并多边形边界内的所有形状或路径。非常欢迎提出建议。
更新 2:
我现在有一个基于@Rutger Kassies 和 Roland Smith 建议的脚本的工作版本。我最终使用 org 阅读了 Shapefile,效果相对较好。它适用于标准 lmes_64.shp 文件,但当我使用更详细的 LME 文件时,其中每个 LME 可能由多个多边形组成,此脚本崩溃了。我将不得不找到一种方法来合并具有相同 LME 名称的各种多边形以使其工作。我附上了我最终得到的脚本,以防有人看到它。我非常感谢有关如何改进此脚本或使其更通用的评论。该脚本在我从 netcdf 文件读取的多边形区域内创建多边形并提取数据。输入文件的网格是-180到180和-90到90。
import numpy as np
import math
from pylab import *
import matplotlib.patches as patches
import string, os, sys
import datetime, types
from netCDF4 import Dataset
import matplotlib.nxutils as nx
from mpl_toolkits.basemap import Basemap
import ogr
import matplotlib.path as mpath
import matplotlib.patches as patches
def getLMEpolygon(coordinatefile,mymap,index,first):
ds = ogr.Open(coordinatefile)
lyr = ds.GetLayer(0)
numberOfPolygons=lyr.GetFeatureCount()
if first is False:
ft = lyr.GetFeature(index)
print "Found polygon:", ft.items()['LME_NAME']
geom = ft.GetGeometryRef()
codes = []
all_x = []
all_y = []
all_XY= []
if (geom.GetGeometryType() == ogr.wkbPolygon):
for i in range(geom.GetGeometryCount()):
r = geom.GetGeometryRef(i)
x = [r.GetX(j) for j in range(r.GetPointCount())]
y = [r.GetY(j) for j in range(r.GetPointCount())]
codes += [mpath.Path.MOVETO] + (len(x)-1)*[mpath.Path.LINETO]
all_x += x
all_y += y
all_XY +=mymap(x,y)
if len(all_XY)==0:
all_XY=None
mypoly=None
else:
mypoly=np.empty((len(all_XY[:][0]),2))
mypoly[:,0]=all_XY[:][0]
mypoly[:,1]=all_XY[:][3]
else:
print "Will extract data for %s polygons"%(numberOfPolygons)
mypoly=None
first=False
return mypoly, first, numberOfPolygons
def openCMIP5file(CMIP5name,myvar,mymap):
if os.path.exists(CMIP5name):
myfile=Dataset(CMIP5name)
print "Opened CMIP5 file: %s"%(CMIP5name)
else:
print "Could not find CMIP5 input file %s : abort"%(CMIP5name)
sys.exit()
mydata=np.squeeze(myfile.variables[myvar][-1,:,:]) - 273.15
lonCMIP5=np.squeeze(myfile.variables["lon"][:])
latCMIP5=np.squeeze(myfile.variables["lat"][:])
lons,lats=np.meshgrid(lonCMIP5,latCMIP5)
lons=lons.flatten()
lats=lats.flatten()
mygrid=np.empty((len(lats),2))
mymapgrid=np.empty((len(lats),2))
for i in xrange(len(lats)):
mygrid[i,0]=lons[i]
mygrid[i,1]=lats[i]
X,Y=mymap(lons[i],lats[i])
mymapgrid[i,0]=X
mymapgrid[i,1]=Y
return mydata, mygrid, mymapgrid
def drawMap(NUM_COLORS):
ax = plt.subplot(111)
cm = plt.get_cmap('RdBu')
ax.set_color_cycle([cm(1.*j/NUM_COLORS) for j in range(NUM_COLORS)])
mymap = Basemap(resolution='l',projection='robin',lon_0=0)
mymap.drawcountries()
mymap.drawcoastlines()
mymap.fillcontinents(color='grey',lake_color='white')
mymap.drawparallels(np.arange(-90.,120.,30.))
mymap.drawmeridians(np.arange(0.,360.,60.))
mymap.drawmapboundary(fill_color='white')
return ax, mymap, cm
"""Edit the correct names below:"""
LMEcoordinatefile='ShapefileBoundaries/lmes_64.shp'
CMIP5file='tos_Omon_CCSM4_rcp85_r1i1p1_200601-210012_regrid.nc'
mydebug=False
doPoints=False
first=True
"""initialize the map:"""
mymap=None
mypolyXY, first, numberOfPolygons = getLMEpolygon(LMEcoordinatefile, mymap, 0,first)
NUM_COLORS=numberOfPolygons
ax, mymap, cm = drawMap(NUM_COLORS)
"""Get the CMIP5 data together with the grid"""
SST,mygrid, mymapgrid = openCMIP5file(CMIP5file,"tos",mymap)
"""For each LME of interest create a polygon of coordinates defining the boundaries"""
for counter in xrange(numberOfPolygons-1):
mypolyXY,first,numberOfPolygons = getLMEpolygon(LMEcoordinatefile, mymap,counter,first)
if mypolyXY != None:
"""Find the indices inside the grid that are within the polygon"""
insideBoolean = plt.mlab.inside_poly(np.c_[mymapgrid[:,0],mymapgrid[:,1]],np.c_[mypolyXY[:,0],mypolyXY[:,1]])
SST=SST.flatten()
SST=np.ma.masked_where(SST>50,SST)
mymapgrid=np.c_[mymapgrid[:,0],mymapgrid[:,1]]
myaverageSST=np.mean(SST[insideBoolean])
mycolor=cm(myaverageSST/SST.max())
scaled_z = (myaverageSST - SST.min()) / SST.ptp()
colors = plt.cm.coolwarm(scaled_z)
scatter([p[0] for p in mypolyXY],[p[1] for p in mypolyXY],2)
p = Polygon(mypolyXY,facecolor=colors,edgecolor='none')
ax.add_artist(p)
if doPoints is True:
for point in xrange(len(insideBoolean)):
pointX=mymapgrid[insideBoolean[point],0]
pointY=mymapgrid[insideBoolean[point],1]
ax.scatter(pointX,pointY,8,color=colors)
ax.hold(True)
if doPoints is True:
colorbar()
print "Extracted average values for %s LMEs"%(numberOfPolygons)
plt.savefig('LMEs.png',dpi=300)
plt.show()
附上最终图片。感谢所有帮助。
干杯,特隆
最佳答案
只有一个点数组是不够的。您需要知道点的顺序。通常,多边形的点按顺序给出。所以你从第一点到第二点画一条线,然后从第二点到第三点等等。
如果您的列表没有按顺序排列,您需要额外的信息才能制作一个按顺序排列的列表。
shapefile(参见 documentation)包含一个形状列表,例如空形状、点、折线、多边形,其变体还包含 Z 和 M(测量)坐标。所以仅仅倾倒点数是不行的。您必须将它们分成不同的形状并渲染您感兴趣的形状。在这种情况下可能是 PolyLine 或 Polygon。有关这些形状的数据格式,请参见上面的链接。请记住,文件的某些部分是大端,而其他部分是小端。真是一团糟。
我建议使用 struct模块来解析二进制 .shp
文件,因为再次根据文档,单个多边形的点 是有序的,它们形成一个闭合链(最后一个点与第一个相同)。
您可以尝试使用当前坐标列表的另一件事是从一个点开始,然后在列表中进一步查找相同的点。这些相同点之间的一切都应该是一个多边形。这可能不是万无一失的,但看看它能让你走多远。
关于python - 从边界点创建闭合多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326068/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!