def spatial_density_all_time_all_crimes(): poly = get_camden_region() camden_mpoly = geodjango_to_shapely([get_camden_region()]) oo = osm.OsmRendererBase(poly, buffer=100) cbg = CadByGrid() a = cbg.all_time_aggregate() fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, projection=ccrs.OSGB()) ax.set_extent([523000, 533000, 179000, 190000], ccrs.OSGB()) ax.background_patch.set_visible(False) oo.render(ax) a = a.sum(axis=1) cmap = mpl.cm.Reds norm = mpl.colors.Normalize() norm.autoscale(a) cax = mpl.colorbar.make_axes(ax, location='bottom', pad=0.02, fraction=0.05, shrink=0.9) cbar = mpl.colorbar.ColorbarBase(cax[0], cmap=cmap, norm=norm, orientation='horizontal') sm = mpl.cm.ScalarMappable(norm=norm, cmap=cmap) for j in range(cbg.m): val = a.values[j] fc = sm.to_rgba(val) if val else 'none' ax.add_geometries(geodjango_to_shapely([cbg.grid[j].mpoly]), ccrs.OSGB(), facecolor=fc, alpha=0.3) ax.add_geometries(camden_mpoly, ccrs.OSGB(), facecolor='none', edgecolor='black') plt.show()
def get_camden_wards(as_shapely=True): camden = models.Division.objects.get(type='borough', name__iexact='camden') wards = models.Division.objects.filter(type='ward', mpoly__intersects=camden.mpoly) polys = [] for t in wards: if t.mpoly.intersection(camden.mpoly).area / t.mpoly.area > 1e-3: polys.append(t.mpoly.simplify()) if as_shapely: polys = [geodjango_to_shapely(p) for p in polys] return polys
def plot_domains(name=None, ax=None, set_axis=True): if name is None and ax is not None: raise AttributeError("If no single domain is specified, we have to make new figures for each plot") city = geodjango_to_shapely( models.ChicagoDivision.objects.get(name='ChicagoFilled').mpoly.simplify(0) ) domains = get_chicago_side_polys(as_shapely=True) if name is not None: plot_domain(city, domains[name], ax=ax, set_axis=set_axis) else: for k, v in domains.items(): plot_domain(city, v)
def __init__(self, grid=None, **kwargs): # defer dedupe until after spatial aggregation super(CadSpatialGrid, self).__init__(**kwargs) # load grid or use one provided self.grid = grid or models.Division.objects.filter( type='cad_250m_grid') self.shapely_grid = [ geodjango_to_shapely(x.mpoly)[0] for x in self.grid ] # self.shapely_grid = pandas.Series([geodjango_to_shapely([x.mpoly]) for x in self.grid], # index=[x.name for x in self.grid]) # gridded data self.data = self.aggregate()
def __init__(self, data, t0, data_index=None, grid=None): """ :param data: N x 3 np.ndarray or DataArray (t, x, y), all float :param data_index: length N array containing indices corresponding to data. If not supplied, lookup index used. :param t0: datetime corresponding to first record :param grid: optional list of grid (multi)polygons, default is CAD 250m grid :return: """ self.grid = grid or [ t.mpoly for t in models.Division.objects.filter(type='cad_250m_grid') ] self.shapely_grid = [geodjango_to_shapely(x)[0] for x in self.grid] # preliminary cad filter self.data = data self.t0 = t0 self.data_index = np.array( data_index) if data_index is not None else np.arange(len(data)) self.dates, self.indices = self.compute_array()
def get_camden_region(as_shapely=False): poly = models.Division.objects.get(type='borough', name__iexact='camden') poly = poly.mpoly.simplify() # type Polygon if as_shapely: poly = geodjango_to_shapely(poly) return poly
def local_morans_i(): nicl_numbers = [1, 3, (6, 7)] short_names = ['Violence', 'Burglary dwelling', 'Theft of/from vehicle'] camden_mpoly = geodjango_to_shapely( [models.Division.objects.get(name='Camden', type='borough').mpoly]) cbg = CadByGrid(nicl_numbers=nicl_numbers) a = cbg.all_time_aggregate() W = rook_boolean_connectivity(cbg.grid) fig = plt.figure(figsize=(15, 6)) axes = [ fig.add_subplot(1, cbg.l, i + 1, projection=ccrs.OSGB()) for i in range(cbg.l) ] fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.03, hspace=0.01) local_i = [] standard_local_i = [] max_li = 0. min_li = 1e6 for i in range(cbg.l): ds = a[cbg.nicl_names[i]] this_res = lmi(ds, W) local_i.append(this_res) standard_local_i.append(this_res / this_res.std()) max_li = max(max_li, max(standard_local_i[i])) min_li = min(min_li, min(standard_local_i[i])) for i in range(cbg.l): ax = axes[i] ax.set_title(short_names[i]) ax.set_extent([523000, 533000, 179000, 190000], ccrs.OSGB()) ax.background_patch.set_visible(False) # ax.outline_patch.set_visible(False) cmap = mpl.cm.jet norm = mpl.colors.Normalize(vmin=min_li, vmax=max_li) # norm.autoscale(local_i[i]) cax = mpl.colorbar.make_axes(ax, location='bottom', pad=0.02, fraction=0.05, shrink=0.9) cbar = mpl.colorbar.ColorbarBase(cax[0], cmap=cmap, norm=norm, orientation='horizontal') sm = mpl.cm.ScalarMappable(norm=norm, cmap=cmap) for j in range(cbg.m): val = standard_local_i[i].values[j] fc = sm.to_rgba(val) if val else 'none' ax.add_geometries(geodjango_to_shapely([cbg.grid[j].mpoly]), ccrs.OSGB(), facecolor=fc) ax.add_geometries(camden_mpoly, ccrs.OSGB(), facecolor='none', edgecolor='black') plt.show() return local_i
def spatial_density_weekday_evening(): nicl_numbers = [3, 6, 10] short_names = ['Burglary Dwelling', 'Veh Theft', 'Crim Damage'] camden_mpoly = geodjango_to_shapely( [models.Division.objects.get(name='Camden', type='borough').mpoly]) cbg = CadByGrid(nicl_numbers=nicl_numbers) a = cbg.weekday_weekend_aggregate() b = cbg.daytime_evening_aggregate() # combine for x in b.items: a[x] = b[x] b = a.transpose(2, 0, 1) for i in range(cbg.l): # crime types df = b.iloc[i] n = df.shape[0] fig = plt.figure(figsize=(20, 6)) axes = [ fig.add_subplot(1, n, t + 1, projection=ccrs.OSGB()) for t in range(n) ] fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.03, hspace=0.01) for j in range(n): ax = axes[j] ds = df.iloc[j] ax.set_title(ds.name) ax.set_extent([523000, 533000, 179000, 190000], ccrs.OSGB()) ax.background_patch.set_visible(False) # ax.outline_patch.set_visible(False) cmap = mpl.cm.cool norm = mpl.colors.Normalize() norm.autoscale(ds) cax = mpl.colorbar.make_axes(ax, location='bottom', pad=0.02, fraction=0.05, shrink=0.9) cbar = mpl.colorbar.ColorbarBase(cax[0], cmap=cmap, norm=norm, orientation='horizontal') sm = mpl.cm.ScalarMappable(norm=norm, cmap=cmap) for j in range(cbg.m): val = ds.values[j] fc = sm.to_rgba(val) if val else 'none' ax.add_geometries(geodjango_to_shapely([cbg.grid[j].mpoly]), ccrs.OSGB(), facecolor=fc) ax.add_geometries(camden_mpoly, ccrs.OSGB(), facecolor='none', edgecolor='black') plt.show()