-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from azavea/feature/ked/add-default-speed-to-…
…analysis-job Add default speed to AnalysisJob model and display on the frontend
- Loading branch information
Showing
18 changed files
with
239 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/angularjs/src/app/leaflet/map-speed-limit-legend.control.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(function() { | ||
|
||
// Custom private Leaflet control implements a simple legend for all map layers | ||
L.Control.SpeedLegend = L.Control.extend({ | ||
initialize: function (options) { | ||
L.Util.setOptions(this, options); | ||
this.speedLimit = options.speedLimit; | ||
if (this.speedLimit <= 25) { | ||
this.stressLevel = 'low' | ||
} else if (this.speedLimit <= 30) { | ||
this.stressLevel = 'med.' | ||
} else { | ||
this.stressLevel = 'high' | ||
} | ||
}, | ||
onAdd: function () { | ||
var div = L.DomUtil.create('div', 'leaflet-control-layers pfb-speed-limit-legend'); | ||
var kphLimit = Math.round(this.speedLimit * 1.609); | ||
div.innerHTML += '<h6>Residential Speed Limit</h6>'; | ||
div.innerHTML += '<div class="speed-limit-inner">' + | ||
'<div class="speed-limit-block"><div class="speed-limit">' + this.speedLimit + '</div><div>mph</div></div>' + | ||
'<div class="speed-limit-block"><div class="speed-limit">' + kphLimit + '</div><div>km/h</div></div>' + | ||
'</div>'; | ||
div.innerHTML += '<div style="margin: auto; background: navy;"><div class="speed-stress">Stress impact: ' + this.stressLevel + '</div></div>'; | ||
return div; | ||
}, | ||
stressLevel: function () { | ||
|
||
} | ||
}); | ||
if (!L.control.speedLegend) { | ||
L.control.speedLegend = function (opts) { | ||
return new L.Control.SpeedLegend(opts); | ||
} | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/django/pfb_analysis/management/commands/load_residential_speed_limit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import csv | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from pfb_analysis.models import AnalysisJob | ||
|
||
|
||
def load_speed_limit(job, csv_filename): | ||
with open(csv_filename, 'r') as csv_file: | ||
reader = csv.DictReader(csv_file) | ||
row = next(reader) | ||
# Take the city value if there is one, or fall back to the state value. | ||
# In both cases check for truthiness, since a missing value will come back as zero | ||
if row.get("city_speed"): | ||
job.default_speed_limit = row["city_speed"] | ||
job.speed_limit_src = AnalysisJob.SpeedLimitSource.CITY | ||
elif row.get("state_speed"): | ||
job.default_speed_limit = row["state_speed"] | ||
job.speed_limit_src = AnalysisJob.SpeedLimitSource.STATE | ||
job.save() | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """ Load 'residential_speed_limit.csv' produced by the analysis into | ||
AnalysisJob.default_speed_limit | ||
Expected CSV format: one line, with state and city FIPS codes and speeds. | ||
The city code and speed can be blank. | ||
state_fips_code,city_fips_code,state_speed,city_speed | ||
24,2404000,30,25 | ||
Saves the city speed limit if present, otherwise the state limit. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('job_uuid', type=str) | ||
parser.add_argument('csv_file', type=str, | ||
help='Absolute path to residential speed limit csv to load') | ||
|
||
def handle(self, *args, **options): | ||
job_uuid = options['job_uuid'] | ||
csv_filename = options['csv_file'] | ||
|
||
try: | ||
job = AnalysisJob.objects.get(pk=job_uuid) | ||
except (AnalysisJob.DoesNotExist): | ||
print('WARNING: Tried to set default_speed_limit for invalid job {} ' | ||
'from file {}'.format(job_uuid, csv_filename)) | ||
raise | ||
load_speed_limit(job, csv_filename) | ||
self.stdout.write('{}: Loaded default_speed_limit from {}'.format(job, csv_filename)) |
23 changes: 23 additions & 0 deletions
23
src/django/pfb_analysis/migrations/0041_auto_20201006_2320.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 2.2.10 on 2020-10-06 23:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('pfb_analysis', '0040_neighborhood_analysis_job_ondelete'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='analysisjob', | ||
name='default_speed_limit', | ||
field=models.PositiveIntegerField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='analysisjob', | ||
name='speed_limit_src', | ||
field=models.CharField(blank=True, choices=[('State', 'State'), ('City', 'City')], max_length=20, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.