1

I have this code to plot a choropleth from a GeoPandas DataFrame.

def create_gpd_dataframe(self
    d = {'Angle of point': yard_list, 'Latitude': yard_lat_list, 
    'Longitude': yard_lon_list}
    df = pd.DataFrame(d)
    self.geopd = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
    self.optimal_gdf = (self.geopd[(self.geopd['Angle of point'] >= 88) & (self.geopd['Angle of point'] <= 100)])
    self.optimal_gdf.crs = {'init': 'epsg:4326'}
    a = self.optimal_gdf['Latitude'].tolist()
    b = self.optimal_gdf['Longitude'].tolist()

    self.polygon_optimal_list = list(zip(a, b))

    lat = (self.geopd[(self.geopd['Angle of point'] >= 49) & (self.geopd['Angle of point'] <= 88)])
    lon = (self.geopd[(self.geopd['Angle of point'] >= 100) & (self.geopd['Angle of point'] <= 150)])

    self.polygon_plot = list(zip(lat, lon))

def plotter(self,
            midlat=midlat,
            midlon=midlon,
            gdt1=gdt1,
            gdt2=gdt2,
            target=target
            ):

    f_map = folium.Map(
        location=[midlat, midlon],
        tiles='Stamen Terrain',
        zoom_start=10
    )

    folium.Marker(
        location=[gdt1[0], gdt1[1]],
        popup='GDT1'.format(gdt1),
        icon=folium.Icon(color='red')
    ).add_to(f_map)

    folium.Marker(
        location=[gdt2[0], gdt2[1]],
        popup='GDT2'.format(gdt2),
        icon=folium.Icon(color='red')
    ).add_to(f_map)

    folium.Marker(
        location=[target[0], target[1]],
        popup='Target Area'.format(target),
        icon=folium.Icon(color='red')
    ).add_to(f_map)

    folium.Choropleth(
        self.optimal_gdf['geometry'],
        key_on='feature.id',
        fill_color='YlGnBu',
        fill_opacity=1,
        line_opacity=0.2,
        bins=[0, 20, 30, 40, 50, 60]
    ).add_to(f_map)

    f_map.save('in5.html')

    new = 2
    url = "/home/yovel/PycharmProjects/TriangulationCalc/in5.html"
    webbrowser.open(url, new=new)

get = PlotOnMap()
get.create_gpd_dataframe()
get.plotter()

And for some reason my output is this:

output

  • 1
    what were you expecting? – Ian Turton Jan 16 at 8:42
  • @IanTurton for the data to be colored as one polygon, not every location individually. – yovel cohen Jan 16 at 8:46
  • You always use markers. There is no polygon your code. What do you mean by one poygon? The code is uncomplete. Where do you call plotter method. – Kadir Şahbaz Jan 16 at 9:01
  • @KadirŞahbaz like in the doc: python-visualization.github.io/folium/… , where the plotting is in colors not markers. – yovel cohen Jan 16 at 9:08
  • @KadirŞahbaz how can I change the points in the dataframe to a polygon? I tried to create the dataframe with shaply polygon I've created from same list of locations but than it can't be used in a gpd and plotted with folium. – yovel cohen Jan 16 at 10:11