Last active
August 29, 2015 14:00
-
-
Save grabbeh/11246914 to your computer and use it in GitHub Desktop.
Trade mark & geojson sorting functions
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
// routes/main.js | |
exports.getRangeOfGeoJsonForAllYears = function(req, res){ | |
helper.getPortfolioSortedByFilingYear(function(obj){ | |
helper.convertYearPortfolioAndSupplementLiveGeoJSON(obj, function(o){ | |
res.json(o); | |
}) | |
}) | |
} | |
//routes/helper.js | |
exports.getPortfolioSortedByFilingYear = function(fn){ | |
var obj = {}; | |
trademark.find({}).lean().exec(function(err, trademarks){ | |
var currentYear = moment().year() + 1; | |
var oldestYear = 1980; | |
for (var i = oldestYear; i < currentYear; i++){ | |
obj[i] = []; | |
filterForOlderThanGivenYear(i, trademarks, function(arr){ | |
obj[i] = arr; | |
}); | |
}; | |
fn(obj); | |
}); | |
} | |
exports.convertYearPortfolioAndSupplementLiveGeoJSON = function(obj, fn){ | |
// empty object to capture end values | |
var target = {}; | |
// call to database to retrieve array of geojson for various countries | |
geoj.find({}).lean().exec(function(err, geojson){ | |
// extract keys of object containing arrays of trade mark objects keyed by year, showing a gradual accumulation | |
// in marks over the years, then iterate over them to create geojson file for each year with trade marks | |
Object.keys(obj).forEach(function(year){ | |
// takes array of marks for a particular year, and returns object of trade marks sorted by country keys | |
convertPortfolio(obj[year], function(revisedportfolio){ | |
// takes geojson and adds in trademarks as a property | |
supplementLiveGeoJson(geojson, revisedportfolio, function(gj){ | |
// console.log logs status for each year of a test country and shows that gj returned | |
// reflects differing values of differing years as intended - however attaching to target gives all years | |
// as the latest geojson rather than the geojson for the particular year. | |
gj.forEach(function(country){ | |
if (country.id === 'RUS') | |
console.log(country.properties.status); | |
}); | |
target[year] = gj; | |
}); | |
}); | |
}); | |
fn(target); | |
}); | |
} | |
function supplementLiveGeoJson(gj, tms, fn){ | |
gj.forEach(function(c){ | |
for (var key in tms) { | |
if (key === c.id) { | |
c.properties.trademarks = tms[key]; | |
} | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered != undefined | |
&& tms[c.id].Pending === undefined | |
&& tms[c.id].Published === undefined){ | |
c.properties.status = "only registered"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered === undefined | |
&& tms[c.id].Pending != undefined | |
&& tms[c.id].Published === undefined){ | |
c.properties.status = "only pending"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered === undefined | |
&& tms[c.id].Pending === undefined | |
&& tms[c.id].Published != undefined){ | |
c.properties.status = "only published"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered != undefined | |
&& tms[c.id].Pending != undefined | |
&& tms[c.id].Published != undefined){ | |
c.properties.status = "registered pending published"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered === undefined | |
&& tms[c.id].Pending != undefined | |
&& tms[c.id].Published != undefined){ | |
c.properties.status = "pending published"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered != undefined | |
&& tms[c.id].Pending != undefined | |
&& tms[c.id].Published === undefined){ | |
c.properties.status = "registered pending"; | |
} | |
if (tms[c.id] != undefined | |
&& tms[c.id].Registered != undefined | |
&& tms[c.id].Pending === undefined | |
&& tms[c.id].Published != undefined){ | |
c.properties.status = "registered published"; | |
} | |
if (tms[c.id] === undefined){ | |
c.properties.status = false; | |
} | |
}); | |
fn(gj); | |
} | |
function convertPortfolio(portfolio, fn){ | |
groupByCountry(portfolio, function(TMsGroupedByCountry){ | |
addEUTrademarks(TMsGroupedByCountry['EU'], TMsGroupedByCountry, function(revisedGroupByCountry){ | |
groupTrademarksByStatus(revisedGroupByCountry, function(splitByStatus){ | |
fn(splitByStatus) | |
}); | |
}); | |
}); | |
} | |
function filterForOlderThanGivenYear(year, trademarks, fn){ | |
var arr = []; | |
trademarks.forEach(function(tm){ | |
if (typeof tm.filingDate.stringDate === "string") { | |
var appYear = moment(tm.filingDate.stringDate).year(); | |
if (appYear <= year){ | |
arr.push(tm); | |
} | |
} | |
}); | |
fn(arr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment