Contour plot data (lat,lon,value) within boundaries and export GeoJSON

前端 未结 2 1145
离开以前
离开以前 2021-02-11 09:42

I\'m trying to interpolate data within boundaries and plot contour lines (polygons) based on Latitude, longitude, value data from csv files.

Results I want print as geoj

2条回答
  •  野性不改
    2021-02-11 10:28

    Almost I got what I excepted. Based on mgc answer. I change griddata as you suggest to scipy.interpolate.griddata and interpolation method to nearest. Now its works like I want.

    Only last thing that I need is to limit this interpolation to polygon (boundaries) also from geoJson.

    The other problem is to export to geojson polygons WITH colors as attributes.

    import numpy as np
    import matplotlib.pyplot as plt
    #from matplotlib.mlab import griddata
    from scipy.interpolate import griddata
    
    from geopandas import GeoDataFrame
    from shapely.geometry import Polygon, MultiPolygon
    
    def collec_to_gdf(collec_poly):
        """Transform a `matplotlib.contour.QuadContourSet` to a GeoDataFrame"""
        polygons = []
        for i, polygon in enumerate(collec_poly.collections):
            mpoly = []
            for path in polygon.get_paths():
                try:
                    path.should_simplify = False
                    poly = path.to_polygons()
                    # Each polygon should contain an exterior ring + maybe hole(s):
                    exterior, holes = [], []
                    if len(poly) > 0 and len(poly[0]) > 3:
                        # The first of the list is the exterior ring :
                        exterior = poly[0]
                        # Other(s) are hole(s):
                        if len(poly) > 1:
                            holes = [h for h in poly[1:] if len(h) > 3]
                    mpoly.append(Polygon(exterior, holes))
                except:
                    print('Warning: Geometry error when making polygon #{}'
                          .format(i))
            if len(mpoly) > 1:
                mpoly = MultiPolygon(mpoly)
                polygons.append(mpoly)
            elif len(mpoly) == 1:
                polygons.append(mpoly[0])
        return GeoDataFrame(geometry=polygons, crs={'init': 'epsg:4326'})
    
    data = np.genfromtxt('temp2.csv', delimiter=',')
    x = data[..., 1]
    y = data[..., 2]
    z = data[..., 4]
    xi = np.linspace(x.min(), x.max(), num=100)
    yi = np.linspace(y.min(), y.max(), num=100)
    
    #zi = griddata(x, y, z, xi, yi, interp='nn') # You could also take a look to scipy.interpolate.griddata
    zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='nearest')
    
    nb_class = [5,10,15,20,25,30,35,40,45,50] # Set the number of class for contour creation
    # The class can also be defined by their limits like [0, 122, 333]
    collec_poly = plt.contourf(
        xi, yi, zi, nb_class, vmax=abs(zi).max(), vmin=-abs(zi).max())
    
    gdf = collec_to_gdf(collec_poly)
    #gdf.to_json()
    print gdf.to_json()
    
    plt.plot(x,y)
    
    plt.show()
    

提交回复
热议问题