CrimeMap.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:BlueLines 作者: JacksYou 项目源码 文件源码
def load(self):
        """loads shapefile onto graphical representation of data using basemap and fiona"""
        shape = fiona.open("data/shapefiles/chicago.shp")
        bounds = shape.bounds
        extra = 0.01
        lower_left = (bounds[0], bounds[1])
        upper_right = (bounds[2], bounds[3])
        coords = list(chain(lower_left, upper_right))
        width, height = coords[2] - coords[0], coords[3] - coords[1]

        self.base_map = Basemap(
            projection="tmerc",
            lon_0=-87.,
            lat_0=41.,
            ellps="WGS84",
            llcrnrlon=coords[0] - extra * width,
            llcrnrlat=coords[1] - extra + 0.01 * height,
            urcrnrlon=coords[2] + extra * width,
            urcrnrlat=coords[3] + extra + 0.01 * height,
            lat_ts=0,
            resolution='i',
            suppress_ticks=True
        )

        self.base_map.readshapefile(
            "data/shapefiles/chicago",
            'chicago',
            color='none',
            zorder=2
        )

        self.data_map = pd.DataFrame({
            'poly': [Polygon(xy) for xy in self.base_map.chicago],
            'community_name': [ward['community'] for ward in self.base_map.chicago_info]})
        self.data_map['area_m'] = self.data_map['poly'].map(lambda x: x.area)
        self.data_map['area_km'] = self.data_map['area_m'] / 100000
        self.data_map['patches'] = self.data_map['poly'].map(lambda x: PolygonPatch(x,
                                                                                    fc='#555555',
                                                                                    ec='#787878', lw=.25, alpha=.9,
                                                                                    zorder=4))

        plt.close()
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111, axisbg='w', frame_on=False)

        self.ax.add_collection(PatchCollection(self.data_map['patches'].values, match_original=True))

        self.base_map.drawmapscale(
            coords[0] + 0.08, coords[1] + 0.015,
            coords[0], coords[1],
            10.,
            barstyle='fancy', labelstyle='simple',
            fillcolor1='w', fillcolor2='#555555',
            fontcolor='#555555',
            zorder=5)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号