Search in sources :

Example 1 with CsvMapReader

use of org.supercsv.io.CsvMapReader in project Xponents by OpenSextant.

the class TestXCoord method open.

/**
     * Create a typical CSV writer -- Excel compliant
     *
     * @param file
     * @return
     * @throws IOException
     */
public CsvMapReader open(File file) throws IOException {
    InputStreamReader rdr = FileUtility.getInputStream(file.getAbsolutePath(), "UTF-8");
    CsvMapReader R = new CsvMapReader(rdr, CsvPreference.STANDARD_PREFERENCE);
    return R;
}
Also used : InputStreamReader(java.io.InputStreamReader) CsvMapReader(org.supercsv.io.CsvMapReader)

Example 2 with CsvMapReader

use of org.supercsv.io.CsvMapReader in project Xponents by OpenSextant.

the class TestXCoord method fileTruth.

/**
     * This will accomodate any test file that has at least the following style:
     *
     * FAMILY-XXX COORDINATE TEXT "FAIL"
     *
     * Where the first FAMILY token is
     *
     * @param coordfile
     */
public void fileTruth(File coordfile) {
    xcoord.match_UTM(true);
    xcoord.match_MGRS(true);
    xcoord.match_DD(true);
    xcoord.match_DMS(true);
    xcoord.match_DM(true);
    try {
        //String _file = coordfile.trim();
        String fname = FilenameUtils.getBaseName(coordfile.getName());
        TestXCoordReporter tester = new TestXCoordReporter("./results/xcoord_" + fname + "-rows.csv");
        //
        tester.full_report = false;
        CsvMapReader in = open(coordfile);
        String text = null;
        int linenum = 0;
        String[] columns = in.getHeader(true);
        Map<String, String> testRow = null;
        // id, enumeration, test, true_lat, true_lon, remark
        while ((testRow = in.read(columns)) != null) {
            String patid = testRow.get("id");
            if (patid == null) {
                continue;
            }
            patid = patid.trim();
            if (patid.startsWith("#")) {
                continue;
            }
            if (patid.isEmpty()) {
                continue;
            }
            String fam = find_family(patid);
            int famx = XConstants.get_CCE_family(fam);
            if (famx == XConstants.UNK_PATTERN) {
                log.error("Unknown test pattern TEXT=" + text);
                continue;
            }
            text = testRow.get("enumeration");
            linenum = Integer.parseInt(text);
            text = testRow.get("test");
            text = text.replace("$NL", "\n");
            String rmks = testRow.get("remark");
            // "Patid # rowid" == test instance id
            // DMS07#12  -- 12th example of DMS07 test.
            //
            GeocoordTestCase tst = new GeocoordTestCase(patid + "#" + linenum, fam, text);
            tst.match.setLatitude(testRow.get("true_lat"));
            tst.match.setLongitude(testRow.get("true_lon"));
            tst.setRemarks(rmks);
            TextMatchResult results = xcoord.extract_coordinates(tst.text, tst.id);
            /**
                 * tst.family_id
                 */
            results.add_trace("Test Payload: " + tst.text);
            if (!results.evaluated) {
                continue;
            }
            log.info("=========FILE TEST " + tst.id + " FOUND:" + (results.matches.isEmpty() ? "NOTHING" : results.matches.size()));
            tester.save_result(tst, results);
        }
        tester.close_report();
        log.info("=== FILE TESTS DONE ===");
    } catch (Exception err) {
        log.error("TEST BY LINES", err);
    }
}
Also used : GeocoordTestCase(org.opensextant.extractors.xcoord.GeocoordTestCase) CsvMapReader(org.supercsv.io.CsvMapReader) TextMatchResult(org.opensextant.extractors.flexpat.TextMatchResult) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with CsvMapReader

use of org.supercsv.io.CsvMapReader in project Xponents by OpenSextant.

the class GeonamesUtility method loadFeatureMetaMap.

/**
     * Load "feature-metadata" CSV
     * @throws IOException
     */
private void loadFeatureMetaMap() throws IOException {
    String uri = "/feature-metadata-2013.csv";
    try (Reader featIO = new InputStreamReader(GeonamesUtility.class.getResourceAsStream(uri))) {
        CsvMapReader featreader = new CsvMapReader(featIO, CsvPreference.EXCEL_PREFERENCE);
        String[] columns = featreader.getHeader(true);
        Map<String, String> featMap = null;
        //
        while ((featMap = featreader.read(columns)) != null) {
            String feat_code = featMap.get("Feature Designation Code");
            String desc = featMap.get("Feature Designation Name");
            String feat_class = featMap.get("Feature Class");
            if (feat_code == null) {
                continue;
            }
            if (feat_code.startsWith("#")) {
                continue;
            }
            features.put(String.format("%s/%s", feat_class, feat_code), desc);
        }
        featreader.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) CsvMapReader(org.supercsv.io.CsvMapReader) BufferedReader(java.io.BufferedReader) CsvMapReader(org.supercsv.io.CsvMapReader)

Example 4 with CsvMapReader

use of org.supercsv.io.CsvMapReader in project Xponents by OpenSextant.

the class GeonamesUtility method loadCountryNameMap.

private void loadCountryNameMap() throws IOException {
    java.io.InputStream io = getClass().getResourceAsStream("/country-names-2015.csv");
    java.io.Reader countryIO = new InputStreamReader(io);
    CsvMapReader countryMap = new CsvMapReader(countryIO, CsvPreference.EXCEL_PREFERENCE);
    String[] columns = countryMap.getHeader(true);
    Map<String, String> country_names = null;
    while ((country_names = countryMap.read(columns)) != null) {
        String n = country_names.get("country_name");
        String cc = country_names.get("ISO2_cc");
        String iso3 = country_names.get("ISO3_cc");
        String fips = country_names.get("FIPS_cc");
        //ISO2
        iso2fips.put(cc, fips);
        iso2fips.put(iso3, fips);
        fips2iso.put(fips, cc);
        if (n == null || cc == null) {
            continue;
        }
        cc = cc.toUpperCase(Locale.ENGLISH);
        fips = fips.toUpperCase(Locale.ENGLISH);
        // Unique Name?  E.g., "Georgia" country name is not unique.
        // This flag helps inform Disambiguation choose countries and places.
        boolean isUniq = Boolean.parseBoolean(country_names.get("is_unique_name"));
        boolean isTerr = Boolean.parseBoolean(country_names.get("territory"));
        // FIPS could be *, but as long as we use ISO2, we're fine. if
        // ("*".equals(cc)){ cc = fips.toUpperCase(); }
        // Normalize: "US" =&gt; "united states of america"
        defaultCountryNames.put(cc, n.toLowerCase(Locale.ENGLISH));
        Country C = new Country(cc, n);
        C.CC_FIPS = fips;
        C.CC_ISO2 = cc;
        C.CC_ISO3 = iso3;
        C.setUniqueName(isUniq);
        C.isTerritory = isTerr;
        // ISO
        if (!C.isTerritory) {
            isoCountries.put(cc, C);
            isoCountries.put(iso3, C);
        }
        // FIPS  -- mostly unique.
        if (!fips.equals("*")) {
            fipsCountries.put(fips, C);
        }
        countries.add(C);
    }
    countryMap.close();
    if (defaultCountryNames.isEmpty()) {
        throw new IOException("No data found in country name map");
    }
    // Less standard country/territory codes:
    //
    // Gaza Strip, GAZ is occassionally used.
    isoCountries.put("GAZ", isoCountries.get("PS"));
    // East Timor-Leste; 
    isoCountries.put("TMP", isoCountries.get("TLS"));
    // Missing Country. 
    // Valid value for country code, just means no geo-political affiliation.
    // Note, Undersea Features and International waters are mapped to ZZ. 
    // This overrides those settings.
    Country noCountryAffiliation = new Country("ZZ", "(No Country)");
    noCountryAffiliation.CC_FIPS = "ZZ";
    noCountryAffiliation.CC_ISO3 = "ZZZ";
    isoCountries.put(noCountryAffiliation.CC_ISO2, noCountryAffiliation);
    isoCountries.put(noCountryAffiliation.CC_ISO3, noCountryAffiliation);
    /* Once all country data is known, associate territories appropriately.
         * 
         */
    for (Country C : countries) {
        if (C.isTerritory) {
            Country owningCountry = isoCountries.get(C.getCountryCode());
            if (owningCountry != null) {
                owningCountry.addTerritory(C);
            } else {
                // This territory is unique, and country code does not conflict with anything else.
                // 
                isoCountries.put(C.getCountryCode(), C);
                isoCountries.put(C.CC_ISO3, C);
            }
        }
    }
    /**
         * Important data for many tools where time-of-day or other metadata is meaningful.
         */
    loadCountryTimezones();
}
Also used : InputStreamReader(java.io.InputStreamReader) CsvMapReader(org.supercsv.io.CsvMapReader) Country(org.opensextant.data.Country) Reader(java.io.Reader) IOException(java.io.IOException) InputStream(java.io.InputStream)

Example 5 with CsvMapReader

use of org.supercsv.io.CsvMapReader in project voltdb by VoltDB.

the class Symbols method loadFile.

public void loadFile(String filename) {
    // Schema for CSV file
    final CellProcessor[] processors = new CellProcessor[] { // Symbol
    new UniqueHashCode(), // Name
    new NotNull(), // LastSale
    new NotNull(), // MarketCap
    new NotNull(), // ADR TSO
    new NotNull(), // IPOyear
    new NotNull(), // Sector
    new NotNull(), // Industry
    new NotNull(), // Summary Quote
    new NotNull(), // blank column
    new Optional() };
    ICsvMapReader mapReader = null;
    try {
        mapReader = new CsvMapReader(new FileReader(filename), CsvPreference.STANDARD_PREFERENCE);
        // the header columns are used as the keys to the Map
        final String[] header = mapReader.getHeader(true);
        Map<String, Object> tuple;
        int rowsRead = 0;
        while ((tuple = mapReader.read(header, processors)) != null) {
            Symbol s = new Symbol();
            s.symbol = (String) tuple.get("Symbol");
            String price = (String) tuple.get("LastSale");
            if (price.equals("n/a")) {
                price = "20";
            }
            BigDecimal priceBD = new BigDecimal(price);
            s.price = priceBD.multiply(BD10000).intValue();
            symbols.add(s);
            rowsRead++;
        }
        System.out.printf("Read %d rows from CSV file at: %s\n", rowsRead, filename);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    } finally {
        if (mapReader != null) {
            try {
                mapReader.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : Optional(org.supercsv.cellprocessor.Optional) NotNull(org.supercsv.cellprocessor.constraint.NotNull) BigDecimal(java.math.BigDecimal) UniqueHashCode(org.supercsv.cellprocessor.constraint.UniqueHashCode) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) FileReader(java.io.FileReader) ICsvMapReader(org.supercsv.io.ICsvMapReader)

Aggregations

CsvMapReader (org.supercsv.io.CsvMapReader)17 InputStreamReader (java.io.InputStreamReader)11 IOException (java.io.IOException)8 Reader (java.io.Reader)5 ArrayList (java.util.ArrayList)5 ICsvMapReader (org.supercsv.io.ICsvMapReader)5 HashMap (java.util.HashMap)4 CellProcessor (org.supercsv.cellprocessor.ift.CellProcessor)4 HashSet (java.util.HashSet)3 CsvPreference (org.supercsv.prefs.CsvPreference)3 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 ReusableStringReader (org.apache.apex.malhar.lib.util.ReusableStringReader)2 Country (org.opensextant.data.Country)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 Optional (org.supercsv.cellprocessor.Optional)2 CommentStartsWith (org.supercsv.comment.CommentStartsWith)2 AmazonEC2ClientBuilder (com.amazonaws.services.ec2.AmazonEC2ClientBuilder)1 AwsAccountDetailDto (com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto)1