use of net.osmand.data.MapObject in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method writeBinaryAddressIndex.
public void writeBinaryAddressIndex(BinaryMapIndexWriter writer, String regionName, IProgress progress) throws IOException, SQLException {
processPostcodes();
cleanCityPart();
streetDAO.close();
closePreparedStatements(addressCityStat);
mapConnection.commit();
createDatabaseIndexes(mapConnection);
mapConnection.commit();
Map<String, City> postcodes = new TreeMap<String, City>();
updatePostcodeBoundaries(progress, postcodes);
mapConnection.commit();
List<String> additionalTags = new ArrayList<String>();
Map<String, Integer> tagRules = new HashMap<String, Integer>();
int ind = 0;
for (String lng : langAttributes) {
additionalTags.add("name:" + lng);
tagRules.put("name:" + lng, ind);
ind++;
}
writer.startWriteAddressIndex(regionName, additionalTags);
Map<CityType, List<City>> cities = readCities(mapConnection);
PreparedStatement streetstat = //
mapConnection.prepareStatement(// $NON-NLS-1$
"SELECT A.id, A.name, A.name_en, A.latitude, A.longitude, " + // $NON-NLS-1$
"B.id, B.name, B.name_en, B.latitude, B.longitude, B.postcode, A.cityPart, " + " B.name2, B.name_en2, B.lat2, B.lon2, B.interval, B.interpolateType, A.cityPart == C.name as MainTown " + // $NON-NLS-1$
"FROM street A LEFT JOIN building B ON B.street = A.id JOIN city C ON A.city = C.id " + // $NON-NLS-1$
"WHERE A.city = ? ORDER BY MainTown DESC, A.name ASC");
PreparedStatement waynodesStat = // $NON-NLS-1$
mapConnection.prepareStatement("SELECT A.id, A.latitude, A.longitude FROM street_node A WHERE A.street = ? ");
// collect suburbs with is in value
List<City> suburbs = new ArrayList<City>();
List<City> cityTowns = new ArrayList<City>();
List<City> villages = new ArrayList<City>();
for (CityType t : cities.keySet()) {
if (t == CityType.CITY || t == CityType.TOWN) {
cityTowns.addAll(cities.get(t));
} else {
villages.addAll(cities.get(t));
}
if (t == CityType.SUBURB) {
for (City c : cities.get(t)) {
if (c.getIsInValue() != null) {
suburbs.add(c);
}
}
}
}
Map<String, List<MapObject>> namesIndex = new TreeMap<String, List<MapObject>>(Collator.getInstance());
// $NON-NLS-1$
progress.startTask(Messages.getString("IndexCreator.SERIALIZING_ADDRESS"), cityTowns.size() + villages.size() / 100 + 1);
writeCityBlockIndex(writer, CITIES_TYPE, streetstat, waynodesStat, suburbs, cityTowns, postcodes, namesIndex, tagRules, progress);
writeCityBlockIndex(writer, VILLAGES_TYPE, streetstat, waynodesStat, null, villages, postcodes, namesIndex, tagRules, progress);
// write postcodes
List<BinaryFileReference> refs = new ArrayList<BinaryFileReference>();
writer.startCityBlockIndex(POSTCODES_TYPE);
ArrayList<City> posts = new ArrayList<City>(postcodes.values());
for (City s : posts) {
refs.add(writer.writeCityHeader(s, -1, tagRules));
}
for (int i = 0; i < posts.size(); i++) {
City postCode = posts.get(i);
BinaryFileReference ref = refs.get(i);
putNamedMapObject(namesIndex, postCode, ref.getStartPointer());
ArrayList<Street> streets = new ArrayList<Street>(postCode.getStreets());
Collections.sort(streets, new Comparator<Street>() {
final net.osmand.Collator clt = OsmAndCollator.primaryCollator();
@Override
public int compare(Street o1, Street o2) {
return clt.compare(o1.getName(), o2.getName());
}
});
writer.writeCityIndex(postCode, streets, null, ref, tagRules);
}
writer.endCityBlockIndex();
progress.finishTask();
writer.writeAddressNameIndex(namesIndex);
writer.endWriteAddressIndex();
writer.flush();
streetstat.close();
if (waynodesStat != null) {
waynodesStat.close();
}
}
use of net.osmand.data.MapObject in project OsmAnd-tools by osmandapp.
the class BinaryMerger method combineAddressIndex.
private void combineAddressIndex(String name, BinaryMapIndexWriter writer, AddressRegion[] addressRegions, BinaryMapIndexReader[] indexes) throws IOException {
Set<String> attributeTagsTableSet = new TreeSet<String>();
for (int i = 0; i != addressRegions.length; i++) {
AddressRegion region = addressRegions[i];
attributeTagsTableSet.addAll(region.getAttributeTagsTable());
}
writer.startWriteAddressIndex(name, attributeTagsTableSet);
List<String> attributeTagsTable = new ArrayList<String>();
attributeTagsTable.addAll(attributeTagsTableSet);
Map<String, Integer> tagRules = new HashMap<String, Integer>();
Map<String, List<MapObject>> namesIndex = new TreeMap<String, List<MapObject>>(Collator.getInstance());
ListIterator<String> it = attributeTagsTable.listIterator();
while (it.hasNext()) {
tagRules.put(it.next(), it.previousIndex());
}
for (int type : BinaryMapAddressReaderAdapter.CITY_TYPES) {
Map<City, BinaryMapIndexReader> cityMap = new HashMap<City, BinaryMapIndexReader>();
for (int i = 0; i < addressRegions.length; i++) {
AddressRegion region = addressRegions[i];
final BinaryMapIndexReader index = indexes[i];
for (City city : index.getCities(region, null, type)) {
normalizePostcode(city, extractCountryName(index));
if (cityMap.containsKey(city)) {
cityMap.remove(city);
}
cityMap.put(city, index);
}
}
List<City> cities = new ArrayList<City>(cityMap.keySet());
Map<City, List<City>> mergeCityGroup = new HashMap<City, List<City>>();
Collections.sort(cities, MapObject.BY_NAME_COMPARATOR);
mergeCitiesByNameDistance(cities, mergeCityGroup, cityMap, type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
List<BinaryFileReference> refs = new ArrayList<BinaryFileReference>();
// 1. write cities
writer.startCityBlockIndex(type);
Map<City, Map<Street, List<Node>>> namesakesStreetNodes = new HashMap<City, Map<Street, List<Node>>>();
for (int i = 0; i < cities.size(); i++) {
City city = cities.get(i);
BinaryMapIndexReader rindex = cityMap.get(city);
preloadStreetsAndBuildings(rindex, city, namesakesStreetNodes);
List<City> namesakes = mergeCityGroup.get(city);
if (namesakes != null) {
for (City namesake : namesakes) {
preloadStreetsAndBuildings(cityMap.get(namesake), namesake, namesakesStreetNodes);
city = mergeCities(city, namesake, namesakesStreetNodes);
}
}
int cityType = city.isPostcode() ? -1 : city.getType().ordinal();
BinaryFileReference ref = writer.writeCityHeader(city, cityType, tagRules);
refs.add(ref);
writer.writeCityIndex(city, city.getStreets(), namesakesStreetNodes.get(city), ref, tagRules);
IndexAddressCreator.putNamedMapObject(namesIndex, city, ref.getStartPointer());
if (!city.isPostcode()) {
for (Street s : city.getStreets()) {
IndexAddressCreator.putNamedMapObject(namesIndex, s, s.getFileOffset());
}
}
city.getStreets().clear();
namesakesStreetNodes.clear();
}
writer.endCityBlockIndex();
}
writer.writeAddressNameIndex(namesIndex);
writer.endWriteAddressIndex();
}
Aggregations