渲染叶片对象

时间:2019-03-20 11:27:12

标签: python jupyter-notebook folium

我正在为Folium写一个包装器,以绘制我经常使用的数据。我的课看起来像...

"""
Plot journeys using Folium
"""
import folium  

class PlotJourney():
    """
    Plot journeys using Folium
    """

    def __init__(self, journey,
                 zoom_start=14,
                 color='blue',
                 weight=4,
                 opacity=1):
        """
        Initialise the class.

        Parameters
        ----------
        journey : Pandas df
            Pandas gps data
        zoom_start :  int
            Zoom level on map (default is 14, if this doesn't capture whole journey then reduce).
        color : int
            Colour of journey line to be plotted.
        weight : int
            Thickness of journey line to be plotted.
        opacity : int
            Opacity of journey line to be lotted.
        """
        self.journey = journey
        self.zoom_start = zoom_start
        self.color = color
        self.weight = weight
        self.opacity = opacity
        self.folium_map = None

    def map_journey(self):
        """
        Map the journey and plot data points.

        Returns
        -------

        Folium Map object (folium.folium.Map)
        """
        self._center_map()
        self._plot_journey()
        return self.folium_map

    def _center_map(self):
        """
        Instantiate a Folium map centered on the mid-point of the journey.
        """
        mid_gps_point = int(len(self.gps) / 2)
        lat = self.journey.get_gps().iloc[mid_gps_point, 1]
        lon = self.journey.get_gps().iloc[mid_gps_point, 2]

        self.folium_map = folium.Map(location=[gps.lat, gps.lon],
                                     zoom_start=self.zoom_start)

    def _plot_journey(self):
        """
        Plot the journey on a Folium map.

        Returns
        -------

        """
        points = []
        for point in self.gps.itertuples():
            points.append(tuple([getattr(point, 'lat'),
                                 getattr(point, 'lon')]))
        folium.PolyLine(points,
                        color=self.color,
                        weight=self.weight,
                        opacity=self.opacity).add_to(self.folium_map)

...还有一些示例数据...

> gps.head(60)

time    lat lon alt accuracy    epoch   speed   bearing has_speed   has_bearing
0   3938    53.388886   -1.467034   51.868667   8.0 1548424516371   4.958602    292.80603   True    True
1   4963    53.388907   -1.467131   51.776321   6.0 1548424517371   4.958602    284.70547   True    True
2   5958    53.388908   -1.467218   51.696368   6.0 1548424518371   5.487597    278.67032   True    True
3   6991    53.388907   -1.467324   51.542282   6.0 1548424519371   6.479943    237.81479   True    True
4   7970    53.388867   -1.467443   51.297575   6.0 1548424520371   9.512295    227.80710   True    True
5   8974    53.388802   -1.467564   50.852740   6.0 1548424521371   9.512295    229.77907   True    True
6   9965    53.388728   -1.467698   50.320224   6.0 1548424522371   10.650246   228.23433   True    True
7   10980   53.388656   -1.467838   49.781136   6.0 1548424523371   12.169792   226.67421   True    True
8   11975   53.388575   -1.467977   58.991536   6.0 1548424524371   12.833956   224.14210   True    True
9   12981   53.388493   -1.468120   59.354830   8.0 1548424525371   12.833956   224.14210   True    True
10  13976   53.388403   -1.468257   59.437485   8.0 1548424526371   13.042620   220.66881   True    True
11  14979   53.388308   -1.468385   59.708062   6.0 1548424527371   13.238512   215.60231   True    True
12  15965   53.388208   -1.468515   59.296351   6.0 1548424528371   13.871293   209.57590   True    True
13  16970   53.388101   -1.468622   51.660798   6.0 1548424529371   13.625889   209.57590   True    True
14  17961   53.387991   -1.468709   52.981496   6.0 1548424530371   13.625889   202.18289   True    True
15  18962   53.387888   -1.468796   53.916590   6.0 1548424531371   12.490627   200.32661   True    True
16  19975   53.387784   -1.468875   57.255894   6.0 1548424532371   12.822588   201.84820   True    True
17  20965   53.387680   -1.468945   57.119045   6.0 1548424533371   12.490627   207.00688   True    True
18  21970   53.387587   -1.469025   57.561872   6.0 1548424534371   12.020948   212.86082   True    True
19  22970   53.387507   -1.469108   50.470560   6.0 1548424535371   9.671459    220.38089   True    True
20  23991   53.387438   -1.469202   49.985855   6.0 1548424536371   9.671459    221.17906   True    True
21  24998   53.387381   -1.469281   50.069660   6.0 1548424537371   8.676711    221.17906   True    True
22  26007   53.387338   -1.469342   50.269601   6.0 1548424538371   6.622233    221.17906   True    True
23  27004   53.387310   -1.469388   50.465225   6.0 1548424539371   3.526981    230.89244   True    True
24  28002   53.387299   -1.469420   50.622989   6.0 1548424540371   3.526981    230.89244   True    True
25  28997   53.387297   -1.469439   50.738569   6.0 1548424541371   1.123686    228.41452   True    True
26  30032   53.387295   -1.469451   50.819053   6.0 1548424542371   1.029650    223.59712   True    True
27  30996   53.387298   -1.469446   50.872083   6.0 1548424543371   0.026778    226.58867   True    True
28  32001   53.387300   -1.469447   50.905676   6.0 1548424544371   0.060990    227.13960   True    True
29  32977   53.387308   -1.469450   50.926833   8.0 1548424545371   0.053592    227.21925   True    True
30  33994   53.387312   -1.469445   50.940148   6.0 1548424546371   0.048401    227.13814   True    True
31  34983   53.387313   -1.469440   50.948531   6.0 1548424547371   0.109131    227.43109   True    True
32  36002   53.387306   -1.469422   50.952020   6.0 1548424548371   0.052090    227.56128   True    True
33  37004   53.387307   -1.469418   50.947837   6.0 1548424549372   0.041504    227.60867   True    True
34  37995   53.387308   -1.469413   50.942715   6.0 1548424550372   0.071604    227.62416   True    True
35  38985   53.387311   -1.469413   50.937452   6.0 1548424551372   0.046862    227.85240   True    True
36  39994   53.387314   -1.469416   50.933595   6.0 1548424552372   0.069447    227.83664   True    True
37  40998   53.387316   -1.469416   50.931678   6.0 1548424553372   0.052803    227.86447   True    True
38  42001   53.387309   -1.469413   50.930216   6.0 1548424554372   0.069447    230.89244   True    True
39  42999   53.387305   -1.469425   50.929122   6.0 1548424555372   1.181171    230.89244   True    True
40  43997   53.387290   -1.469460   50.933373   6.0 1548424556372   4.368997    230.89244   True    True
41  44996   53.387263   -1.469523   50.943439   6.0 1548424557372   4.368997    234.79124   True    True
42  46004   53.387234   -1.469605   50.950515   6.0 1548424558372   5.774909    236.01671   True    True
43  47005   53.387209   -1.469700   50.955048   6.0 1548424559372   7.456284    236.01671   True    True
44  47998   53.387169   -1.469818   50.957904   6.0 1548424560372   10.010792   246.38654   True    True
45  48997   53.387135   -1.469969   50.981900   6.0 1548424561372   10.010792   251.24115   True    True
46  49999   53.387109   -1.470143   51.070027   6.0 1548424562372   11.330110   257.62120   True    True
47  51003   53.387085   -1.470334   51.175718   6.0 1548424563372   13.143827   265.61157   True    True
48  52005   53.387072   -1.470541   51.310900   6.0 1548424564372   14.031583   268.02994   True    True
49  53000   53.387072   -1.470769   51.528922   6.0 1548424565372   14.031583   273.96310   True    True
50  54008   53.387085   -1.471001   51.825265   6.0 1548424566372   14.883696   276.29077   True    True
51  55127   53.387101   -1.471232   52.054645   6.0 1548424567372   15.086695   275.23720   True    True
52  56109   53.387114   -1.471459   52.265689   6.0 1548424568372   15.086695   275.63900   True    True
53  57102   53.387134   -1.471680   52.544293   6.0 1548424569372   14.997134   276.34448   True    True
54  58111   53.387151   -1.471894   52.807707   6.0 1548424570372   14.283136   276.66174   True    True
55  59106   53.387163   -1.472107   52.961442   6.0 1548424571372   14.186031   276.77936   True    True
56  60108   53.387178   -1.472321   53.084145   6.0 1548424572372   14.186031   276.85873   True    True
57  61100   53.387191   -1.472531   53.158330   6.0 1548424573372   14.126818   277.44763   True    True
58  62104   53.387203   -1.472731   53.205148   6.0 1548424574372   13.779898   278.57642   True    True
59  63113   53.387219   -1.472923   53.471567   6.0 1548424575372   12.568898   278.76767   True    True

当我将类加载到Jupyter Notebook中时,实例化并调用map_journey()方法(该方法将创建地图和标绘线),返回的只是内存中对象的地址,要渲染的地图本身。...

PlotJourney(gps)
<__main__.PlotJourney at 0x7f35a9bc2518>

如果我拉出关键步骤并在Notebook中交互运行它们,则效果会很好...

m = folium.Map([gps.lat[0], gps.lon[0]], zoom_start=15)
points = []
for point in gps2.itertuples():
    points.append(tuple([getattr(point, 'lat'),
                         getattr(point, 'lon')]))
folium.PolyLine(points).add_to(m)
m

对于在课堂上出错的地方,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个命名问题,您遇到journeygps。我在center_map和plot_journey中更改了名称。要调用该函数,请运行PlotJourney(gps).map_journey()

"""
Plot journeys using Folium
"""
import folium  


class PlotJourney():
    """
    Plot journeys using Folium
    """

    def __init__(self, journey,
                 zoom_start=14,
                 color='blue',
                 weight=4,
                 opacity=1):
        """
        Initialise the class.

        Parameters
        ----------
        journey : Pandas df
            Pandas gps data
        zoom_start :  int
            Zoom level on map (default is 14, if this doesn't capture whole journey then reduce).
        color : int
            Colour of journey line to be plotted.
        weight : int
            Thickness of journey line to be plotted.
        opacity : int
            Opacity of journey line to be lotted.
        """
        self.journey = journey
        self.zoom_start = zoom_start
        self.color = color
        self.weight = weight
        self.opacity = opacity
        self.folium_map = None


    def _center_map(self):
        """
        Instantiate a Folium map centered on the mid-point of the journey.
        """
        mid_gps_point = int(len(self.journey) / 2)
        lat = self.journey.iloc[mid_gps_point, 1]
        lon = self.journey.iloc[mid_gps_point, 2]

        self.folium_map = folium.Map(location=[lat, lon],
                                     zoom_start=self.zoom_start)

    def _plot_journey(self):
        """
        Plot the journey on a Folium map.

        Returns
        -------

        """
        points = []
        for point in self.journey.itertuples():
            points.append(tuple([getattr(point, 'lat'),
                                 getattr(point, 'lon')]))
        folium.PolyLine(points,
                        color=self.color,
                        weight=self.weight,
                        opacity=self.opacity).add_to(self.folium_map)

    def map_journey(self):
        """
        Map the journey and plot data points.

        Returns
        -------

        Folium Map object (folium.folium.Map)
        """
        self._center_map()
        self._plot_journey()
        return self.folium_map