use of net.osmand.data.City.CityType 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.CityType in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method extractBoundary.
private Boundary extractBoundary(Entity e, OsmDbAccessorContext ctx) throws SQLException {
if (e instanceof Node) {
return null;
}
long centerId = 0;
CityType ct = CityType.valueFromString(e.getTag(OSMTagKey.PLACE));
// if a place that has addr_place is a neighbourhood mark it as a suburb (made for the suburbs of Venice)
boolean isNeighbourhood = e.getTag(OSMTagKey.ADDR_PLACE) != null && "neighbourhood".equals(e.getTag(OSMTagKey.PLACE));
if ((ct == null && "townland".equals(e.getTag(OSMTagKey.LOCALITY))) || isNeighbourhood) {
if (e instanceof Relation) {
ctx.loadEntityRelation((Relation) e);
}
final City city = createMissingCity(e, CityType.SUBURB);
if (city != null) {
centerId = city.getId();
ct = CityType.SUBURB;
}
}
boolean administrative = "administrative".equals(e.getTag(OSMTagKey.BOUNDARY));
boolean postalCode = "postal_code".equals(e.getTag(OSMTagKey.BOUNDARY));
if (administrative || postalCode || ct != null) {
if (e instanceof Way && visitedBoundaryWays.contains(e.getId())) {
return null;
}
String bname = e.getTag(OSMTagKey.NAME);
MultipolygonBuilder m = new MultipolygonBuilder();
if (e instanceof Relation) {
Relation aRelation = (Relation) e;
ctx.loadEntityRelation(aRelation);
for (RelationMember es : aRelation.getMembers()) {
if (es.getEntity() instanceof Way) {
// $NON-NLS-1$
boolean inner = "inner".equals(es.getRole());
if (inner) {
m.addInnerWay((Way) es.getEntity());
} else {
String wName = es.getEntity().getTag(OSMTagKey.NAME);
// if name are not equal keep the way for further check (it could be different suburb)
if (Algorithms.objectEquals(wName, bname) || wName == null) {
visitedBoundaryWays.add(es.getEntity().getId());
}
m.addOuterWay((Way) es.getEntity());
}
} else if (es.getEntity() instanceof Node && ("admin_centre".equals(es.getRole()) || "admin_center".equals(es.getRole()))) {
centerId = es.getEntity().getId();
} else if (es.getEntity() instanceof Node && ("label".equals(es.getRole()) && centerId == 0)) {
centerId = es.getEntity().getId();
}
}
} else if (e instanceof Way) {
m.addOuterWay((Way) e);
}
Boundary boundary = new Boundary(m);
boundary.setName(bname);
// Goteborg
boundary.setAltName(e.getTag("short_name"));
boundary.setAdminLevel(extractBoundaryAdminLevel(e));
boundary.setBoundaryId(e.getId());
boundary.setCityType(ct);
if (centerId != 0) {
boundary.setAdminCenterId(centerId);
}
return boundary;
} else {
return null;
}
}
use of net.osmand.data.City.CityType 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.City.CityType in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method readCities.
public Map<CityType, List<City>> readCities(Connection c) throws SQLException {
Map<CityType, List<City>> cities = new LinkedHashMap<City.CityType, List<City>>();
for (CityType t : CityType.values()) {
cities.put(t, new ArrayList<City>());
}
Statement stat = c.createStatement();
// $NON-NLS-1$
ResultSet set = stat.executeQuery("select id, latitude, longitude , name , name_en , city_type from city");
while (set.next()) {
CityType type = CityType.valueFromString(set.getString(6));
City city = new City(type);
city.copyNames(set.getString(4), null, Algorithms.decodeMap(set.getString(5)));
city.setLocation(set.getDouble(2), set.getDouble(3));
city.setId(set.getLong(1));
cities.get(type).add(city);
if (DEBUG_FULL_NAMES) {
Boundary cityB = cityBoundaries.get(city);
if (cityB != null) {
city.setName(city.getName() + " " + cityB.getAdminLevel() + ":" + cityB.getName());
}
}
}
set.close();
stat.close();
Comparator<City> comparator = new Comparator<City>() {
@Override
public int compare(City o1, City o2) {
return Collator.getInstance().compare(o1.getName(), o2.getName());
}
};
for (List<City> t : cities.values()) {
Collections.sort(t, comparator);
}
return cities;
}
Aggregations