2016-02-19 21 views
1

我試圖在葉片上繪製雷達數據,而且我幾乎在那裏。我遵循此示例(Contour plot data (lat,lon,value) within boundaries and export GeoJSON)將我的數據轉換爲GeoJson格式。用葉片中的地理數據框繪製彩色多邊形

nb_class = 20 
collec_poly = plt.contourf(lons,lats,np.array(poshdata), nb_class,alpha=0.5) 

gdf = collec_to_gdf(collec_poly) # From link above 
gdf.to_json() 
colors = [p.get_facecolor().tolist()[0] for p in collec_poly.collections] 
gdf['RGBA'] = colors 

gdf 

這會輸出兩列:geometry和RGBA。

RGBA geometry 
0 [0.0, 0.0, 0.713903743316, 1.0] (POLYGON ((-71.57032079644679 42.2775236331535... 
1 [0.0, 0.0960784313725, 1.0, 1.0] (POLYGON ((-71.56719970703125 42.2721176147460... 
2 [0.0, 0.503921568627, 1.0, 1.0] (POLYGON ((-71.55678558349609 42.2721176147460... 
3 [0.0, 0.896078431373, 0.970904490829, 1.0] (POLYGON ((-71.52552795410156 42.2849182620049... 
4 [0.325743200506, 1.0, 0.641998734978, 1.0] (POLYGON ((-71.49427795410156 42.2939676156927... 
5 [0.641998734978, 1.0, 0.325743200506, 1.0] (POLYGON ((-71.47344207763672 42.3003084448852... 
6 [0.970904490829, 0.959331880901, 0.0, 1.0] (POLYGON ((-71.26508331298828 42.3200411822557... 
7 [1.0, 0.581699346405, 0.0, 1.0] (POLYGON ((-71.15048217773438 42.3333218460720... 

從那裏我做我的大青葉地圖:

import folium  

# Picked location between Sudbury and Somerville: 
maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=11,tiles="Stamen Toner") 

folium.GeoJson(gdf).add_to(maploc) 

這造成我的漂亮大青葉地圖,但多邊形沒有被着色。如何讓輪廓充滿正確的顏色?並修復不透明度?

enter image description here

回答

1

我想我想通了。在我之前的代碼中,polygon.get_facecolor()返回範圍從0到1的RGBA值列表。我添加了這個功能(從this後修飾):

def convert_to_hex(rgba_color) : 
    red = str(hex(int(rgba_color[0]*255)))[2:].capitalize() 
    green = str(hex(int(rgba_color[1]*255)))[2:].capitalize() 
    blue = str(hex(int(rgba_color[2]*255)))[2:].capitalize() 

    if blue=='0': 
     blue = '00' 
    if red=='0': 
     red = '00' 
    if green=='0': 
     green='00' 

    return '#'+ red + green + blue 

將其轉換爲十六進制字符串。然後:

gdf['RGBA'] = convert_to_hex(colors) 

然後繪製顏色大青葉,我做的:

maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=10,tiles="Stamen Toner") 

colors = [] 
folium.GeoJson(
    gdf, 
    style_function=lambda feature: { 
     'fillColor': feature['properties']['RGBA'], 
     'color' : feature['properties']['RGBA'], 
     'weight' : 1, 
     'fillOpacity' : 0.5, 
     } 
    ).add_to(maploc) 

,並創建一個非常好看的情節! (屬性名稱有點誤導 - 它實際上不是RGBA值,而是十六進制字符串。)

0

不是專家。我剛開始用大青葉和jupyter和有類似的問題,但用線。 你說你有GeoJson和多邊形,並且顏色包含在我假設的json中。

style_function可能會幫助你得到你想要的東西?

下面的例子,製作了網頁:http://geojson.io/ 我不得不這樣做是一個「映射」與style_function。 它也可以使用自定義的功能,請參閱: https://github.com/python-visualization/folium/blob/master/examples/Colormaps.ipynb

import folium 
geoJsonData = { 
    "features": [ 
     { 
      "geometry": { 
       "coordinates": [ 
        [ 
         12.98583984375, 
         56.70450561416937 
        ], 
        [ 
         14.589843749999998, 
         57.604221411628735 
        ], 
        [ 
         13.590087890625, 
         58.15331598640629 
        ], 
        [ 
         11.953125, 
         57.955674494979526 
        ], 
        [ 
         11.810302734375, 
         58.76250326278713 
        ] 
       ], 
       "type": "LineString" 
      }, 
      "properties": { 
       "stroke": "#fc1717", 
       "stroke-opacity": 1, 
       "stroke-width": 2 
      }, 
      "type": "Feature" 
     }, 
     { 
      "geometry": { 
       "coordinates": [ 
        [ 
         14.9468994140625, 
         57.7569377956732 
        ], 
        [ 
         15.078735351562498, 
         58.06916140721414 
        ], 
        [ 
         15.4302978515625, 
         58.09820267068277 
        ], 
        [ 
         15.281982421875002, 
         58.318144965188246 
        ], 
        [ 
         15.4852294921875, 
         58.36427519285588 
        ] 
       ], 
       "type": "LineString" 
      }, 
      "properties": { 
       "stroke": "#1f1a95", 
       "stroke-opacity": 1, 
       "stroke-width": 2 
      }, 
      "type": "Feature" 
     } 
    ], 
    "type": "FeatureCollection" 
} 
m = folium.Map(location=[ 56.7, 12.9], zoom_start=6) 
folium.GeoJson(geoJsonData, 
    style_function=lambda x: { 
     'color' : x['properties']['stroke'], 
     'weight' : x['properties']['stroke-width'], 
     'opacity': 0.6, 
     'fillColor' : x['properties']['fill'], 
     }).add_to(m) 
m 

上git的樞紐大青葉源代碼包含幾個很好的例子還有:
https://github.com/python-visualization/folium/tree/master/examples
這裏你可以找到一起玩的選項:
http://leafletjs.com/reference.html#path-options

希望這將帶給你前進!