areas.cpp
/*
    Takes json dir and populated the area contaoner with that data which matches the filer.
    if a filter is missing or emtpy (<0,0> for yearsfiler) all data is imporated

  @param is
    The input stream from InputSource

  @param cols
    A map of the enum BethyYw::SourceColumnMapping (see datasets.h) to strings
    that give the column header in the CSV file

  @param areasFilter
    An umodifiable pointer to set of umodifiable strings of areas to import,
    or an empty set if all areas should be imported

  @param measuresFilter
    An umodifiable pointer to set of umodifiable strings of measures to import,
    or an empty set if all measures should be imported

  @param yearsFilter
    An umodifiable pointer to an umodifiable tuple of two unsigned integers,
    where if both values are 0, then all years should be imported, otherwise
    they should be treated as the range of years to be imported (inclusively)

  @return
    void

  @throws 
    std::runtime_error if a parsing error occurs (e.g. due to a malformed file)
    std::out_of_range if there are not enough columns in cols

  @see
    See datasets.h for details of how the variable cols is organised

  @see
    See bethyw.cpp for details of how the variable areasFilter is created

  @example
    InputFile input("data/popu1009.json");
    auto is = input.open();

    auto cols = InputFiles::DATASETS["popden"].COLS;

    auto areasFilter = BethYw::parseAreasArg();
    auto measuresFilter = BethYw::parseMeasuresArg();
    auto yearsFilter = BethYw::parseMeasuresArg();

    Areas data = Areas();
    areas.populateFromWelshStatsJSON(
      is,
      cols,
      &areasFilter,
      &measuresFilter,
      &yearsFilter);
*/
Takes json dir and populated the area contaoner with that data which matches the filer.
if a filter is missing or emtpy (<0,0> for yearsfiler) all data is imporated
Takes json dir and populated the area contaoner with that data which matches the filer . if a filter is missing or emtpy ( <0 0 > for yearsfiler ) all data is imporated
void Areas::populateFromWelshStatsJSON(std::istream &is,
            const BethYw::SourceColumnMapping &cols,
            const StringFilterSet * const areasFilter,
            const StringFilterSet * const measuresFilter,
            const YearFilterTuple * const yearsFilter){
    unsigned int yearStart = std::get<0>(*yearsFilter);
    unsigned int yearEnd = std::get<1>(*yearsFilter);
    json j;
    is >> j;
    for (auto& el : j["value"].items()) {
        auto &data = el.value();
        std::string localAuthorityCode = data[cols.at(BethYw::SourceColumn::AUTH_CODE)];
        if(isFilterEmpty(areasFilter)|| filterContains(areasFilter, localAuthorityCode)){
            if(areas.find(localAuthorityCode) == areas.end()){
                Area temp = Area(localAuthorityCode);
                temp.setName("eng", data[cols.at(BethYw::SourceColumn::AUTH_NAME_ENG)]);
                areas.insert({localAuthorityCode, temp});
            }
            std::string measureCode;
            std::string measureName;
            try{
                measureName = data[cols.at(BethYw::SourceColumn::MEASURE_NAME)];
                measureCode = data[cols.at(BethYw::SourceColumn::MEASURE_CODE)];
            }catch(const std::out_of_range& error){
                measureName = cols.at(BethYw::SourceColumn::SINGLE_MEASURE_NAME);
                measureCode = cols.at(BethYw::SourceColumn::SINGLE_MEASURE_CODE);
            }
            if(isFilterEmpty(measuresFilter) || filterContains(measuresFilter, BethYw::convertToLower(measureCode))){
                double reading;
                try{
                    reading = data[cols.at(BethYw::SourceColumn::VALUE)];
                }catch(const nlohmann::detail::type_error& error){
                    std::string temp = data[cols.at(BethYw::SourceColumn::VALUE)];
                    reading = std::stod(temp);
                }
                Measure measure = Measure(measureCode, measureName);
                unsigned int year = BethYw::validateYear(data[cols.at(BethYw::SourceColumn::YEAR)]);
                if((yearsFilter == nullptr ||(yearStart == 0 && yearEnd == 0)) || (year >= yearStart && year <= yearEnd))
                    measure.setValue(year, reading);
                areas.at(localAuthorityCode).setMeasure(measureCode,measure);
            }
        }
    }
}
void Areas :: populateFromWelshStatsJSON ( std :: istream & is , const BethYw :: SourceColumnMapping & cols , const StringFilterSet * const areasFilter , const StringFilterSet * const measuresFilter , const YearFilterTuple * const yearsFilter ) { unsigned int yearStart = std :: get < 0 > ( * yearsFilter ) ; unsigned int yearEnd = std :: get < 1 > ( * yearsFilter ) ; json j ; is >> j ; for ( auto & el : j [ " " ] . items ( ) ) { auto & data = el . value ( ) ; std :: string localAuthorityCode = data [ cols . at ( BethYw :: SourceColumn :: AUTH_CODE ) ] ; if ( isFilterEmpty ( areasFilter ) || filterContains ( areasFilter , localAuthorityCode ) ) { if ( areas . find ( localAuthorityCode ) == areas . end ( ) ) { Area temp = Area ( localAuthorityCode ) ; temp . setName ( " " , data [ cols . at ( BethYw :: SourceColumn :: AUTH_NAME_ENG ) ] ) ; areas . insert ( { localAuthorityCode , temp } ) ; } std :: string measureCode ; std :: string measureName ; try { measureName = data [ cols . at ( BethYw :: SourceColumn :: MEASURE_NAME ) ] ; measureCode = data [ cols . at ( BethYw :: SourceColumn :: MEASURE_CODE ) ] ; } catch ( const std :: out_of_range & error ) { measureName = cols . at ( BethYw :: SourceColumn :: SINGLE_MEASURE_NAME ) ; measureCode = cols . at ( BethYw :: SourceColumn :: SINGLE_MEASURE_CODE ) ; } if ( isFilterEmpty ( measuresFilter ) || filterContains ( measuresFilter , BethYw :: convertToLower ( measureCode ) ) ) { double reading ; try { reading = data [ cols . at ( BethYw :: SourceColumn :: VALUE ) ] ; } catch ( const nlohmann :: detail :: type_error & error ) { std :: string temp = data [ cols . at ( BethYw :: SourceColumn :: VALUE ) ] ; reading = std :: stod ( temp ) ; } Measure measure = Measure ( measureCode , measureName ) ; unsigned int year = BethYw :: validateYear ( data [ cols . at ( BethYw :: SourceColumn :: YEAR ) ] ) ; if ( ( yearsFilter == nullptr || ( yearStart == 0 && yearEnd == 0 ) ) || ( year >= yearStart && year <= yearEnd ) ) measure . setValue ( year , reading ) ; areas . at ( localAuthorityCode ) . setMeasure ( measureCode , measure ) ; } } } }
Takes json dir and populated the area contaoner with that data which matches the filer.
Takes json dir and populated the area contaoner with that data which matches the filer .
//get years for readability //area in not already store and it in the filter or we are imporating them all /* Here in case a JSON doesn't have a MEASURE_NAME/MEASURE_CODE
             * if they don't it will use SINGE_MEASURE_****. */ //turns the year string into unsigned int and happened to do some small validation