From 5578226647981e622a21e61060f98f91ea338a54 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Tue, 21 Jun 2016 01:24:25 +0200 Subject: [PATCH] Fix merge indexes --- .../src/net/osmand/CollatorStringMatcher.java | 27 ++++++++++++++----- .../net/osmand/binary/BinaryInspector.java | 5 ++-- .../binary/BinaryMapAddressReaderAdapter.java | 4 +-- .../osmand/binary/BinaryMapIndexReader.java | 21 +++++++++------ .../net/osmand/binary/GeocodingUtilities.java | 3 ++- OsmAnd-java/src/net/osmand/data/City.java | 14 ++++++++-- .../RegionAddressRepositoryBinary.java | 4 ++- 7 files changed, 56 insertions(+), 22 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/CollatorStringMatcher.java b/OsmAnd-java/src/net/osmand/CollatorStringMatcher.java index 8ae8e3cf92..96e4f92d44 100644 --- a/OsmAnd-java/src/net/osmand/CollatorStringMatcher.java +++ b/OsmAnd-java/src/net/osmand/CollatorStringMatcher.java @@ -20,6 +20,7 @@ public class CollatorStringMatcher implements StringMatcher { CHECK_ONLY_STARTS_WITH, CHECK_STARTS_FROM_SPACE, CHECK_STARTS_FROM_SPACE_NOT_BEGINNING, + CHECK_EQUALS_FROM_SPACE, CHECK_CONTAINS } @@ -43,12 +44,14 @@ public class CollatorStringMatcher implements StringMatcher { switch (mode) { case CHECK_CONTAINS: return ccontains(collator, base, part); + case CHECK_EQUALS_FROM_SPACE: + return cstartsWith(collator, base, part, true, true, true); case CHECK_STARTS_FROM_SPACE: - return cstartsWith(collator, base, part, true, true); + return cstartsWith(collator, base, part, true, true, false); case CHECK_STARTS_FROM_SPACE_NOT_BEGINNING: - return cstartsWith(collator, base, part, false, true); + return cstartsWith(collator, base, part, false, true, false); case CHECK_ONLY_STARTS_WITH: - return cstartsWith(collator, base, part, true, false); + return cstartsWith(collator, base, part, true, false, false); } return false; } @@ -97,7 +100,7 @@ public class CollatorStringMatcher implements StringMatcher { * @return true if searchIn starts with token */ public static boolean cstartsWith(Collator collator, String searchInParam, String theStart, - boolean checkBeginning, boolean checkSpaces) { + boolean checkBeginning, boolean checkSpaces, boolean equals) { String searchIn = searchInParam.toLowerCase(Locale.getDefault()); int startLength = theStart.length(); int searchInLength = searchIn.length(); @@ -111,14 +114,26 @@ public class CollatorStringMatcher implements StringMatcher { if (checkBeginning) { boolean starts = collator.equals(searchIn.substring(0, startLength), theStart); if (starts) { - return true; + if (equals) { + if (startLength == searchInLength || isSpace(searchIn.charAt(startLength))) { + return true; + } + } else { + return true; + } } } if (checkSpaces) { for (int i = 1; i <= searchInLength - startLength; i++) { if (isSpace(searchIn.charAt(i - 1)) && !isSpace(searchIn.charAt(i))) { if (collator.equals(searchIn.substring(i, i + startLength), theStart)) { - return true; + if(equals) { + if(i + startLength == searchInLength || isSpace(searchIn.charAt(i + startLength))) { + return true; + } + } else { + return true; + } } } } diff --git a/OsmAnd-java/src/net/osmand/binary/BinaryInspector.java b/OsmAnd-java/src/net/osmand/binary/BinaryInspector.java index 9f42bb92d5..ce90e8d951 100644 --- a/OsmAnd-java/src/net/osmand/binary/BinaryInspector.java +++ b/OsmAnd-java/src/net/osmand/binary/BinaryInspector.java @@ -74,10 +74,11 @@ public class BinaryInspector { // "-vmap", "-vmapobjects", // "-vmapcoordinates", // "-vrouting", "-vaddress", "-vcities","-vstreetgroups", - "-vstreets", "-vbuildings", "-vintersections", + "-vstreets", // "-vbuildings", "-vintersections", + "-lang=ru", // "-bbox=12.8145,50.8025,12.9107,50.7365", // "-osm="+System.getProperty("maps.dir")+"/map.obf.osm" - System.getProperty("maps.dir")+"/Map.obf" + System.getProperty("maps.dir")+"/Ukraine_kiev-city_europe.obf" }); } else { in.inspector(args); diff --git a/OsmAnd-java/src/net/osmand/binary/BinaryMapAddressReaderAdapter.java b/OsmAnd-java/src/net/osmand/binary/BinaryMapAddressReaderAdapter.java index d7030016e5..6e9b061e91 100644 --- a/OsmAnd-java/src/net/osmand/binary/BinaryMapAddressReaderAdapter.java +++ b/OsmAnd-java/src/net/osmand/binary/BinaryMapAddressReaderAdapter.java @@ -555,9 +555,9 @@ public class BinaryMapAddressReaderAdapter { public void searchAddressDataByName(AddressRegion reg, SearchRequest req, List typeFilter) throws IOException { TIntArrayList loffsets = new TIntArrayList(); - CollatorStringMatcher stringMatcher = new CollatorStringMatcher(req.nameQuery, StringMatcherMode.CHECK_STARTS_FROM_SPACE); + CollatorStringMatcher stringMatcher = new CollatorStringMatcher(req.nameQuery, req.matcherMode); String postcode = Postcode.normalize(req.nameQuery, map.getCountryName()); - final CityMatcher postcodeMatcher = new DefaultCityMatcher(new CollatorStringMatcher(postcode, StringMatcherMode.CHECK_STARTS_FROM_SPACE)); + final CityMatcher postcodeMatcher = new DefaultCityMatcher(new CollatorStringMatcher(postcode, req.matcherMode)); final CityMatcher cityMatcher = new DefaultCityMatcher(stringMatcher); final CityMatcher cityPostcodeMatcher = new CityMatcher() { @Override diff --git a/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java index f1695317bc..49eb79c5e2 100644 --- a/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java @@ -1455,10 +1455,12 @@ public class BinaryMapIndexReader { } - public static SearchRequest buildAddressByNameRequest(ResultMatcher resultMatcher, String nameRequest) { + public static SearchRequest buildAddressByNameRequest(ResultMatcher resultMatcher, String nameRequest, + StringMatcherMode matcherMode) { SearchRequest request = new SearchRequest(); request.resultMatcher = resultMatcher; request.nameQuery = nameRequest; + request.matcherMode = matcherMode; return request; } @@ -1651,7 +1653,7 @@ public class BinaryMapIndexReader { String nameQuery = null; - + StringMatcherMode matcherMode = StringMatcherMode.CHECK_STARTS_FROM_SPACE; SearchFilter searchFilter = null; SearchPoiTypeFilter poiTypeFilter = null; @@ -2000,8 +2002,9 @@ public class BinaryMapIndexReader { private static boolean testMapSearch = false; private static boolean testAddressSearch = false; + private static boolean testAddressSearchName = true; private static boolean testAddressJustifySearch = false; - private static boolean testPoiSearch = true; + private static boolean testPoiSearch = false; private static boolean testPoiSearchOnPath = false; private static boolean testTransportSearch = false; private static int sleft = MapUtils.get31TileNumberX(6.3); @@ -2015,8 +2018,8 @@ public class BinaryMapIndexReader { } public static void main(String[] args) throws IOException { -// File fl = new File("/Users/victorshcherb/osmand/maps/Synthetic_test_rendering.obf"); - File fl = new File("/Users/victorshcherb/osmand/maps/Netherlands_europe_2.road.obf"); +// File fl = new File(System.getProperty("maps") + /Synthetic_test_rendering.obf"); + File fl = new File(System.getProperty("maps") + "/Netherlands_noord-holland_europe_merge.obf"); RandomAccessFile raf = new RandomAccessFile(fl, "r"); BinaryMapIndexReader reader = new BinaryMapIndexReader(raf, fl); @@ -2026,8 +2029,10 @@ public class BinaryMapIndexReader { if (testMapSearch) { testMapSearch(reader); } - if (testAddressSearch) { + if (testAddressSearchName) { testAddressSearchByName(reader); + } + if (testAddressSearch) { testAddressSearch(reader); } if (testAddressJustifySearch) { @@ -2299,7 +2304,7 @@ public class BinaryMapIndexReader { public boolean isCancelled() { return false; } - }, "Reynaldo"); + }, "P", StringMatcherMode.CHECK_ONLY_STARTS_WITH); reader.searchAddressDataByName(req); } @@ -2334,7 +2339,7 @@ public class BinaryMapIndexReader { public boolean isCancelled() { return false; } - }, streetName); + }, streetName, StringMatcherMode.CHECK_EQUALS_FROM_SPACE); reader.searchAddressDataByName(req); TreeMap resMap = new TreeMap(new Comparator() { diff --git a/OsmAnd-java/src/net/osmand/binary/GeocodingUtilities.java b/OsmAnd-java/src/net/osmand/binary/GeocodingUtilities.java index 649e5e433f..34fef16e45 100644 --- a/OsmAnd-java/src/net/osmand/binary/GeocodingUtilities.java +++ b/OsmAnd-java/src/net/osmand/binary/GeocodingUtilities.java @@ -2,6 +2,7 @@ package net.osmand.binary; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; +import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.binary.BinaryMapIndexReader.SearchRequest; import net.osmand.data.Building; import net.osmand.data.Building.BuildingInterpolation; @@ -276,7 +277,7 @@ public class GeocodingUtilities { public boolean isCancelled() { return result != null && result.isCancelled(); } - }, mainWord); + }, mainWord, StringMatcherMode.CHECK_EQUALS_FROM_SPACE);// TODO search boundaries reader.searchAddressDataByName(req); } diff --git a/OsmAnd-java/src/net/osmand/data/City.java b/OsmAnd-java/src/net/osmand/data/City.java index dff46e1656..f27d7fc5e0 100644 --- a/OsmAnd-java/src/net/osmand/data/City.java +++ b/OsmAnd-java/src/net/osmand/data/City.java @@ -1,7 +1,9 @@ package net.osmand.data; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; public class City extends MapObject { @@ -135,15 +137,23 @@ public class City extends MapObject { this.isin = isin; } - public void mergeWith(City city) { + public Map mergeWith(City city) { + Map m = new LinkedHashMap<>(); for (Street street : city.listOfStreets) { if (listOfStreets.contains(street)) { listOfStreets.get(listOfStreets.indexOf(street)).mergeWith(street); } else { - listOfStreets.add(street); + Street s = new Street(this); + s.copyNames(street); + s.setLocation(street.getLocation().getLatitude(), street.getLocation().getLongitude()); + s.setId(street.getId()); + s.buildings.addAll(street.getBuildings()); + m.put(street, s); + listOfStreets.add(s); } } copyNames(city); + return m; } } diff --git a/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java b/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java index 163edb2b2c..2a75c5a0e7 100644 --- a/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java +++ b/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.TreeMap; import net.osmand.Collator; +import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; @@ -171,7 +172,8 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository { // StringMatcherMode.CHECK_STARTS_FROM_SPACE_NOT_BEGINNING}; public synchronized List searchMapObjectsByName(String name, ResultMatcher resultMatcher, List typeFilter) { - SearchRequest req = BinaryMapIndexReader.buildAddressByNameRequest(resultMatcher, name); + SearchRequest req = BinaryMapIndexReader.buildAddressByNameRequest(resultMatcher, name, + StringMatcherMode.CHECK_STARTS_FROM_SPACE); try { log.debug("file=" + file + "; req=" + req); file.searchAddressDataByName(req, typeFilter);