Search in sources :

Example 1 with IndexPoiCreator

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

the class ObfFileInMemory method writeFile.

public void writeFile(File targetFile, boolean doNotSimplifyObjects) throws IOException, RTreeException, SQLException {
    boolean gzip = targetFile.getName().endsWith(".gz");
    File nonGzip = targetFile;
    if (gzip) {
        nonGzip = new File(targetFile.getParentFile(), targetFile.getName().substring(0, targetFile.getName().length() - 3));
    }
    final RandomAccessFile raf = new RandomAccessFile(nonGzip, "rw");
    // write files
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            raf.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            raf.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            raf.write(b, off, len);
        }
    });
    timestamp = timestamp == 0 ? System.currentTimeMillis() : timestamp;
    int version = IndexConstants.BINARY_MAP_VERSION;
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, timestamp);
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(raf, ous);
    String defName = targetFile.getName().substring(0, targetFile.getName().indexOf('.'));
    if (mapObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteMapIndex(Algorithms.capitalizeFirstLetter(name));
        writer.writeMapEncodingRules(mapIndex.decodingRules);
        Iterator<Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> it = mapObjects.entrySet().iterator();
        while (it.hasNext()) {
            Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> n = it.next();
            writeMapData(writer, n.getKey(), n.getValue(), targetFile, doNotSimplifyObjects);
        }
        writer.endWriteMapIndex();
    }
    if (routeObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteRouteIndex(name);
        writer.writeRouteRawEncodingRules(routeIndex.routeEncodingRules);
        writeRouteData(writer, routeObjects, targetFile);
        writer.endWriteRouteIndex();
    }
    if (poiObjects.size() > 0) {
        String name = "";
        boolean overwriteIds = false;
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
        final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
        File poiFile = new File(targetFile.getParentFile(), IndexCreator.getPoiFileName(name));
        indexPoiCreator.createDatabaseStructure(poiFile);
        for (Map<String, Amenity> mp : poiObjects.valueCollection()) {
            for (Amenity a : mp.values()) {
                indexPoiCreator.insertAmenityIntoPoi(a);
            }
        }
        indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
        indexPoiCreator.commitAndClosePoiFile(System.currentTimeMillis());
        indexPoiCreator.removePoiFile();
    }
    // TODO Write Transport
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
    raf.close();
    if (gzip) {
        nonGzip.setLastModified(timestamp);
        FileInputStream fis = new FileInputStream(nonGzip);
        GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(targetFile));
        Algorithms.streamCopy(fis, gzout);
        fis.close();
        gzout.close();
        nonGzip.delete();
    }
    targetFile.setLastModified(timestamp);
}
Also used : Amenity(net.osmand.data.Amenity) CodedOutputStream(com.google.protobuf.CodedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) IOException(java.io.IOException) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) FileInputStream(java.io.FileInputStream) Entry(java.util.Map.Entry) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexWriter(net.osmand.data.preparation.BinaryMapIndexWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with IndexPoiCreator

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

the class BinaryMerger method combinePoiIndex.

private void combinePoiIndex(String name, BinaryMapIndexWriter writer, long dateCreated, PoiRegion[] poiRegions, BinaryMapIndexReader[] indexes) throws IOException, SQLException {
    final int[] writtenPoiCount = { 0 };
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
    boolean overwriteIds = false;
    final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
    indexPoiCreator.createDatabaseStructure(new File(new File(System.getProperty("user.dir")), IndexCreator.getPoiFileName(name)));
    final Map<Long, List<Amenity>> amenityRelations = new HashMap<Long, List<Amenity>>();
    final TLongHashSet set = new TLongHashSet();
    final long[] generatedRelationId = { -1 };
    for (int i = 0; i < poiRegions.length; i++) {
        BinaryMapIndexReader index = indexes[i];
        final TLongHashSet file = new TLongHashSet();
        log.info("Region: " + extractRegionName(index));
        index.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, new ResultMatcher<Amenity>() {

            @Override
            public boolean publish(Amenity amenity) {
                try {
                    boolean isRelation = amenity.getId() < 0;
                    if (isRelation) {
                        long j = latlon(amenity);
                        List<Amenity> list;
                        if (!amenityRelations.containsKey(j)) {
                            list = new ArrayList<Amenity>(1);
                            amenityRelations.put(j, list);
                        } else {
                            list = amenityRelations.get(j);
                        }
                        boolean unique = true;
                        for (Amenity a : list) {
                            if (a.getType() == amenity.getType() && Algorithms.objectEquals(a.getSubType(), amenity.getSubType())) {
                                unique = false;
                                break;
                            }
                        }
                        if (unique) {
                            amenity.setId(generatedRelationId[0]--);
                            amenityRelations.get(j).add(amenity);
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    } else {
                        if (!set.contains(amenity.getId())) {
                            file.add(amenity.getId());
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    }
                    return false;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public boolean isCancelled() {
                return false;
            }
        }));
        set.addAll(file);
    }
    indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
    indexPoiCreator.commitAndClosePoiFile(dateCreated);
    // REMOVE_POI_DB = false;
    if (REMOVE_POI_DB) {
        indexPoiCreator.removePoiFile();
    }
    log.info("Written " + writtenPoiCount[0] + " POI.");
}
Also used : Amenity(net.osmand.data.Amenity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) TLongHashSet(gnu.trove.set.hash.TLongHashSet) List(java.util.List) ArrayList(java.util.ArrayList) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 Amenity (net.osmand.data.Amenity)2 IndexPoiCreator (net.osmand.data.preparation.IndexPoiCreator)2 MapRenderingTypesEncoder (net.osmand.osm.MapRenderingTypesEncoder)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)1