Search in sources :

Example 31 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject in project Osmand by osmandapp.

the class OsmandRegions method prepareFile.

public BinaryMapIndexReader prepareFile(String fileName) throws IOException {
    reader = new BinaryMapIndexReader(new RandomAccessFile(fileName, "r"), new File(fileName));
    // final Collator clt = OsmAndCollator.primaryCollator();
    final Map<String, String> parentRelations = new LinkedHashMap<String, String>();
    final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {

        @Override
        public boolean publish(BinaryMapDataObject object) {
            initTypes(object);
            int[] types = object.getTypes();
            for (int i = 0; i < types.length; i++) {
                TagValuePair tp = object.getMapIndex().decodeType(types[i]);
                if ("boundary".equals(tp.value)) {
                    return false;
                }
            }
            WorldRegion rd = initRegionData(parentRelations, object);
            if (rd == null) {
                return false;
            }
            if (rd.regionDownloadName != null) {
                downloadNamesToFullNames.put(rd.regionDownloadName, rd.regionFullName);
            }
            fullNamesToRegionData.put(rd.regionFullName, rd);
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }
    };
    iterateOverAllObjects(resultMatcher);
    // post process download names
    for (Map.Entry<String, String> e : parentRelations.entrySet()) {
        String fullName = e.getKey();
        String parentFullName = e.getValue();
        // String parentParentFulName = parentRelations.get(parentFullName); // could be used for japan/russia
        WorldRegion rd = fullNamesToRegionData.get(fullName);
        WorldRegion parent = fullNamesToRegionData.get(parentFullName);
        if (parent != null && rd != null) {
            parent.addSubregion(rd);
        }
    }
    structureWorldRegions(new ArrayList<WorldRegion>(fullNamesToRegionData.values()));
    return reader;
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ResultMatcher(net.osmand.ResultMatcher) LinkedHashMap(java.util.LinkedHashMap) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 32 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject in project Osmand by osmandapp.

the class OsmandRegions method cacheAllCountries.

public Map<String, LinkedList<BinaryMapDataObject>> cacheAllCountries() throws IOException {
    quadTree = new QuadTree<String>(new QuadRect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE), 8, 0.55f);
    final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {

        // int c = 0;
        @Override
        public boolean publish(BinaryMapDataObject object) {
            if (object.getPointsLength() < 1) {
                return false;
            }
            initTypes(object);
            String nm = mapIndexFields.get(mapIndexFields.downloadNameType, object);
            if (!countriesByDownloadName.containsKey(nm)) {
                LinkedList<BinaryMapDataObject> ls = new LinkedList<BinaryMapDataObject>();
                countriesByDownloadName.put(nm, ls);
                ls.add(object);
            } else {
                countriesByDownloadName.get(nm).add(object);
            }
            int maxx = object.getPoint31XTile(0);
            int maxy = object.getPoint31YTile(0);
            int minx = maxx;
            int miny = maxy;
            for (int i = 1; i < object.getPointsLength(); i++) {
                int x = object.getPoint31XTile(i);
                int y = object.getPoint31YTile(i);
                if (y < miny) {
                    miny = y;
                } else if (y > maxy) {
                    maxy = y;
                }
                if (x < minx) {
                    minx = x;
                } else if (x > maxx) {
                    maxx = x;
                }
            }
            quadTree.insert(nm, new QuadRect(minx, miny, maxx, maxy));
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }
    };
    iterateOverAllObjects(resultMatcher);
    return countriesByDownloadName;
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) ResultMatcher(net.osmand.ResultMatcher) QuadRect(net.osmand.data.QuadRect) LinkedList(java.util.LinkedList)

Example 33 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject in project Osmand by osmandapp.

the class OsmandRegions method findBinaryMapDataObject.

public BinaryMapDataObject findBinaryMapDataObject(LatLon latLon) throws IOException {
    int point31x = MapUtils.get31TileNumberX(latLon.getLongitude());
    int point31y = MapUtils.get31TileNumberY(latLon.getLatitude());
    BinaryMapDataObject res = null;
    List<BinaryMapDataObject> mapDataObjects;
    try {
        mapDataObjects = queryBbox(point31x, point31x, point31y, point31y);
    } catch (IOException e) {
        throw new IOException("Error while calling queryBbox");
    }
    if (mapDataObjects != null) {
        Iterator<BinaryMapDataObject> it = mapDataObjects.iterator();
        while (it.hasNext()) {
            BinaryMapDataObject o = it.next();
            if (o.getTypes() != null) {
                boolean isRegion = true;
                for (int i = 0; i < o.getTypes().length; i++) {
                    TagValuePair tp = o.getMapIndex().decodeType(o.getTypes()[i]);
                    if ("boundary".equals(tp.value)) {
                        isRegion = false;
                        break;
                    }
                }
                WorldRegion downloadRegion = getRegionData(getFullName(o));
                if (!isRegion || downloadRegion == null || !downloadRegion.isRegionMapDownload() || !contain(o, point31x, point31y)) {
                    it.remove();
                }
            }
        }
        double smallestArea = -1;
        for (BinaryMapDataObject o : mapDataObjects) {
            double area = OsmandRegions.getArea(o);
            if (smallestArea == -1) {
                smallestArea = area;
                res = o;
            } else if (area < smallestArea) {
                smallestArea = area;
                res = o;
            }
        }
    }
    return res;
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) IOException(java.io.IOException) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair)

Example 34 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject in project Osmand by osmandapp.

the class OsmandRegions method testCountry.

private static void testCountry(OsmandRegions or, double lat, double lon, String... test) throws IOException {
    long t = System.currentTimeMillis();
    List<BinaryMapDataObject> cs = or.query(MapUtils.get31TileNumberX(lon), MapUtils.get31TileNumberY(lat));
    Set<String> expected = new TreeSet<String>(Arrays.asList(test));
    Set<String> found = new TreeSet<String>();
    for (BinaryMapDataObject b : cs) {
        String nm = b.getNameByType(or.mapIndexFields.nameEnType);
        if (nm == null) {
            nm = b.getName();
        }
        if (or.isDownloadOfType(b, MAP_TYPE)) {
            found.add(nm.toLowerCase());
        }
    }
    if (!found.equals(expected)) {
        throw new IllegalStateException(" Expected " + expected + " but was " + found);
    }
    System.out.println("Found " + expected + " in " + (System.currentTimeMillis() - t) + " ms");
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TreeSet(java.util.TreeSet)

Example 35 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject in project Osmand by osmandapp.

the class RenderingRuleProperty method createAdditionalStringProperty.

public static RenderingRuleProperty createAdditionalStringProperty(String name) {
    return new RenderingRuleProperty(name, STRING_TYPE, true) {

        @Override
        public boolean accept(int ruleValue, int renderingProperty, RenderingRuleSearchRequest req) {
            BinaryMapDataObject obj = req.getObject();
            String val = req.getStorage().getStringValue(ruleValue);
            if (obj == null) {
                int vl = req.getIntPropertyValue(this);
                if (vl == -1) {
                    return false;
                }
                String val2 = req.getStorage().getStringValue(vl);
                return val != null && val.equals(val2);
            }
            int k = val.indexOf('=');
            if (k != -1) {
                String ts = val.substring(0, k);
                String vs = val.substring(k + 1);
                Integer ruleInd = req.getObject().getMapIndex().getRule(ts, vs);
                if (ruleInd != null) {
                    if (req.getObject().containsAdditionalType(ruleInd)) {
                        return true;
                    }
                }
            } else {
                String ts = val;
                int[] additionalTypes = obj.getAdditionalTypes();
                for (int i = 0; i < additionalTypes.length; i++) {
                    TagValuePair vp = obj.getMapIndex().decodeType(additionalTypes[i]);
                    if (vp != null && ts.equals(vp.tag)) {
                        return true;
                    }
                }
            }
            return false;
        }
    };
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair)

Aggregations

BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)41 IOException (java.io.IOException)12 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 WorldRegion (net.osmand.map.WorldRegion)10 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)9 File (java.io.File)9 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)8 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)8 Paint (android.graphics.Paint)7 TIntArrayList (gnu.trove.list.array.TIntArrayList)7 LinkedList (java.util.LinkedList)7 OsmandRegions (net.osmand.map.OsmandRegions)7 HashMap (java.util.HashMap)6 TreeSet (java.util.TreeSet)6 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)6 QuadRect (net.osmand.data.QuadRect)6 TextPaint (android.text.TextPaint)5 RandomAccessFile (java.io.RandomAccessFile)5 Map (java.util.Map)5