Search in sources :

Example 6 with MapObject

use of net.osmand.data.MapObject in project OsmAnd-tools by osmandapp.

the class IndexAddressCreator method writeBinaryAddressIndex.

public void writeBinaryAddressIndex(BinaryMapIndexWriter writer, String regionName, IProgress progress) throws IOException, SQLException {
    processPostcodes();
    cleanCityPart();
    streetDAO.close();
    closePreparedStatements(addressCityStat);
    mapConnection.commit();
    createDatabaseIndexes(mapConnection);
    mapConnection.commit();
    Map<String, City> postcodes = new TreeMap<String, City>();
    updatePostcodeBoundaries(progress, postcodes);
    mapConnection.commit();
    List<String> additionalTags = new ArrayList<String>();
    Map<String, Integer> tagRules = new HashMap<String, Integer>();
    int ind = 0;
    for (String lng : langAttributes) {
        additionalTags.add("name:" + lng);
        tagRules.put("name:" + lng, ind);
        ind++;
    }
    writer.startWriteAddressIndex(regionName, additionalTags);
    Map<CityType, List<City>> cities = readCities(mapConnection);
    PreparedStatement streetstat = // 
    mapConnection.prepareStatement(// $NON-NLS-1$
    "SELECT A.id, A.name, A.name_en, A.latitude, A.longitude, " + // $NON-NLS-1$
    "B.id, B.name, B.name_en, B.latitude, B.longitude, B.postcode, A.cityPart, " + " B.name2, B.name_en2, B.lat2, B.lon2, B.interval, B.interpolateType, A.cityPart == C.name as MainTown " + // $NON-NLS-1$
    "FROM street A LEFT JOIN building B ON B.street = A.id JOIN city C ON A.city = C.id " + // $NON-NLS-1$
    "WHERE A.city = ? ORDER BY MainTown DESC, A.name ASC");
    PreparedStatement waynodesStat = // $NON-NLS-1$
    mapConnection.prepareStatement("SELECT A.id, A.latitude, A.longitude FROM street_node A WHERE A.street = ? ");
    // collect suburbs with is in value
    List<City> suburbs = new ArrayList<City>();
    List<City> cityTowns = new ArrayList<City>();
    List<City> villages = new ArrayList<City>();
    for (CityType t : cities.keySet()) {
        if (t == CityType.CITY || t == CityType.TOWN) {
            cityTowns.addAll(cities.get(t));
        } else {
            villages.addAll(cities.get(t));
        }
        if (t == CityType.SUBURB) {
            for (City c : cities.get(t)) {
                if (c.getIsInValue() != null) {
                    suburbs.add(c);
                }
            }
        }
    }
    Map<String, List<MapObject>> namesIndex = new TreeMap<String, List<MapObject>>(Collator.getInstance());
    // $NON-NLS-1$
    progress.startTask(Messages.getString("IndexCreator.SERIALIZING_ADDRESS"), cityTowns.size() + villages.size() / 100 + 1);
    writeCityBlockIndex(writer, CITIES_TYPE, streetstat, waynodesStat, suburbs, cityTowns, postcodes, namesIndex, tagRules, progress);
    writeCityBlockIndex(writer, VILLAGES_TYPE, streetstat, waynodesStat, null, villages, postcodes, namesIndex, tagRules, progress);
    // write postcodes
    List<BinaryFileReference> refs = new ArrayList<BinaryFileReference>();
    writer.startCityBlockIndex(POSTCODES_TYPE);
    ArrayList<City> posts = new ArrayList<City>(postcodes.values());
    for (City s : posts) {
        refs.add(writer.writeCityHeader(s, -1, tagRules));
    }
    for (int i = 0; i < posts.size(); i++) {
        City postCode = posts.get(i);
        BinaryFileReference ref = refs.get(i);
        putNamedMapObject(namesIndex, postCode, ref.getStartPointer());
        ArrayList<Street> streets = new ArrayList<Street>(postCode.getStreets());
        Collections.sort(streets, new Comparator<Street>() {

            final net.osmand.Collator clt = OsmAndCollator.primaryCollator();

            @Override
            public int compare(Street o1, Street o2) {
                return clt.compare(o1.getName(), o2.getName());
            }
        });
        writer.writeCityIndex(postCode, streets, null, ref, tagRules);
    }
    writer.endCityBlockIndex();
    progress.finishTask();
    writer.writeAddressNameIndex(namesIndex);
    writer.endWriteAddressIndex();
    writer.flush();
    streetstat.close();
    if (waynodesStat != null) {
        waynodesStat.close();
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) CityType(net.osmand.data.City.CityType) ArrayList(java.util.ArrayList) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) SimpleStreet(net.osmand.data.preparation.address.DBStreetDAO.SimpleStreet) Street(net.osmand.data.Street) List(java.util.List) ArrayList(java.util.ArrayList) MapObject(net.osmand.data.MapObject) PreparedStatement(java.sql.PreparedStatement) City(net.osmand.data.City) TreeMap(java.util.TreeMap)

Example 7 with MapObject

use of net.osmand.data.MapObject in project OsmAnd-tools by osmandapp.

the class BinaryMerger method combineAddressIndex.

private void combineAddressIndex(String name, BinaryMapIndexWriter writer, AddressRegion[] addressRegions, BinaryMapIndexReader[] indexes) throws IOException {
    Set<String> attributeTagsTableSet = new TreeSet<String>();
    for (int i = 0; i != addressRegions.length; i++) {
        AddressRegion region = addressRegions[i];
        attributeTagsTableSet.addAll(region.getAttributeTagsTable());
    }
    writer.startWriteAddressIndex(name, attributeTagsTableSet);
    List<String> attributeTagsTable = new ArrayList<String>();
    attributeTagsTable.addAll(attributeTagsTableSet);
    Map<String, Integer> tagRules = new HashMap<String, Integer>();
    Map<String, List<MapObject>> namesIndex = new TreeMap<String, List<MapObject>>(Collator.getInstance());
    ListIterator<String> it = attributeTagsTable.listIterator();
    while (it.hasNext()) {
        tagRules.put(it.next(), it.previousIndex());
    }
    for (int type : BinaryMapAddressReaderAdapter.CITY_TYPES) {
        Map<City, BinaryMapIndexReader> cityMap = new HashMap<City, BinaryMapIndexReader>();
        for (int i = 0; i < addressRegions.length; i++) {
            AddressRegion region = addressRegions[i];
            final BinaryMapIndexReader index = indexes[i];
            for (City city : index.getCities(region, null, type)) {
                normalizePostcode(city, extractCountryName(index));
                if (cityMap.containsKey(city)) {
                    cityMap.remove(city);
                }
                cityMap.put(city, index);
            }
        }
        List<City> cities = new ArrayList<City>(cityMap.keySet());
        Map<City, List<City>> mergeCityGroup = new HashMap<City, List<City>>();
        Collections.sort(cities, MapObject.BY_NAME_COMPARATOR);
        mergeCitiesByNameDistance(cities, mergeCityGroup, cityMap, type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
        List<BinaryFileReference> refs = new ArrayList<BinaryFileReference>();
        // 1. write cities
        writer.startCityBlockIndex(type);
        Map<City, Map<Street, List<Node>>> namesakesStreetNodes = new HashMap<City, Map<Street, List<Node>>>();
        for (int i = 0; i < cities.size(); i++) {
            City city = cities.get(i);
            BinaryMapIndexReader rindex = cityMap.get(city);
            preloadStreetsAndBuildings(rindex, city, namesakesStreetNodes);
            List<City> namesakes = mergeCityGroup.get(city);
            if (namesakes != null) {
                for (City namesake : namesakes) {
                    preloadStreetsAndBuildings(cityMap.get(namesake), namesake, namesakesStreetNodes);
                    city = mergeCities(city, namesake, namesakesStreetNodes);
                }
            }
            int cityType = city.isPostcode() ? -1 : city.getType().ordinal();
            BinaryFileReference ref = writer.writeCityHeader(city, cityType, tagRules);
            refs.add(ref);
            writer.writeCityIndex(city, city.getStreets(), namesakesStreetNodes.get(city), ref, tagRules);
            IndexAddressCreator.putNamedMapObject(namesIndex, city, ref.getStartPointer());
            if (!city.isPostcode()) {
                for (Street s : city.getStreets()) {
                    IndexAddressCreator.putNamedMapObject(namesIndex, s, s.getFileOffset());
                }
            }
            city.getStreets().clear();
            namesakesStreetNodes.clear();
        }
        writer.endCityBlockIndex();
    }
    writer.writeAddressNameIndex(namesIndex);
    writer.endWriteAddressIndex();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) TreeSet(java.util.TreeSet) Street(net.osmand.data.Street) List(java.util.List) ArrayList(java.util.ArrayList) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) MapObject(net.osmand.data.MapObject) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) City(net.osmand.data.City) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

ArrayList (java.util.ArrayList)7 MapObject (net.osmand.data.MapObject)7 Street (net.osmand.data.Street)6 List (java.util.List)5 City (net.osmand.data.City)4 TreeMap (java.util.TreeMap)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ResultMatcher (net.osmand.ResultMatcher)2 CityType (net.osmand.data.City.CityType)2 LatLon (net.osmand.data.LatLon)2 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)2 Message (android.os.Message)1 ByteString (com.google.protobuf.ByteString)1 TByteArrayList (gnu.trove.list.array.TByteArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 PreparedStatement (java.sql.PreparedStatement)1 Map (java.util.Map)1