Ejemplo n.º 1
0
def create_grid_squares_shapefile(outfile, side_length=250):
    mpoly = get_boundary()
    ipoly, fpoly, full = spatial.create_spatial_grid(mpoly, side_length)
    field_description = {'id': {'fieldType': 'N'}}
    id = range(1, len(fpoly) + 1)
    full_polys = [spatial.shapely_rectangle_from_vertices(*t) for t in fpoly]
    spatial.write_polygons_to_shapefile(outfile,
                                        full_polys,
                                        field_description=field_description,
                                        id=id)
Ejemplo n.º 2
0
 def set_sample_units(self, side_length, *args, **kwargs):
     '''
     Set the ROC grid.
     :param length_or_arr: Either a scalar, interpreted as the side length of the grid square, OR an array of
       shapely Polygon or shapely MultiPolygon objects
     :param args: Passed to set_sample_points
     :param kwargs: Passed to set_sample_points
     :return: None
     '''
     # reset prediction values
     self.prediction_values = None
     if not self.poly:
         # find minimal bounding rectangle
         self.poly = self.generate_bounding_poly(self.data)
     self.side_length = side_length
     self.grid_polys, self.sample_units, self.full_grid_square = create_spatial_grid(
         self.poly, self.side_length)
     self.set_centroids()
     self.set_sample_points(*args, **kwargs)
Ejemplo n.º 3
0
    def set_sample_units(self, side_length, interval, *args, **kwargs):
        """
        Set the ROC grid.
        :param side_length: side length of grid squares
        :param interval: The length interval between sample points
        :param args: Passed to set_sample_points
        :param kwargs: Passed to set_sample_points.
        :return: None
        """
        # reset prediction values
        self.prediction_values = None

        if not self.poly:
            # find minimal bounding rectangle
            self.poly = self.generate_bounding_poly(self.data)

        # set sample grid
        self.side_length = side_length
        self.grid_polys, self.sample_units, _ = create_spatial_grid(
            self.poly, self.side_length)

        # set network sampling points
        self.set_sample_points(interval, *args, **kwargs)
Ejemplo n.º 4
0
    ax = ax or plt.gca()
    y = [len(t) for t in aggregate_data.values()]
    spatial.plot_shaded_regions(aggregate_data.keys(),
                                y,
                                ax=ax,
                                cmap=plt.get_cmap('autumn_r'),
                                vmin=1e-6,
                                domain=domain,
                                scale_bar_loc=scale_bar_loc)


if __name__ == "__main__":

    chic_s_domain = chicago.get_chicago_side_polys(as_shapely=True)['South']
    camden_domain = cad.get_camden_region(as_shapely=True)
    chic_s_grid = create_spatial_grid(chic_s_domain, 250)[0]
    cad_grid = [t.mpoly for t in models.Division.objects.filter(type='monsuru_250m_grid')]

    ## Camden

    nicl_types = (
        (1, 'Violence'),
        (3, 'Burglary'),
        (13, 'Shoplifting')
    )

    cad_data = {}
    cad_spatial_data = {}

    for k, v in nicl_types:
        a, b, c = cad.get_crimes_by_type(k, convert_dates=False)
    ax = fig.add_subplot(111)
    cm = utils.transparent_colour_map()
    net.plot_network(ax=ax)

    pred_t = 180.
    arr_fun = lambda x, y: plots.txy_to_cartesian_data_array(np.ones_like(x) * pred_t, x, y)
    pred_fun = lambda x, y: r.predict(arr_fun(x, y))
    xx, yy, zz = spatial.plot_surface_function_on_polygon(domain, pred_fun, ax=ax, fmax=0.98, cmap=cm, dx=50)
    ax.axis('off')
    plt.tight_layout()

    fig.savefig('continuous_heatmap.png', dpi=300)
    fig.savefig('continuous_heatmap.pdf', dpi=300)

    # (2) top 10 % grid squares
    ipolys, full_extents, full_grid_square = create_spatial_grid(domain, 200)
    grid_values = []
    for xmin, ymin, xmax, ymax in full_extents:
        i, j = np.where((xx >= xmin) & (xx < xmax) & (yy >= ymin) & (yy < ymax))
        grid_values.append(zz[i, j].sum())
    sort_idx = np.argsort(grid_values)[::-1]
    top_10_pct = sort_idx[:int(len(sort_idx) * 0.1)]
    top_10_pct_grids = [shapely_rectangle_from_vertices(*full_extents[i]) for i in top_10_pct]

    fig = plt.figure(figsize=(5, 6))
    ax = fig.add_subplot(111)
    net.plot_network(ax=ax)
    spatial.plot_shapely_geos(domain, ax=ax)
    spatial.plot_shapely_geos(top_10_pct_grids, ax=ax, fc='r', ec='none', lw=1.5, alpha=0.7)

    ax.axis('off')
def get_chicago_grid(poly):
    ipoly, fpoly, full = spatial.create_spatial_grid(poly, 250)
    return fpoly
Ejemplo n.º 7
0
def create_roc_grid(poly, grid_length=250):
    ipoly, fpoly, full = spatial.create_spatial_grid(poly, grid_length)
    return fpoly