crime_mapping
predspot.crime_mapping
¶
Crime Mapping Module¶
Spatial and temporal crime mapping. This module turns a set of georeferenced, timestamped crime events into a spatio-temporal series: a value per grid cell per time period. Two families of mapping are available:
KDE— kernel density estimation evaluated on a grid of points (seecreate_gridpoints). This is the default approach of Predspot.QuadratCount— plain event counts per cell of a polygonal grid (seecreate_gridhexagonalandcreate_gridsquares).
Both produce the same output format, a pandas.Series named
crime_density indexed by (t, places), so they are interchangeable
inside PredictionPipeline.
The study area itself can be fetched from OpenStreetMap with
load_study_area (requires the optional osmnx
dependency).
SpatioTemporalMapping
¶
Bases: ABC, TransformerMixin, BaseEstimator
Abstract base class for spatio-temporal crime mapping.
Subclasses implement fit_grid, which maps the events of a single
time period onto the grid. transform takes care of splitting the
events into periods, filling periods with no events and assembling the
result into a series indexed by (t, places).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tfreq
|
str
|
Time frequency: |
required |
grid
|
GeoDataFrame
|
Spatial grid with |
required |
start_time
|
str or datetime
|
Force the series to start at this time (periods without events are filled with zeros). |
None
|
end_time
|
str or datetime
|
Force the series to end at this time. |
None
|
Source code in src/predspot/crime_mapping.py
fit_grid
abstractmethod
¶
Map the events of one time period onto the grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_points
|
GeoDataFrame
|
Events of a single period. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/predspot/crime_mapping.py
fit
¶
transform
¶
Build the spatio-temporal series from crime events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_points
|
GeoDataFrame
|
Events with a |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Values named |
Source code in src/predspot/crime_mapping.py
KDE
¶
Bases: SpatioTemporalMapping
Kernel density estimation of crime events evaluated on grid points.
For every time period a Gaussian KDE is fitted to the event coordinates
and evaluated at the grid points (lon/lat columns of the grid).
Periods with fewer than 3 events get a density of zero everywhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tfreq
|
str
|
Time frequency ( |
required |
grid
|
GeoDataFrame
|
Point grid, see
|
required |
start_time
|
str or datetime
|
None
|
|
end_time
|
str or datetime
|
None
|
|
bandwidth
|
str or float
|
|
'silverman'
|
Source code in src/predspot/crime_mapping.py
fit_grid
¶
Fit the KDE to the events of one period and evaluate it on the grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_points
|
GeoDataFrame
|
Events of a single period. |
required |
as_df
|
bool
|
If True return a DataFrame instead of a dict. |
False
|
Returns:
| Type | Description |
|---|---|
dict or DataFrame
|
Density at each grid point. |
Source code in src/predspot/crime_mapping.py
QuadratCount
¶
Bases: SpatioTemporalMapping
Count of crime events per grid cell (quadrat count).
An alternative to KDE that works on polygonal grids (hexagons or
squares, see create_gridhexagonal and
create_gridsquares):
the value of a cell in a period is the number of events that fall in it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tfreq
|
str
|
Time frequency ( |
required |
grid
|
GeoDataFrame
|
Polygonal grid. |
required |
start_time
|
str or datetime
|
None
|
|
end_time
|
str or datetime
|
None
|
Source code in src/predspot/crime_mapping.py
fit_grid
¶
Count the events of one period in each cell of the grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_points
|
GeoDataFrame
|
Events of a single period. |
required |
as_df
|
bool
|
If True return a DataFrame instead of a dict. |
False
|
Returns:
| Type | Description |
|---|---|
dict or DataFrame
|
Number of events per cell. |
Source code in src/predspot/crime_mapping.py
normalize_tfreq
¶
Translate a user-facing time frequency into a pandas offset alias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tfreq
|
str
|
One of |
required |
Returns:
| Type | Description |
|---|---|
str
|
The pandas offset alias ( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/predspot/crime_mapping.py
tfreq_offset
¶
Return the pandas.DateOffset that advances one period of tfreq.
Source code in src/predspot/crime_mapping.py
load_study_area
¶
Fetch the boundary polygon of a place from OpenStreetMap.
Uses osmnx <https://osmnx.readthedocs.io>_ to geocode place with Nominatim and
return its administrative boundary, ready to be used as the
study_area of Dataset or as the bbox of the
create_grid* functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
place
|
str or list
|
Name of the place as Nominatim understands it,
e.g. |
required |
crs
|
str or CRS
|
CRS of the returned GeoDataFrame (default WGS84). |
WGS84
|
which_result
|
int
|
Forwarded to
|
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
One row per place with |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
ValueError
|
If Nominatim returns a point instead of a boundary polygon for the query. |
Source code in src/predspot/crime_mapping.py
get_city_shape
¶
Fetch the shape (polygon) of a city or region from OpenStreetMap.
A thin wrapper around osmnx.geocode_to_gdf that returns the raw
Nominatim result. Prefer load_study_area
when you want the result validated (polygon geometry, tidy columns).
Uses osmnx, installed with Predspot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
place_query
|
str
|
Name of the place, in a format Nominatim accepts,
e.g. |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
One row (or more, if the query is ambiguous) with the place geometry (Polygon/MultiPolygon) in WGS84. |
Example
city = get_city_shape("Natal, RN, Brazil") city.geometry.iloc[0] # shapely Polygon/MultiPolygon
Source code in src/predspot/crime_mapping.py
create_gridpoints
¶
Create a regular grid of points covering a study area.
This is the grid used by KDE: the density is evaluated at each
point. The grid is built in WGS84 with the requested spacing and then
re-projected to the CRS of bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
GeoDataFrame
|
Study area (any CRS, must be set). |
required |
resolution
|
float
|
Spacing between points, in kilometers. |
required |
return_coords
|
bool
|
If True, also return the full |
False
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Points intersecting |
Source code in src/predspot/crime_mapping.py
create_hexagon
¶
Create a flat-topped hexagonal polygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
side
|
float
|
Length of the hexagon side (circumradius), in the units
of |
required |
x
|
float
|
X-coordinate of the center. |
required |
y
|
float
|
Y-coordinate of the center. |
required |
Returns:
| Type | Description |
|---|---|
Polygon
|
The hexagon. |
Source code in src/predspot/crime_mapping.py
create_gridhexagonal
¶
Create a hexagonal grid covering a study area.
Each hexagon has the same area as a square of side resolution km, so
hexagonal and square grids of the same resolution are comparable. The
grid is built in WGS84 and re-projected to the CRS of bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
GeoDataFrame
|
Study area (any CRS, must be set). |
required |
resolution
|
float
|
Equivalent square side, in kilometers. |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Hexagons intersecting |
Source code in src/predspot/crime_mapping.py
create_gridsquares
¶
Create a grid of square cells covering a study area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
GeoDataFrame
|
Study area (any CRS, must be set). |
required |
resolution
|
float
|
Side of each square, in kilometers. |
1
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Squares intersecting |