Search in sources :

Example 56 with QuadRect

use of net.osmand.data.QuadRect in project Osmand by osmandapp.

the class OsmandMapLayer method initBoundIntersections.

@NonNull
public QuadTree<QuadRect> initBoundIntersections(RotatedTileBox tileBox) {
    QuadRect bounds = new QuadRect(0, 0, tileBox.getPixWidth(), tileBox.getPixHeight());
    bounds.inset(-bounds.width() / 4, -bounds.height() / 4);
    return new QuadTree<>(bounds, 4, 0.6f);
}
Also used : QuadTree(net.osmand.data.QuadTree) QuadRect(net.osmand.data.QuadRect) NonNull(android.support.annotation.NonNull)

Example 57 with QuadRect

use of net.osmand.data.QuadRect in project Osmand by osmandapp.

the class OsmandMapLayer method findAmenity.

public Amenity findAmenity(OsmandApplication app, long id, List<String> names, LatLon latLon, int radius) {
    QuadRect rect = MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), radius);
    List<Amenity> amenities = app.getResourceManager().searchAmenities(new BinaryMapIndexReader.SearchPoiTypeFilter() {

        @Override
        public boolean accept(PoiCategory type, String subcategory) {
            return true;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    }, rect.top, rect.left, rect.bottom, rect.right, -1, null);
    Amenity res = null;
    for (Amenity amenity : amenities) {
        Long amenityId = amenity.getId() >> 1;
        if (amenityId == id) {
            res = amenity;
            break;
        }
    }
    if (res == null && names != null && names.size() > 0) {
        for (Amenity amenity : amenities) {
            for (String name : names) {
                if (name.equals(amenity.getName())) {
                    res = amenity;
                    break;
                }
            }
            if (res != null) {
                break;
            }
        }
    }
    return res;
}
Also used : Amenity(net.osmand.data.Amenity) PoiCategory(net.osmand.osm.PoiCategory) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) QuadRect(net.osmand.data.QuadRect)

Example 58 with QuadRect

use of net.osmand.data.QuadRect in project Osmand by osmandapp.

the class POIMapLayer method onPrepareBufferImage.

@Override
public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
    Set<PoiUIFilter> selectedPoiFilters = app.getPoiFilters().getSelectedPoiFilters();
    if (!this.filters.equals(selectedPoiFilters)) {
        this.filters = new TreeSet<>(selectedPoiFilters);
        data.clearCache();
    }
    List<Amenity> objects = Collections.emptyList();
    List<Amenity> fullObjects = new ArrayList<>();
    List<LatLon> fullObjectsLatLon = new ArrayList<>();
    List<LatLon> smallObjectsLatLon = new ArrayList<>();
    if (!filters.isEmpty()) {
        if (tileBox.getZoom() >= startZoom) {
            data.queryNewData(tileBox);
            objects = data.getResults();
            if (objects != null) {
                float iconSize = poiBackground.getWidth() * 3 / 2;
                QuadTree<QuadRect> boundIntersections = initBoundIntersections(tileBox);
                for (Amenity o : objects) {
                    float x = tileBox.getPixXFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude());
                    float y = tileBox.getPixYFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude());
                    if (intersects(boundIntersections, x, y, iconSize, iconSize)) {
                        canvas.drawBitmap(poiBackgroundSmall, x - poiBackgroundSmall.getWidth() / 2, y - poiBackgroundSmall.getHeight() / 2, paintIconBackground);
                        smallObjectsLatLon.add(new LatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude()));
                    } else {
                        fullObjects.add(o);
                        fullObjectsLatLon.add(new LatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude()));
                    }
                }
                for (Amenity o : fullObjects) {
                    int x = (int) tileBox.getPixXFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude());
                    int y = (int) tileBox.getPixYFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude());
                    canvas.drawBitmap(poiBackground, x - poiBackground.getWidth() / 2, y - poiBackground.getHeight() / 2, paintIconBackground);
                    String id = null;
                    PoiType st = o.getType().getPoiTypeByKeyName(o.getSubType());
                    if (st != null) {
                        if (RenderingIcons.containsSmallIcon(st.getIconKeyName())) {
                            id = st.getIconKeyName();
                        } else if (RenderingIcons.containsSmallIcon(st.getOsmTag() + "_" + st.getOsmValue())) {
                            id = st.getOsmTag() + "_" + st.getOsmValue();
                        }
                    }
                    if (id != null) {
                        Bitmap bmp = RenderingIcons.getIcon(view.getContext(), id, false);
                        if (bmp != null) {
                            canvas.drawBitmap(bmp, x - bmp.getWidth() / 2, y - bmp.getHeight() / 2, paintIcon);
                        }
                    }
                }
                this.fullObjectsLatLon = fullObjectsLatLon;
                this.smallObjectsLatLon = smallObjectsLatLon;
            }
        }
    }
    mapTextLayer.putData(this, objects);
}
Also used : Amenity(net.osmand.data.Amenity) ArrayList(java.util.ArrayList) PoiType(net.osmand.osm.PoiType) QuadRect(net.osmand.data.QuadRect) Paint(android.graphics.Paint) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter) LatLon(net.osmand.data.LatLon) Bitmap(android.graphics.Bitmap)

Example 59 with QuadRect

use of net.osmand.data.QuadRect in project Osmand by osmandapp.

the class QuickSearchHelper method findAmenity.

public Amenity findAmenity(String name, double lat, double lon, String lang, boolean transliterate) {
    QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
    List<Amenity> amenities = app.getResourceManager().searchAmenities(new SearchPoiTypeFilter() {

        @Override
        public boolean accept(PoiCategory type, String subcategory) {
            return true;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    }, rect.top, rect.left, rect.bottom, rect.right, -1, null);
    MapPoiTypes types = app.getPoiTypes();
    for (Amenity amenity : amenities) {
        String poiSimpleFormat = OsmAndFormatter.getPoiStringWithoutType(amenity, lang, transliterate);
        if (poiSimpleFormat.equals(name)) {
            return amenity;
        }
    }
    for (Amenity amenity : amenities) {
        String amenityName = amenity.getName(lang, transliterate);
        if (Algorithms.isEmpty(amenityName)) {
            AbstractPoiType st = types.getAnyPoiTypeByKey(amenity.getSubType());
            if (st != null) {
                amenityName = st.getTranslation();
            } else {
                amenityName = amenity.getSubType();
            }
        }
        if (name.contains(amenityName)) {
            return amenity;
        }
    }
    return null;
}
Also used : Amenity(net.osmand.data.Amenity) SearchPoiTypeFilter(net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter) PoiCategory(net.osmand.osm.PoiCategory) AbstractPoiType(net.osmand.osm.AbstractPoiType) QuadRect(net.osmand.data.QuadRect) MapPoiTypes(net.osmand.osm.MapPoiTypes)

Example 60 with QuadRect

use of net.osmand.data.QuadRect in project Osmand by osmandapp.

the class HillshadeLayer method indexHillshadeFiles.

private void indexHillshadeFiles(final OsmandApplication app) {
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        private SQLiteDatabase sqliteDb;

        @Override
        protected Void doInBackground(Void... params) {
            File tilesDir = app.getAppPath(IndexConstants.TILES_INDEX_DIR);
            File cacheDir = app.getCacheDir();
            // fix http://stackoverflow.com/questions/26937152/workaround-for-nexus-9-sqlite-file-write-operations-on-external-dirs
            sqliteDb = SQLiteDatabase.openDatabase(new File(cacheDir, HILLSHADE_CACHE).getPath(), null, SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING | SQLiteDatabase.CREATE_IF_NECESSARY);
            if (sqliteDb.getVersion() == 0) {
                sqliteDb.setVersion(1);
                sqliteDb.execSQL("CREATE TABLE TILE_SOURCES(filename varchar2(256), date_modified int, left int, right int, top int, bottom int)");
            }
            Map<String, Long> fileModified = new HashMap<String, Long>();
            Map<String, SQLiteTileSource> rs = readFiles(app, tilesDir, fileModified);
            indexCachedResources(fileModified, rs);
            indexNonCachedResources(fileModified, rs);
            sqliteDb.close();
            resources = rs;
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            app.getResourceManager().reloadTilesFromFS();
        }

        private void indexNonCachedResources(Map<String, Long> fileModified, Map<String, SQLiteTileSource> rs) {
            for (String filename : fileModified.keySet()) {
                try {
                    log.info("Indexing hillshade file " + filename);
                    ContentValues cv = new ContentValues();
                    cv.put("filename", filename);
                    cv.put("date_modified", fileModified.get(filename));
                    SQLiteTileSource ts = rs.get(filename);
                    QuadRect rt = ts.getRectBoundary(ZOOM_BOUNDARY, 1);
                    if (rt != null) {
                        indexedResources.insert(filename, rt);
                        cv.put("left", (int) rt.left);
                        cv.put("right", (int) rt.right);
                        cv.put("top", (int) rt.top);
                        cv.put("bottom", (int) rt.bottom);
                        sqliteDb.insert("TILE_SOURCES", null, cv);
                    }
                } catch (RuntimeException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

        private void indexCachedResources(Map<String, Long> fileModified, Map<String, SQLiteTileSource> rs) {
            Cursor cursor = sqliteDb.rawQuery("SELECT filename, date_modified, left, right, top, bottom FROM TILE_SOURCES", new String[0]);
            if (cursor.moveToFirst()) {
                do {
                    String filename = cursor.getString(0);
                    long lastModified = cursor.getLong(1);
                    Long read = fileModified.get(filename);
                    if (rs.containsKey(filename) && read != null && lastModified == read) {
                        int left = cursor.getInt(2);
                        int right = cursor.getInt(3);
                        int top = cursor.getInt(4);
                        float bottom = cursor.getInt(5);
                        indexedResources.insert(filename, new QuadRect(left, top, right, bottom));
                        fileModified.remove(filename);
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();
        }

        private Map<String, SQLiteTileSource> readFiles(final OsmandApplication app, File tilesDir, Map<String, Long> fileModified) {
            Map<String, SQLiteTileSource> rs = new LinkedHashMap<String, SQLiteTileSource>();
            File[] files = tilesDir.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f != null && f.getName().endsWith(IndexConstants.SQLITE_EXT) && f.getName().toLowerCase().startsWith("hillshade")) {
                        SQLiteTileSource ts = new SQLiteTileSource(app, f, new ArrayList<TileSourceTemplate>());
                        rs.put(f.getName(), ts);
                        fileModified.put(f.getName(), f.lastModified());
                    }
                }
            }
            return rs;
        }
    };
    executeTaskInBackground(task);
}
Also used : ContentValues(android.content.ContentValues) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) OsmandApplication(net.osmand.plus.OsmandApplication) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AsyncTask(android.os.AsyncTask) Cursor(android.database.Cursor) QuadRect(net.osmand.data.QuadRect) SQLiteTileSource(net.osmand.plus.SQLiteTileSource) LinkedHashMap(java.util.LinkedHashMap) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

QuadRect (net.osmand.data.QuadRect)60 Paint (android.graphics.Paint)19 ArrayList (java.util.ArrayList)19 LatLon (net.osmand.data.LatLon)13 Bitmap (android.graphics.Bitmap)8 RotatedTileBox (net.osmand.data.RotatedTileBox)8 WptPt (net.osmand.plus.GPXUtilities.WptPt)8 TIntArrayList (gnu.trove.list.array.TIntArrayList)7 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)6 Amenity (net.osmand.data.Amenity)5 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)4 Point (com.vividsolutions.jts.geom.Point)4 List (java.util.List)4 QuadPointDouble (net.osmand.data.QuadPointDouble)4 TransportStop (net.osmand.data.TransportStop)4 PoiCategory (net.osmand.osm.PoiCategory)4 GPXFile (net.osmand.plus.GPXUtilities.GPXFile)4 Rect (android.graphics.Rect)3 RectF (android.graphics.RectF)3 File (java.io.File)3