use of net.osmand.data.City 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);
}
use of net.osmand.data.City in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method createMissingCity.
private City createMissingCity(Entity e, CityType t) throws SQLException {
City c = EntityParser.parseCity(e, t);
if (c.getLocation() == null) {
return null;
}
// long centerId = e.getId();
regCity(c, e);
writeCity(c);
commitWriteCity();
return c;
}
use of net.osmand.data.City in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method getStreetInCity.
public Set<Long> getStreetInCity(Set<String> isInNames, String name, Map<String, String> names, final LatLon location) throws SQLException {
if (location == null) {
return Collections.emptySet();
}
name = normalizeStreetName(name);
Set<City> result = new LinkedHashSet<City>();
List<City> nearestObjects = new ArrayList<City>();
nearestObjects.addAll(cityManager.getClosestObjects(location.getLatitude(), location.getLongitude()));
nearestObjects.addAll(cityVillageManager.getClosestObjects(location.getLatitude(), location.getLongitude()));
// either we found a city boundary the street is in
for (City c : nearestObjects) {
Boundary boundary = cityBoundaries.get(c);
if (isInNames.contains(c.getName()) || (boundary != null && boundary.containsPoint(location))) {
result.add(c);
}
}
// or we need to find closest city
Collections.sort(nearestObjects, new Comparator<City>() {
@Override
public int compare(City c1, City c2) {
double r1 = relativeDistance(location, c1);
double r2 = relativeDistance(location, c2);
return Double.compare(r1, r2);
}
});
for (City c : nearestObjects) {
if (relativeDistance(location, c) > 0.2) {
if (result.isEmpty()) {
result.add(c);
}
break;
} else if (!result.contains(c)) {
// city doesn't have boundary or there is a mistake in boundaries and we found nothing before
if (!cityBoundaries.containsKey(c) || result.isEmpty()) {
result.add(c);
}
}
}
return registerStreetInCities(name, names, location, result);
}
use of net.osmand.data.City in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method readStreetsBuildings.
private List<Street> readStreetsBuildings(PreparedStatement streetBuildingsStat, City city, PreparedStatement waynodesStat, Map<Street, List<Node>> streetNodes, List<City> citySuburbs) throws SQLException {
TLongObjectHashMap<Street> visitedStreets = new TLongObjectHashMap<Street>();
Map<String, List<Street>> uniqueNames = new TreeMap<String, List<Street>>(OsmAndCollator.primaryCollator());
// read streets for city
readStreetsAndBuildingsForCity(streetBuildingsStat, city, waynodesStat, streetNodes, visitedStreets, uniqueNames);
// read streets for suburbs of the city
if (citySuburbs != null) {
for (City suburb : citySuburbs) {
readStreetsAndBuildingsForCity(streetBuildingsStat, suburb, waynodesStat, streetNodes, visitedStreets, uniqueNames);
}
}
mergeStreetsWithSameNames(streetNodes, uniqueNames);
return new ArrayList<Street>(streetNodes.keySet());
}
use of net.osmand.data.City in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method findCityPart.
private String findCityPart(LatLon location, City city) {
String cityPart = city.getName();
boolean found = false;
Boundary cityBoundary = cityBoundaries.get(city);
if (cityBoundary != null) {
List<City> subcities = boundaryToContainingCities.get(cityBoundary);
if (subcities != null) {
for (City subpart : subcities) {
if (subpart != city) {
Boundary subBoundary = cityBoundaries.get(subpart);
if (cityBoundary != null && subBoundary != null && subBoundary.getAdminLevel() > cityBoundary.getAdminLevel()) {
// old code
// subpart.getName();
cityPart = findNearestCityOrSuburb(subBoundary, location);
// ?FIXME
if (subBoundary.containsPoint(location)) {
cityPart = subpart.getName();
found = true;
break;
}
}
}
}
}
}
if (!found) {
Boundary b = cityBoundaries.get(city);
cityPart = findNearestCityOrSuburb(b, location);
}
return cityPart;
}
Aggregations