In [1]:
Copied!
import urban_mapper as um
# Initialise UrbanMapper
mapper = um.UrbanMapper()
# Step 1: Create urban layer for intersections
layer = (
mapper.urban_layer
.with_type("streets_intersections")
.from_place("Downtown Brooklyn, New York City, USA", network_type="drive")
.build()
)
import urban_mapper as um
# Initialise UrbanMapper
mapper = um.UrbanMapper()
# Step 1: Create urban layer for intersections
layer = (
mapper.urban_layer
.with_type("streets_intersections")
.from_place("Downtown Brooklyn, New York City, USA", network_type="drive")
.build()
)
In [2]:
Copied!
# Step 2: Load collision data
# Note: For the documentation interactive mode, we only query 5000 records from the dataset. Feel free to remove for a more realistic analysis.
data = (
mapper.loader
.from_huggingface("oscur/NYC_vehicle_collisions", number_of_rows=5000, streaming=True)
.with_columns(longitude_column="LONGITUDE", latitude_column="LATITUDE")
.load()
)
data['LONGITUDE'] = data['LONGITUDE'].astype(float)
data['LATITUDE'] = data['LATITUDE'].astype(float)
# Step 2: Load collision data
# Note: For the documentation interactive mode, we only query 5000 records from the dataset. Feel free to remove for a more realistic analysis.
data = (
mapper.loader
.from_huggingface("oscur/NYC_vehicle_collisions", number_of_rows=5000, streaming=True)
.with_columns(longitude_column="LONGITUDE", latitude_column="LATITUDE")
.load()
)
data['LONGITUDE'] = data['LONGITUDE'].astype(float)
data['LATITUDE'] = data['LATITUDE'].astype(float)
In [3]:
Copied!
# Step 3: Impute missing coordinates
imputer = (
mapper.imputer
.with_type("SimpleGeoImputer")
.on_columns(longitude_column="LONGITUDE", latitude_column="LATITUDE",)
.build()
)
data = imputer.transform(data, layer)
# Step 3: Impute missing coordinates
imputer = (
mapper.imputer
.with_type("SimpleGeoImputer")
.on_columns(longitude_column="LONGITUDE", latitude_column="LATITUDE",)
.build()
)
data = imputer.transform(data, layer)
In [4]:
Copied!
# Step 4: Filter to bounding box
filter_step = mapper.filter.with_type("BoundingBoxFilter").build()
data = filter_step.transform(data, layer)
# Step 4: Filter to bounding box
filter_step = mapper.filter.with_type("BoundingBoxFilter").build()
data = filter_step.transform(data, layer)
In [5]:
Copied!
# Step 5: Map to nearest intersections
_, mapped_data = layer.map_nearest_layer(
data,
longitude_column="LONGITUDE",
latitude_column="LATITUDE",
output_column="nearest_intersection"
)
# Step 5: Map to nearest intersections
_, mapped_data = layer.map_nearest_layer(
data,
longitude_column="LONGITUDE",
latitude_column="LATITUDE",
output_column="nearest_intersection"
)
In [6]:
Copied!
# Step 6: Enrich with collision counts
enricher = (
mapper.enricher
.with_data(group_by="nearest_intersection")
.count_by(output_column="collision_count")
.build()
)
enriched_layer = enricher.enrich(mapped_data, layer)
# Step 6: Enrich with collision counts
enricher = (
mapper.enricher
.with_data(group_by="nearest_intersection")
.count_by(output_column="collision_count")
.build()
)
enriched_layer = enricher.enrich(mapped_data, layer)
In [7]:
Copied!
# Step 7: Visualize interactively
visualiser = (
mapper.visual
.with_type("Interactive")
.with_style({"tiles": "CartoDB dark_matter", "colorbar_text_color": "white"})
.build()
)
fig_interactive = visualiser.render(enriched_layer.get_layer(), columns=["collision_count"])
fig_interactive
# Step 7: Visualize interactively
visualiser = (
mapper.visual
.with_type("Interactive")
.with_style({"tiles": "CartoDB dark_matter", "colorbar_text_color": "white"})
.build()
)
fig_interactive = visualiser.render(enriched_layer.get_layer(), columns=["collision_count"])
fig_interactive
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [8]:
Copied!
# Step 8: Visualize statically
visualiser = mapper.visual.with_type("Static").build()
fig_static = visualiser.render(enriched_layer.get_layer(), columns=["collision_count"])
# Step 8: Visualize statically
visualiser = mapper.visual.with_type("Static").build()
fig_static = visualiser.render(enriched_layer.get_layer(), columns=["collision_count"])