Search in sources :

Example 6 with RecordDefinitionBuilder

use of com.revolsys.record.schema.RecordDefinitionBuilder in project com.revolsys.open by revolsys.

the class CategoryLabelCountMap method writeCounts.

public void writeCounts(final Object target, final String labelTitle, final Iterable<String> categoryNames) {
    final RecordDefinitionBuilder recordDefinitionBuilder = new RecordDefinitionBuilder("Counts");
    recordDefinitionBuilder.addField(labelTitle, DataTypes.STRING, 50);
    final Set<String> allLabels = new TreeSet<>();
    final List<String> matchedCategoryNames = new ArrayList<>();
    for (final String categoryName : categoryNames) {
        final LabelCountMap labelCountMap = this.labelCountMapByCategory.get(categoryName);
        if (labelCountMap != null) {
            matchedCategoryNames.add(categoryName);
            recordDefinitionBuilder.addField(categoryName, DataTypes.LONG, 10);
            final Set<String> labels = labelCountMap.getLabels();
            allLabels.addAll(labels);
        }
    }
    final RecordDefinition recordDefinition = recordDefinitionBuilder.getRecordDefinition();
    try (RecordWriter recordWriter = RecordWriter.newRecordWriter(recordDefinition, target)) {
        final List<Object> row = new ArrayList<>(matchedCategoryNames.size() + 1);
        for (final String label : allLabels) {
            row.clear();
            row.add(label);
            for (final String categoryName : matchedCategoryNames) {
                final Long count = getCount(categoryName, label);
                row.add(count);
            }
            recordWriter.write(row);
        }
    }
}
Also used : RecordDefinitionBuilder(com.revolsys.record.schema.RecordDefinitionBuilder) RecordWriter(com.revolsys.record.io.RecordWriter) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

RecordDefinitionBuilder (com.revolsys.record.schema.RecordDefinitionBuilder)6 RecordDefinition (com.revolsys.record.schema.RecordDefinition)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)3 RecordWriter (com.revolsys.record.io.RecordWriter)2 RecordStoreLayer (com.revolsys.swing.map.layer.record.RecordStoreLayer)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Before (org.junit.Before)1