Search in sources :

Example 6 with BinaryFileReference

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

the class ObfFileInMemory method writeRouteData.

private void writeRouteData(BinaryMapIndexWriter writer, TLongObjectHashMap<RouteDataObject> routeObjs, File fileToWrite) throws IOException, RTreeException, SQLException {
    File nonpackRtree = new File(fileToWrite.getParentFile(), "nonpackroute." + fileToWrite.getName() + ".rtree");
    File packRtree = new File(fileToWrite.getParentFile(), "packroute." + fileToWrite.getName() + ".rtree");
    RTree rtree = null;
    try {
        rtree = new RTree(nonpackRtree.getAbsolutePath());
        for (long key : routeObjs.keys()) {
            RouteDataObject obj = routeObjs.get(key);
            int minX = obj.getPoint31XTile(0);
            int maxX = obj.getPoint31XTile(0);
            int maxY = obj.getPoint31YTile(0);
            int minY = obj.getPoint31YTile(0);
            for (int i = 1; i < obj.getPointsLength(); i++) {
                minX = Math.min(minX, obj.getPoint31XTile(i));
                minY = Math.min(minY, obj.getPoint31YTile(i));
                maxX = Math.max(maxX, obj.getPoint31XTile(i));
                maxY = Math.max(maxY, obj.getPoint31YTile(i));
            }
            try {
                rtree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), obj.getId()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        rtree = AbstractIndexPartCreator.packRtreeFile(rtree, nonpackRtree.getAbsolutePath(), packRtree.getAbsolutePath());
        TLongObjectHashMap<BinaryFileReference> treeHeader = new TLongObjectHashMap<BinaryFileReference>();
        long rootIndex = rtree.getFileHdr().getRootIndex();
        rtree.Node root = rtree.getReadNode(rootIndex);
        Rect rootBounds = IndexUploader.calcBounds(root);
        if (rootBounds != null) {
            IndexRouteCreator.writeBinaryRouteTree(root, rootBounds, rtree, writer, treeHeader, false);
            RouteWriteContext wc = new RouteWriteContext(null, treeHeader, null, routeObjs);
            IndexRouteCreator.writeBinaryMapBlock(root, rootBounds, rtree, writer, wc, false);
        }
    } finally {
        if (rtree != null) {
            RandomAccessFile file = rtree.getFileHdr().getFile();
            file.close();
        }
        nonpackRtree.delete();
        packRtree.delete();
        RTree.clearCache();
    }
}
Also used : Rect(rtree.Rect) RouteWriteContext(net.osmand.data.preparation.IndexRouteCreator.RouteWriteContext) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) SQLException(java.sql.SQLException) RTreeException(rtree.RTreeException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RouteDataObject(net.osmand.binary.RouteDataObject) RTree(rtree.RTree) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LeafElement(rtree.LeafElement)

Example 7 with BinaryFileReference

use of net.osmand.data.preparation.BinaryFileReference 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

BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)7 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 File (java.io.File)3 RandomAccessFile (java.io.RandomAccessFile)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)3 City (net.osmand.data.City)3 Street (net.osmand.data.Street)3 LeafElement (rtree.LeafElement)3 RTree (rtree.RTree)3 Rect (rtree.Rect)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 MapObject (net.osmand.data.MapObject)2 SimpleStreet (net.osmand.data.preparation.address.DBStreetDAO.SimpleStreet)2 Node (net.osmand.osm.edit.Node)2