Search in sources :

Example 26 with Relation

use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.

the class IndexAddressCreator method indexBoundariesRelation.

public void indexBoundariesRelation(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    Boundary boundary = extractBoundary(e, ctx);
    // Bucharest has admin level 4
    boolean boundaryValid = boundary != null && (!boundary.hasAdminLevel() || boundary.getAdminLevel() >= 4) && boundary.getCenterPoint() != null && !Algorithms.isEmpty(boundary.getName());
    if (boundaryValid) {
        LatLon boundaryCenter = boundary.getCenterPoint();
        List<City> citiesToSearch = new ArrayList<City>();
        citiesToSearch.addAll(cityManager.getClosestObjects(boundaryCenter.getLatitude(), boundaryCenter.getLongitude(), 3));
        citiesToSearch.addAll(cityVillageManager.getClosestObjects(boundaryCenter.getLatitude(), boundaryCenter.getLongitude(), 3));
        City cityFound = null;
        String boundaryName = boundary.getName().toLowerCase();
        String altBoundaryName = Algorithms.isEmpty(boundary.getAltName()) ? "" : boundary.getAltName().toLowerCase();
        if (boundary.hasAdminCenterId()) {
            for (City c : citiesToSearch) {
                if (c.getId() == boundary.getAdminCenterId()) {
                    cityFound = c;
                    break;
                }
            }
        }
        if (cityFound == null) {
            for (City c : citiesToSearch) {
                if ((boundaryName.equalsIgnoreCase(c.getName()) || altBoundaryName.equalsIgnoreCase(c.getName())) && boundary.containsPoint(c.getLocation())) {
                    cityFound = c;
                    break;
                }
            }
        }
        // False case : London Borough of Richmond upon Thames (bigger) -> Richmond!
        if (cityFound == null) {
            for (City c : citiesToSearch) {
                String lower = c.getName().toLowerCase();
                if (nameContains(boundaryName, lower) || nameContains(altBoundaryName, lower)) {
                    if (boundary.containsPoint(c.getLocation())) {
                        cityFound = c;
                        break;
                    }
                }
            }
        }
        // It could be wrong if the boundary doesn't match center point
        if (cityFound == null && /*&& !boundary.hasAdminLevel() */
        (boundary.getCityType() == CityType.TOWN || // boundary.getCityType() == CityType.CITY ||
        boundary.getCityType() == CityType.HAMLET || boundary.getCityType() == CityType.SUBURB || boundary.getCityType() == CityType.VILLAGE)) {
            if (e instanceof Relation) {
                ctx.loadEntityRelation((Relation) e);
            }
            cityFound = createMissingCity(e, boundary.getCityType());
            boundary.setAdminCenterId(cityFound.getId());
        }
        if (cityFound != null) {
            putCityBoundary(boundary, cityFound);
        } else {
            logBoundaryChanged(boundary, null, 0, 0);
            notAssignedBoundaries.add(boundary);
        }
        attachAllCitiesToBoundary(boundary);
    } else if (boundary != null) {
        if (logMapDataWarn != null) {
            logMapDataWarn.warn("Not using boundary: " + boundary + " " + boundary.getBoundaryId());
        } else {
            log.info("Not using boundary: " + boundary + " " + boundary.getBoundaryId());
        }
    }
}
Also used : LatLon(net.osmand.data.LatLon) Relation(net.osmand.osm.edit.Relation) ArrayList(java.util.ArrayList) City(net.osmand.data.City) Boundary(net.osmand.data.Boundary)

Example 27 with Relation

use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.

the class IndexAddressCreator method iterateMainEntity.

public void iterateMainEntity(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    Map<String, String> tags = e.getTags();
    // index not only buildings but also nodes that belongs to addr:interpolation ways
    // currently not supported because nodes are indexed first with buildings
    String interpolation = e.getTag(OSMTagKey.ADDR_INTERPOLATION);
    if (e instanceof Way && interpolation != null) {
        BuildingInterpolation type = null;
        int interpolationInterval = 0;
        try {
            type = BuildingInterpolation.valueOf(interpolation.toUpperCase());
        } catch (RuntimeException ex) {
            try {
                interpolationInterval = Integer.parseInt(interpolation);
            } catch (NumberFormatException ex2) {
            }
        }
        if (type != null || interpolationInterval > 0) {
            List<Node> nodesWithHno = new ArrayList<Node>();
            for (Node n : ((Way) e).getNodes()) {
                if (n.getTag(OSMTagKey.ADDR_HOUSE_NUMBER) != null) {
                    String strt = n.getTag(OSMTagKey.ADDR_STREET);
                    if (strt == null) {
                        strt = n.getTag(OSMTagKey.ADDR_PLACE);
                    }
                    if (strt != null) {
                        nodesWithHno.add(n);
                    }
                }
            }
            if (nodesWithHno.size() > 1) {
                for (int i = 1; i < nodesWithHno.size(); i++) {
                    Node first = nodesWithHno.get(i - 1);
                    Node second = nodesWithHno.get(i);
                    boolean exist = streetDAO.findBuilding(first);
                    if (exist) {
                        streetDAO.removeBuilding(first);
                    }
                    LatLon l = e.getLatLon();
                    String strt = first.getTag(OSMTagKey.ADDR_STREET);
                    if (strt == null) {
                        strt = first.getTag(OSMTagKey.ADDR_PLACE);
                    }
                    Set<Long> idsOfStreet = getStreetInCity(first.getIsInNames(), strt, null, l);
                    if (!idsOfStreet.isEmpty()) {
                        Building building = EntityParser.parseBuilding(first);
                        building.setInterpolationInterval(interpolationInterval);
                        building.setInterpolationType(type);
                        building.setName(first.getTag(OSMTagKey.ADDR_HOUSE_NUMBER));
                        building.setName2(second.getTag(OSMTagKey.ADDR_HOUSE_NUMBER));
                        building.setLatLon2(second.getLatLon());
                        streetDAO.writeBuilding(idsOfStreet, building);
                    }
                }
            }
        }
    }
    String houseName = e.getTag(OSMTagKey.ADDR_HOUSE_NAME);
    String houseNumber = e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER);
    String street = null;
    if (houseNumber != null) {
        street = e.getTag(OSMTagKey.ADDR_STREET);
        if (street == null) {
            street = e.getTag(OSMTagKey.ADDR_PLACE);
        }
    }
    String street2 = e.getTag(OSMTagKey.ADDR_STREET2);
    if ((houseName != null || houseNumber != null)) {
        if (e instanceof Relation) {
            ctx.loadEntityRelation((Relation) e);
            Collection<Entity> outs = ((Relation) e).getMemberEntities("outer");
            if (!outs.isEmpty()) {
                e = outs.iterator().next();
            }
        }
        // skip relations
        boolean exist = e instanceof Relation || streetDAO.findBuilding(e);
        if (!exist) {
            LatLon l = e.getLatLon();
            Set<Long> idsOfStreet = getStreetInCity(e.getIsInNames(), street, null, l);
            if (!idsOfStreet.isEmpty()) {
                Building building = EntityParser.parseBuilding(e);
                String hname = null;
                String second = null;
                if (DataExtractionSettings.getSettings().isHousenumberPrefered()) {
                    hname = houseNumber;
                    second = houseName;
                } else {
                    hname = houseName;
                    second = houseNumber;
                }
                if (hname == null) {
                    hname = second;
                    second = null;
                }
                String additionalHname = "";
                if (DataExtractionSettings.getSettings().isAdditionalInfo() && second != null)
                    additionalHname = " - [" + second + "]";
                int i = hname.indexOf('-');
                if (i != -1 && interpolation != null) {
                    building.setInterpolationInterval(1);
                    try {
                        building.setInterpolationType(BuildingInterpolation.valueOf(interpolation.toUpperCase()));
                    } catch (RuntimeException ex) {
                        try {
                            building.setInterpolationInterval(Integer.parseInt(interpolation));
                        } catch (NumberFormatException ex2) {
                        }
                    }
                    building.setName(hname.substring(0, i));
                    building.setName2(hname.substring(i + 1));
                } else if ((street2 != null) && !street2.isEmpty()) {
                    int secondNumber = hname.indexOf('/');
                    if (secondNumber == -1 || !(secondNumber < hname.length() - 1)) {
                        building.setName(hname + additionalHname);
                    } else {
                        building.setName(hname.substring(0, secondNumber) + additionalHname);
                        Building building2 = EntityParser.parseBuilding(e);
                        building2.setName(hname.substring(secondNumber + 1) + additionalHname);
                        Set<Long> ids2OfStreet = getStreetInCity(e.getIsInNames(), street2, null, l);
                        // remove duplicated entries!
                        ids2OfStreet.removeAll(idsOfStreet);
                        if (!ids2OfStreet.isEmpty()) {
                            streetDAO.writeBuilding(ids2OfStreet, building2);
                        } else {
                            building.setName2(building2.getName() + additionalHname);
                        }
                    }
                } else {
                    building.setName(hname + additionalHname);
                }
                streetDAO.writeBuilding(idsOfStreet, building);
            }
        }
    } else if (e instanceof Way && /* && OSMSettings.wayForCar(e.getTag(OSMTagKey.HIGHWAY)) */
    e.getTag(OSMTagKey.HIGHWAY) != null && e.getTag(OSMTagKey.NAME) != null && isStreetTag(e.getTag(OSMTagKey.HIGHWAY))) {
        // suppose that streets with names are ways for car
        // Ignore all ways that have house numbers and highway type
        // if we saved address ways we could checked that we registered before
        boolean exist = streetDAO.findStreetNode(e);
        // check that street way is not registered already
        if (!exist) {
            LatLon l = e.getLatLon();
            Set<Long> idsOfStreet = getStreetInCity(e.getIsInNames(), e.getTag(OSMTagKey.NAME), getOtherNames(e), l);
            if (!idsOfStreet.isEmpty()) {
                streetDAO.writeStreetWayNodes(idsOfStreet, (Way) e);
            }
        }
    }
    if (e.getTag(OSMTagKey.POSTAL_CODE) != null) {
        if ("postal_code".equals(e.getTag(OSMTagKey.BOUNDARY))) {
            Boundary boundary = extractBoundary(e, ctx);
            if (boundary != null) {
                postcodeBoundaries.put(e, boundary);
            }
        } else if (e instanceof Relation) {
            ctx.loadEntityRelation((Relation) e);
            postalCodeRelations.add((Relation) e);
        }
    }
}
Also used : Building(net.osmand.data.Building) Entity(net.osmand.osm.edit.Entity) ResultSet(java.sql.ResultSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) BuildingInterpolation(net.osmand.data.Building.BuildingInterpolation) Way(net.osmand.osm.edit.Way) Boundary(net.osmand.data.Boundary) LatLon(net.osmand.data.LatLon) Relation(net.osmand.osm.edit.Relation)

Example 28 with Relation

use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.

the class OsmStorageWriter method saveStorage.

public void saveStorage(OutputStream output, OsmBaseStorage storage, Collection<EntityId> interestedObjects, boolean includeLinks) throws XMLStreamException, IOException {
    Map<EntityId, Entity> entities = storage.getRegisteredEntities();
    Map<EntityId, EntityInfo> entityInfo = storage.getRegisteredEntityInfo();
    Set<Node> nodes = new LinkedHashSet<Node>();
    Set<Way> ways = new LinkedHashSet<Way>();
    Set<Relation> relations = new LinkedHashSet<Relation>();
    if (interestedObjects == null) {
        interestedObjects = entities.keySet();
    }
    Stack<EntityId> toResolve = new Stack<EntityId>();
    toResolve.addAll(interestedObjects);
    while (!toResolve.isEmpty()) {
        EntityId l = toResolve.pop();
        if (entities.get(l) instanceof Node) {
            nodes.add((Node) entities.get(l));
        } else if (entities.get(l) instanceof Way) {
            ways.add((Way) entities.get(l));
            if (includeLinks) {
                toResolve.addAll(((Way) entities.get(l)).getEntityIds());
            }
        } else if (entities.get(l) instanceof Relation) {
            relations.add((Relation) entities.get(l));
            if (includeLinks) {
                for (RelationMember rm : ((Relation) entities.get(l)).getMembers()) {
                    toResolve.add(rm.getEntityId());
                }
            }
        }
    }
    writeOSM(output, entityInfo, nodes, ways, relations);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entity(net.osmand.osm.edit.Entity) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) Stack(java.util.Stack) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) EntityInfo(net.osmand.osm.edit.EntityInfo)

Example 29 with Relation

use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.

the class AugmentedDiffsInspector method prepareRegions.

private void prepareRegions(Context ctx, Map<EntityId, Entity> ids, Map<String, Set<EntityId>> regionsMap, OsmandRegions osmandRegions) throws IOException {
    Map<EntityId, Set<String>> mp = new HashMap<Entity.EntityId, Set<String>>();
    for (Entity e : ids.values()) {
        if (e instanceof Node) {
            int y = MapUtils.get31TileNumberY(((Node) e).getLatitude());
            int x = MapUtils.get31TileNumberX(((Node) e).getLongitude());
            EntityId id = EntityId.valueOf(e);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(id, lst);
            if (osmandRegions == null) {
                addEntityToRegion(regionsMap, id, lst, DEFAULT_REGION);
            } else {
                List<BinaryMapDataObject> l = osmandRegions.query(x, y);
                for (BinaryMapDataObject b : l) {
                    if (osmandRegions.contain(b, x, y)) {
                        String dw = osmandRegions.getDownloadName(b);
                        if (!Algorithms.isEmpty(dw) && osmandRegions.isDownloadOfType(b, OsmandRegions.MAP_TYPE)) {
                            addEntityToRegion(regionsMap, id, lst, dw);
                        }
                    }
                }
            }
        }
    }
    // 2. add ways and complete ways with missing nodes
    for (Entity e : ids.values()) {
        if (e instanceof Way) {
            Way w = (Way) e;
            EntityId wid = EntityId.valueOf(w);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(wid, lst);
            for (EntityId it : w.getEntityIds()) {
                Set<String> countries = mp.get(it);
                for (String cnt : countries) {
                    regionsMap.get(cnt).add(wid);
                }
                lst.addAll(countries);
            }
            // complete ways with missing nodes
            for (EntityId it : w.getEntityIds()) {
                mp.get(it).addAll(lst);
                for (String s : lst) {
                    regionsMap.get(s).add(it);
                }
            }
        }
    }
    // 3. add relations (not complete with ways or nodes)
    for (Entity e : ids.values()) {
        if (e instanceof Relation) {
            Relation r = (Relation) e;
            EntityId rid = EntityId.valueOf(r);
            TreeSet<String> lst = new TreeSet<String>();
            mp.put(rid, lst);
            for (RelationMember it : r.getMembers()) {
                Set<String> countries = mp.get(it.getEntityId());
                for (String cnt : countries) {
                    regionsMap.get(cnt).add(rid);
                }
                lst.addAll(countries);
            }
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TreeSet(java.util.TreeSet)

Example 30 with Relation

use of net.osmand.osm.edit.Relation in project Osmand by osmandapp.

the class OsmBaseStorage method startElement.

public void startElement(XmlPullParser parser, String name) {
    if (!parseStarted) {
        initRootElement(parser, name);
    }
    if (ELEM_MODIFY.equals(name)) {
        currentModify = Entity.MODIFY_MODIFIED;
    } else if (ELEM_CREATE.equals(name)) {
        currentModify = Entity.MODIFY_CREATED;
    } else if (ELEM_DELETE.equals(name)) {
        currentModify = Entity.MODIFY_DELETED;
    } else if (currentParsedEntity == null) {
        progressEntity++;
        if (progress != null && ((progressEntity % moduleProgress) == 0) && !progress.isIndeterminate() && streamForProgress != null) {
            try {
                progress.remaining(streamForProgress.available());
            } catch (IOException e) {
                progress.startWork(-1);
            }
        }
        if (ELEM_NODE.equals(name)) {
            currentParsedEntity = new Node(parseDouble(parser, ATTR_LAT, 0), parseDouble(parser, ATTR_LON, 0), parseId(parser, ATTR_ID, -1));
            currentParsedEntity.setVersion(parseVersion(parser));
        } else if (ELEM_WAY.equals(name)) {
            currentParsedEntity = new Way(parseId(parser, ATTR_ID, -1));
            currentParsedEntity.setVersion(parseVersion(parser));
        } else if (ELEM_RELATION.equals(name)) {
            currentParsedEntity = new Relation(parseId(parser, ATTR_ID, -1));
        } else {
        // this situation could be logged as unhandled
        }
        if (currentParsedEntity != null) {
            currentParsedEntity.setModify(currentModify);
            if (parseEntityInfo) {
                currentParsedEntityInfo = new EntityInfo();
                currentParsedEntityInfo.setChangeset(parser.getAttributeValue("", ATTR_CHANGESET));
                currentParsedEntityInfo.setTimestamp(parser.getAttributeValue("", ATTR_TIMESTAMP));
                currentParsedEntityInfo.setUser(parser.getAttributeValue("", ATTR_USER));
                currentParsedEntityInfo.setVersion(parser.getAttributeValue("", ATTR_VERSION));
                currentParsedEntityInfo.setVisible(parser.getAttributeValue("", ATTR_VISIBLE));
                currentParsedEntityInfo.setUid(parser.getAttributeValue("", ATTR_UID));
            }
        }
    } else {
        if (ELEM_TAG.equals(name)) {
            String key = parser.getAttributeValue("", ATTR_K);
            if (key != null) {
                if (convertTagsToLC) {
                    currentParsedEntity.putTag(key, parser.getAttributeValue("", ATTR_V));
                } else {
                    currentParsedEntity.putTagNoLC(key, parser.getAttributeValue("", ATTR_V));
                }
            }
        } else if (ELEM_ND.equals(name)) {
            Long id = parseId(parser, ATTR_REF, -1);
            if (id != -1 && currentParsedEntity instanceof Way) {
                ((Way) currentParsedEntity).addNode(id);
            }
        } else if (ELEM_MEMBER.equals(name)) {
            try {
                Long id = parseId(parser, ATTR_REF, -1);
                if (id != -1 && currentParsedEntity instanceof Relation) {
                    EntityType type = EntityType.valueOf(parser.getAttributeValue("", ATTR_TYPE).toUpperCase());
                    ((Relation) currentParsedEntity).addMember(id, type, parser.getAttributeValue("", ATTR_ROLE));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
        // this situation could be logged as unhandled
        }
    }
}
Also used : EntityType(net.osmand.osm.edit.Entity.EntityType) Relation(net.osmand.osm.edit.Relation) EntityInfo(net.osmand.osm.edit.EntityInfo) Node(net.osmand.osm.edit.Node) IOException(java.io.IOException) Way(net.osmand.osm.edit.Way) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SAXException(org.xml.sax.SAXException)

Aggregations

Relation (net.osmand.osm.edit.Relation)30 Way (net.osmand.osm.edit.Way)17 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)14 EntityId (net.osmand.osm.edit.Entity.EntityId)11 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)7 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 LatLon (net.osmand.data.LatLon)3 EntityType (net.osmand.osm.edit.Entity.EntityType)3 FileOutputStream (java.io.FileOutputStream)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2