Search in sources :

Example 6 with MapRoot

use of net.osmand.binary.BinaryMapIndexReader.MapRoot in project OsmAnd-tools by osmandapp.

the class NativeSwingRendering method updateBoundaries.

private void updateBoundaries(File f, String nm) {
    try {
        BinaryMapIndexReader r = new BinaryMapIndexReader(new RandomAccessFile(f, "r"), f);
        try {
            if (r.getMapIndexes().isEmpty() || r.getMapIndexes().get(0).getRoots().isEmpty()) {
                return;
            }
            MapRoot rt = r.getMapIndexes().get(0).getRoots().get(0);
            if (!diffs.containsKey(nm)) {
                MapDiff mm = new MapDiff();
                mm.baseName = nm;
                diffs.put(nm, mm);
            }
            MapDiff dd = diffs.get(nm);
            dd.baseFile = f;
            dd.timestamp = r.getDateCreated();
            dd.bounds = new QuadRect(MapUtils.get31LongitudeX(rt.getLeft()), MapUtils.get31LatitudeY(rt.getTop()), MapUtils.get31LongitudeX(rt.getRight()), MapUtils.get31LatitudeY(rt.getBottom()));
            Iterator<String> iterator = dd.diffs.keySet().iterator();
            while (iterator.hasNext()) {
                dd.selected = iterator.next();
            }
        } finally {
            r.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) IOException(java.io.IOException) QuadRect(net.osmand.data.QuadRect)

Example 7 with MapRoot

use of net.osmand.binary.BinaryMapIndexReader.MapRoot in project Osmand by osmandapp.

the class BinaryInspector method combineParts.

@SuppressWarnings("unchecked")
public static List<Float> combineParts(File fileToExtract, Map<File, String> partsToExtractFrom) throws IOException {
    BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[partsToExtractFrom.size()];
    RandomAccessFile[] rafs = new RandomAccessFile[partsToExtractFrom.size()];
    LinkedHashSet<Float>[] partsSet = new LinkedHashSet[partsToExtractFrom.size()];
    int c = 0;
    Set<String> addressNames = new LinkedHashSet<String>();
    int version = -1;
    // Go through all files and validate conistency
    for (File f : partsToExtractFrom.keySet()) {
        if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
            System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
            return null;
        }
        rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
        indexes[c] = new BinaryMapIndexReader(rafs[c], f);
        partsSet[c] = new LinkedHashSet<Float>();
        if (version == -1) {
            version = indexes[c].getVersion();
        } else {
            if (indexes[c].getVersion() != version) {
                System.err.println("Error : Different input files has different input versions " + indexes[c].getVersion() + " != " + version);
                return null;
            }
        }
        LinkedHashSet<Float> temp = new LinkedHashSet<Float>();
        String pattern = partsToExtractFrom.get(f);
        boolean minus = true;
        for (int i = 0; i < indexes[c].getIndexes().size(); i++) {
            partsSet[c].add(i + 1f);
            BinaryIndexPart part = indexes[c].getIndexes().get(i);
            if (part instanceof MapIndex) {
                List<MapRoot> roots = ((MapIndex) part).getRoots();
                int rsize = roots.size();
                for (int j = 0; j < rsize; j++) {
                    partsSet[c].add((i + 1f) + (j + 1) / 10f);
                }
            }
        }
        if (pattern != null) {
            minus = pattern.startsWith("-");
            String[] split = pattern.substring(1).split(",");
            for (String s : split) {
                temp.add(Float.valueOf(s));
            }
        }
        Iterator<Float> p = partsSet[c].iterator();
        while (p.hasNext()) {
            Float part = p.next();
            if (minus) {
                if (temp.contains(part)) {
                    p.remove();
                }
            } else {
                if (!temp.contains(part)) {
                    p.remove();
                }
            }
        }
        c++;
    }
    // write files
    FileOutputStream fout = new FileOutputStream(fileToExtract);
    CodedOutputStream ous = CodedOutputStream.newInstance(fout, BUFFER_SIZE);
    List<Float> list = new ArrayList<Float>();
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, System.currentTimeMillis());
    for (int k = 0; k < indexes.length; k++) {
        LinkedHashSet<Float> partSet = partsSet[k];
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs[k];
        for (int i = 0; i < index.getIndexes().size(); i++) {
            if (!partSet.contains(Float.valueOf(i + 1f))) {
                continue;
            }
            list.add(i + 1f);
            BinaryIndexPart part = index.getIndexes().get(i);
            String map;
            if (part instanceof MapIndex) {
                ous.writeTag(OsmandOdb.OsmAndStructure.MAPINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Map";
            } else if (part instanceof AddressRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Address";
                if (addressNames.contains(part.getName())) {
                    System.err.println("Error : going to merge 2 addresses with same names. Skip " + part.getName());
                    continue;
                }
                addressNames.add(part.getName());
            } else if (part instanceof TransportIndex) {
                ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Transport";
            } else if (part instanceof PoiRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "POI";
            } else if (part instanceof RouteRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Routing";
            } else {
                throw new UnsupportedOperationException();
            }
            writeInt(ous, part.getLength());
            copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
            System.out.println(MessageFormat.format("{2} part {0} is extracted {1} bytes", new Object[] { part.getName(), part.getLength(), map }));
        }
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
    fout.close();
    return list;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion) CodedOutputStream(com.google.protobuf.CodedOutputStream) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) RandomAccessFile(java.io.RandomAccessFile) FileOutputStream(java.io.FileOutputStream) MapObject(net.osmand.data.MapObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)7 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)5 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)5 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)5 TransportIndex (net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)5 RandomAccessFile (java.io.RandomAccessFile)4 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)4 File (java.io.File)3 CitiesBlock (net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 TLongArrayList (gnu.trove.list.array.TLongArrayList)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)2 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)2 RouteSubregion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion)2 AddressPart (net.osmand.binary.OsmandIndex.AddressPart)2 CityBlock (net.osmand.binary.OsmandIndex.CityBlock)2