Search in sources :

Example 1 with SortKey

use of uk.me.parabola.imgfmt.app.srt.SortKey in project mkgmap by openstreetmap.

the class Mdr25 method sortCities.

/**
 * Cities are sorted by country and then by the mdr5 city record number.
 * @param list The complete list of cities from mdr5.
 */
public void sortCities(List<Mdr5Record> list) {
    Sort sort = getConfig().getSort();
    List<SortKey<Mdr5Record>> keys = new ArrayList<SortKey<Mdr5Record>>();
    for (Mdr5Record c : list) {
        SortKey<Mdr5Record> key = sort.createSortKey(c, c.getMdrCountry().getName(), c.getGlobalCityIndex());
        keys.add(key);
    }
    Collections.sort(keys);
    String lastName = null;
    Mdr5Record lastCity = null;
    int record = 0;
    for (SortKey<Mdr5Record> key : keys) {
        Mdr5Record city = key.getObject();
        if (lastCity == null || (!city.getName().equals(lastCity.getName()) || !(city.getRegionName().equals(lastCity.getRegionName())))) {
            record++;
            // Record in the 29 index if there is one for this record
            Mdr14Record mdrCountry = city.getMdrCountry();
            Mdr29Record mdr29 = mdrCountry.getMdr29();
            String name = mdr29.getName();
            assert mdrCountry.getName().equals(name);
            if (!name.equals(lastName)) {
                mdr29.setMdr25(record);
                lastName = name;
            }
            cities.add(city);
            lastCity = city;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Sort(uk.me.parabola.imgfmt.app.srt.Sort) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey)

Example 2 with SortKey

use of uk.me.parabola.imgfmt.app.srt.SortKey in project mkgmap by openstreetmap.

the class Mdr26 method sortMdr28.

public void sortMdr28(List<Mdr28Record> in) {
    Sort sort = getConfig().getSort();
    List<SortKey<Mdr28Record>> sortList = new ArrayList<SortKey<Mdr28Record>>();
    int record = 0;
    for (Mdr28Record mdr28 : in) {
        SortKey<Mdr28Record> key = sort.createSortKey(mdr28, mdr28.getMdr14().getName(), ++record);
        sortList.add(key);
    }
    Collections.sort(sortList);
    addToIndex(sortList);
}
Also used : ArrayList(java.util.ArrayList) Sort(uk.me.parabola.imgfmt.app.srt.Sort) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey)

Example 3 with SortKey

use of uk.me.parabola.imgfmt.app.srt.SortKey in project mkgmap by openstreetmap.

the class Mdr27 method sortCities.

/**
 * Cities are sorted by region and then by the mdr5 city record number.
 * @param list The complete list of cities from mdr5.
 */
public void sortCities(List<Mdr5Record> list) {
    Sort sort = getConfig().getSort();
    List<SortKey<Mdr5Record>> keys = new ArrayList<SortKey<Mdr5Record>>();
    for (Mdr5Record c : list) {
        Mdr13Record mdrRegion = c.getMdrRegion();
        if (mdrRegion != null) {
            SortKey<Mdr5Record> key = sort.createSortKey(c, mdrRegion.getName(), c.getGlobalCityIndex());
            keys.add(key);
        }
    }
    Collections.sort(keys);
    String lastName = null;
    int record = 0;
    for (SortKey<Mdr5Record> key : keys) {
        record++;
        Mdr5Record city = key.getObject();
        Mdr13Record mdrRegion = city.getMdrRegion();
        Mdr28Record mdr28 = mdrRegion.getMdr28();
        String name = mdr28.getName();
        if (!name.equals(lastName)) {
            mdr28.setMdr27(record);
            lastName = name;
        }
        cities.add(city);
    }
}
Also used : ArrayList(java.util.ArrayList) Sort(uk.me.parabola.imgfmt.app.srt.Sort) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey)

Example 4 with SortKey

use of uk.me.parabola.imgfmt.app.srt.SortKey in project mkgmap by openstreetmap.

the class TYPFile method writeLabels.

private void writeLabels(ImgFileWriter in) {
    if (data.getIcons().isEmpty())
        return;
    SectionWriter writer = header.getLabels().makeSectionWriter(in);
    List<SortKey<TypIconSet>> keys = new ArrayList<SortKey<TypIconSet>>();
    Sort sort = data.getSort();
    for (TypIconSet icon : data.getIcons()) {
        String label = icon.getLabel();
        if (label != null) {
            SortKey<TypIconSet> key = sort.createSortKey(icon, label);
            keys.add(key);
        }
    }
    Collections.sort(keys);
    // Offset 0 is reserved to mean no label.
    writer.put((byte) 0);
    for (SortKey<TypIconSet> key : keys) {
        int off = writer.position();
        TypIconSet icon = key.getObject();
        int type = icon.getTypeForFile();
        String label = icon.getLabel();
        if (label != null) {
            CharBuffer cb = CharBuffer.wrap(label);
            CharsetEncoder encoder = data.getEncoder();
            try {
                ByteBuffer buffer = encoder.encode(cb);
                writer.put(buffer);
                // If we succeeded then note offsets for indexes
                strToType.put(off, type);
                typeToStr.put(type, off);
            } catch (CharacterCodingException ignore) {
                String name = encoder.charset().name();
                throw new TypLabelException(name);
            }
            writer.put((byte) 0);
        }
    }
    Utils.closeFile(writer);
}
Also used : SectionWriter(uk.me.parabola.imgfmt.app.SectionWriter) ArrayList(java.util.ArrayList) CharBuffer(java.nio.CharBuffer) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) Sort(uk.me.parabola.imgfmt.app.srt.Sort)

Example 5 with SortKey

use of uk.me.parabola.imgfmt.app.srt.SortKey in project mkgmap by openstreetmap.

the class Mdr5 method calcMdr20SortPos.

/**
 * Calculate a position when sorting by name, region, and country- This is used for MDR20.
 */
private void calcMdr20SortPos() {
    List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
    Sort sort = getConfig().getSort();
    for (Mdr5Record m : allCities) {
        if (m.getName() == null)
            continue;
        // Sort by city name, region name, and country name .
        SortKey<Mdr5Record> sortKey = sort.createSortKey(m, m.getName());
        SortKey<Mdr5Record> regionKey = sort.createSortKey(null, m.getRegionName());
        SortKey<Mdr5Record> countryKey = sort.createSortKey(null, m.getCountryName());
        sortKey = new MultiSortKey<>(sortKey, regionKey, countryKey);
        sortKeys.add(sortKey);
    }
    Collections.sort(sortKeys);
    SortKey<Mdr5Record> lastKey = null;
    int pos = 0;
    for (SortKey<Mdr5Record> key : sortKeys) {
        Mdr5Record c = key.getObject();
        if (lastKey == null || key.compareTo(lastKey) != 0)
            pos++;
        c.setMdr20SortPos(pos);
        lastKey = key;
    }
}
Also used : ArrayList(java.util.ArrayList) Sort(uk.me.parabola.imgfmt.app.srt.Sort) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey) MultiSortKey(uk.me.parabola.imgfmt.app.srt.MultiSortKey)

Aggregations

SortKey (uk.me.parabola.imgfmt.app.srt.SortKey)13 Sort (uk.me.parabola.imgfmt.app.srt.Sort)12 ArrayList (java.util.ArrayList)9 MultiSortKey (uk.me.parabola.imgfmt.app.srt.MultiSortKey)5 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 Collator (java.text.Collator)1 HashMap (java.util.HashMap)1 Label (uk.me.parabola.imgfmt.app.Label)1 SectionWriter (uk.me.parabola.imgfmt.app.SectionWriter)1 City (uk.me.parabola.imgfmt.app.lbl.City)1 DoubleSortKey (uk.me.parabola.imgfmt.app.srt.DoubleSortKey)1 IntegerSortKey (uk.me.parabola.imgfmt.app.srt.IntegerSortKey)1