| 1 | /* |
| 2 | * Copyright (C) 2009. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 3 or |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | package uk.me.parabola.imgfmt.app.mdr; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import uk.me.parabola.mkgmap.general.MapPoint; |
| 19 | import uk.me.parabola.imgfmt.app.srt.Sort; |
| 20 | import uk.me.parabola.imgfmt.app.srt.SortKey; |
| 21 | |
| 22 | /** |
| 23 | * A bunch of static routines for use in creating the MDR file. |
| 24 | */ |
| 25 | public class MdrUtils { |
| 26 | |
| 27 | public static final int STREET_INDEX_PREFIX_LEN = 4; |
| 28 | public static final int POI_INDEX_PREFIX_LEN = 4; |
| 29 | public static final int MAX_GROUP = 13; |
| 30 | |
| 31 | private MdrUtils () { |
| 32 | // private constructor to hide the implicit public one. |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the group number for the poi. This is the first byte of the records |
| 37 | * in mdr9. |
| 38 | * |
| 39 | * Not entirely sure about how this works yet. |
| 40 | * @param fullType The primary type of the object. |
| 41 | * @return The group number. This is a number between 1 and MAX_GROUP, later |
| 42 | * might be as high as 0x40, so do not assume there are no gaps. |
| 43 | * |
| 44 | * Group Type Filed under |
| 45 | * 1 Cities (actual range defined by isCityType) |
| 46 | * 2 0x2a Food and Drink |
| 47 | * 3 0x2b Lodgings |
| 48 | * 4 0x2c Attractions/Recreation/Community |
| 49 | * 5 0x2d Entertainment/Recreation |
| 50 | * 6 0x2e Shopping |
| 51 | * 7 0x2f Auto/Transport/Community/Other |
| 52 | * 8 0x30 Civic |
| 53 | * 9 0x28 Island. Reason for having this is no longer known |
| 54 | * 10 unused |
| 55 | * 11 0x64 Geographic > Manmade Places |
| 56 | * 12 0x65 Geographic > Water Features |
| 57 | * 13 0x66 Geographic > Land Features |
| 58 | * |
| 59 | * display MdrCheck.java:toType() needs to be in-step with this |
| 60 | */ |
| 61 | public static int getGroupForPoi(int fullType) { |
| 62 | // We group pois based on their type. This may not be the final thoughts on this. |
| 63 | int type = getTypeFromFullType(fullType); |
| 64 | int group = 0; |
| 65 | if (MapPoint.isCityType(fullType)) |
| 66 | group = 1; |
| 67 | else if (type >= 0x2a && type <= 0x30) |
| 68 | group = type - 0x2a + 2; |
| 69 | else if (type == 0x28) |
| 70 | group = 9; |
| 71 | else if (type >= 0x64 && type <= 0x66) |
| 72 | group = type - 0x64 + 11; |
| 73 | assert group >= 0 && group <= MAX_GROUP : "invalid group " + Integer.toHexString(group); |
| 74 | return group; |
| 75 | } |
| 76 | |
| 77 | public static boolean canBeIndexed(int fullType) { |
| 78 | return getGroupForPoi(fullType) != 0; |
| 79 | } |
| 80 | |
| 81 | public static int getTypeFromFullType(int fullType) { |
| 82 | return (fullType>>8) & 0xfff; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the subtype |
| 87 | * @param fullType The type in the so-called 'full' format. |
| 88 | * @return If there is a subtype, then it is returned, else 0. |
| 89 | */ |
| 90 | public static int getSubtypeFromFullType(int fullType) { |
| 91 | return fullType & 0xff; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sort records that are to be sorted by a name. The appropriate sort order will be used. |
| 96 | * @param sort The sort to be applied. |
| 97 | * @param list The list to be sorted. |
| 98 | * @param <T> One of the Mdr?Record types that need to be sorted on a text field, eg street name. |
| 99 | * @return A list of sort keys in the sorted order. The original object is retrieved from the key |
| 100 | * by calling getObject(). |
| 101 | */ |
| 102 | public static <T extends NamedRecord> List<SortKey<T>> sortList(Sort sort, List<T> list) { |
| 103 | List<SortKey<T>> toSort = new ArrayList<>(list.size()); |
| 104 | for (T m : list) { |
| 105 | SortKey<T> sortKey = sort.createSortKey(m, m.getName(), m.getMapIndex()); |
| 106 | toSort.add(sortKey); |
| 107 | } |
| 108 | toSort.sort(null); |
| 109 | return toSort; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * The 'natural' type is always a combination of the type and subtype with the type |
| 114 | * shifted 5 bits and the sub type in the low 5 bits. |
| 115 | * |
| 116 | * For various reasons, we use 'fullType' in which the type is shifted up a full byte |
| 117 | * or is in the lower byte. |
| 118 | * |
| 119 | * @param ftype The so-called full type of the object. |
| 120 | * @return The natural type as defined above. |
| 121 | */ |
| 122 | public static int fullTypeToNaturalType(int ftype) { |
| 123 | int type = getTypeFromFullType(ftype); |
| 124 | int sub = getSubtypeFromFullType(ftype); |
| 125 | assert sub <= 0x1f: "Subtype doesn't fit into 5 bits: " + uk.me.parabola.mkgmap.reader.osm.GType.formatType(ftype); |
| 126 | return type << 5 | sub; |
| 127 | } |
| 128 | } |