Search in sources :

Example 11 with Street

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

the class BinaryMapAddressReaderAdapter method readCityStreets.

protected void readCityStreets(SearchRequest<Street> resultMatcher, City city, List<String> attributeTagsTable) throws IOException {
    int x = MapUtils.get31TileNumberX(city.getLocation().getLongitude());
    int y = MapUtils.get31TileNumberY(city.getLocation().getLatitude());
    while (true) {
        int t = codedIS.readTag();
        int tag = WireFormat.getTagFieldNumber(t);
        switch(tag) {
            case 0:
                return;
            case OsmandOdb.CityBlockIndex.STREETS_FIELD_NUMBER:
                Street s = new Street(city);
                s.setFileOffset(codedIS.getTotalBytesRead());
                int length = codedIS.readRawVarint32();
                int oldLimit = codedIS.pushLimit(length);
                readStreet(s, null, false, x >> 7, y >> 7, city.isPostcode() ? city.getName() : null, attributeTagsTable);
                if (resultMatcher == null || resultMatcher.publish(s)) {
                    city.registerStreet(s);
                }
                if (resultMatcher != null && resultMatcher.isCancelled()) {
                    codedIS.skipRawBytes(codedIS.getBytesUntilLimit());
                }
                codedIS.popLimit(oldLimit);
                break;
            case OsmandOdb.CityBlockIndex.BUILDINGS_FIELD_NUMBER:
                // buildings for the town are not used now
                skipUnknownField(t);
            default:
                skipUnknownField(t);
                break;
        }
    }
}
Also used : Street(net.osmand.data.Street)

Example 12 with Street

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

the class BinaryMapIndexReader method testAddressJustifySearch.

/**
 * @param reader
 * @throws IOException
 */
/**
 * @param reader
 * @throws IOException
 */
private static void testAddressJustifySearch(BinaryMapIndexReader reader) throws IOException {
    final String streetName = "Logger";
    final double lat = 52.28212d;
    final double lon = 4.86269d;
    // test address index search
    final List<Street> streetsList = new ArrayList<Street>();
    SearchRequest<MapObject> req = buildAddressByNameRequest(new ResultMatcher<MapObject>() {

        @Override
        public boolean publish(MapObject object) {
            if (object instanceof Street && object.getName().equalsIgnoreCase(streetName)) {
                if (MapUtils.getDistance(object.getLocation(), lat, lon) < 20000) {
                    streetsList.add((Street) object);
                    return true;
                }
                return false;
            }
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }
    }, streetName, StringMatcherMode.CHECK_EQUALS_FROM_SPACE);
    reader.searchAddressDataByName(req);
    TreeMap<MapObject, Street> resMap = new TreeMap<MapObject, Street>(new Comparator<MapObject>() {

        @Override
        public int compare(MapObject o1, MapObject o2) {
            LatLon l1 = o1.getLocation();
            LatLon l2 = o2.getLocation();
            if (l1 == null || l2 == null) {
                return l2 == l1 ? 0 : (l1 == null ? -1 : 1);
            }
            return Double.compare(MapUtils.getDistance(l1, lat, lon), MapUtils.getDistance(l2, lat, lon));
        }
    });
    for (Street s : streetsList) {
        resMap.put(s, s);
        reader.preloadBuildings(s, null);
        for (Building b : s.getBuildings()) {
            if (MapUtils.getDistance(b.getLocation(), lat, lon) < 100) {
                resMap.put(b, s);
            }
        }
    }
    for (MapObject e : resMap.keySet()) {
        Street s = resMap.get(e);
        if (e instanceof Building && MapUtils.getDistance(e.getLocation(), lat, lon) < 40) {
            Building b = (Building) e;
            System.out.println(b.getName() + "   " + s);
        } else if (e instanceof Street) {
            System.out.println(s + "   " + ((Street) s).getCity());
        }
    }
}
Also used : Building(net.osmand.data.Building) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) LatLon(net.osmand.data.LatLon) Street(net.osmand.data.Street) MapObject(net.osmand.data.MapObject)

Example 13 with Street

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

the class GeocodingUtilities method justifyReverseGeocodingSearch.

public List<GeocodingResult> justifyReverseGeocodingSearch(final GeocodingResult road, BinaryMapIndexReader reader, double knownMinBuildingDistance, final ResultMatcher<GeocodingResult> result) throws IOException {
    // test address index search
    final List<GeocodingResult> streetsList = new ArrayList<GeocodingResult>();
    final List<String> streetNamesUsed = prepareStreetName(road.streetName, true);
    final List<String> streetNamesPacked = streetNamesUsed.size() == 0 ? prepareStreetName(road.streetName, false) : streetNamesUsed;
    if (streetNamesPacked.size() > 0) {
        log.info("Search street by name " + road.streetName + " " + streetNamesPacked);
        String mainWord = "";
        for (int i = 0; i < streetNamesPacked.size(); i++) {
            String s = streetNamesPacked.get(i);
            if (s.length() > mainWord.length()) {
                mainWord = s;
            }
        }
        SearchRequest<MapObject> req = BinaryMapIndexReader.buildAddressByNameRequest(new ResultMatcher<MapObject>() {

            @Override
            public boolean publish(MapObject object) {
                if (object instanceof Street && prepareStreetName(object.getName(), true).equals(streetNamesUsed)) {
                    double d = MapUtils.getDistance(object.getLocation(), road.searchPoint.getLatitude(), road.searchPoint.getLongitude());
                    // double check to suport old format
                    if (d < DISTANCE_STREET_NAME_PROXIMITY_BY_NAME) {
                        GeocodingResult rs = new GeocodingResult(road);
                        rs.street = (Street) object;
                        // set connection point to sort
                        rs.connectionPoint = rs.street.getLocation();
                        rs.city = rs.street.getCity();
                        streetsList.add(rs);
                        return true;
                    }
                    return false;
                }
                return false;
            }

            @Override
            public boolean isCancelled() {
                return result != null && result.isCancelled();
            }
        }, mainWord, StringMatcherMode.CHECK_EQUALS_FROM_SPACE);
        req.setBBoxRadius(road.getLocation().getLatitude(), road.getLocation().getLongitude(), DISTANCE_STREET_NAME_PROXIMITY_BY_NAME);
        reader.searchAddressDataByName(req);
    }
    final List<GeocodingResult> res = new ArrayList<GeocodingResult>();
    if (streetsList.size() == 0) {
        res.add(road);
    } else {
        Collections.sort(streetsList, DISTANCE_COMPARATOR);
        double streetDistance = 0;
        for (GeocodingResult street : streetsList) {
            if (streetDistance == 0) {
                streetDistance = street.getDistance();
            } else if (street.getDistance() > streetDistance + DISTANCE_STREET_FROM_CLOSEST_WITH_SAME_NAME) {
                continue;
            }
            street.connectionPoint = road.connectionPoint;
            final List<GeocodingResult> streetBuildings = loadStreetBuildings(road, reader, street);
            Collections.sort(streetBuildings, DISTANCE_COMPARATOR);
            if (streetBuildings.size() > 0) {
                Iterator<GeocodingResult> it = streetBuildings.iterator();
                if (knownMinBuildingDistance == 0) {
                    GeocodingResult firstBld = it.next();
                    knownMinBuildingDistance = firstBld.getDistance();
                    res.add(firstBld);
                }
                while (it.hasNext()) {
                    GeocodingResult nextBld = it.next();
                    if (nextBld.getDistance() > knownMinBuildingDistance * THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
                        break;
                    }
                    res.add(nextBld);
                }
            }
            res.add(street);
        }
    }
    Collections.sort(res, DISTANCE_COMPARATOR);
    return res;
}
Also used : ArrayList(java.util.ArrayList) RouteSegmentPoint(net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint) Street(net.osmand.data.Street) MapObject(net.osmand.data.MapObject)

Example 14 with Street

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

the class SearchStreetByNameActivity method filterLoop.

@Override
protected boolean filterLoop(String query, Collection<Street> list) {
    final boolean[] result = { false };
    if (searchWithCity == -1) {
        filter(query, list);
    } else if (searchWithCity == 0) {
        for (Street obj : list) {
            if (namesFilter.isCancelled) {
                break;
            }
            if (filterObject(obj, query)) {
                result[0] = true;
                Message msg = uiHandler.obtainMessage(MESSAGE_ADD_ENTITY, obj);
                msg.sendToTarget();
            }
        }
    } else {
        searchWithCity = 0;
        final List res = region.searchMapObjectsByName(query, new ResultMatcher<MapObject>() {

            @Override
            public boolean publish(MapObject object) {
                if (object instanceof Street) {
                    if (city == null || MapUtils.getDistance(city.getLocation(), object.getLocation()) < 100 * 1000) {
                        result[0] = true;
                        Message msg = uiHandler.obtainMessage(MESSAGE_ADD_ENTITY, object);
                        msg.sendToTarget();
                        return true;
                    }
                }
                return false;
            }

            @Override
            public boolean isCancelled() {
                return namesFilter.isCancelled;
            }
        });
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                finishInitializing(res);
            }
        });
    }
    return result[0];
}
Also used : Message(android.os.Message) Street(net.osmand.data.Street) ArrayList(java.util.ArrayList) List(java.util.List) ResultMatcher(net.osmand.ResultMatcher) MapObject(net.osmand.data.MapObject)

Example 15 with Street

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

the class QuickSearchListItem method getTypeName.

public static String getTypeName(SampleApplication app, SearchResult searchResult) {
    switch(searchResult.objectType) {
        case CITY:
            City city = (City) searchResult.object;
            return getCityTypeStr(app, city.getType());
        case POSTCODE:
            return app.getString("postcode");
        case VILLAGE:
            city = (City) searchResult.object;
            if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) {
                if (searchResult.distRelatedObjectName > 0) {
                    return getCityTypeStr(app, city.getType()) + " • " + SampleFormatter.getFormattedDistance((float) searchResult.distRelatedObjectName, app) + " " + app.getString("shared_string_from") + " " + searchResult.localeRelatedObjectName;
                } else {
                    return getCityTypeStr(app, city.getType()) + ", " + searchResult.localeRelatedObjectName;
                }
            } else {
                return getCityTypeStr(app, city.getType());
            }
        case STREET:
            StringBuilder streetBuilder = new StringBuilder();
            if (searchResult.localeName.endsWith(")")) {
                int i = searchResult.localeName.indexOf('(');
                if (i > 0) {
                    streetBuilder.append(searchResult.localeName.substring(i + 1, searchResult.localeName.length() - 1));
                }
            }
            if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) {
                if (streetBuilder.length() > 0) {
                    streetBuilder.append(", ");
                }
                streetBuilder.append(searchResult.localeRelatedObjectName);
            }
            return streetBuilder.toString();
        case HOUSE:
            if (searchResult.relatedObject != null) {
                Street relatedStreet = (Street) searchResult.relatedObject;
                if (relatedStreet.getCity() != null) {
                    return searchResult.localeRelatedObjectName + ", " + relatedStreet.getCity().getName(searchResult.requiredSearchPhrase.getSettings().getLang(), true);
                } else {
                    return searchResult.localeRelatedObjectName;
                }
            }
            return "";
        case STREET_INTERSECTION:
            Street street = (Street) searchResult.object;
            if (street.getCity() != null) {
                return street.getCity().getName(searchResult.requiredSearchPhrase.getSettings().getLang(), true);
            }
            return "";
        case POI_TYPE:
            String res = "";
            if (searchResult.object instanceof AbstractPoiType) {
                AbstractPoiType abstractPoiType = (AbstractPoiType) searchResult.object;
                if (abstractPoiType instanceof PoiCategory) {
                    res = "";
                } else if (abstractPoiType instanceof PoiFilter) {
                    PoiFilter poiFilter = (PoiFilter) abstractPoiType;
                    res = poiFilter.getPoiCategory() != null ? poiFilter.getPoiCategory().getTranslation() : "";
                } else if (abstractPoiType instanceof PoiType) {
                    PoiType poiType = (PoiType) abstractPoiType;
                    res = poiType.getParentType() != null ? poiType.getParentType().getTranslation() : null;
                    if (res == null) {
                        res = poiType.getCategory() != null ? poiType.getCategory().getTranslation() : null;
                    }
                    if (res == null) {
                        res = "";
                    }
                } else {
                    res = "";
                }
            } else if (searchResult.object instanceof CustomSearchPoiFilter) {
                res = ((CustomSearchPoiFilter) searchResult.object).getName();
            }
            return res;
        case POI:
            Amenity amenity = (Amenity) searchResult.object;
            PoiCategory pc = amenity.getType();
            PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType());
            String typeStr = amenity.getSubType();
            if (pt != null) {
                typeStr = pt.getTranslation();
            } else if (typeStr != null) {
                typeStr = Algorithms.capitalizeFirstLetterAndLowercase(typeStr.replace('_', ' '));
            }
            return typeStr;
        case LOCATION:
            LatLon latLon = searchResult.location;
            if (searchResult.localeRelatedObjectName == null) {
                String locationCountry = app.getRegions().getCountryName(latLon);
                searchResult.localeRelatedObjectName = locationCountry == null ? "" : locationCountry;
            }
            return searchResult.localeRelatedObjectName;
        case REGION:
            BinaryMapIndexReader binaryMapIndexReader = (BinaryMapIndexReader) searchResult.object;
            System.out.println(binaryMapIndexReader.getFile().getAbsolutePath() + " " + binaryMapIndexReader.getCountryName());
            break;
        case UNKNOWN_NAME_FILTER:
            break;
    }
    return searchResult.objectType.name();
}
Also used : Amenity(net.osmand.data.Amenity) PoiFilter(net.osmand.osm.PoiFilter) CustomSearchPoiFilter(net.osmand.search.core.CustomSearchPoiFilter) AbstractPoiType(net.osmand.osm.AbstractPoiType) PoiType(net.osmand.osm.PoiType) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) City(net.osmand.data.City) AbstractPoiType(net.osmand.osm.AbstractPoiType) LatLon(net.osmand.data.LatLon) PoiCategory(net.osmand.osm.PoiCategory) Street(net.osmand.data.Street) CustomSearchPoiFilter(net.osmand.search.core.CustomSearchPoiFilter)

Aggregations

Street (net.osmand.data.Street)29 ArrayList (java.util.ArrayList)16 City (net.osmand.data.City)14 List (java.util.List)11 LatLon (net.osmand.data.LatLon)10 Node (net.osmand.osm.edit.Node)9 Building (net.osmand.data.Building)7 MapObject (net.osmand.data.MapObject)7 LinkedHashMap (java.util.LinkedHashMap)6 TIntArrayList (gnu.trove.list.array.TIntArrayList)5 Amenity (net.osmand.data.Amenity)5 SimpleStreet (net.osmand.data.preparation.address.DBStreetDAO.SimpleStreet)5 TreeMap (java.util.TreeMap)4 ByteString (com.google.protobuf.ByteString)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)3 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)3 Message (android.os.Message)2