Search in sources :

Example 1 with CityType

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);
}
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 CityType

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;
    }
}
Also used : Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) CityType(net.osmand.data.City.CityType) Node(net.osmand.osm.edit.Node) City(net.osmand.data.City) Way(net.osmand.osm.edit.Way) MultipolygonBuilder(net.osmand.data.MultipolygonBuilder) Boundary(net.osmand.data.Boundary)

Example 3 with CityType

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();
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) CityType(net.osmand.data.City.CityType) ArrayList(java.util.ArrayList) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) SimpleStreet(net.osmand.data.preparation.address.DBStreetDAO.SimpleStreet) Street(net.osmand.data.Street) List(java.util.List) ArrayList(java.util.ArrayList) MapObject(net.osmand.data.MapObject) PreparedStatement(java.sql.PreparedStatement) City(net.osmand.data.City) TreeMap(java.util.TreeMap)

Example 4 with CityType

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;
}
Also used : CityType(net.osmand.data.City.CityType) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) City(net.osmand.data.City) LinkedHashMap(java.util.LinkedHashMap) Boundary(net.osmand.data.Boundary) Comparator(java.util.Comparator)

Aggregations

City (net.osmand.data.City)4 CityType (net.osmand.data.City.CityType)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PreparedStatement (java.sql.PreparedStatement)2 LinkedHashMap (java.util.LinkedHashMap)2 Boundary (net.osmand.data.Boundary)2 MapObject (net.osmand.data.MapObject)2 Street (net.osmand.data.Street)2 ByteString (com.google.protobuf.ByteString)1 TByteArrayList (gnu.trove.list.array.TByteArrayList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 AddressNameIndexDataAtom (net.osmand.binary.OsmandOdb.AddressNameIndexDataAtom)1