Search in sources :

Example 1 with MapObject

use of net.osmand.data.MapObject in project OsmAnd-tools by osmandapp.

the class BinaryMapIndexWriter method writeAddressNameIndex.

public void writeAddressNameIndex(Map<String, List<MapObject>> namesIndex) throws IOException {
    checkPeekState(ADDRESS_INDEX_INIT);
    codedOutStream.writeTag(OsmAndAddressIndex.NAMEINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
    preserveInt32Size();
    Map<String, BinaryFileReference> res = writeIndexedTable(OsmAndAddressNameIndexData.TABLE_FIELD_NUMBER, namesIndex.keySet());
    for (Entry<String, List<MapObject>> entry : namesIndex.entrySet()) {
        BinaryFileReference ref = res.get(entry.getKey());
        codedOutStream.writeTag(OsmAndAddressNameIndexData.ATOM_FIELD_NUMBER, FieldType.MESSAGE.getWireType());
        codedOutStream.flush();
        long pointer = getFilePointer();
        if (ref != null) {
            ref.writeReference(raf, getFilePointer());
        }
        AddressNameIndexData.Builder builder = AddressNameIndexData.newBuilder();
        // collapse same name ?
        for (MapObject o : entry.getValue()) {
            AddressNameIndexDataAtom.Builder atom = AddressNameIndexDataAtom.newBuilder();
            // this is optional
            // atom.setName(o.getName());
            // if(checkEnNameToWrite(o)){
            // atom.setNameEn(o.getEnName());
            // }
            int type = 1;
            if (o instanceof City) {
                if (((City) o).isPostcode()) {
                    type = 2;
                } else {
                    CityType ct = ((City) o).getType();
                    if (ct != CityType.CITY && ct != CityType.TOWN) {
                        type = 3;
                    }
                }
            } else if (o instanceof Street) {
                type = 4;
            }
            atom.setType(type);
            LatLon ll = o.getLocation();
            int x = (int) MapUtils.getTileNumberX(16, ll.getLongitude());
            int y = (int) MapUtils.getTileNumberY(16, ll.getLatitude());
            atom.addXy16((x << 16) + y);
            atom.addShiftToIndex((int) (pointer - o.getFileOffset()));
            if (o instanceof Street) {
                atom.addShiftToCityIndex((int) (pointer - ((Street) o).getCity().getFileOffset()));
            }
            builder.addAtom(atom.build());
        }
        codedOutStream.writeMessageNoTag(builder.build());
    }
    int len = writeInt32Size();
    log.info("ADDRESS NAME INDEX SIZE : " + len);
}
Also used : CityType(net.osmand.data.City.CityType) AddressNameIndexDataAtom(net.osmand.binary.OsmandOdb.AddressNameIndexDataAtom) ByteString(com.google.protobuf.ByteString) City(net.osmand.data.City) LatLon(net.osmand.data.LatLon) Street(net.osmand.data.Street) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TByteArrayList(gnu.trove.list.array.TByteArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) MapObject(net.osmand.data.MapObject) OsmAndAddressNameIndexData(net.osmand.binary.OsmandOdb.OsmAndAddressNameIndexData) AddressNameIndexData(net.osmand.binary.OsmandOdb.OsmAndAddressNameIndexData.AddressNameIndexData)

Example 2 with MapObject

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

the class RegionAddressRepositoryBinary method fillWithCities.

private List<City> fillWithCities(String name, final ResultMatcher<City> resultMatcher, final List<Integer> typeFilter) throws IOException {
    List<City> result = new ArrayList<City>();
    ResultMatcher<MapObject> matcher = new ResultMatcher<MapObject>() {

        List<City> cache = new ArrayList<City>();

        @Override
        public boolean publish(MapObject o) {
            City c = (City) o;
            City.CityType type = c.getType();
            if (type != null && type.ordinal() >= City.CityType.VILLAGE.ordinal()) {
                if (c.getLocation() != null) {
                    City ct = getClosestCity(c.getLocation(), cache);
                    c.setClosestCity(ct);
                }
            }
            return resultMatcher.publish(c);
        }

        @Override
        public boolean isCancelled() {
            return resultMatcher.isCancelled();
        }
    };
    List<MapObject> foundCities = searchMapObjectsByName(name, matcher, typeFilter);
    for (MapObject o : foundCities) {
        result.add((City) o);
        if (resultMatcher.isCancelled()) {
            return result;
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) City(net.osmand.data.City) ResultMatcher(net.osmand.ResultMatcher) MapObject(net.osmand.data.MapObject)

Example 3 with MapObject

use of net.osmand.data.MapObject 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 4 with MapObject

use of net.osmand.data.MapObject 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 5 with MapObject

use of net.osmand.data.MapObject 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)

Aggregations

ArrayList (java.util.ArrayList)7 MapObject (net.osmand.data.MapObject)7 Street (net.osmand.data.Street)6 List (java.util.List)5 City (net.osmand.data.City)4 TreeMap (java.util.TreeMap)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ResultMatcher (net.osmand.ResultMatcher)2 CityType (net.osmand.data.City.CityType)2 LatLon (net.osmand.data.LatLon)2 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)2 Message (android.os.Message)1 ByteString (com.google.protobuf.ByteString)1 TByteArrayList (gnu.trove.list.array.TByteArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 PreparedStatement (java.sql.PreparedStatement)1 Map (java.util.Map)1