Skip to content

Commit

Permalink
resolving codacy issue
Browse files Browse the repository at this point in the history
  • Loading branch information
davidplummer-pe committed Jul 10, 2020
1 parent c4499e8 commit 1dd2aed
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@
//fetch the tract data for the current state
census.getTractData($scope.currentState)
.then((layerJSON)=>{
if(!layerJSON || layerJSON.error){
alert('The census tract layer could not be loaded: '+layerJSON.error);
return;
}

//add the layer to the map

var mvtSource = new L.TileLayer.MVTSource(layerJSON);

// departmentMap.addLayer(mvtSource,'Census Tract');
Expand All @@ -130,7 +134,7 @@

})
.catch((error)=>{
debugger
alert('The census tract layer could not be loaded: '+error);
})
});
}
Expand Down
127 changes: 70 additions & 57 deletions firecares/firestation/static/firestation/js/services/censusService.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,28 @@
* @param {String} abbreviation
*/
stateAbbreviationToCode(abbreviation) {
for (var x in this.stateCodes) {
if (x == abbreviation) return this.stateCodes[x].id;
}
try{
for (var x in this.stateCodes) {
if (x == abbreviation) return this.stateCodes[x].id;
}
}catch(ex){
console.error(ex);
return null; }
},

/** fetch the census statistical data for the given state
* @param {String} stateAbbreviation
*/
getTractData(stateAbbreviation) {
return new Promise((res, rej) => {

var stateId = this.stateAbbreviationToCode(stateAbbreviation);
if(!stateId){
return res({
'success':false,
'error':'The state ID is invalid.'
})
}
$http({
method: "GET",
url:
Expand All @@ -99,7 +110,7 @@
data.push(e);
}

res(this.generateLayerJSON(data));
res((await this.generateLayerJSON(data)));
} else {
rej();
}
Expand Down Expand Up @@ -156,66 +167,68 @@
* @param {Object} response
*/
generateLayerJSON(response) {
//extract the density values

var values = response
.map(function (county) {
//return Math.random() * 33;
return new Promise((res, rej) => {
//extract the density values

return parseFloat(county.DENSITY);
})
.sort((a, b) => a - b);
var values = response
.map(function (county) {
//return Math.random() * 33;

//define the color scale for the features
var colorScale = chroma.scale(["#cccccc", "#ff0000"]).domain(values);
function getColor(val) {
return colorScale(val).alpha(1).css();
}
return parseFloat(county.DENSITY);
})
.sort((a, b) => a - b);

//generate style expressions for each of the features
var colorJSON = {};
var colors = {};
var GEOIDs = [];
response.forEach((county) => {
var GEOID = county.state + county.county + county.tract;
GEOIDs.push(GEOID);
var value = county.DENSITY;
var color = getColor(value);
if (!colors[color]) {
colors[color] = [];
//define the color scale for the features
var colorScale = chroma.scale(["#cccccc", "#ff0000"]).domain(values);
function getColor(val) {
return colorScale(val).alpha(1).css();
}
colors[color].push(GEOID);
});
var colorExpression = ["match", ["get", "GEOID"]];
Object.entries(colors).forEach(function ([color, GEOIDs]) {
colorExpression.push(GEOIDs, color);
GEOIDs.forEach((item) => {
colorJSON[item] = color;

//generate style expressions for each of the features
var colorJSON = {};
var colors = {};
var GEOIDs = [];
response.forEach((county) => {
var GEOID = county.state + county.county + county.tract;
GEOIDs.push(GEOID);
var value = county.DENSITY;
var color = getColor(value);
if (!colors[color]) {
colors[color] = [];
}
colors[color].push(GEOID);
});
});
colorExpression.push("rgba(0,0,0,0)");
var colorExpression = ["match", ["get", "GEOID"]];
Object.entries(colors).forEach(function ([color, GEOIDs]) {
colorExpression.push(GEOIDs, color);
GEOIDs.forEach((item) => {
colorJSON[item] = color;
});
});
colorExpression.push("rgba(0,0,0,0)");

// return the layer json
return {
url:
"https://gis-server.data.census.gov/arcgis/rest/services/Hosted/VT_2018_140_00_PY_D1/VectorTileServer/tile/{z}/{y}/{x}.pbf",
// return the layer json
res({
url:
"https://gis-server.data.census.gov/arcgis/rest/services/Hosted/VT_2018_140_00_PY_D1/VectorTileServer/tile/{z}/{y}/{x}.pbf",

/** only keep the features with in the selected state */
filter(feature) {
if (GEOIDs.indexOf(feature.properties.GEOID) > -1) return true;
else return false;
},
/** set the color for each feature in the layer */
style(feature) {
return {
color: colorJSON[feature.properties.GEOID],
outline: {
color: "#ffffff",
size: 1,
},
};
},
};
/** only keep the features with in the selected state */
filter(feature) {
if (GEOIDs.indexOf(feature.properties.GEOID) > -1) return true;
else return false;
},
/** set the color for each feature in the layer */
style(feature) {
return {
color: colorJSON[feature.properties.GEOID],
outline: {
color: "#ffffff",
size: 1,
},
};
},
});
});
},
};
}
Expand Down

0 comments on commit 1dd2aed

Please sign in to comment.