Skip to content

Commit

Permalink
update to use geojson for warning points
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Aug 11, 2017
1 parent b550ff4 commit 67cbc45
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
23 changes: 16 additions & 7 deletions tethysapp/streamflow_prediction_tool/controllers_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,15 @@ def generate_warning_points(request):
if return_period == 20:
warning_points_file = os.path.join(path_to_output_files,
forecast_folder,
"return_20_points.txt")
"return_20_points.geojson")
elif return_period == 10:
warning_points_file = os.path.join(path_to_output_files,
forecast_folder,
"return_10_points.txt")
"return_10_points.geojson")
elif return_period == 2:
warning_points_file = os.path.join(path_to_output_files,
forecast_folder,
"return_2_points.txt")
"return_2_points.geojson")
else:
raise InvalidData('Invalid return period.')

Expand All @@ -630,10 +630,19 @@ def generate_warning_points(request):
with open(warning_points_file, 'rb') as infile:
warning_points = json_load(infile)

return JsonResponse({
'success': "Warning Points Sucessfully Returned!",
'warning_points': warning_points,
})
if not isinstance(warning_points, dict):
warning_points = {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:4326'
}
},
'features': warning_points
}

return JsonResponse(warning_points)


@require_GET
Expand Down
60 changes: 20 additions & 40 deletions tethysapp/streamflow_prediction_tool/public/js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ var ERFP_MAP = (function() {
};

//FUNCTION: ol case insensitive get feature property
getCI = function(obj,prop){
getCI = function(obj, prop){
prop = prop.toLowerCase();
for(var key in obj.getProperties()){
if(prop == key.toLowerCase()){
Expand Down Expand Up @@ -657,7 +657,7 @@ var ERFP_MAP = (function() {
style = [];
for (var i=0; i<size; i++) {
style.push(new ol.style.Style({
image: symbols[features[i].get('point_size')]
image: symbols[features[i].getProperties().size]
}));
}
}
Expand Down Expand Up @@ -1224,11 +1224,11 @@ var ERFP_MAP = (function() {
},
})
var xhr2 = xhr.done(function (data) {
var feature_count = data.warning_points.length;
if (feature_count > 0) {
var feature_array = (new ol.format.GeoJSON()).readFeatures(data, {featureProjection: 'EPSG:3857'});
if (feature_array.length > 0) {
$(group_id).parent().removeClass('hidden');
var first_layer = null;
var feature_dict = {}
var feature_dict = {};
watershed_layer_group.getLayers().forEach(function(sublayer, j) {
var peak_date = stringToUTCDate(datetime_string);
peak_date.setUTCDate(peak_date.getUTCDate()+j);
Expand All @@ -1239,36 +1239,16 @@ var ERFP_MAP = (function() {
first_layer = sublayer;
}
});
var feature_array = [];
var geometry, symbol, warning_points_layer;
for (var i = 0; i < feature_count; ++i) {
geometry = new ol.geom.Point(ol.proj.transform([data.warning_points[i].lon,
data.warning_points[i].lat],
'EPSG:4326', m_map_projection));

if (typeof data.warning_points[i].peak_date == "undefined") {
feature_array.push(new ol.Feature({
geometry: geometry,
point_size: data.warning_points[i].size,
}));
} else {
feature_dict[data.warning_points[i].peak_date].push(new ol.Feature({
geometry: geometry,
point_size: data.warning_points[i].size,
}));
}
}
if (feature_array.length > 0) {
first_layer.getSource().getSource().addFeatures(feature_array);
first_layer.setVisible(true);
watershed_layer_group.set("daily_warnings", false);
} else {
watershed_layer_group.getLayers().forEach(function(sublayer, j) {
sublayer.getSource().getSource().addFeatures(feature_dict[sublayer.get('peak_date_str')]);
sublayer.setVisible(true);
});
watershed_layer_group.set("daily_warnings", true);

for (var i = 0; i < feature_array.length; ++i) {
feature_dict[feature_array[i].getProperties().peak_date].push(feature_array[i]);
}

watershed_layer_group.getLayers().forEach(function(sublayer, j) {
sublayer.getSource().getSource().addFeatures(feature_dict[sublayer.get('peak_date_str')]);
sublayer.setVisible(true);
});
watershed_layer_group.set("daily_warnings", true);
m_map.render();
}
})
Expand Down Expand Up @@ -1598,7 +1578,7 @@ var ERFP_MAP = (function() {
//create symbols for warnings
var twenty_symbols = [new ol.style.RegularShape({
points: 3,
radius: 5,
radius: 9,
fill: new ol.style.Fill({
color: 'rgba(128,0,128,0.8)'
}),
Expand All @@ -1608,7 +1588,7 @@ var ERFP_MAP = (function() {
}),
}),new ol.style.RegularShape({
points: 3,
radius: 9,
radius: 12,
fill: new ol.style.Fill({
color: 'rgba(128,0,128,0.3)'
}),
Expand All @@ -1621,7 +1601,7 @@ var ERFP_MAP = (function() {
//symbols
var ten_symbols = [new ol.style.RegularShape({
points: 3,
radius: 5,
radius: 9,
fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.7)'
}),
Expand All @@ -1631,7 +1611,7 @@ var ERFP_MAP = (function() {
}),
}),new ol.style.RegularShape({
points: 3,
radius: 9,
radius: 12,
fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.3)'
}),
Expand All @@ -1644,7 +1624,7 @@ var ERFP_MAP = (function() {
//symbols
var two_symbols = [new ol.style.RegularShape({
points: 3,
radius: 5,
radius: 9,
fill: new ol.style.Fill({
color: 'rgba(255,255,0,0.7)'
}),
Expand All @@ -1654,7 +1634,7 @@ var ERFP_MAP = (function() {
}),
}),new ol.style.RegularShape({
points: 3,
radius: 9,
radius: 12,
fill: new ol.style.Fill({
color: 'rgba(255,255,0,0.3)'
}),
Expand Down

0 comments on commit 67cbc45

Please sign in to comment.