Javascript 将Matplotlib等高线图导出为geojson多边形

Javascript 将Matplotlib等高线图导出为geojson多边形,javascript,python,json,d3.js,geojson,Javascript,Python,Json,D3.js,Geojson,关于我使用的数据的一些背景知识。我正在从NCEP NOMADS数据服务器(特别是从00p25度GFS)绘制2米的温度等值线 我发现了GeoJSonTour python3模块,该模块工作得非常好,但是它创建了一个带有线条特征集合的geojson文件。我对轮廓之间的区域进行着色感兴趣…因此,似乎我需要一组多边形来完成这项工作…geojsoncontour不支持这一点 我曾尝试使用geojson python包从matplotlib等高线图中的顶点数据创建geojson文件……但事实证明,这比我想象

关于我使用的数据的一些背景知识。我正在从NCEP NOMADS数据服务器(特别是从00p25度GFS)绘制2米的温度等值线

我发现了GeoJSonTour python3模块,该模块工作得非常好,但是它创建了一个带有线条特征集合的geojson文件。我对轮廓之间的区域进行着色感兴趣…因此,似乎我需要一组多边形来完成这项工作…geojsoncontour不支持这一点

我曾尝试使用geojson python包从matplotlib等高线图中的顶点数据创建geojson文件……但事实证明,这比我想象的要困难得多。有些区域的数据中有一个极小值……并且几个多边形彼此重叠()。我不知道如何检查一个多边形中是否有多边形,这样我就可以从另一个多边形中创建一个带有“孔”的多边形,以便可以适当地设置数据的样式

正如我所说,这证明是相当复杂的。有人对如何从matplotlib等高线数据创建多边形特征集合有什么建议吗?也许有一种不同的方法可以从我的数据文件中创建阴影,可以直接在浏览器中完成…也许可以使用画布(我也找不到太多帮助)

谢谢

    #Code attempting to create polygons from contour data
    #Code is only complete for the case where there are minima in the data 
    #(eg. where the contour is closed)
    def contour_to_geojson(contour, geojson_filepath, contour_levels):
        collections = contour.collections
        contour_index = 0
        assert len(contour_levels) == len(collections)
        poly_features = []
        for collection in collections:
            paths = collection.get_paths()
            for path in paths:
                vert = path.vertices
                #Check to see if the path is already closed
                #There are issues with comparing floats, so set up a cutoff
                #threshold instead of directly comparing values
                bounds_lon = abs(vert[0][0]-vert[len(vert)-1][0])
                bounds_lat = abs(vert[0][1]-vert[len(vert)-1][1])
                #print(bounds_lon)
                #0.000001 is essentially 0
                coordinates = []
                if bounds_lon < 0.00001 and bounds_lat < 0.00001:
                    compliant_vert = []
                    for v in vert:
                        lat = np.asscalar(vert[v][1])
                        lon = np.asscalar(vert[v][0])
                        compliant_vert.append((lon, lat))
                    coordinates.append(compliant_vert)
                else:
                    #Still need to write code for this section
                    #Will create polygon coords using contours on either side as bounds
                if contour_index < len(contour.collections):
                    poly = Polygon(coordinates)
                    properties = {
                    "title": "%.2f" % contour_levels[contour_index]
                    }
                    poly_features.append(Feature(geometry=poly,    properties=properties))
         contour_index += 1

    feature_collection = FeatureCollection(poly_features)
    dump = geojson.dumps(feature_collection, sort_keys=True)
    with open(geojson_filepath, 'w') as fileout:
        fileout.write(dump)
#尝试从等高线数据创建多边形的代码
#仅当数据中存在极小值时,代码才完整
#(例如,轮廓闭合时)
def contour_to_geojson(contour、geojson_文件路径、contour_级别):
collections=contour.collections
等高线指数=0
断言len(等高线水平)=len(集合)
多边形功能=[]
对于集合中的集合:
路径=集合。获取路径()
对于路径中的路径:
顶点=路径。顶点
#检查路径是否已关闭
#比较浮动时存在问题,因此设置一个截止点
#阈值,而不是直接比较值
边界=绝对值(垂直[0][0]-垂直[len(垂直)-1][0])
边界=绝对值(垂直[0][1]-垂直[len(垂直)-1][1])
#打印(边界)
#0.000001本质上是0
坐标=[]
如果界限长度<0.00001且界限长度<0.00001:
兼容的_vert=[]
对于垂直方向的v:
lat=np.ASCALAR(垂直[v][1])
lon=np.ASCALAR(垂直[v][0])
合规垂直附加((纵向,横向))
坐标。追加(符合垂直)
其他:
#仍然需要为此部分编写代码
#将使用任意一侧的轮廓作为边界创建多边形坐标
如果等高线索引
您的代码在哪里?请检查我编辑的问题,我更想用psuedo代码寻找答案,但我已经包含了我的代码,以防有帮助