Search in sources :

Example 11 with MapIndex

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

the class CombineSRTMIntoFile method main.

public static void main(String[] args) throws IOException {
    File directoryWithSRTMFiles = new File(args[0]);
    File directoryWithTargetFiles = new File(args[1]);
    String ocbfFile = args[2];
    boolean dryRun = true;
    // mauritius
    String filter = null;
    for (int i = 3; i < args.length; i++) {
        if ("--dry-run".equals(args[i])) {
            dryRun = true;
        } else if (args[i].startsWith("--filter")) {
            filter = args[i].substring("--filter".length());
        }
    }
    OsmandRegions or = new OsmandRegions();
    BinaryMapIndexReader fl = or.prepareFile(ocbfFile);
    Map<String, LinkedList<BinaryMapDataObject>> allCountries = or.cacheAllCountries();
    MapIndex mapIndex = fl.getMapIndexes().get(0);
    int srtm = mapIndex.getRule("region_srtm", "yes");
    int downloadName = mapIndex.getRule("download_name", null);
    int boundary = mapIndex.getRule("osmand_region", "boundary");
    int cnt = 1;
    Set<String> failedCountries = new HashSet<String>();
    for (String fullName : allCountries.keySet()) {
        LinkedList<BinaryMapDataObject> lst = allCountries.get(fullName);
        if (fullName == null || (filter != null && !fullName.contains(filter))) {
            continue;
        }
        BinaryMapDataObject rc = null;
        for (BinaryMapDataObject r : lst) {
            if (!r.containsType(boundary)) {
                rc = r;
                break;
            }
        }
        System.out.println(fullName);
        if (rc != null && rc.containsAdditionalType(srtm)) {
            String dw = rc.getNameByType(downloadName);
            System.out.println("Region " + fullName + " " + cnt++ + " out of " + lst.size());
            try {
                process(rc, lst.subList(1, lst.size()), dw, directoryWithSRTMFiles, directoryWithTargetFiles, dryRun);
            } catch (Exception e) {
                failedCountries.add(fullName);
                e.printStackTrace();
            }
        }
    }
    if (!failedCountries.isEmpty()) {
        throw new IllegalStateException("Failed countries " + failedCountries);
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) LinkedList(java.util.LinkedList) SQLException(java.sql.SQLException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) OsmandRegions(net.osmand.map.OsmandRegions) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) File(java.io.File) HashSet(java.util.HashSet)

Example 12 with MapIndex

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

the class ObfFileInMemory method readBinaryMapData.

private TLongObjectHashMap<BinaryMapDataObject> readBinaryMapData(BinaryMapIndexReader index, MapIndex mi, int zoom) throws IOException {
    final TLongObjectHashMap<BinaryMapDataObject> result = new TLongObjectHashMap<>();
    final SearchRequest<BinaryMapDataObject> req = BinaryMapIndexReader.buildSearchRequest(MapUtils.get31TileNumberX(lonleft), MapUtils.get31TileNumberX(lonright), MapUtils.get31TileNumberY(lattop), MapUtils.get31TileNumberY(latbottom), zoom, new SearchFilter() {

        @Override
        public boolean accept(TIntArrayList types, MapIndex index) {
            return true;
        }
    }, new ResultMatcher<BinaryMapDataObject>() {

        @Override
        public boolean publish(BinaryMapDataObject obj) {
            result.put(obj.getId(), obj);
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }
    });
    index.searchMapIndex(req, mi);
    return result;
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) SearchFilter(net.osmand.binary.BinaryMapIndexReader.SearchFilter) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 13 with MapIndex

use of net.osmand.binary.BinaryMapIndexReader.MapIndex 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)

Example 14 with MapIndex

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

the class BinaryMapIndexFilter method process.

private Stat process(final int zoom) throws IOException {
    final Stat stat = new Stat();
    final Map<TagValuePair, Integer> map = new HashMap<TagValuePair, Integer>();
    SearchFilter sf = new SearchFilter() {

        @Override
        public boolean accept(TIntArrayList types, MapIndex index) {
            boolean polygon = false;
            boolean polyline = false;
            for (int j = 0; j < types.size(); j++) {
                int wholeType = types.get(j);
                TagValuePair pair = index.decodeType(wholeType);
                if (pair != null) {
                    int t = wholeType & 3;
                    if (t == RenderingRulesStorage.POINT_RULES) {
                        stat.pointCount++;
                    } else if (t == RenderingRulesStorage.LINE_RULES) {
                        stat.wayCount++;
                        polyline = true;
                    } else {
                        polygon = true;
                        stat.polygonCount++;
                        if (!map.containsKey(pair)) {
                            map.put(pair, 0);
                        }
                        map.put(pair, map.get(pair) + 1);
                    }
                }
            }
            stat.totalCount++;
            return polyline;
        }
    };
    ResultMatcher<BinaryMapDataObject> matcher = new ResultMatcher<BinaryMapDataObject>() {

        TIntHashSet set = new TIntHashSet();

        @Override
        public boolean isCancelled() {
            return false;
        }

        @Override
        public boolean publish(BinaryMapDataObject object) {
            // double area = calculateArea(object, zoom);
            double len = calculateLength(object, zoom);
            if (/*tilesCovers(object, zoom, set) >= 2  && */
            len > 100) {
                stat.polygonBigSize++;
                if (stat.polygonBigSize % 10000 == 0) {
                    return true;
                }
            }
            return false;
        }
    };
    SearchRequest<BinaryMapDataObject> req = BinaryMapIndexReader.buildSearchRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, zoom, sf, matcher);
    List<BinaryMapDataObject> result = reader.searchMapIndex(req);
    ArrayList<TagValuePair> list = new ArrayList<TagValuePair>(map.keySet());
    Collections.sort(list, new Comparator<TagValuePair>() {

        @Override
        public int compare(TagValuePair o1, TagValuePair o2) {
            return -map.get(o1) + map.get(o2);
        }
    });
    for (TagValuePair tp : list) {
        Integer i = map.get(tp);
        if (i > 10) {
        // System.out.println(tp.toString() + " " + i);
        }
    }
    for (BinaryMapDataObject obj : result) {
        System.out.println("id " + (obj.getId() >> 3) + " " + calculateArea(obj, zoom));
    }
    return stat;
}
Also used : HashMap(java.util.HashMap) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) SearchFilter(net.osmand.binary.BinaryMapIndexReader.SearchFilter) ResultMatcher(net.osmand.ResultMatcher) TIntArrayList(gnu.trove.list.array.TIntArrayList) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

Example 15 with MapIndex

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

the class MapRenderRepositories method readRouteDataAsMapObjects.

private void readRouteDataAsMapObjects(SearchRequest<BinaryMapDataObject> sr, BinaryMapIndexReader c, final ArrayList<BinaryMapDataObject> tempResult, final TLongSet ids) {
    final boolean basemap = c.isBasemap();
    try {
        for (RouteRegion reg : c.getRoutingIndexes()) {
            List<RouteSubregion> parent = sr.getZoom() < 15 ? reg.getBaseSubregions() : reg.getSubregions();
            List<RouteSubregion> searchRouteIndexTree = c.searchRouteIndexTree(sr, parent);
            final MapIndex nmi = new MapIndex();
            c.loadRouteIndexData(searchRouteIndexTree, new ResultMatcher<RouteDataObject>() {

                @Override
                public boolean publish(RouteDataObject r) {
                    if (basemap) {
                        renderedState |= 1;
                    } else {
                        renderedState |= 2;
                    }
                    if (checkForDuplicateObjectIds && !basemap) {
                        if (ids.contains(r.getId()) && r.getId() > 0) {
                            // do not add object twice
                            return false;
                        }
                        ids.add(r.getId());
                    }
                    int[] coordinantes = new int[r.getPointsLength() * 2];
                    int[] roTypes = r.getTypes();
                    for (int k = 0; k < roTypes.length; k++) {
                        int type = roTypes[k];
                        registerMissingType(nmi, r, type);
                    }
                    for (int k = 0; k < coordinantes.length / 2; k++) {
                        coordinantes[2 * k] = r.getPoint31XTile(k);
                        coordinantes[2 * k + 1] = r.getPoint31YTile(k);
                    }
                    BinaryMapDataObject mo = new BinaryMapDataObject(r.getId(), coordinantes, new int[0][], RenderingRulesStorage.LINE_RULES, true, roTypes, null);
                    TIntObjectHashMap<String> names = r.getNames();
                    if (names != null) {
                        TIntObjectIterator<String> it = names.iterator();
                        while (it.hasNext()) {
                            it.advance();
                            registerMissingType(nmi, r, it.key());
                            mo.putObjectName(it.key(), it.value());
                        }
                    }
                    mo.setMapIndex(nmi);
                    tempResult.add(mo);
                    return false;
                }

                private void registerMissingType(final MapIndex nmi, RouteDataObject r, int type) {
                    if (!nmi.isRegisteredRule(type)) {
                        RouteTypeRule rr = r.region.quickGetEncodingRule(type);
                        String tag = rr.getTag();
                        int additional = ("highway".equals(tag) || "route".equals(tag) || "railway".equals(tag) || "aeroway".equals(tag) || "aerialway".equals(tag)) ? 0 : 1;
                        nmi.initMapEncodingRule(additional, type, rr.getTag(), rr.getValue());
                    }
                }

                @Override
                public boolean isCancelled() {
                    return !interrupted;
                }
            });
        }
    } catch (IOException e) {
        // $NON-NLS-1$
        log.debug("Search failed " + c.getRegionNames(), e);
    }
}
Also used : RouteSubregion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion) TIntObjectIterator(gnu.trove.iterator.TIntObjectIterator) RouteTypeRule(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule) IOException(java.io.IOException) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) RouteDataObject(net.osmand.binary.RouteDataObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

Aggregations

MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)16 TIntArrayList (gnu.trove.list.array.TIntArrayList)8 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)8 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)6 TransportIndex (net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)6 TLongArrayList (gnu.trove.list.array.TLongArrayList)5 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)5 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)5 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)5 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 CitiesBlock (net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock)3 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)3 RouteSubregion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2