Search in sources :

Example 11 with Relation

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

the class OsmDbCreator method acceptEntityToLoad.

@Override
public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity e) {
    // put all nodes into temporary db to get only required nodes after loading all data
    if (VALIDATE_DUPLICATES) {
        long l = (e.getId() << 2) + entityId.getType().ordinal();
        if (!idSet.add(l)) {
            throw new IllegalStateException("Duplicate id '" + e.getId() + "' " + entityId.getType());
        }
    }
    try {
        e.removeTags(tagsToIgnore);
        ByteArrayOutputStream tags = new ByteArrayOutputStream();
        try {
            for (Entry<String, String> i : e.getTags().entrySet()) {
                // UTF-8 default
                tags.write(i.getKey().getBytes("UTF-8"));
                tags.write(0);
                tags.write(i.getValue().getBytes("UTF-8"));
                tags.write(0);
            }
        } catch (IOException es) {
            throw new RuntimeException(es);
        }
        long id = convertId(e);
        boolean delete = AugmentedDiffsInspector.OSMAND_DELETE_VALUE.equals(e.getTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG));
        if (e.getTags().isEmpty()) {
            e.putTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG, AugmentedDiffsInspector.OSMAND_DELETE_VALUE);
            delete = true;
        }
        if (ovewriteIds || e instanceof Relation) {
            checkEntityExists(e, id, delete);
        }
        if (e instanceof Node) {
            currentCountNode++;
            if (!e.getTags().isEmpty()) {
                allNodes++;
            }
            prepNode.setLong(1, id);
            prepNode.setDouble(2, ((Node) e).getLatitude());
            prepNode.setDouble(3, ((Node) e).getLongitude());
            prepNode.setBytes(4, tags.toByteArray());
            prepNode.addBatch();
            if (currentCountNode >= BATCH_SIZE_OSM) {
                prepNode.executeBatch();
                // clear memory
                dbConn.commit();
                currentCountNode = 0;
            }
        } else if (e instanceof Way) {
            allWays++;
            int ord = 0;
            TLongArrayList nodeIds = ((Way) e).getNodeIds();
            boolean city = CityType.valueFromString(((Way) e).getTag(OSMTagKey.PLACE)) != null;
            int boundary = ((Way) e).getTag(OSMTagKey.BOUNDARY) != null || city ? 1 : 0;
            for (int j = 0; j < nodeIds.size(); j++) {
                currentWaysCount++;
                if (ord == 0) {
                    prepWays.setBytes(4, tags.toByteArray());
                }
                prepWays.setLong(1, id);
                prepWays.setLong(2, nodeIds.get(j));
                prepWays.setLong(3, ord++);
                prepWays.setInt(5, boundary);
                prepWays.addBatch();
            }
            if (currentWaysCount >= BATCH_SIZE_OSM) {
                prepWays.executeBatch();
                // clear memory
                dbConn.commit();
                currentWaysCount = 0;
            }
        } else {
            // osm change can't handle relations properly
            allRelations++;
            short ord = 0;
            for (RelationMember i : ((Relation) e).getMembers()) {
                currentRelationsCount++;
                if (ord == 0) {
                    prepRelations.setBytes(6, tags.toByteArray());
                }
                prepRelations.setLong(1, id);
                prepRelations.setLong(2, i.getEntityId().getId());
                prepRelations.setLong(3, i.getEntityId().getType().ordinal());
                prepRelations.setString(4, i.getRole());
                prepRelations.setLong(5, ord++);
                prepRelations.setInt(7, delete ? 1 : 0);
                prepRelations.addBatch();
            }
            // System.out.println(id + " " + delete);
            if (currentRelationsCount >= BATCH_SIZE_OSM) {
                prepRelations.executeBatch();
                // clear memory
                dbConn.commit();
                currentRelationsCount = 0;
            }
        }
    } catch (SQLException ex) {
        // $NON-NLS-1$
        log.error("Could not save in db (entity " + entityId + ") ", ex);
    }
    // do not add to storage
    return false;
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) SQLException(java.sql.SQLException) Node(net.osmand.osm.edit.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Way(net.osmand.osm.edit.Way) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember)

Example 12 with Relation

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

the class TagsTransformer method handleRelationPropogatedTags.

public void handleRelationPropogatedTags(Relation e, MapRenderingTypesEncoder renderingTypes, OsmDbAccessorContext ctx, EntityConvertApplyType at) throws SQLException {
    Map<MapRulType, Map<MapRulType, String>> propogated = renderingTypes.getRelationPropogatedTags((Relation) e, at);
    if (propogated != null && propogated.size() > 0) {
        if (ctx != null) {
            ctx.loadEntityRelation((Relation) e);
        }
        for (RelationMember ids : ((Relation) e).getMembers()) {
            if (!propogatedTags.containsKey(ids.getEntityId())) {
                propogatedTags.put(ids.getEntityId(), new LinkedHashMap<String, String>());
            }
            Map<String, String> map = propogatedTags.get(ids.getEntityId());
            Iterator<Entry<MapRulType, Map<MapRulType, String>>> itMain = propogated.entrySet().iterator();
            while (itMain.hasNext()) {
                Entry<MapRulType, Map<MapRulType, String>> ev = itMain.next();
                Map<MapRulType, String> pr = ev.getValue();
                MapRulType propagateRule = ev.getKey();
                if (propagateRule.isRelationGroup()) {
                    Iterator<Entry<MapRulType, String>> it = pr.entrySet().iterator();
                    int modifier = 1;
                    String s = propagateRule.getTag() + "__" + propagateRule.getValue() + "_";
                    while (map.containsKey(s + modifier)) {
                        modifier++;
                    }
                    map.put(s + modifier, s);
                    while (it.hasNext()) {
                        Entry<MapRulType, String> es = it.next();
                        String key = es.getKey().getTag();
                        map.put(key + "_" + modifier, es.getValue());
                    }
                } else {
                    Iterator<Entry<MapRulType, String>> it = pr.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<MapRulType, String> es = it.next();
                        String key = es.getKey().getTag();
                        if (es.getKey().isText() && map.containsKey(key)) {
                            String res = sortAndAttachUniqueValue(map.get(key), es.getValue());
                            map.put(key, res);
                        } else {
                            map.put(key, es.getValue());
                        }
                    }
                }
            }
        }
    }
}
Also used : MapRulType(net.osmand.osm.MapRenderingTypes.MapRulType) Relation(net.osmand.osm.edit.Relation) Entry(java.util.Map.Entry) RelationMember(net.osmand.osm.edit.Relation.RelationMember) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with Relation

use of net.osmand.osm.edit.Relation 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 14 with Relation

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

the class IndexAddressCreator method indexAddressRelation.

public void indexAddressRelation(Relation i, OsmDbAccessorContext ctx) throws SQLException {
    if ("street".equals(i.getTag(OSMTagKey.TYPE)) || "associatedStreet".equals(i.getTag(OSMTagKey.TYPE))) {
        // $NON-NLS-1$
        LatLon l = null;
        String streetName = null;
        Set<String> isInNames = null;
        ctx.loadEntityRelation(i);
        streetName = i.getTag(OSMTagKey.NAME);
        Iterator<Entity> it = i.getMemberEntities(null).iterator();
        while (l == null && it.hasNext()) {
            // get coordinates from any relation member
            l = it.next().getLatLon();
        }
        isInNames = i.getIsInNames();
        String postcode = i.getTag(OSMTagKey.ADDR_POSTCODE);
        if (streetName == null) {
            // use relation name as a street name
            Collection<Entity> members = i.getMemberEntities("street");
            for (Entity street : members) {
                // find the first street member with name and use it as a street name
                String name = street.getTag(OSMTagKey.NAME);
                if (name != null) {
                    streetName = name;
                    l = street.getLatLon();
                    isInNames = street.getIsInNames();
                    break;
                }
            }
        }
        if (streetName != null) {
            Set<Long> idsOfStreet = getStreetInCity(isInNames, streetName, null, l);
            if (!idsOfStreet.isEmpty()) {
                // both house and address roles can have address
                Collection<Entity> houses = i.getMemberEntities("house");
                houses.addAll(i.getMemberEntities("address"));
                for (Entity house : houses) {
                    String hname = null;
                    String second = null;
                    if (DataExtractionSettings.getSettings().isHousenumberPrefered()) {
                        hname = house.getTag(OSMTagKey.ADDR_HOUSE_NUMBER);
                        second = house.getTag(OSMTagKey.ADDR_HOUSE_NAME);
                    } else {
                        hname = house.getTag(OSMTagKey.ADDR_HOUSE_NAME);
                        second = house.getTag(OSMTagKey.ADDR_HOUSE_NUMBER);
                    }
                    if (hname == null) {
                        hname = second;
                        second = null;
                    }
                    if (hname == null)
                        continue;
                    if (DataExtractionSettings.getSettings().isAdditionalInfo() && second != null)
                        hname += " - [" + second + "]";
                    if (!streetDAO.findBuilding(house)) {
                        // process multipolygon (relation) houses - preload members to create building with correct latlon
                        if (house instanceof Relation)
                            ctx.loadEntityRelation((Relation) house);
                        Building building = EntityParser.parseBuilding(house);
                        if (building.getLocation() == null) {
                            log.warn("building with empty location! id: " + house.getId());
                        } else {
                            building.setName(hname);
                            if (Algorithms.isEmpty(building.getPostcode())) {
                                building.setPostcode(postcode);
                            }
                            streetDAO.writeBuilding(idsOfStreet, building);
                        }
                    }
                }
            }
        }
    }
}
Also used : Building(net.osmand.data.Building) LatLon(net.osmand.data.LatLon) Entity(net.osmand.osm.edit.Entity) Relation(net.osmand.osm.edit.Relation)

Example 15 with Relation

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

the class AugmentedDiffsInspector method parseFile.

private Context parseFile(File file) throws XmlPullParserException, IOException {
    XmlPullParser parser = PlatformUtil.newXMLPullParser();
    InputStream fis = new FileInputStream(file);
    if (file.getName().endsWith(".gz")) {
        fis = new GZIPInputStream(fis);
    }
    parser.setInput(fis, "UTF-8");
    int next;
    int modify = Entity.MODIFY_UNKNOWN;
    Entity currentEntity = null;
    Way currentWay = null;
    Context ctx = new Context();
    boolean old = false;
    while ((next = parser.next()) != XmlPullParser.END_DOCUMENT) {
        if (next == XmlPullParser.END_TAG) {
            String name = parser.getName();
            if (name.equals("node") || name.equals("way") || name.equals("relation")) {
                if (currentEntity != null) {
                    if (old) {
                        updateTags(currentEntity, "name", "type", "area", "fixme");
                        ctx.oldIds.put(EntityId.valueOf(currentEntity), currentEntity);
                    } else if (modify != Entity.MODIFY_DELETED) {
                        ctx.newIds.put(EntityId.valueOf(currentEntity), currentEntity);
                    }
                }
            }
        } else if (next == XmlPullParser.START_TAG) {
            String name = parser.getName();
            if ("action".equals(name)) {
                String type = parser.getAttributeValue("", "type");
                if ("modify".equals(type)) {
                    modify = Entity.MODIFY_MODIFIED;
                } else if ("delete".equals(type)) {
                    modify = Entity.MODIFY_DELETED;
                } else if ("create".equals(type)) {
                    modify = Entity.MODIFY_CREATED;
                }
                old = false;
            } else if (name.equals("old")) {
                old = true;
            } else if (name.equals("new")) {
                old = false;
            } else if (name.equals("tag")) {
                currentEntity.putTag(parser.getAttributeValue("", "k"), parser.getAttributeValue("", "v"));
            } else if (name.equals("node")) {
                if (old || modify != Entity.MODIFY_DELETED) {
                    long id = Long.parseLong(parser.getAttributeValue("", "id"));
                    currentEntity = new Node(Double.parseDouble(parser.getAttributeValue("", "lat")), Double.parseDouble(parser.getAttributeValue("", "lon")), id);
                    parseVersion(parser, currentEntity);
                }
            } else if (name.equals("relation")) {
                long id = Long.parseLong(parser.getAttributeValue("", "id"));
                currentEntity = new Relation(id);
                parseVersion(parser, currentEntity);
            } else if (name.equals("way")) {
                long id = Long.parseLong(parser.getAttributeValue("", "id"));
                currentWay = new Way(id);
                currentEntity = currentWay;
                parseVersion(parser, currentEntity);
            } else if (name.equals("member")) {
                String tp = parser.getAttributeValue("", "type");
                long ref = Long.parseLong(parser.getAttributeValue("", "ref"));
                String role = parser.getAttributeValue("", "role");
                EntityType type = tp.equals("node") ? EntityType.NODE : (tp.equals("way") ? EntityType.WAY : EntityType.RELATION);
                EntityId nid = new EntityId(type, ref);
                Map<EntityId, Entity> o = old ? ctx.oldIds : ctx.newIds;
                boolean skip = false;
                currentWay = null;
                if (!o.containsKey(nid) || old) {
                    if (type == EntityType.NODE) {
                        Node nd = registerNewNode(parser, ctx, old, nid);
                        nid = EntityId.valueOf(nd);
                    } else if (type == EntityType.WAY) {
                        currentWay = new Way(ID_BASE--);
                        currentWay.putTag("oid", nid.getId().toString());
                        registerEntity(ctx, old, currentWay);
                        registerByOldId(ctx, old, currentWay, nid);
                        nid = EntityId.valueOf(currentWay);
                    } else if (type == EntityType.RELATION) {
                        // skip subrelations
                        // throw new UnsupportedOperationException();
                        skip = true;
                    }
                }
                if (!skip) {
                    ((Relation) currentEntity).addMember(nid.getId(), type, role);
                }
            } else if (name.equals("nd") && currentWay != null) {
                String rf = parser.getAttributeValue("", "ref");
                Node nd = null;
                EntityId nid = null;
                if (!Algorithms.isEmpty(rf) && !old) {
                    nid = new EntityId(EntityType.NODE, Long.parseLong(rf));
                    Map<EntityId, Entity> o = old ? ctx.oldIds : ctx.newIds;
                    nd = (Node) o.get(nid);
                }
                if (nd == null) {
                    nd = registerNewNode(parser, ctx, old, nid);
                }
                ((Way) currentWay).addNode(nd.getId());
            }
        }
    }
    return ctx;
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(net.osmand.osm.edit.Node) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) GZIPInputStream(java.util.zip.GZIPInputStream) EntityType(net.osmand.osm.edit.Entity.EntityType) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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